Python🐍

Hang man game

hoho0311 2025. 1. 17. 23:33

아스키 아트


초등학교 영어시간에 보통 게임을 많이 했는데 그중 가장 기억에 남는 게임이 행맨이다.

 

행맨이란?

술래 같은 역할을 받은 사람이 머릿속으로 영어 단어를 정하고,

칠판에 영단어의 철자 개수만큼 _ 를 그어준다.

예를 들어 'apple'을 생각했다면 칠판에 _ _ _ _ _ 라고 적어준다.

 

행맨

이런 식으로 사람을 그려서 전부 그려지면 출제자가 이기는 게임이다.

지금 보니 행맨이란 교수형 집행인을 뜻하는 단어라고 한다...

어쩐지 왜 사람을 저렇게 달아놓나 했더니 사형하는 거였다..

 

정확한 정보는 아래에 링크를 작성하겠다.

https://namu.wiki/w/%ED%96%89%EB%A7%A8

 

행맨

사형 , 그중에서도 교수형 집행인을 뜻하는 영어 . 놀이 파일:external/upload.wikimedia.

namu.wiki

 

import random
from hangman_words import word_list
from hangman_art import stages, logo
# TODO-1: - Update the word list to use the 'word_list' from hangman_words.py

lives = 6

# TODO-3: - Import the logo from hangman_art.py and print it at the start of the game.

chosen_word = random.choice(word_list)
print(chosen_word)

placeholder = ""
word_length = len(chosen_word)
for position in range(word_length):
    placeholder += "_"
print(logo)
print("Word to guess: " + placeholder)

game_over = False
correct_letters = []

먼저 출제 단어를 리스트로 만든 뒤 임포트 한다.

행맨 그림도 아스키 아트로 만들어 임포트한다.

위 폴더는 게시물 마지막에 올리겠다.

 

총목숨은 6개로 정하며, 정답과 이미 입력한 정답을 다시 입력하는 경우에는 목숨을 유지한다.

오답을 입력 시 "You guessed d, that's not in the word. You lose a life"라는 문장과 함께

행맨이 그려진다.

 

이러식으로 콘솔이 나온다.

Word to guess : _ _ _ _ _ 는 맞춰야 하는 단어의 개수를 말한다.

Guess a letter : 뒤에는 사용자가 영단어를 입력한다.

아래는 행맨을 아스키 아트로 입력했다.

 

while not game_over:

    # TODO-6: - Update the code below to tell the user how many lives they have left.
    print(f"****************************{lives}/6 LIVES LEFT****************************")
    guess = input("Guess a letter: ").lower()

    # TODO-4: - If the user has entered a letter they've already guessed, print the letter and let them know.
    if guess in correct_letters:
        print("You entered a letter they've already guessed.")

    display = ""

    for letter in chosen_word:
        if letter == guess:
            display += letter
            correct_letters.append(guess)
        elif letter in correct_letters:
            display += letter
        else:
            display += "_"

    print("Word to guess: " + display)

    # TODO-5: - If the letter is not in the chosen_word, print out the letter and let them know it's not in the word.
    #  e.g. You guessed d, that's not in the word. You lose a life.

    if guess not in chosen_word:
        lives -= 1
        print(f"You guessed {guess}, that's not in the word. You lose a life")
        if lives == 0:
            game_over = True

            # TODO 7: - Update the print statement below to give the user the correct word they were trying to guess.
            print(f"************IT WAS {chosen_word} YOU LOSE******************")

    if "_" not in display:
        game_over = True
        print("****************************YOU WIN****************************")

    # TODO-2: - Update the code below to use the stages List from the file hangman_art.py
    print(stages[lives])

 

반복문을 사용했다.

왜냐면 사용자의 목숨이 다하거나 정답을 모두 맞힐 때까지 반복해야 하기 때문이다.

 

for문을 작성하는데 어려웠다.

사용자가 입력한 글자가 정답에 있으면 _ 대신 입력한 글자를 나오게 코딩하였다.

하지만 계속 입력된 글자가 초기화되길래 while문 밖에 correct_letters[ ] 리스트를 만들었다.

 

 

그 결과 위 사진처럼 완벽하게 작동했다.

 

난이도가 너무 어렵다.. 생전 들어도 본적 없는 단어가 자꾸 튀어나온다.

정답 맞힌 사진을 찍어보려다 포기했다

 

아래는 임포트 한 파일들이다.

영단어 리스트는 너무 길어서 따로 구하는 게 좋을 거 같다.

stages = [r'''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', r'''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', r'''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']

logo = r''' 
 _                                             
| |                                            
| |__   __ _ _ __   __ _ _ __ ___   __ _ _ __  
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \ 
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
                    __/ |                      
                   |___/    '''