-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_terminal.py
69 lines (61 loc) · 2.3 KB
/
game_terminal.py
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
66
67
68
69
import random
def validate_guess(guess, ans):
# iterates through guess and answer lists element-by-element. Whenever it finds a match,
# removes the value from a copy of answer so that nothing is double counted.
hints = []
ans_no_match = []
guess_no_match = []
for guess_elem, ans_elem in zip(guess, ans):
if guess_elem == ans_elem:
hints.append("B")
else:
guess_no_match.append(guess_elem)
ans_no_match.append(ans_elem)
for guess_elem in guess_no_match:
if guess_elem in ans_no_match:
hints.append("W")
ans_no_match.remove(guess_elem)
return hints
# main loop
# almost no error handling at all, but this is just to demonstrate
while True:
answer = [random.randint(0, 4) for _ in range(5)]
answer_str = "".join([str(num) for num in answer])
guesses_remaining = 6
while guesses_remaining > 0:
while True:
guess_str = input("Guess a 5 number combination using 0 - 4: ")
guess = [int(num) for num in guess_str]
if len(guess) != 5:
print("Guess must be 5 numbers exactly")
continue
else:
break
hints = validate_guess(guess, answer)
random.shuffle(hints)
hints = "".join(hints)
if hints == "BBBBB":
print(hints, "\n")
print(f"You win! Well done! Answer was {answer_str}")
while True:
restart_q = input("Do you want to play again? (Y \\ N): ")
if restart_q == "Y":
print("Restarting game...")
break
elif restart_q == "N":
exit()
else:
guesses_remaining -= 1
if guesses_remaining == 0:
print(hints, "\n")
print(f"You lose! Correct answer: {answer_str}")
while True:
restart_q = input("Do you want to play again? (Y \\ N): ")
if restart_q == "Y":
print("Restarting game...")
break
elif restart_q == "N":
exit()
else:
print(hints, "\n")
print(f"Guesses Remaining: {guesses_remaining}")