forked from socksinbed/384-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastermind_testing.py
216 lines (165 loc) · 4.92 KB
/
mastermind_testing.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from mastermind import *
import time
def get_blacks(code, guess):
blacks = 0
for i, x in enumerate(code):
if guess[i] == x:
blacks += 1
return blacks
#actual whites = get_whites - get_blacks
def get_whites(code, guess):
whites = 0
lguess = list(guess) #make list so can remove
for x in code:
if x in lguess:
lguess.remove(x)
whites += 1
return whites
def test(code, colors, pegs, strategy, printing=True):
m = Mastermind(colors, pegs)
initial_guess = None
if colors > 9 or colors < 2:
print("Invalid number of colors. Choose between (2-9)")
return
if pegs > 4 or pegs < 2:
print("Invalid number of pegs. Choose between (2-4)")
return
if pow(colors, pegs) > 1296:
print("Warning! Very large search space!\nThis may timeout or take an extremely long time.")
if strategy == 'dk': # donald knuth 5 guess algorithm
guesser = m.best_guess
initial_guess = first_guess(colors, pegs)
elif strategy == 'dkr': # donald knuth on only remaining
guesser = m.best_guess_from_remaining
initial_guess = first_guess(colors, pegs)
elif strategy == 'minimax': # min maxes, no first guess, same as dk
guesser = m.best_guess
elif strategy == 'mmr': # min max with only remaining guesses
guesser = m.best_guess_from_remaining
elif strategy == 'random': # random guesses from remaining
guesser = m.random_guess
else:
print("Invalid strategy")
return
guesses = 0
blacks = 0
whites = 0
stime = time.process_time()
while blacks != pegs:
if guesses == 0 and initial_guess:
next_guess = initial_guess
else:
next_guess = guesser()
whites = get_whites(code, next_guess)
blacks = get_blacks(code, next_guess)
guesses += 1
m.eliminate(next_guess, (whites - blacks, blacks))
ftime = time.process_time() - stime
if printing:
print("Time taken {} colors, {} pegs, code: {}: {} seconds".format(colors, pegs, code, ftime))
print("Guesses taken: {}".format(guesses))
return (ftime, guesses)
# for benchmarking tests using random as one test
# is not indicative of overall performance.
def average_test(code, colors, pegs):
guesses = 0
max_num = 0
times = 0
max_time = 0
for i in range(100):
(time, guess) = test(code, colors, pegs, 'random', False)
guesses += guess
times += time
if guess > max_num:
max_num = guess
if time > max_time:
max_time = time
return (max_num, guesses / 100, max_time, time / 100)
def doallrand(colours, pegs):
guesses = 0
codes = 0
max_num = 0
max_t = 0
time = 0
for code in Mastermind(colours, pegs).codes():
(m, guess, m_t, t) = average_test(code, colours, pegs)
guesses += guess
time += t
if m > max_num:
max_num = m
if m_t > max_t:
max_t = m_t
codes += 1
print("avg time: " + str(time/codes))
print("max time: " + str(max_t))
print("max num: " + str(max_num))
print("avg num: " + str(guesses/codes))
return
def doall(colours, pegs, strategy):
guesses = 0
codes = 0
max_num = 0
max_t = 0
time = 0
for code in Mastermind(colours, pegs).codes():
(t, g) = test(code, colours, pegs, strategy, False)
guesses += g
time += t
if g > max_num:
max_num = g
if t > max_t:
max_t = t
codes += 1
print("avg time: " + str(time/codes))
print("max time: " + str(max_t))
print("max num: " + str(max_num))
print("avg num: " + str(guesses/codes))
return guesses
if __name__ == '__main__':
print("Tests for Random Guess")
print("----- 3,3")
doallrand(3,3)
print("----- 4,3")
doallrand(4,3)
print("----- 5,3")
doallrand(5,3)
print("----- 6,3")
doallrand(6,3)
print("----- 4,4")
doallrand(4,4)
print("----- 5,4")
doallrand(5,4)
print("----- 6,4")
doallrand(6,4)
print("\n")
print("Tests for Donald Knuth's Five Guess Algorithm")
print("----- 3,3")
doall(3,3,'dk')
print("----- 4,3")
doall(4,3,'dk')
print("----- 5,3")
doall(5,3,'dk')
print("----- 6,3")
doall(6,3,'dk')
print("----- 4,4")
doall(4,4,'dk')
print("----- 5,4")
doall(5,4,'dk')
print("----- 6,4")
doall(6,4,'dk')
print("\n")
print("Tests for Donald Knuth’s Algorithm on Remaining Guesses")
print("----- 3,3")
doall(3,3,'dkr')
print("----- 4,3")
doall(4,3,'dkr')
print("----- 5,3")
doall(5,3,'dkr')
print("----- 6,3")
doall(6,3,'dkr')
print("----- 4,4")
doall(4,4,'dkr')
print("----- 5,4")
doall(5,4,'dkr')
print("----- 6,4")
doall(6,4,'dkr')