-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gobang.py
369 lines (322 loc) · 13.9 KB
/
Gobang.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
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
# 2023-05-09 CHEN, KE-RONG
# Gobang Game
from tkinter import *
import tkinter.messagebox
window = Tk()
window.configure(bg="sky blue")
window.title("Gobang") # window title
window.geometry("+400+100") # window position
# ==========<board const variable>==========
GAME_COLUMN_SIZE = 15 # game column size
GAME_ROW_SIZE = 15 # game row size
CANVAS_LINE_OFFSET = 40 # game line offset
GAME_WHITE_SITE = 0 # game white side size
GAME_STAR_X = [3, 3, 11, 11, 7] # game star point position x
GAME_STAR_Y = [3, 11, 3, 11, 7] # game star point position y
GAME_PIECE_SIZE = 15 # game piece size
GAME_STAR_SIZE = 3 # game star circle size
GAME_CLICK_OFFSET = 10 # game click offset
GAME_BOUNDARY_OFFSET = 15 # game boundary offset
GAME_BOUNDARY_SIZE = 3 # game boundary size
PIECE_COLOR = ["black", "white"] # piece color
# ==========<global variable>==========
mouse_click_x = 0 # game mouse click x position
mouse_click_y = 0 # game mouse click y position
board = [] # game data
game_piece_chose = 0 # game piece chose number
game_winner = -1 # winner color
game_over = 0 # game stop
pre_y = -1
pre_x = -1
text_var = StringVar()
canvas = Canvas(window,
bg="sandyBrown", # background color
width=(GAME_COLUMN_SIZE + 1) * CANVAS_LINE_OFFSET + GAME_WHITE_SITE, # width size
height=(GAME_ROW_SIZE + 1) * CANVAS_LINE_OFFSET) # height size
def menu_operation():
main()
def game_exit():
global window
window.destroy()
# ==========<menu>==========
menuBar = Menu(window)
menuFile = Menu(menuBar,
tearoff=False) # set file menu
menuBar.add_cascade(label="set",
font=("Tempus Sans ITC", 15),
menu=menuFile) # menu set
menuFile.add_command(label="New game",
font=("Tempus Sans ITC", 15),
command=menu_operation,
accelerator='(F2)') # new game
window.bind('<F2>', lambda event: menu_operation()) # button bind
menuFile.add_command(label="Exit",
font=("Tempus Sans ITC", 15),
command=game_exit,
accelerator="(Ctrl+Z)") # exit gmae
window.bind('<Control-z>',
lambda event: game_exit()) # button bind
window.config(menu=menuBar)
# ==========<draw the checkerboard>==========
def drawBoardReset():
global canvas
canvas.delete("all")
canvas.grid(row=0, # game grid row position
column=0, # game grid column position
rowspan=10)
# ==========<draw board>==========
for star_run in range(GAME_COLUMN_SIZE):
# ==========<draw column line>==========
canvas.create_line(CANVAS_LINE_OFFSET, # start x position
CANVAS_LINE_OFFSET * star_run + CANVAS_LINE_OFFSET, # start y position
CANVAS_LINE_OFFSET * GAME_COLUMN_SIZE, # end x position
CANVAS_LINE_OFFSET * star_run + CANVAS_LINE_OFFSET) # end y position
# ==========<draw row line>==========
canvas.create_line(CANVAS_LINE_OFFSET * star_run + CANVAS_LINE_OFFSET, # start x position
CANVAS_LINE_OFFSET, # start y position
CANVAS_LINE_OFFSET * star_run + CANVAS_LINE_OFFSET, # end x position
CANVAS_LINE_OFFSET * GAME_COLUMN_SIZE) # end y position
# ==========<draw star>==========
for star_run in range(len(GAME_STAR_X)):
canvas.create_oval(CANVAS_LINE_OFFSET * GAME_STAR_X[star_run] + CANVAS_LINE_OFFSET - GAME_STAR_SIZE,
# start x position
CANVAS_LINE_OFFSET * GAME_STAR_Y[star_run] + CANVAS_LINE_OFFSET - GAME_STAR_SIZE,
# start y position
CANVAS_LINE_OFFSET * GAME_STAR_X[star_run] + CANVAS_LINE_OFFSET + GAME_STAR_SIZE,
# end x position
CANVAS_LINE_OFFSET * GAME_STAR_Y[star_run] + CANVAS_LINE_OFFSET + GAME_STAR_SIZE,
# end y position
fill="black") # fill star color
# ==========<draw column boundary line>==========
for b in range(GAME_BOUNDARY_SIZE):
canvas.create_line(CANVAS_LINE_OFFSET - GAME_BOUNDARY_OFFSET - 2, # start x position
CANVAS_LINE_OFFSET - GAME_BOUNDARY_OFFSET - b, # start y position
GAME_COLUMN_SIZE * CANVAS_LINE_OFFSET + GAME_BOUNDARY_OFFSET + 3, # end x position
CANVAS_LINE_OFFSET - GAME_BOUNDARY_OFFSET - b) # end y position
for b in range(GAME_BOUNDARY_SIZE):
canvas.create_line(CANVAS_LINE_OFFSET - GAME_BOUNDARY_OFFSET - 2, # start x position
GAME_COLUMN_SIZE * CANVAS_LINE_OFFSET + GAME_BOUNDARY_OFFSET + b, # start y position
GAME_COLUMN_SIZE * CANVAS_LINE_OFFSET + GAME_BOUNDARY_OFFSET + 3, # end x position
GAME_COLUMN_SIZE * CANVAS_LINE_OFFSET + GAME_BOUNDARY_OFFSET + b) # end y position
# ==========<draw row boundary line>==========
for b in range(GAME_BOUNDARY_SIZE):
canvas.create_line(CANVAS_LINE_OFFSET - GAME_BOUNDARY_OFFSET - b, # start y position
CANVAS_LINE_OFFSET - GAME_BOUNDARY_OFFSET, # start x position
CANVAS_LINE_OFFSET - GAME_BOUNDARY_OFFSET - b, # end y position
GAME_COLUMN_SIZE * CANVAS_LINE_OFFSET + GAME_BOUNDARY_OFFSET) # end x position
for b in range(GAME_BOUNDARY_SIZE):
canvas.create_line(GAME_COLUMN_SIZE * CANVAS_LINE_OFFSET + GAME_BOUNDARY_OFFSET + b, # start y position
CANVAS_LINE_OFFSET - GAME_BOUNDARY_OFFSET, # start x position
GAME_COLUMN_SIZE * CANVAS_LINE_OFFSET + GAME_BOUNDARY_OFFSET + b, # end y position
GAME_COLUMN_SIZE * CANVAS_LINE_OFFSET + GAME_BOUNDARY_OFFSET) # end x position
def boardDataReset():
# ==========<board data>==========
global board
global text_var
global game_piece_chose
global game_winner
global game_over
global pre_y
global pre_x
pre_y = - 1
pre_x = - 1
del board[:]
game_piece_chose = 0
game_winner = -1
game_over = 0
text_var.set(PIECE_COLOR [game_piece_chose] + "\'s turn")
for y in range(GAME_ROW_SIZE):
board.append([])
for x in range(GAME_COLUMN_SIZE):
board[y].append('-')
return
def mousePosition(event):
# ==========<mouse click position>==========
global mouse_click_y # mouse click y position
global mouse_click_x # mouse click x position
mouse_click_y = event.y # change global y variable
mouse_click_x = event.x # change global x variable
mouseJudge()
return
def mouseJudge():
# ==========<judge mouse click is ok>==========
global mouse_click_y # mouse click y position
global mouse_click_x # mouse click x position
get_y_pos = (mouse_click_y + GAME_CLICK_OFFSET) // CANVAS_LINE_OFFSET
get_x_pos = (mouse_click_x + GAME_CLICK_OFFSET) // CANVAS_LINE_OFFSET
get_y_offset = abs(mouse_click_y - get_y_pos * CANVAS_LINE_OFFSET)
get_x_offset = abs(mouse_click_x - get_x_pos * CANVAS_LINE_OFFSET)
if get_y_offset <= GAME_CLICK_OFFSET and get_x_offset <= GAME_CLICK_OFFSET:
if get_y_pos <= 0 or get_x_pos <= 0 or get_y_pos > GAME_ROW_SIZE or get_x_pos > GAME_COLUMN_SIZE:
# print('click ok but out of range')
return
else:
# print('click yes')
putPiece(get_y_pos, get_x_pos)
return
else:
# print('click not ok')
return
def lastPutPiece():
if pre_y != -1 and pre_x != -1:
# ==========<draw last point>==========
for star_run in range(len(GAME_STAR_X)):
canvas.create_oval(CANVAS_LINE_OFFSET * pre_x + CANVAS_LINE_OFFSET - GAME_STAR_SIZE,
# start x position
CANVAS_LINE_OFFSET * pre_y + CANVAS_LINE_OFFSET - GAME_STAR_SIZE,
# start y position
CANVAS_LINE_OFFSET * pre_x + CANVAS_LINE_OFFSET + GAME_STAR_SIZE,
# end x position
CANVAS_LINE_OFFSET * pre_y + CANVAS_LINE_OFFSET + GAME_STAR_SIZE,
# end y position
fill="red", # fill star color
outline='')
def recoverLastPutPiece():
if pre_y != -1 and pre_x != -1:
# ==========<draw last point>==========
for star_run in range(len(GAME_STAR_X)):
canvas.create_oval(CANVAS_LINE_OFFSET * pre_x + CANVAS_LINE_OFFSET - GAME_STAR_SIZE,
# start x position
CANVAS_LINE_OFFSET * pre_y + CANVAS_LINE_OFFSET - GAME_STAR_SIZE,
# start y position
CANVAS_LINE_OFFSET * pre_x + CANVAS_LINE_OFFSET + GAME_STAR_SIZE,
# end x position
CANVAS_LINE_OFFSET * pre_y + CANVAS_LINE_OFFSET + GAME_STAR_SIZE,
# end y position
fill=PIECE_COLOR[game_piece_chose], # fill star color
outline='')
def checkVictory(get_y, get_x):
global game_winner
column_cnt_w = 0
column_cnt_b = 0
for x in range(get_x - 4, get_x + 5):
if 0 <= x < GAME_COLUMN_SIZE and board[get_y][x] == 'X':
column_cnt_b += 1
column_cnt_w = 0
elif 0 <= x < GAME_COLUMN_SIZE and board[get_y][x] == 'O':
column_cnt_w += 1
column_cnt_b = 0
else:
column_cnt_w = 0
column_cnt_b = 0
if column_cnt_w >= 5:
game_winner = 1
return
if column_cnt_b >= 5:
game_winner = 0
return
row_cnt_w = 0
row_cnt_b = 0
for y in range(get_y - 4, get_y + 5):
if 0 <= y < GAME_ROW_SIZE and board[y][get_x] == 'X':
row_cnt_b += 1
row_cnt_w = 0
elif 0 <= y < GAME_ROW_SIZE and board[y][get_x] == 'O':
row_cnt_w += 1
row_cnt_b = 0
else:
row_cnt_w = 0
row_cnt_b = 0
if row_cnt_w >= 5:
game_winner = 1
return
if row_cnt_b >= 5:
game_winner = 0
return
slopeL_w = 0
slopeL_b = 0
for t in range(-4, 5):
if 0 <= get_y + t < GAME_ROW_SIZE and 0 <= get_x + t < GAME_COLUMN_SIZE and \
board[get_y + t][get_x + t] == 'X':
slopeL_b += 1
slopeL_w = 0
elif 0 <= get_y + t < GAME_ROW_SIZE and 0 <= get_x + t < GAME_COLUMN_SIZE and\
board[get_y + t][get_x + t] == 'O':
slopeL_w += 1
slopeL_b = 0
else:
slopeL_w = 0
slopeL_b = 0
if slopeL_w >= 5:
game_winner = 1
return
if slopeL_b >= 5:
game_winner = 0
return
slopeR_w = 0
slopeR_b = 0
for t in range(-4, 5):
if 0 <= get_y + t < GAME_ROW_SIZE and 0 <= get_x + (-t) < GAME_COLUMN_SIZE and \
board[get_y + t][get_x + (-t)] == 'X':
slopeR_b += 1
slopeR_w = 0
elif 0 <= get_y + t < GAME_ROW_SIZE and 0 <= get_x + (-t) < GAME_COLUMN_SIZE and \
board[get_y + t][get_x + (-t)] == 'O':
slopeR_w += 1
slopeR_b = 0
else:
slopeR_w = 0
slopeR_b = 0
if slopeR_w >= 5:
game_winner = 1
return
if slopeR_b >= 5:
game_winner = 0
return
def putPiece(get_y, get_x):
global game_piece_chose
global text_var
global game_over
global pre_y
global pre_x
if game_over:
return
if board[get_y - 1][get_x - 1] == '-':
canvas.create_oval(get_x * CANVAS_LINE_OFFSET - GAME_PIECE_SIZE, get_y * CANVAS_LINE_OFFSET - GAME_PIECE_SIZE,
get_x * CANVAS_LINE_OFFSET + GAME_PIECE_SIZE, get_y * CANVAS_LINE_OFFSET + GAME_PIECE_SIZE,
fill=PIECE_COLOR[game_piece_chose], tags="piece")
if game_piece_chose == 0:
board[get_y - 1][get_x - 1] = 'X'
game_piece_chose = 1
else:
board[get_y - 1][get_x - 1] = 'O'
game_piece_chose = 0
recoverLastPutPiece()
pre_y = get_y - 1
pre_x = get_x - 1
lastPutPiece()
checkVictory(get_y - 1, get_x - 1)
if game_winner == -1:
text_var.set(PIECE_COLOR [game_piece_chose] + "\'s turn")
else:
text_var.set(PIECE_COLOR[game_winner] + "\'s winner")
game_over = 1
return
else:
# print('This position have other piece')
return
canvas.bind("<Button-1>", mousePosition) # bind left button click and find position
def main():
drawBoardReset()
boardDataReset()
return
button1 = Button(window,
text="Re-Start",
fg='black',
bg='pink',
width=15,
height=4,
command=main)
button1.grid(row=4,
column=1)
text_var.set(PIECE_COLOR [game_piece_chose] + "\'s turn")
text_label = Label(window,
textvariable=text_var,
font=('Helvetica bold', 26),
bg="sky blue",
)
text_label.grid(row=2,
column=1)
if __name__ == '__main__':
main()
window.mainloop()