Created
May 27, 2020 21:00
-
-
Save btipling/379e76d11ab85df8d5f295e6c38fb722 to your computer and use it in GitHub Desktop.
A 1 to 10 number guessing game in PowerShell
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
function Write-Text([string]$text) { | |
Write-Host -ForegroundColor Green $text | |
} | |
function Remove-Value([int]$value, [int[]]$list) { | |
[int[]] $new_list = $null | |
foreach ($v in $list) { | |
if ($v -ne $value) { | |
$new_list += $v | |
} | |
} | |
$new_list | |
} | |
$possibilities = 1..10 | |
Write-Text -text "Hello! Think of a number from 1-10, I will try to guess it." | |
:game while ($true) { | |
$guess = Get-Random $possibilities | |
Write-Text -text ("Is your number " + [string]$guess + "?") | |
$response = Read-Host "Please type y, n, or q" | |
switch ($response) { | |
"y" { | |
Write-Text -text "Yay I got it! Thanks for playing. Bye!" | |
break game | |
} | |
"n" { | |
Write-Text "I will try again!" | |
$possibilities = Remove-Value -value $guess -list $possibilities | |
if ($possibilities.Count -lt 2) { | |
Write-Text -text "Oh, I guess it must have been $possibilities! Too bad I didn't guess your number. Bye!" | |
break game | |
} | |
continue | |
} | |
"q" { | |
Write-Text -text "OK, we can play again some other time. Bye!" | |
break game | |
} | |
default { | |
Write-Text -text "I don't know what that means, please type y for yes, n for no or q for quit next time, thanks!" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment