-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbalone_main.py
297 lines (257 loc) · 11.5 KB
/
Abalone_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
289
290
291
292
293
294
295
296
297
from math import sqrt
import tkinter
from time import gmtime, asctime
from AbaloneGrid import AbaloneType, AbaloneGame, Position, TypeAction
from game_tools import gui, tools
# see https://github.com/Darkduv/Games # game_tools
def my_print(*args, **kwargs):
"""for the moment critical information (when a bug occurs for example)
It is displayed in the 'console' of your Python editor,
with information on the date."""
print("-", asctime(gmtime()), "GMT", ":", end="\n ")
print(*args, **kwargs)
# Todo : move's counter
# TODO = make a format for 'historic' and provide the option to save a game,
# TODO = import a saved game and its historic.
###########################################
# #
# #
# Game of Abalone 3.3 #
# #
# Implementation in progress #
# #
# #
# Version of 06/30/2018 #
# - Licence : ??? #
###########################################
class Panel(tkinter.Frame):
"""Panel de jeu (grille de n x m cases)"""
margin_int = 3
margin_ext = 5
def __init__(self):
# The panel of game is constituted of a re-scaling grid
# containing it-self a canvas. at each re-scaling of the
# grid,we calculate the tallest size possible for the
# cases (squared) of the grid, et the dimensions of the
# canvas are adapted in consequence.
super().__init__()
self.mode = AbaloneType.NORMAL
self.n_lig, self.n_col = 9, 9 # initial grid = 9 x 9
self.game = AbaloneGame(self.mode)
# Link of the event <resize> with an adapted manager :
self.bind("<Configure>", self.rescale)
# Canvas :
self.can = tkinter.Canvas(
self, bg="dark olive green", borderwidth=0,
highlightthickness=1, highlightbackground="white")
self.width, self.height = 2, 2
self.cote = 0
self._action = [[]]
# Link of the event <click of the mouse> with its manager :
self.can.bind("<Button-1>", self.click)
self.can.bind("<Button1-Motion>", self.mouse_move)
self.can.bind("<Button1-ButtonRelease>", self.mouse_up)
self.can.pack(side=tkinter.LEFT)
# self.side_panel = Label(text="player 1")
# self.side_panel.pack(side=RIGHT)
self.side_panel = tkinter.Canvas(self, bg="white", borderwidth=0,
highlightthickness=1,
highlightbackground="white")
self.side_panel.pack(side=tkinter.RIGHT,
expand=tkinter.YES, fill=tkinter.BOTH)
self.message = tkinter.Label(self.side_panel, text="White's\n turn",
font="Helvetica 18 bold",
bg="white",
borderwidth=0)
self.message.place(relx=0.5, rely=0.33, anchor="center")
self.print_score = tkinter.Label(self.side_panel, text="0 / 0",
font="Helvetica 18 bold",
bg="white",
borderwidth=0)
self.print_score.place(relx=0.5, rely=0.2, anchor="center")
_side_canvas = tkinter.Canvas(self.side_panel,
bg="white", borderwidth=0,
highlightthickness=0,
width=80, height=80)
_side_canvas.place(relx=0.5, rely=0.666, anchor="center")
_circle = _side_canvas.create_oval(0, 0, 79, 79,
outline="red",
width=1, fill="white")
def _config_circle(fill):
_side_canvas.itemconfig(_circle, fill=fill)
self._config_message_circle = _config_circle
self.history = tools.SimpleHistoric()
self.init_jeu()
def init_jeu(self):
self.game.init_game()
self.trace_grille()
def rescale(self, event):
"""Operations made at each rescaling"""
# the properties which are linked to the event of reconfiguration
# contain all the new sizes of the panel :
self.width, self.height = event.width - 4, event.height - 4
# The subtraction of 4 pixels allowed to compensate the width
# of the 'highlight bordure' rolling the canvas
self.trace_grille()
def trace_grille(self):
"""Layout of the grid, in function of dimensions and options"""
# maximal width and height possibles for the cases :
l_max = (self.width - 2 * self.margin_ext) / self.n_col
h_max = (self.height - 2 * self.margin_ext) * 2 / (
2 + sqrt(3) * (self.n_lig - 1))
# the side of a case would be the smallest of the two dimensions :
self.cote = min(l_max, h_max)
# -> establishment of new dimensions for the canvas :
wide, high = self.cote * self.n_col + 2*self.margin_ext,\
self.cote * (1 + (self.n_lig - 1)*sqrt(3)/2) + 2*self.margin_ext
self.can.configure(width=wide, height=high)
# Layout of the grid:
self.can.delete(tkinter.ALL) # erasing of the past Layouts
# Layout of all the pawns,
# white or black according to the state of the game :
for lig in range(self.n_lig):
x0 = (lig - 4) * 1 / 2 * self.cote + self.margin_ext
y1 = self.margin_ext + lig * sqrt(3)/2 * self.cote + self.margin_int
y2 = y1 + self.cote - 2 * self.margin_int
for c in range(self.n_col):
try:
x1 = c * self.cote + x0 + self.margin_int
x2 = x1 + self.cote - 2 * self.margin_int
color = [None, "white", "black"][
self.game.grid[lig, c]]
self.can.create_oval(x1, y1, x2, y2, outline="grey",
width=1, fill=color)
except IndexError:
continue
n_b, n_w = self.game.marbles_down
if self.game.player % 2 == 1:
n_b, n_w = [n_b, n_w][::-1]
self.print_score.config(text=f"{n_b} / {n_w}")
if 6 in self.game.marbles_down:
if self.game.marbles_down[0] == 6:
nb = 1
else:
nb = 0
self.message.config(text=["White", "Black"][nb] + " has won")
return # ?
else:
self.message.config(
text=[None, "White", "Black"][self.game.player+1] + "'s\n turn")
self._config_message_circle(
["red", "white", "black"][self.game.player+1])
def get_space(self, event):
"""get the space linked to the event"""
item, = self.can.find_closest(event.x, event.y)
x0, y0, x1, y1 = self.can.coords(item)
x = (x0 + x1) / 2 - self.margin_ext
y = (y0 + y1) / 2 - self.margin_ext
h = self.cote * sqrt(3) / 2
lig = int(y / h + 1 - 2/sqrt(3))
col = int(x / self.cote - (lig - 4) * 1 / 2)
try:
self.game.grid[lig, col]
except IndexError:
raise tools.InvalidActionError("Selected space not in grid")
return item, lig, col
def click(self, event):
"""Management of the mouse click : move the pawns"""
# We start to determinate the line and the columns of the pawn touched:
item, lig, col = self.get_space(event)
pos = Position(lig, col)
if not self._action:
self._action.append([])
if not self._action[0]:
try:
ok = self.game.check_selection([pos])
except tools.InvalidActionError: # ?
self._action = [[]]
self.trace_grille()
return
if ok:
self._action[0].append(pos)
self.can.itemconfig(item, width=3, outline='red')
else:
self.can.itemconfig(item, width=3, outline='yellow')
self._action.append(pos)
try:
type_action, _ = self.game.check_action(*self._action)
except tools.InvalidActionError:
self._action = [[]]
self.trace_grille()
return
if type_action != TypeAction.INVALID:
self.move()
else:
self.trace_grille()
self._action = [[]]
def mouse_move(self, event):
# We start to determinate the line and the columns of the pawn touched:
if len(self._action[0]) == 3:
return
item, lig, col = self.get_space(event)
pos = Position(lig, col)
if len(self._action) != 1:
return
if self.game.check_selection(self._action[0] + [pos]):
self._action[0].append(pos)
self.can.itemconfig(item, width=3, outline='red')
def mouse_up(self, event):
pass
def move(self):
# todo : for the moment saving blindly, check if move is possible ?
self.history.save_new(self.game.copy())
_ = self.game.play(tuple(self._action))
self.trace_grille()
class AbaloneGUI(tkinter.Tk):
"""corps principal du programme"""
def __init__(self):
super().__init__()
self.geometry("950x750")
self.title(" Game of abalone")
menu_config = [
('File', [('Undo', self.undo),
('Restart', self.reset),
('Quit', self.destroy)]),
('Options', [('Normal', self.normal_mode),
('Split', self.split_mode)]),
('Help', [('Principle of the game', self.principle),
('About...', self.about)])
]
# add options borderwidth=2, relief=GROOVE to Menu ?
self.m_bar = gui.RecursiveMenuBar(self)
self.m_bar.config_menu(menu_config)
self.jeu = Panel()
self.jeu.pack(expand=tkinter.YES, fill=tkinter.BOTH, padx=8, pady=8)
self.bind("<Command-z>", self.undo)
self.bind("<Command-r>", self.reset)
def reset(self, event=None):
""" french! """
self.jeu.history = tools.SimpleHistoric()
self.jeu.init_jeu()
def principle(self):
"""Small description of the principle of this game"""
msg = tkinter.Toplevel(self)
tkinter.Message(msg, bg="navy", fg="ivory", width=400,
font="Helvetica 10 bold",
text="Put six marbles of the adversary "
"out of the grid\n\n"
"Réf : MAXIMIN PUISSANT") \
.pack(padx=10, pady=10)
def about(self):
"""About the development : author and type of licence"""
msg = tkinter.Toplevel(self)
tkinter.Message(msg, width=200, aspect=100, justify=tkinter.CENTER,
text="Jeu de Abalone 3.3\n\n Maximin Duvillard"
"\n Last update : 30/06/2018"
"\n Licence = None").pack(padx=10, pady=10)
def undo(self, event=None):
state = self.jeu.history.undo()
self.jeu.game = state
self.jeu.trace_grille()
def normal_mode(self):
self.jeu.mode = AbaloneType.NORMAL
def split_mode(self):
self.jeu.mode = AbaloneType.SPLIT
if __name__ == '__main__':
Ab = AbaloneGUI()
Ab.mainloop()