-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolver.py
59 lines (47 loc) · 2.31 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
import pickle
import itertools
import collections
from dict_maker import score_guess
# this is a one-time use class at the moment since internal variables get modified.
# copying them was too time and memory intensive, think it's better to just load
# from stored_objects every time
class MastermindSolver:
def __init__(self, answer):
self.all_answers = itertools.product(["R", "G", "B", "Y", "P"], repeat=5)
self.all_answers = {"".join(answer) for answer in self.all_answers}
self.guesses_left = 6
self.guess_history = []
self.hint_history = []
self.answer = answer
FileStore = open("stored_objects/score_dict.pickle", "rb")
self.score_dict = pickle.load(FileStore)
FileStore.close()
def make_guess(self):
guesses_to_try = []
for guess, scores_by_answer_dict in self.score_dict.items():
# reduce possible_score_dict to only include possible answers
scores_by_answer_dict = {answer: score for answer, score in scores_by_answer_dict.items()
if answer in self.all_answers}
self.score_dict[guess] = scores_by_answer_dict
# find how often a score appears in scores_by_answer_dict, get max
possibilities_per_score = collections.Counter(scores_by_answer_dict.values())
worst_case = max(possibilities_per_score.values())
# prefer possible guesses over impossible ones
impossible_guess = guess not in self.all_answers
guesses_to_try.append((worst_case, impossible_guess, guess))
return min(guesses_to_try)[-1]
def solve(self):
while self.guesses_left > 0:
if self.guesses_left == 6:
guess = "RRGGB"
else:
guess = self.make_guess()
self.guess_history.append(guess)
# reduce amount of possible answers by checking answer against guess and score
score = score_guess(guess, self.answer)
self.hint_history.append(score)
self.all_answers = {answer for answer in self.all_answers
if self.score_dict[guess][answer] == score}
self.guesses_left -= 1
if guess == self.answer:
return self.guess_history, self.hint_history