숫자 맞추기 게임

발행: (2026년 3월 26일 오후 01:41 GMT+9)
3 분 소요
원문: Dev.to

Source: Dev.to

개요

우리는 시스템이 무작위 숫자를 생성하고 플레이어가 “높음” 또는 “낮음”과 같은 힌트를 받아가며 맞추는 숫자 맞추기 게임을 만들었습니다. 난이도에 따라 숫자 범위가 달라집니다:

  • Easy: 1–20
  • Medium: 1–50
  • Hard: 1–100

이 게임은 시드(보통 시스템 시간)를 이용한 의사 난수 생성, 사용자 입력을 신중히 처리하는 방법, 그리고 플레이어 성과를 저장하고 표시하는 간단한 리더보드와 같은 개념을 보여줍니다.

구현

import random

leaderboard = []

def get_difficulty():
    print("1. Easy (1-20)")
    print("2. Medium (1-50)")
    print("3. Hard (1-100)")

    while True:
        try:
            choice = int(input("Choose difficulty: "))
            if choice == 1:
                return "easy", 20, 10
            elif choice == 2:
                return "medium", 50, 8
            elif choice == 3:
                return "hard", 100, 5
            else:
                print("Invalid choice")
        except:
            print("Enter a valid number")

def play_game():
    name = input("Enter your name: ")
    difficulty, max_range, attempts = get_difficulty()

    secret_number = random.randint(1, max_range)

    print(f"\nGuess number between 1 and {max_range}")

    for attempt in range(1, attempts + 1):
        try:
            guess = int(input(f"Attempt {attempt}: "))
        except:
            print("Invalid input")
            continue

        if guess == secret_number:
            print("Correct!")
            leaderboard.append({
                "name": name,
                "difficulty": difficulty,
                "attempts": attempt
            })
            return
        elif guess < secret_number:
            print("Higher")
        else:
            print("Lower")

    print(f"Game Over! Number was {secret_number}")

def view_leaderboard():
    if not leaderboard:
        print("No data yet")
        return

    difficulty_order = {"easy": 1, "medium": 2, "hard": 3}

    sorted_data = sorted(
        leaderboard,
        key=lambda x: (difficulty_order[x["difficulty"]], x["attempts"])
    )

    print("\nLeaderboard:")
    for p in sorted_data:
        print(p["name"], p["difficulty"], p["attempts"])

def main():
    while True:
        print("\n1. Play Game")
        print("2. View Leaderboard")
        print("3. Exit")

        choice = input("Enter choice: ")

        if choice == "1":
            play_game()
        elif choice == "2":
            view_leaderboard()
        elif choice == "3":
            break
        else:
            print("Invalid choice")

main()

보안

시스템 간 통신은 HTTPS를 통해 암호화되어 이루어지며, 이를 통해 데이터가 공격자에게 가로채이지 않도록 보장합니다.

교훈

이번 세션을 통해 간단한 게임이라도 동시성, 락킹, 데이터베이스 설계, 난수 생성, 보안 등 중요한 개념을 이해하는 데 도움이 될 수 있음을 보여주었습니다. 작은 프로그램도 실제 시스템을 이해하기 위한 탄탄한 기반을 마련해 줍니다.

0 조회
Back to Blog

관련 글

더 보기 »

j.s에서 looping이란 무엇인가

JavaScript에서 루프는 같은 코드를 반복해서 작성하지 않고도 동일한 작업을 계속 수행하고 싶을 때 유용합니다. 유형…