-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.py
179 lines (140 loc) · 5.45 KB
/
python.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
from tkinter import *
from tkinter.messagebox import *
import random
from combinatorics import all_colours
def inconsistent(p, guesses):
for guess in guesses:
res = check(guess[0], p)
(rightly_positioned, permutated) = guess[1]
if res != [rightly_positioned, permutated]:
return True # inconsistent
return False # i.e. consistent
def answer_ok(a):
(rightly_positioned, permutated) = a
if (rightly_positioned + permutated > number_of_positions) \
or (rightly_positioned + permutated < len(colours) - number_of_positions):
return False
if rightly_positioned == 3 and permutated == 1:
return False
return True
def get_evaluation():
rightly_positioned = int(entryWidget_both.get())
permutated = int(entryWidget_only_colours.get())
return (rightly_positioned, permutated)
def new_evaluation(current_colour_choices):
rightly_positioned, permutated = get_evaluation()
if rightly_positioned == number_of_positions:
return (current_colour_choices, (rightly_positioned, permutated))
if not answer_ok((rightly_positioned, permutated)):
print("Erreur!!!!!")
return (current_colour_choices, (-1, permutated))
guesses.append((current_colour_choices, (rightly_positioned, permutated)))
view_guesses()
current_colour_choices = create_new_guess()
show_current_guess(current_colour_choices)
if not current_colour_choices:
return (current_colour_choices, (-1, permutated))
return (current_colour_choices, (rightly_positioned, permutated))
def check(p1, p2):
blacks = 0
whites = 0
for i in range(len(p1)):
if p1[i] == p2[i]:
blacks += 1
else:
if p1[i] in p2:
whites += 1
return [blacks, whites]
def create_new_guess():
next_choice = next(permutation_iterator)
while inconsistent(next_choice, guesses):
try:
next_choice = next(permutation_iterator)
except StopIteration:
print("Erreur!!")
return ()
return next_choice
def new_evaluation_tk():
global current_colour_choices
res = new_evaluation(current_colour_choices)
current_colour_choices = res[0]
def show_current_guess(new_guess):
row = 1
Label(root, text=" Nouvelle réponse: ").grid(row=row,
column=0,
columnspan=4)
row += 1
col_count = 0
for c in new_guess:
print(c)
l = Label(root, text=" ", bg=c)
l.grid(row=row, column=col_count, sticky=W, padx=2)
col_count += 1
def view_guesses():
row = 3
Label(root, text="Ancienne réponses").grid(row=row,
column=0,
columnspan=4)
Label(root, text="c&p").grid(row=row,
padx=5,
column=number_of_positions + 1)
Label(root, text="p").grid(row=row,
padx=5,
column=number_of_positions + 2)
# dummy label for distance:
Label(root, text=" ").grid(row=row,
column=number_of_positions + 3)
row += 1
# vertical dummy label for distance:
Label(root, text=" ").grid(row=row,
column=0,
columnspan=5)
for guess in guesses:
guessed_colours = guess[0]
col_count = 0
row += 1
for c in guessed_colours:
print(guessed_colours[col_count])
l = Label(root, text=" ", bg=guessed_colours[col_count])
l.grid(row=row, column=col_count, sticky=W, padx=2)
col_count += 1
# evaluation:
for i in (0, 1):
l = Label(root, text=str(guess[1][i]))
l.grid(row=row, column=col_count + i + 1, padx=2)
if __name__ == "__main__":
colours = ["rouge", "vert", "bleu", "jaune", "orange", "rose"]
guesses = []
number_of_positions = 4
permutation_iterator = all_colours(colours, number_of_positions)
current_colour_choices = next(permutation_iterator)
new_guess = (current_colour_choices, (0, 0))
row_offset = 1
root = Tk()
root.title("Mastermind")
root["padx"] = 30
root["pady"] = 20
entryLabel = Label(root)
entryLabel["text"] = "Correct:"
entryLabel.grid(row=row_offset,
sticky=E,
padx=5,
column=number_of_positions + 4)
entryWidget_both = Entry(root)
entryWidget_both["width"] = 5
entryWidget_both.grid(row=row_offset, column=number_of_positions + 5)
entryLabel = Label(root)
entryLabel["text"] = "Position KO:"
entryLabel.grid(row=row_offset + 1,
sticky=E,
padx=5,
column=number_of_positions + 4)
entryWidget_only_colours = Entry(root)
entryWidget_only_colours["width"] = 5
entryWidget_only_colours.grid(row=row_offset + 1, column=number_of_positions + 5)
submit_button = Button(root, text="envoyer", command=new_evaluation_tk)
submit_button.grid(row=4, column=number_of_positions + 4)
quit_button = Button(root, text="Quitter", command=root.quit)
quit_button.grid(row=4, column=number_of_positions + 5)
show_current_guess(current_colour_choices)
root.mainloop()