Skip to content

Instantly share code, notes, and snippets.

/Guessy Secret

Created February 8, 2016 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/37c039e7abb4789710ee to your computer and use it in GitHub Desktop.
Save anonymous/37c039e7abb4789710ee to your computer and use it in GitHub Desktop.
# 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