-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ_032_150xp_hangman.nim
More file actions
65 lines (47 loc) · 1.17 KB
/
Q_032_150xp_hangman.nim
File metadata and controls
65 lines (47 loc) · 1.17 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import strutils
import random
import helpers
const
words = [
"hello",
"world",
]
maxTries = 3
when isMainModule:
randomize()
let
word = sample(words).toLowerAscii.strip
var
masked = "_".repeat(len(word))
remaining = maxTries
incorrect: seq[char] = @[]
while masked != word and remaining > 0:
echo "Word: ", masked
echo "Remaining: ", remaining
echo "Incorrect: ", incorrect.join("")
let input = askForInput("Guess a character").toLowerAscii
if input.len != 1:
echo "Enter SINGLE letter"
continue
let character = input[0]
if character in incorrect or character in masked:
echo "You have picked this character. Try again."
continue
if character in word:
# Nope. grug, remember: complexity bad
#
# let indices = toSeq(0 ..< len(word)).filter(
# (i: int)->bool => word[i] == character
# )
# for i in indices:
# masked[i] = character
#
for i, c in word:
if c == character:
masked[i] = character
else:
dec remaining
if masked == word:
echo "You won!"
else:
echo "You Lose!"