top of page

25% Discount For All Pricing Plans "welcome"

Hangman Game


What is Hangman?


Hangman is a game based on word guessing. The player tries to guess the letters of a hidden word. As incorrect guesses are made, a "man" faces the danger of being hanged. The aim of the game is to correctly guess the word before the man is completely hanged. Hangman is known as a fun and educational game and is especially played to improve vocabulary.


Project Goals and Student Benefits

With this project, students will reinforce their skills in developing simple and functional programs using basic Python knowledge. Additionally, by understanding how text-based games are coded, they will gain experience in game simulation and algorithm development.


Project Outcomes

  • Creating a game simulation using basic Python structures and control flows.

  • Receiving data from the user and managing the game flow by processing this data.

  • Writing more modular and readable code with the help of functions.


Step-by-Step Tasks of the Project

  1. Creating a word list and selecting a random word.

  2. Starting the game and defining the necessary variables.

  3. Player guessing letters and checking the guesses.

  4. Displaying the progress of the game and the player's status on the screen.

  5. Determining the result of the game and displaying it on the screen.

  6. Making the code more modular using functions.


Step 1: Creating a Word List and Selecting a Random Word

As the first step, a list of words to be used will be created, and a random word will be selected from this list.

import random

# Word list
kelime_listesi = ["python", "hangman", "challenge", "programming", "development"]

# Selecting a random word
gizli_kelime = random.choice(kelime_listesi)

This code snippet creates the word list to be used in the game and selects a random word from this list.


Step 2: Starting the Game and Defining the Necessary Variables

In the second step, the game will be started, and the necessary variables will be defined.

# Starting the game and defining the necessary variables
tahmin_edilenler = []  # Letters guessed by the player
yanlis_tahmin_sayisi = 0  # Number of incorrect guesses
maksimum_yanlis_tahmin = 6  # Maximum allowed incorrect guesses
oyun_bitti = False

print(f"The word consists of {len(gizli_kelime)} letters.")

This code snippet defines the necessary variables for the game and informs the player about the number of letters in the hidden word.


Step 3: Player Guessing Letters and Checking the Guesses

In the third step, the player will guess letters and these guesses will be checked.

# Player guessing letters and checking the guesses
while not oyun_bitti:
    tahmin = input("Guess a letter: ").lower()

    if tahmin in tahmin_edilenler:
        print("You already guessed this letter. Try another one.")
        continue

    tahmin_edilenler.append(tahmin)

    if tahmin in gizli_kelime:
        print("Correct guess!")
    else:
        print("Incorrect guess!")
        yanlis_tahmin_sayisi += 1

    kelime_durumu = [harf if harf in tahmin_edilenler else "_" for harf in gizli_kelime]
    print("Word: ", " ".join(kelime_durumu))
    print(f"Number of incorrect guesses: {yanlis_tahmin_sayisi}")

    if yanlis_tahmin_sayisi >= maksimum_yanlis_tahmin:
        print("Unfortunately, you lost! The hidden word was:", gizli_kelime)
        oyun_bitti = True
    elif "_" not in kelime_durumu:
        print("Congratulations! You guessed the word correctly:", gizli_kelime)
        oyun_bitti = True

This code snippet allows the player to guess letters and checks these guesses. Correct and incorrect guesses are displayed on the screen, and the player's current status is updated.


Step 4: Displaying the Progress of the Game and the Player's Status

In the fourth step, the progress of the game and the player's status will be displayed on the screen.

    kelime_durumu = [harf if harf in tahmin_edilenler else "_" for harf in gizli_kelime]
    print("Word: ", " ".join(kelime_durumu))
    print(f"Number of incorrect guesses: {yanlis_tahmin_sayisi}")

    if yanlis_tahmin_sayisi >= maksimum_yanlis_tahmin:
        print("Unfortunately, you lost! The hidden word was:", gizli_kelime)
        oyun_bitti = True
    elif "_" not in kelime_durumu:
        print("Congratulations! You guessed the word correctly:", gizli_kelime)
        oyun_bitti = True

This code snippet displays the player's current status and the parts of the word that have been guessed. It checks whether the game is over and ends the game.


Step 5: Determining the Results and Displaying Them on the Screen

