patternshellMinor
Power-Guessing The Number
Viewed 0 times
numberguessingthepower
Problem
I decided to help @Matt out a bit and ask a PowerShell question. This is a simple guess-the-number game learning how to use functions with and without parameters, and use the input and output functions. All comments are welcome.
Function GetNumber([int]$min, [int]$max) {
Return Get-Random -minimum $min -maximum ($max + 1)
}
Function PlayGame() {
Write-Host "This is a guess-the-number game.`r`nYou have 6 guesses to guess a number between 1 and 100."
$secretNum = GetNumber 1 100
[int]1..6 | ForEach {
$guess = Read-Host
If ($guess -eq $secretNum) {
Write-Host "Correct!"
Return
}
ElseIf ($guess -gt $secretNum) {
Write-Host "Too high!"
}
Else {
Write-Host "Too low!"
}
$remainingGuesses = 6 - $_
Write-Host "You have $remainingGuesses guesses left"
}
Write-Host "The number was $secretNum"
}
PlayGame
# Borrowed from https://technet.microsoft.com/en-us/library/ff730938.aspx
Write-Host "Press any key to continue ..."
$host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")Solution
I have three points to make:
The naming indicates it may be for integer conversion. While the truth is far from that, renaming might be a good idea.
Instead of having the function outside the main loop, have the max passed into
Multiline strings:
Instead of including new line characters in the string, use a multiline string, like so:
Note that any indentation you add ends up in the resulting string.
Type comparison
In PowerShell, as written in this Stack Overflow post:
On comparison a right value is converted to the type of a left value. Thus,
This is a potential bug. I would recommend a helper method that converts the input to an integer, and tests its boundaries. Obviously in a number guessing game, 100000000 isn't a valid guess for a limit of 100. Not to mention the above type coercion.
If you don't really care about people putting in massive amounts because they're idiots, than you can just swap the order of the operators, and the guess will be coerced to an integer.
GetNumber:The naming indicates it may be for integer conversion. While the truth is far from that, renaming might be a good idea.
Instead of having the function outside the main loop, have the max passed into
PlayGame and simply do the operation in the game loop:Function PlayGame([int]$max) {
#...
$secretNum = Get-Random -minimum 1 -maximum ($max + 1)
#...
}
PlayGame 100Multiline strings:
Instead of including new line characters in the string, use a multiline string, like so:
Write-Host @"
This is a guess-the-number game.
You have 6 guesses to guess a number between 1 and 100.
"@Note that any indentation you add ends up in the resulting string.
Type comparison
In PowerShell, as written in this Stack Overflow post:
On comparison a right value is converted to the type of a left value. Thus,
'2' -gt 9 becomes '2' -gt '9', that is False, and '2' -gt 10 becomes '2' -gt '10', that is True.This is a potential bug. I would recommend a helper method that converts the input to an integer, and tests its boundaries. Obviously in a number guessing game, 100000000 isn't a valid guess for a limit of 100. Not to mention the above type coercion.
If you don't really care about people putting in massive amounts because they're idiots, than you can just swap the order of the operators, and the guess will be coerced to an integer.
Code Snippets
Function PlayGame([int]$max) {
#...
$secretNum = Get-Random -minimum 1 -maximum ($max + 1)
#...
}
PlayGame 100Write-Host @"
This is a guess-the-number game.
You have 6 guesses to guess a number between 1 and 100.
"@Context
StackExchange Code Review Q#119156, answer score: 8
Revisions (0)
No revisions yet.