/
numguess.ps1
48 lines (39 loc) · 1.37 KB
/
numguess.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"Welcome to NumGuess PowerShell script version!"
""
$name = read-host "Enter your name"
if ($name -eq "") { $name = "Player" }
""
$limit = (read-host "Welcome $name, enter upper limit") -as [int]
if ($limit -eq $null -or $limit -lt 10) { $limit = 10 }
$max_tries = [math]::floor([math]::log($limit, 2)) + 1
do {
$num = get-random -minimum 1 -maximum $limit
$tries = 0
""
"Guess my number between 1 and $limit!"
""
do {
$guess = (read-host "Guess") -as [int]
if ($guess -eq $null) { "That's just plain wrong." }
elseif ($guess -lt 1 -or $guess -gt $limit) { "Out of range." }
else {
$tries += 1
if ($guess -lt $num) { "Too low!" }
elseif ($guess -gt $num) { "Too high!" }
}
} until($guess -eq $num)
$tries_word = @{$true="try";$false="tries"}[$tries -eq 1]
""
"Well done $name, you guessed my number from $tries $tries_word!"
if ($tries -eq 1) { "You're one lucky bastard!" }
elseif ($tries -lt $max_tries) { "You are the master of this game!" }
elseif ($tries -eq $max_tries) { "You are a machine!" }
elseif ($tries -le $max_tries * 1.1) { "Very good result!" }
elseif ($tries -le $limit) { "Try harder, you can do better!" }
else { "I find your lack of skill disturbing!" }
# Have to call read-host without prompt to avoid appended colon
write-host "Play again [y/N]? " -NoNewLine
$again = (read-host).ToUpper()
} while ($again -eq "Y")
""
"Okay, bye."