In the fifth step, the results of the game are determined and displayed on the screen.

    if yanlis_tahmin_sayisi >= maksimum_yanlis_tahmin:
        print("Unfortunately, you lost! The hidden word was:", gizli_kelime)
        oyun_bitti = True
    elif "_" not in kelime_durumu:
        print("Congratulations! You guessed the word correctly:", gizli_kelime)
        oyun_bitti = True

This code snippet checks whether the player has won or lost and displays the result on the screen.


Step 6: Solution with Functions


Function to Select a Random Word

This function allows the selection of a random word from the word list.

def rastgele_kelime_sec(kelime_listesi):
    """Selects a random word from the word list."""
    return random.choice(kelime_listesi)

Selects and returns a random word from the word list. This function is used to determine the word to be used at the beginning of the game.


Function to Update the Word Status

This function updates the current status of the word according to the guessed letters.

def kelime_durumunu_guncelle(gizli_kelime, tahmin_edilenler):
    """Updates the current status of the word according to the guessed letters."""
    return [harf if harf in tahmin_edilenler else "_" for harf in gizli_kelime]

Updates and returns the current status of the word according to the hidden word and guessed letters. This function is used to keep track of the player's correct guesses.


Function to Check the Player's Guess

This function checks the letter guessed by the player and determines whether it is correct or incorrect.

def tahmini_kontrol_et(tahmin, gizli_kelime, tahmin_edilenler, yanlis_tahmin_sayisi):
    """Checks the letter guessed by the player and determines whether it is correct or incorrect."""
    if tahmin in tahmin_edilenler:
        return "You already guessed this letter. Try another one.", yanlis_tahmin_sayisi

    tahmin_edilenler.append(tahmin)

    if tahmin in gizli_kelime:
        return "Correct guess!", yanlis_tahmin_sayisi
    else:
        return "Incorrect guess!", yanlis_tahmin_sayisi + 1

Checks the letter guessed by the player and determines whether it is correct or incorrect. Additionally, it updates the guessed letters and increases the number of incorrect guesses.


Function to Start the Game

This function starts the Hangman game and manages the flow of the game.

def oyunu_baslat(kelime_listesi):
    """Starts the Hangman game."""
    gizli_kelime = rastgele_kelime_sec(kelime_listesi)
    tahmin_edilenler = []
    yanlis_tahmin_sayisi = 0
    maksimum_yanlis_tahmin = 6
    oyun_bitti = False

    print(f"The word consists of {len(gizli_kelime)} letters.")

    while not oyun_bitti:
        tahmin = input("Guess a letter: ").lower()
        mesaj, yanlis_tahmin_sayisi = tahmini_kontrol_et(tahmin, gizli_kelime, tahmin_edilenler, yanlis_tahmin_sayisi)
        print(mesaj)

        kelime_durumu = kelime_durumunu_guncelle(gizli_kelime, tahmin_edilenler)
        print("Word: ", " ".join(kelime_durumu))
        print(f"Number of incorrect guesses: {yanlis_tahmin_sayisi}")

        if yanlis_tahmin_sayisi >= maksimum_yanlis_tahmin:
            print("Unfortunately, you lost! The hidden word was:", gizli_kelime)
            oyun_bitti = True
        elif "_" not in kelime_durumu:
            print("Congratulations! You guessed the word correctly:", gizli_kelime)
            oyun_bitti = True

# Creating the word list
kelime_listesi = ["python", "hangman", "challenge", "programming", "development"]

# Starting the game
oyunu_baslat(kelime_listesi)

This function starts the Hangman game and manages the flow of the game. It takes the player's guesses, checks whether the guesses are correct or incorrect, and determines the result of the game.


Topics Covered

  • Data Types: In this project, lists (word list, guessed letters) and strings (hidden word, guess) are used.

  • Lists: Guessed letters and word status are kept in the form of lists.

  • Conditional Statements: if-else structures are used to check the player's guesses and game status.

  • Functions: Functions ensure that the code is modular and reusable.

  • Loops: while loops are used to take the player's guesses and manage the flow of the game.

  • Random Module: The random.choice() function is used to select a random word from the word list.

コメント


コメント機能がオフになっています。
bottom of page