DATA 사전캠프

15일차 Python 스터디: 단어 맞추기 게임

sawo11 2024. 11. 23. 02:53
  • 어를 주어진 기회 안에 맞추는 게임을 만들어보세요

정답은 Picture.

  1. 컴퓨터가 랜덤으로 영어단어를 선택합니다.
    1. 영어단어의 자리수를 알려줍니다.
    2. ex ) PICTURE = 7자리
    • 힌트
    • [ "airplane", "apple", "arm", "bakery", "banana", "bank", "bean", "belt", "bicycle", "biography", "blackboard", "boat", "bowl", "broccoli", "bus", "car", "carrot", "chair", "cherry", "cinema", "class", "classroom", "cloud", "coat", "cucumber", "desk", "dictionary", "dress", "ear", "eye", "fog", "foot", "fork", "fruits", "hail", "hand", "head", "helicopter", "hospital", "ice", "jacket", "kettle", "knife", "leg", "lettuce", "library", "magazine", "mango", "melon", "motorcycle", "mouth", "newspaper", "nose", "notebook", "novel", "onion", "orange", "peach", "pharmacy", "pineapple", "plate", "pot", "potato", "rain", "shirt", "shoe", "shop", "sink", "skateboard", "ski", "skirt", "sky", "snow", "sock", "spinach", "spoon", "stationary", "stomach", "strawberry", "student", "sun", "supermarket", "sweater", "teacher", "thunderstorm", "tomato", "trousers", "truck", "vegetables", "vehicles", "watermelon", "wind" ]
  2. 사용자는 A 부터 Z 까지의 알파벳 중에서 하나를 선택합니다.
    1. 맞출 경우 해당 알파벳이 들어간 자리를 전부 보여줍니다.
    2. 틀릴 경우 목숨이 하나 줄어듭니다.
  3. 사용자가 9번 틀리면 게임오버됩니다.
  4. 게임오버 되기 전에 영어단어의 모든 자리를 알아내면 플레이어의 승리입니다.
import random

# 단어 리스트
words = [
    "airplane", "apple", "arm", "bakery", "banana", "bank", "bean", "belt",
    "bicycle", "biography", "blackboard", "boat", "bowl", "broccoli", "bus",
    "car", "carrot", "chair", "cherry", "cinema", "class", "classroom",
    "cloud", "coat", "cucumber", "desk", "dictionary", "dress", "ear", "eye",
    "fog", "foot", "fork", "fruits", "hail", "hand", "head", "helicopter",
    "hospital", "ice", "jacket", "kettle", "knife", "leg", "lettuce",
    "library", "magazine", "mango", "melon", "motorcycle", "mouth",
    "newspaper", "nose", "notebook", "novel", "onion", "orange", "peach",
    "pharmacy", "pineapple", "plate", "pot", "potato", "rain", "shirt",
    "shoe", "shop", "sink", "skateboard", "ski", "skirt", "sky", "snow",
    "sock", "spinach", "spoon", "stationary", "stomach", "strawberry",
    "student", "sun", "supermarket", "sweater", "teacher", "thunderstorm",
    "tomato", "trousers", "truck", "vegetables", "vehicles", "watermelon",
    "wind"
]

# 컴퓨터가 랜덤으로 단어 선택
word = random.choice(words).upper()
word_length = len(word)
hidden_word = ["_" for _ in word]  # 단어를 숨김 처리
attempts_left = 9  # 최대 틀릴 수 있는 횟수
guessed_letters = []  # 사용자가 추측한 글자

# 게임 시작
print("=== 단어 맞추기 게임 ===")
print(f"단어는 {word_length}자리입니다.")
print(" ".join(hidden_word))

while attempts_left > 0 and "_" in hidden_word:
    # 사용자 입력
    guess = input("알파벳 하나를 입력하세요 (A-Z): ").upper()

    # 유효성 검사
    if len(guess) != 1 or not guess.isalpha():
        print("잘못된 입력입니다. 알파벳 한 글자만 입력해주세요.")
        continue

    if guess in guessed_letters:
        print(f"'{guess}'는 이미 시도한 글자입니다.")
        continue

    guessed_letters.append(guess)

    if guess in word:
        # 맞춘 경우
        print(f"'{guess}'는 단어에 포함되어 있습니다!")
        for idx, letter in enumerate(word):
            if letter == guess:
                hidden_word[idx] = guess
    else:
        # 틀린 경우
        attempts_left -= 1
        print(f"'{guess}'는 단어에 없습니다. 남은 목숨: {attempts_left}")

    # 현재 상태 출력
    print(" ".join(hidden_word))

# 결과 출력
if "_" not in hidden_word:
    print(f"축하합니다! 단어를 맞췄습니다: {word}")
else:
    print(f"게임 오버! 정답은 {word}였습니다.")