-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtk_mangorona.py
More file actions
442 lines (404 loc) · 16 KB
/
tk_mangorona.py
File metadata and controls
442 lines (404 loc) · 16 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
'''<h1>Jeu de Mangorona</h1>
<h2>nouveau jeu , new game</h2>
<h3>configuration</h3>
on peut configurer les éléments suivants
pour un nouveau jeu:
<h4>lattice: réseau</h4>
<ul>
<li>star: le réseau habituel du jeu
<li>diamond: idem à star mais inversé.
mais moins intéressant car les coins sont "morts"
<li>cubic: on joue seulement sur les lignes horizontales et
verticales.<!-- intéressant pour une grande dimension et
avec les configurations "maximun" sur "no" et "all"
sur "yes"e&. -->
<li>web: plus de lignes sur le plateau mais il y a des endroits où
on ne peut pas se placer (les intersections à l'intérieur
des cases)
<li>X: idem à web mais sans les lignes horizontales et verticales
</ul>
les deux derniers <!-- ne sont pas vraiment jouables; mais --> ont été
rajouté car ce sont seulement 2 lignes de codes dans le programme entier.
<h4>dimension</h4>
comme son nom l'indique.<br>
on peut rajouter des dimensions, mais cela peut ne pas être
jouable si la dimension est grande et l'écran petit.
<br>pour ajouter les dimensions: dans le code, localiser les
lignes suivantes:
<pre>
list_dimension = ['5 x 7',
'5 x 9',
'7 x 9',
'3 x 5',
'3 x 7',
'5 x 11',
'7 x 11',
'9 x 11']
</pre>
rajouter les dimension voulues, par ex:
<pre>
list_dimension = ['5 x 7',
'5 x 9',
'7 x 9',
'3 x 5',
'3 x 7',
'5 x 11',
'7 x 11',
'9 x 11',
'3 x 9',
'3 x 11',
'5 x 5',]
</pre>
ici on a ajouté:
<pre>
'3 x 9',
'3 x 11',
'5 x 5',
</pre>
enregister et jouer...
<br>
<b>note: les dimensions doivent être des nombres impairs.</b>
<h4>maximum</h4>
AI de la machine.<br>
si sur "yes" la machine essaie de prendre
le maximum de pièces. sinon, elle joue un peu n'importe comment.
<h4>all</h4>
si sur "yes" on peut jouer tant qu'il y ait des pièces
sur le plateau, tant qu'on peut se mouvoir. sinon, le jeu s'arrêtte
dès que l'on ne peut plus prendre de pièces.
<h4>who ?</h4>
qui commence? <br>les rouges.
<hr>
les configurations par défaut:<br>
<ul>
<li>lattice:star
<li>dimension:5 x 9
<li>maximum:yes
<li>all:no
<li>who:machine
</ul>
<h2>charger jeu , load game</h2>
on peut charger les jeux enregistrés avec les 2 programmes,
qu'importe le nom, mais avec les extensions .mang pour pouvoir les
rechercher facilement.
<h2>enregister jeu , save game</h2>
idem pour charger, le nom n'a pas d'importance
<h2>quit</h2>
no comment
<h2>aide</h2>
cette page
'''
import tkinter
import time
import random
import traceback
import webbrowser
import tkinter.scrolledtext as ScrolledText
from tkinter.filedialog import asksaveasfilename, askopenfilename
import pickle
from mangorona import *
from mangorona import __doc__ as maindoc
global root, _trace, w_frame
class TkMangorona(Mangorona, tkinter.Frame):
def __init__(self, master, players, lattice, dimension):
tkinter.Frame.__init__(self, master)
Mangorona.__init__(self, players, lattice, dimension)
self.frame = tkinter.Frame(self)
self.frame.pack()
self.framelog = tkinter.Frame(self)
self.framelog.pack(side=tkinter.BOTTOM)
self.textlog = ScrolledText.ScrolledText(self.framelog, height=5)
self.textlog.pack()
self.canvas = tkinter.Canvas(self.frame, bg='white')
self.canvas.pack()
self.player = {self.player1:'red', self.player2:'blue', self.blank:None}
self.height = self.xmax*50
self.width = self.ymax*50
self.canvas['height'] = self.height
self.canvas['width'] = self.width+50
self.coords = [(x*50+10,y*50+10,x*50+26,y*50+26) for (y,x) in self.all]
self.tag = ['(%i,%i)'%(x,y) for (x,y) in self.all]
for (p,w,h) in [(self.player1,self.width+10, 50),\
(self.player2,self.width+10, self.height-50)]:
self.DisplayGain(p,w,h)
self.Board()
def DisplayGain(self, p, w, h):
self.canvas.create_text(w, h, text='0', fill=self.player[p],
font=('arial', 40, 'bold'), tags=p)
def Board(self):
if self.lattice != 'X':
for i in range(self.xmax):
self.canvas.create_line(18,18+i*50,self.width-32,18+i*50)
for i in range(self.ymax):
self.canvas.create_line(18+i*50,18,18+i*50,self.height-32)
for (x,y) in self.all:
a = [i for i in Position((x,y), self.dimension, self.lattice).around if i in self.all]
for (z,t) in a:
self.canvas.create_line(list(map(lambda i:18+i*50, (y,x)+(t,z))))
def Table(self, m):
for i in self.tag:
self.canvas.delete(i)
##self.canvas.delete('all')
self.pawns = [self.player[m.matrix[x][y]] for (x,y) in self.all]
self.oval = [self.canvas.create_oval(xy,fill=c,outline=c,tags=t)
for (xy,c,t) in zip(self.coords,self.pawns,self.tag)
if c is not None]
for p in [self.player1, self.player2]:
self.canvas.itemconfig(p, text=str(m.gain[p]))
for i in [self.canvas.find_withtag(t) for t in self.oval]:
self.canvas.tag_bind(i, '<Button-1>', self.Start)
self.canvas.tag_bind(i, '<B1-Motion>', self.Move)
self.canvas.tag_bind(i, '<ButtonRelease-1>', self.Destination)
self.frame.update()
moving = False
initial = None
final = None
def Start(self, event):
self.finishmoving()
self.nowtag = self.canvas.find_withtag(tkinter.CURRENT)
self.taginitial = self.canvas.gettags(self.nowtag)
self.initial = eval(self.taginitial[0])
self.tagcolor = self.canvas.itemcget(self.nowtag, 'fill')
self.canvas.itemconfig(self.nowtag, fill='green')
self.startmoving(event)
def Move(self, event):
self.keepmoving(event)
def Destination(self, event):
self.canvas.itemconfig(self.nowtag, fill=self.tagcolor)
self.keepmoving(event)
self.finishmoving()
def startmoving(self, event):
self.moving = False
self.ix = self.lastx = event.x
self.iy = self.lasty = event.y
self.moving = True
def keepmoving(self, event):
if not self.moving:
return
self.canvas.move(self.nowtag, event.x - self.lastx, event.y - self.lasty)
self.lastx = event.x
self.lasty = event.y
xix, yiy = self.initial
xfx = xix + int(self.lasty/50)-int(self.iy/50)
yfy = yiy + int(self.lastx/50)-int(self.ix/50)
self.final = (xfx,yfy)
def finishmoving(self):
self.moving = False
move = (self.initial, self.final)
if all(move):
return move
def TkGamevsMachine(root, players, lattice, dimension, maximum, getall, matrix,
whostart=0, tab=0, gain=None):
global _trace
machineplayer = players[whostart]
mc = Mangorona(players, lattice, dimension, matrix=matrix)
mat = TkMangorona(root, players, lattice, dimension)
mat.pack()
mat.Table(mc)
if gain:
mc.gain = gain
## tab = tab
_trace = [(players, lattice, dimension, maximum, getall, mc.matrix,
whostart, tab, mc.gain)]
while True:
try:
turn = players[tab%2]
movable = AllowableMovement(mc, turn).Move(maximum=maximum, getall=getall)
if turn == machineplayer:
machine = random.choice(movable)
mat.textlog.insert(tkinter.END,
'%s move:%s to %s\n'%(mat.player[turn], str(machine[0]), str(machine[1])))
mat.textlog.see(tkinter.END)
mc.Move(turn, machine[0], machine[1])
tab += 1
mat.Table(mc)
_trace.append((players, lattice, dimension, maximum, getall, mc.matrix,
whostart, tab, mc.gain))
else:
while not mat.moving:
mat.Table(mc)
time.sleep(1)
if mat.moving:
human = mat.finishmoving()
break
mc.Move(turn, human[0], human[1])
mat.textlog.insert(tkinter.END,
'%s move:%s to %s\n'%(mat.player[turn], str(human[0]), str(human[1])))
mat.textlog.see(tkinter.END)
mat.Table(mc)
tab += 1
_trace.append((players, lattice, dimension, maximum, getall, mc.matrix,
whostart, tab, mc.gain))
except NoMoreMove:
exc = traceback.format_exception(*sys.exc_info())[-1]
mat.textlog.insert(tkinter.END, exc)
mat.textlog.insert(tkinter.END, 'winner:%s'%mat.player[mc.Winner()])
mat.textlog.see(tkinter.END)
break
except IllegalMove:
exc = traceback.format_exception(*sys.exc_info())[-1]
mat.textlog.insert(tkinter.END, exc)
except KeyboardInterrupt:
raise SystemExit
except:
traceback.print_exc()
raise SystemExit
def TkTest():
global w, root, _trace
root = tkinter.Tk()
root.title('MANGORONA')
menubar = tkinter.Menu(root)
gamemenu = tkinter.Menu(menubar)
gamemenu.add_command(label="new game", command=new_game)
gamemenu.add_command(label="load game", command=load_game)
gamemenu.add_command(label="save game", command=save_game)
gamemenu.add_separator()
gamemenu.add_command(label="quit", command=root.destroy)
helpmenu = tkinter.Menu(menubar, name='help')
helpmenu.add_command(label="aide...blah blah", command=aide)
menubar.add_cascade(label="game", menu=gamemenu)
menubar.add_cascade(label="Aide", menu=helpmenu)
root['menu']=menubar
root.mainloop()
def new_game():
global root, w_frame
w_frame = tkinter.Frame(root)
w_frame.pack()
tkinter.Label(w_frame, text='configuration').pack(side=tkinter.TOP)
bp = tkinter.Button(w_frame, text="play")
bp.pack(side=tkinter.BOTTOM)
## lattice
menu_lattice = tkinter.Frame(w_frame)
menu_lattice.pack(side=tkinter.LEFT)
tkinter.Label(menu_lattice, text='lattice',
bg='green', fg='blue').pack(anchor=tkinter.N)
scrollbar_l = tkinter.Scrollbar(menu_lattice,
orient=tkinter.VERTICAL)
listbox_l = tkinter.Listbox(menu_lattice,height=3, width=10,
yscrollcommand=scrollbar_l.set,
selectmode=tkinter.SINGLE,
exportselection=0)
scrollbar_l.config(command=listbox_l.yview)
scrollbar_l.pack(side=tkinter.RIGHT, fill=tkinter.Y)
listbox_l.pack(side=tkinter.LEFT,
fill=tkinter.BOTH, expand=tkinter.YES)
list_lattice = ['star', 'diamond', 'cubic', 'web', 'X']
for i in list_lattice:
listbox_l.insert(tkinter.END, i)
listbox_l.select_set(0)
## dimension
menu_dimension = tkinter.Frame(w_frame)
menu_dimension.pack(side=tkinter.LEFT)
tkinter.Label(menu_dimension, text='dimension',
bg='green', fg='blue').pack(anchor=tkinter.N)
scrollbar = tkinter.Scrollbar(menu_dimension,
orient=tkinter.VERTICAL)
listbox = tkinter.Listbox(menu_dimension,height=3, width=10,
yscrollcommand=scrollbar.set,
selectmode=tkinter.SINGLE,
exportselection=0)
scrollbar.config(command=listbox.yview)
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
listbox.pack(side=tkinter.LEFT,
fill=tkinter.BOTH, expand=tkinter.YES)
list_dimension = ['5 x 7',
'5 x 9',
'7 x 9',
'3 x 5',
'3 x 7',
'5 x 11',
'7 x 11',
'9 x 11']
for i in list_dimension:
listbox.insert(tkinter.END, i)
listbox.select_set(1)
## machine rule
menu_machine_rule = tkinter.Frame(w_frame)
menu_machine_rule.pack(side=tkinter.LEFT)
tkinter.Label(menu_machine_rule, text='maximum',
bg='green', fg='blue').pack(anchor=tkinter.N)
machine_rule = tkinter.StringVar()
machine_rule.set('yes')
for t in ['yes', 'no']:
tkinter.Radiobutton(menu_machine_rule, text=t,
variable=machine_rule,
value=t).pack(anchor=tkinter.W)
## game rule
menu_game_rule = tkinter.Frame(w_frame)
menu_game_rule.pack(side=tkinter.LEFT)
tkinter.Label(menu_game_rule, text='all',
bg='green', fg='blue').pack(anchor=tkinter.N)
game_rule = tkinter.StringVar()
game_rule.set('no')
for t in ['yes', 'no']:
tkinter.Radiobutton(menu_game_rule, text=t,
variable=game_rule,
value=t).pack(anchor=tkinter.W)
## who starts
menu_starts_rule = tkinter.Frame(w_frame)
menu_starts_rule.pack(side=tkinter.LEFT)
tkinter.Label(menu_starts_rule, text='who ?',
bg='green', fg='blue').pack(anchor=tkinter.N)
starts_rule = tkinter.StringVar()
starts_rule.set('machine')
for t in ['machine', 'human']:
tkinter.Radiobutton(menu_starts_rule, text=t,
variable=starts_rule,
value=t).pack(anchor=tkinter.W)
TF = {'yes':True,'no':False}
MH = {'machine':0, 'human':1}
bp['command']=lambda :run(list_lattice[int(listbox_l.curselection()[0])],
get_dimension(list_dimension[int(listbox.curselection()[0])]),
TF[machine_rule.get()],
TF[game_rule.get()],
MH[starts_rule.get()])
def get_dimension(d):
l = d.split('x')
return int(l[0]), int(l[1])
def load_game():
global root, _trace
try:
filename = askopenfilename(parent=None,
filetypes=[("mangorona", "*.mang"),
("tous", "*")],
initialfile='save.mang'
)
with open(filename, 'rb') as o:
mmt = pickle.load(o)
players, lattice, dimension, maximum, getall, matrix, whostart, tab, gain = mmt[-1]
TkGamevsMachine(root, players, lattice, dimension, maximum, getall, matrix,
whostart, tab, gain)
except:
pass
def run(lattice, dimension, m_r, g_r, w):
global root, _trace, w_frame
w_frame.destroy()
print(lattice, dimension, m_r, g_r, w)
PLAYERS = 'a','b',' '
TkGamevsMachine(root, PLAYERS, lattice, dimension, m_r, g_r, None, whostart=w)
def save_game():
global _trace
try:
filename = asksaveasfilename(parent=None,
filetypes=[("mangorona", ".mang"),
("tous", "*")],
initialfile='save.mang'
)
with open(filename, 'wb') as s:
pickle.dump(_trace, s)
except:
pass
def aide():
top = tkinter.Toplevel()
txt = tkinter.Text(top)
txt.insert(tkinter.END, maindoc)
txt.pack()
## with open('./mangorona.html', 'w') as wbr:
## wbr.write(__doc__)
## webbrowser.open('./mangorona.html', new=0)
## #print(__doc__)
__version__ = '1.0.0'
__author__ = 'nirinA'
__date__ = 'Sun Dec 18 23:01:41 2011'
if __name__ == '__main__':
TkTest()