-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastermind2.py
60 lines (43 loc) · 1.19 KB
/
mastermind2.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
#!/usr/bin/env python
"""
Solving & playing MASTERMIND game
Restarted for keep it simple. Combination are char strings that could be
reduced with a to_int conversion in base $n_{colors}$.
"""
class MasterAnswer:
"""
Represents a answer of the master
rcrp means "right color right place"
rcbp means "right color bad place"
"""
def __init__(self, rcrp = 0, rcbp = 0):
self.rcrp = rcrp
self.rcbp = rcbp
def inc_rcrp(self):
self.rcrp += 1
def inc_rcbp(self):
self.rcbp += 1
def __add__(self, ma):
self.rcrp += ma.rcrp
self.rcbp += ma.rcbp
class Combination:
"""
Represent a combination
"""
def __init__(self, comb = "0110", n_colors = 6):
assert len(comb) > 0
self.comb = comb
assert n_colors > 1 # the game is not funny with 1 color ;)
self.n_colors = n_colors
def len(self):
return(len(self.comb))
def dec(self):
return(Combination(self.comb[1:], self.n_colors))
def __getitem__(self, index):
return(self.comb[index])
def __mod__(self, comb2, combr2=""):
assert self.comb.len() == comb2.len()
if self.comb.len ==0:
return(MasterAnswer())
if self.comb[0] == self.comb2[0]:
return(MasterAnswer(1, 0) + self.comb.dec() % comb2.dec())