Skip to content

Tag: Hangman

How to Code a Hangman Game in PowerShell

Are you looking for a fun project to sharpen your PowerShell skills? Why not try building a classic Hangman game? Hangman is a simple yet entertaining game that challenges players to guess a hidden word letter by letter. In this tutorial, we’ll walk through the process of creating a Hangman game in PowerShell from scratch. We’ll cover everything from generating a random word to handling user input and displaying the game’s progress.

Setting Up the Game

First, let’s define the basic structure of our Hangman game. We’ll need to perform the following steps:

  1. Choose a random word from a predefined list.
  2. Display a series of underscores representing each letter in the word.
  3. Prompt the player to guess a letter.
  4. Check if the guessed letter is in the word.
  5. Update the displayed word with correctly guessed letters.
  6. Keep track of the number of incorrect guesses.
  7. Repeat steps 3-6 until the word is fully guessed or the player runs out of attempts.

The Code

# Hangman Game in PowerShell by ScriptWizards.net

# Define a list of words for the game
$words = "computer", "programming", "hangman", "powershell", "developer", "script", "wizard", "automation"

# Select a random word from the list
$randomWord = Get-Random -InputObject $words

# Convert the word to an array of characters
$hiddenWord = $randomWord.ToCharArray()

# Initialize variables
$displayWord = @('_') * $hiddenWord.Length
$incorrectGuesses = 0
$maxIncorrectGuesses = 6
$lettersGuessed = @()

# Main game loop
while ($incorrectGuesses -lt $maxIncorrectGuesses -and $displayWord -contains '_') {
    Clear-Host
    Write-Host "Guess the word: $($displayWord -join '')"
    Write-Host "Incorrect Guesses: $incorrectGuesses/$maxIncorrectGuesses"
    Write-Host "Letters Guessed: $($lettersGuessed -join ', ')"

    # Prompt for user input
    $guess = Read-Host "Enter a letter"

    # Validate input
    if ($guess -match '^[a-zA-Z]$') {
        $guess = $guess.ToLower()

        # Check if the letter has already been guessed
        if ($lettersGuessed -contains $guess) {
            Write-Host "You've already guessed that letter. Try again."
            Start-Sleep -Seconds 2
            continue
        }

        # Add the guessed letter to the list
        $lettersGuessed += $guess

        # Check if the guessed letter is in the word
        $correctGuess = $false
        for ($i = 0; $i -lt $hiddenWord.Length; $i++) {
            if ($hiddenWord[$i] -eq $guess) {
                $displayWord[$i] = $guess
                $correctGuess = $true
            }
        }
        if (-not $correctGuess) {
            $incorrectGuesses++
        }
    } else {
        Write-Host "Invalid input. Please enter a single letter."
        Start-Sleep -Seconds 2
    }
}

# Check for game outcome
if ($incorrectGuesses -ge $maxIncorrectGuesses) {
    Clear-Host
    Write-Host "You lose! The word was: $randomWord"
} else {
    Clear-Host
    Write-Host "Congratulations! You guessed the word: $randomWord"
}

How It Works

  • We start by defining a list of words and selecting a random word from it.
  • The game loop continues until either the word is fully guessed or the player exceeds the maximum number of incorrect guesses.
  • Inside the loop, the player is prompted to guess a letter. The input is validated to ensure it’s a single letter.
  • If the guessed letter is in the word, the corresponding underscores in the displayed word are replaced with the correct letter.
  • If the guessed letter is not in the word, the number of incorrect guesses is incremented.
  • The game ends with a win message if the word is fully guessed, or a lose message if the player exceeds the maximum allowed incorrect guesses.

Error Handling

  • Input validation ensures that the player enters only single letters. If invalid input is provided, the player is prompted to try again.
  • If the player guesses a letter that has already been guessed, they are notified and prompted to try again.
  • If the player exceeds the maximum number of incorrect guesses, they lose the game, and the correct word is revealed.

Example Input and Output

Now that you’ve built your own Hangman game in PowerShell, feel free to customize it further or challenge your friends to play! Happy coding!

© 2024 ScriptWizards.net - Powered by Coffee & Magic