I'm unable to get the switch statement in my guess the number game to work. Can you tell me what I'm missing?
$playGame = "Yes" #Controls when to quit the game
$randomNo = New-Object System.Random #This variable stores a random object
$status = "Play" #Controls the current round of play
$guess = 0 #Stores the player's guess
$reply = "" #Stores the players's response when asked to play again
$noOfGuesses = 0 #Keeps track of the number of games user has played
$choice = 0
$number = 0
#Loop until the player decides to quit the game
while ($playGame -ne "No") {
Clear-Host
#Loop until the player guesses the secret number
while ($status -ne "Stop") {
#Prompt the player to guess a number
while ($guess -eq "") {
clear-Host
write-Host
#Collect the player's guess
$guess = Read-Host " Select a level of play."
if ($guess -eq "Q") {
Clear-Host
Write-Host "`n`n"
Write-Host " Game over. Thanks for playing."
Write-Host "`n`n`n`n`n`n`n`n"
Write-Host " Press Enter to continue."
Read-Host
$status = "stop"
$playgame = "No"
continue
}
$choice = $guess
switch ($choice)
{
1 {$number = $randomNo.Next(1,11)}
2 {$number = $randomNo.Next(1,51)}
3 {$number = $randomNo.Next(1,101)}
default {write-host "Invalid input. Please try again."
}
if ($guess -lt $number) { #The player's guess was too low
Clear-Host
Write-Host "`n Sorry. Your guess was too low. Please pick a higher number."
write-host "`n Press Enter to guess again."
$guess = "" #reset the player's guess
Read-Host
}
elseif ($guess -gt $number) { #The player's guess was too high
Clear-Host
Write-Host "`n Sorry. Your guess was too high. Try and pick a" `
"mid-range number."
write-host "`n Press Enter to guess again."
$guess = "" #Reset the player's guess
Read-Host
}
else { #The player has guessed the game's secret number
Clear-Host
Write-Host "`n Congratulations. You guessed my number! Press Enter" `
"to continue."
$status = "stop" #Reset the player's guess
Read-Host
}
}
#Prompt the player to play another round
while ($reply -eq "") {
Clear-Host
Write-Host
#Collect the player's answer
$reply = Read-Host " Would you like to play again? (Y/N) "
#Validate player input, allowing only Y and N as acceptable responses
if (($reply -ne "Y") -and ($reply -ne "N")) {
$reply = "" #Reset the variable to its default value
}
}
$noOfgames++
#The player has elected to play again
if ($reply -eq "Y") {
#Reset variable to their default values
$number = 0
$noOfGuesses = 0
$status = "Play"
$guess = 0
}
elseif ($reply -eq "N") {
#The player has decided to quit playing
$playGame = "No" #Modify variable indicating time to terminate gameplay
clear-host
Write-Host "`n Game Statistics"
Write-Host " _________________________________________"
Write-Host "`n You played $noOfGames game(s).`n"
write-host "__________________________________________"
write-host "`n The number of low guesses were $lowguess."
write-host "`n The number of High guesses were $highguess."
write-host "`n`n`n`n`n`n`n`n`n`n` Press Enter to continue."
read-host
}
}
}
Clear-host