-
-
Save anonymous/37c039e7abb4789710ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Borrowed from http://codereview.stackexchange.com/questions/119156/power-guessing-the-number | |
Function Get-ValidGuess([int]$min, [int]$max) { | |
do{ | |
$value = (Read-Host -Prompt "Please enter a number $min and $max") -as [int] | |
} until ($value -ge $min -and $value -le $max) | |
Return $value | |
} | |
Function PlayGame([int]$min, [int]$max, [int]$guessNumber) { | |
Write-Host "This is a guess-the-number game. | |
You have 6 guesses to guess a number between 1 and 100. | |
" | |
[int]$secretNum = Get-Random -minimum $min -maximum ($max + 1) | |
1..$guessNumber | ForEach { | |
[int]$guess = Get-ValidGuess $min $max | |
If ($guess -eq $secretNum) { | |
Write-Host "Correct!" | |
continue | |
} | |
ElseIf ($guess -gt $secretNum) { | |
Write-Host "Too high!" | |
} | |
Else { | |
Write-Host "Too low!" | |
} | |
$remainingGuesses = $guessNumber - $_ | |
Write-Host "You have $remainingGuesses guesses left. | |
" | |
If ($remainingGuesses -eq 0 -and $guess -ne $secretNum) { | |
Write-Host "The number was $secretNum" | |
} | |
} | |
} | |
Function PlayAgain() { | |
$response = Read-Host -Prompt "Would you like to play again? (Enter `"Y`" or `"N`")" | |
Return ($response).ToLower().StartsWith("y") | |
} | |
do { | |
PlayGame 1 100 6 | |
} while (PlayAgain) | |
Pause |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment