-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31. Mastermind.py
82 lines (56 loc) · 1.89 KB
/
31. Mastermind.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
75
76
77
78
79
80
81
82
import random
COLORS = ['P', 'B', 'R', 'Y', 'O', 'W']
Tries = 7
CODE_LENGTH = 4
def generate():
code = []
for _ in range(CODE_LENGTH):
color = random.choice(COLORS)
code.append(color)
return code
code = generate()
def guess_code():
while True:
guess = input("Guess the colors: ").upper().split(" ")
if len(guess) != CODE_LENGTH:
print(f"You must make {CODE_LENGTH} guess.")
break
for color in guess:
if color not in COLORS:
print(f"Invalid Guess: {color}. Try Again.")
break
else:
break
return guess
def check(guess, real):
color_count = {}
correct_pos = 0
incorrect_pos = 0
for color in real:
if color not in color_count:
color_count[color] = 0
color_count[color] += 1
for guess, real in zip(guess, real):
if guess == real:
correct_pos += 1
color_count[guess] -= 1
for guess, real in zip(guess, real):
if guess in real and color_count[guess] > 0:
incorrect_pos += 1
color_count[guess] -= 1
return correct_pos, incorrect_pos
def game():
print(f"Welcome to Matermind Game. You have {Tries} tries to guess the colors. Enjoy!!!")
print("Valid colors are:", *COLORS)
code = generate()
for attempts in range(1, Tries+1):
guess = guess_code()
correct_pos, incorrect_pos = check(guess, code)
if correct_pos == CODE_LENGTH:
print(f"Congratulations! You have guessed the code in {attempts} tries.")
break
print(f"Correct Positions:{correct_pos} | Incorrect Positions: {incorrect_pos}")
else:
print("You ran out of tries. The code was: ", *code)
if __name__ == "__main__":
game()