/
Main.py
288 lines (212 loc) · 9.37 KB
/
Main.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*- coding: utf-8 -*-
# This file is the Main. You can run this file to play.
# Please don't change the code below !
try:
# for Python2 (Linux)
from Tkinter import *
except ImportError:
# for Python3 (Windows)
from tkinter import *
from Grid import *
from Hanjie_Graphics import *
from Utility import *
import Editor, sys
from AI import *
def pointerB1 (event):
"""Change the white box in black and reciprocally with the left click"""
global Origin
global Sens
global Mark
coor=(int(event.x),int(event.y))
if coor[0] > ig.Decalage_droite and coor[0] < ig.Decalage_droite + grid.getLargeur()*ig.Proportion:
if coor[1] > ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut and coor[1] < ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut+ grid.getHauteur()*ig.Proportion:
x = int((coor[0]-ig.Decalage_droite)/ig.Proportion)
y = int((coor[1]-(ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut))/ig.Proportion)
if grid.getValeur(x, y) == 0 or grid.getValeur(x, y) == 2:
grid.setValeur(x, y, 1)
Swap(x, y, 1, ig)
else:
grid.setValeur(x, y, 0)
Draw(grid, ig)
if testEnd(Secret_grid, grid) == True:
End(ig, Secret_grid,fenetre)
ig.Zone_dessin.unbind("<Button-1>")
ig.Zone_dessin.unbind("<Button-3>")
ig.Zone_dessin.unbind("<B1-Motion>")
ig.Zone_dessin.unbind("<B3-Motion>")
Origin = (x,y,grid.getValeur(x,y))
Sens = ""
Mark = [(x, y)]
def pointerB3 (event):
"""Add or remove the cross with the right click"""
global Origin
global Sens
global Mark
coor=(int(event.x),int(event.y))
if coor[0] > ig.Decalage_droite and coor[0] < ig.Decalage_droite + grid.getLargeur()*ig.Proportion:
if coor[1] > ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut and coor[1] < ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut+ grid.getHauteur()*ig.Proportion:
x = int((coor[0]-ig.Decalage_droite)/ig.Proportion)
y = int((coor[1]-(ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut))/ig.Proportion)
if grid.getValeur(x, y) == 0 :
grid.setValeur(x, y, 2)
Swap(x, y, 2, ig)
elif grid.getValeur(x, y) == 1:
grid.setValeur(x, y, 2)
Draw(grid, ig)
else:
grid.setValeur(x, y, 0)
Draw(grid, ig)
print(grid.getValeur(x,y))
Origin = (x,y,grid.getValeur(x,y))
Sens = ""
Mark = [(x, y)]
def slideB1(event):
"""Change the white box in black and reciprocally with the left click being held down"""
global Origin
global Sens
global Mark
coor=[int(event.x),int(event.y)]
if coor[0] > ig.Decalage_droite and coor[0] < ig.Decalage_droite + grid.getLargeur()*ig.Proportion:
if coor[1] > ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut and coor[1] < ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut+ grid.getHauteur()*ig.Proportion:
x = int((coor[0]-ig.Decalage_droite)/ig.Proportion)
y = int((coor[1]-(ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut))/ig.Proportion)
if Sens == "" and x != Origin[0]:
Sens = "h"
elif Sens == "" and y != Origin[1]:
Sens = "v"
if not (x, Origin[1]) in Mark:
if Sens == "h" and Origin[2] == 0:
grid.setValeur(x, Origin[1], 0)
Draw(grid, ig)
Mark = Mark + [(x, Origin[1])]
elif Sens == "h" and Origin[2] == 1:
grid.setValeur(x, Origin[1], 1)
Swap(x, Origin[1], 1, ig)
Mark = Mark + [(x, Origin[1])]
if testEnd(Secret_grid, grid) == True:
End(ig, Secret_grid,fenetre)
ig.Zone_dessin.unbind("<Button-1>")
ig.Zone_dessin.unbind("<Button-3>")
ig.Zone_dessin.unbind("<B1-Motion>")
ig.Zone_dessin.unbind("<B3-Motion>")
if not (Origin[0], y) in Mark:
if Sens == "v" and Origin[2] == 0:
grid.setValeur(Origin[0], y, 0)
Draw(grid, ig)
Mark = Mark + [(Origin[0], y)]
elif Sens == "v" and Origin[2] == 1:
grid.setValeur(Origin[0], y, 1)
Swap(Origin[0], y, 1, ig)
Mark = Mark + [(Origin[0], y)]
if testEnd(Secret_grid, grid) == True:
End(ig, Secret_grid,fenetre)
ig.Zone_dessin.unbind("<Button-1>")
ig.Zone_dessin.unbind("<Button-3>")
ig.Zone_dessin.unbind("<B1-Motion>")
ig.Zone_dessin.unbind("<B3-Motion>")
def slideB3(event):
"""Add or remove the cross with the right click being held down"""
global Origin
global Sens
global Mark
coor=[int(event.x),int(event.y)]
if coor[0] > ig.Decalage_droite and coor[0] < ig.Decalage_droite + grid.getLargeur()*ig.Proportion:
if coor[1] > ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut and coor[1] < ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut+ grid.getHauteur()*ig.Proportion:
x = int((coor[0]-ig.Decalage_droite)/ig.Proportion)
y = int((coor[1]-(ig.NbMaxIndiceH*ig.Proportion+ig.Decalage_haut))/ig.Proportion)
if Sens == "" and x != Origin[0]:
Sens = "h"
elif Sens == "" and y != Origin[1]:
Sens = "v"
if not (x, Origin[1]) in Mark:
if Sens == "h" and Origin[2] == 2:
grid.setValeur(x, Origin[1], 2)
Draw(grid, ig)
Mark = Mark + [(x, Origin[1])]
elif Sens == "h" and Origin[2] != 2:
grid.setValeur(x, Origin[1], 0)
Draw(grid, ig)
Mark = Mark + [(x, Origin[1])]
if not (Origin[0], y) in Mark:
if Sens == "v" and Origin[2] == 2:
grid.setValeur(Origin[0], y, 2)
Draw(grid, ig)
Mark = Mark + [(Origin[0], y)]
elif Sens == "v" and Origin[2] != 2:
grid.setValeur(Origin[0], y, 0)
Draw(grid, ig)
Mark = Mark + [(x, Origin[1])]
def effacer():
"""Delete all boxs"""
grid.Reset()
Draw(grid, ig)
def retour():
"""Back to the menu"""
for widget in fenetre.winfo_children():
widget.destroy()
im = menu(fenetre)
im.Bouton_jouer.config(command=jouer)
im.Bouton_editeur.config(command=editeur)
def jouer():
"""Prints all levels"""
global my_listbox
for widget in fenetre.winfo_children():
widget.destroy()
il = Lvl(fenetre)
my_listbox = il.Listbox_niveau
il.Bouton_lancer.config(command=lancer_niveau)
il.Bouton_retour.config(command=retour)
il.Bouton_AI.config(command=ia)
def editeur():
"""Open the editor to create your own painting"""
Editor.editor()
def ia():
"""It's the computer who plays"""
global ig
global grid
global Secret_grid
Nom = my_listbox.get(my_listbox.curselection())
for widget in fenetre.winfo_children():
widget.destroy()
Secret_grid = Grid()
Secret_grid.Load(Nom)
grid = SolveAI(Secret_grid)
ig = Game(Secret_grid, fenetre)
ig.Bouton_Retour.config(command=retour)
Draw(grid,ig)
def lancer_niveau():
"""Launch a level"""
global ig
global grid
global Secret_grid
Nom = my_listbox.get(my_listbox.curselection())
for widget in fenetre.winfo_children():
widget.destroy()
Secret_grid = Grid()
Secret_grid.Load(Nom)
grid = Grid(Secret_grid.getLargeur(),Secret_grid.getHauteur())
ig = Game(Secret_grid, fenetre)
ig.Zone_dessin.bind("<Button-1>",pointerB1)
ig.Zone_dessin.bind("<Button-3>",pointerB3)
ig.Zone_dessin.bind("<B1-Motion>",slideB1)
ig.Zone_dessin.bind("<B3-Motion>",slideB3)
ig.Bouton_Effacer.config(command=effacer)
ig.Bouton_Retour.config(command=retour)
global my_listbox
global ig
global grid
global Secret_grid
global fenetre
#Fenetre
fenetre=Tk()
fenetre.geometry("700x800")
fenetre.title("Hanjie")
fenetre.resizable(width=False, height=False)
if sys.platform == 'win32' or sys.platform=='win64':
fenetre.iconbitmap('./res/icon/' + os.sep + 'icon.ico')
elif sys.platform == 'linux2':
fenetre.iconbitmap('@' + './res/icon/' + os.sep + 'icon.xbm')
im = menu(fenetre)
im.Bouton_jouer.config(command=jouer)
im.Bouton_editeur.config(command=editeur)
fenetre.mainloop()