-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
74 lines (56 loc) · 2.03 KB
/
Game.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
70
71
72
73
74
import random
COLOURS = ['R', 'G', 'Y', 'B', 'W', 'O']
TRIES = 10
COLOUR_LENGTH = 4
def random_colour():
randomised = []
for i in range(COLOUR_LENGTH):
colour = random.choice(COLOURS)
randomised.append(colour)
return randomised
def guess():
while True:
user_colours = input("Guess the colour: ").upper().split(" ")
if len(user_colours) != COLOUR_LENGTH:
print(f"Error 0: Need {COLOUR_LENGTH} colours")
continue
for colours in user_colours:
if colours not in COLOURS:
print(f"Error 1: Can only have {COLOURS} colours")
break
else:
break
return user_colours
def check_colours(guess_code, real_code):
colour_counts = {}
correct_pos = 0
incorrect_pos = 0
for colour in real_code:
if colour not in colour_counts:
colour_counts[colour] = 0
colour_counts[colour] += 1
for guess_colour, real_colour in zip(guess_code, real_code):
if guess_colour == real_colour:
correct_pos += 1
colour_counts[guess_colour] -= 1
for guess_colour, real_colour in zip(guess_code, real_code):
if guess_colour in colour_counts:
if colour_counts[guess_colour] > 0:
incorrect_pos += 1
colour_counts[guess_colour] -= 1
return correct_pos, incorrect_pos
def game():
print(f"Welcome to Mastermind, you have {TRIES} tries to guess the code")
print(f"The valid colours are", *COLOURS)
randomised = random_colour()
for attempts in range(0, TRIES):
guessed = guess()
correct_pos, incorrect_pos = check_colours(guessed, randomised)
if correct_pos == COLOUR_LENGTH:
print("Well Done, The code was", *randomised, f"and it took you {attempts + 1} tries!")
break
print(f"Correct Positions: {correct_pos}, Incorrect Positions: {incorrect_pos}")
else:
print("You Lose! the code was", *randomised)
if __name__ == "__main__":
game()