-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolver.py
80 lines (65 loc) · 2.48 KB
/
Solver.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
import random
import itertools
COLOURS = ['R', 'G', 'Y', 'B', 'W', 'O']
TRIES = 10
COLOUR_LENGTH = 4
COMBINATION_LENGTH = len(COLOURS) ** COLOUR_LENGTH
def generate_secret_code():
randomised = []
for i in range(COLOUR_LENGTH):
colour = random.choice(COLOURS)
randomised.append(colour)
return randomised
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 minimax(combinations):
# Generate all possible feedback combinations and store them in a list
all_possible_feedbacks = [(i, j - i) for i in range(5) for j in range(5)]
print(all_possible_feedbacks)
best_score = len(combinations)
print(best_score)
best_guess = None
for guess in combinations:
scores = []
for feedback in all_possible_feedbacks:
remaining_codes = [code for code in combinations if check_colours(guess, code) == feedback]
score = len(remaining_codes)
scores.append(score)
guess_score = max(scores)
if guess_score < best_score:
best_score = guess_score
best_guess = guess
return best_guess
def mastermind_solver():
combinations = list(itertools.product(COLOURS, repeat=COLOUR_LENGTH))
secret_code = generate_secret_code()
print("code =", secret_code)
guess = minimax(combinations)
for attempts in range(0, TRIES):
print(f"Guess {attempts}: {guess}")
correct_positions, incorrect_positions = check_colours(guess, secret_code)
if correct_positions == COLOUR_LENGTH:
print("Secret code found!")
break
combinations = [code for code in combinations if check_colours(guess, code) == (correct_positions, incorrect_positions)]
guess = minimax(combinations)
else:
print("You Lose! the code was", *secret_code)
if __name__ == '__main__':
mastermind_solver()