-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
520 lines (445 loc) · 19.5 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
from tkinter import *
from tkinter import messagebox
board = [['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'],
['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'],
['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']
]
buttons = []
cell_size = 60
piece_size = 40
piece_selected = False
turn = "white"
select_memory = {"piece": None, "coordinates": None}
log_history = []
########################## Initialise #######################
window = Tk()
window.title("Chess")
icon = PhotoImage(file="graphics/chess_icon.png") #pyimage1
white_block = PhotoImage(file="graphics/white_block.PNG") #pyimage2
gray_block = PhotoImage(file="graphics/gray_block.PNG") #pyimage3
sel_block = PhotoImage(file="graphics/turquoise_block.PNG") #pyimage4
move_block = PhotoImage(file="graphics/purple_block.PNG") #pyimage6
capture_block = PhotoImage(file="graphics/red_block.PNG") #pyimage7
window.iconphoto(False, icon)
window.geometry("570x600")
window.resizable(False, False)
# TODO: Create the alphabet and numeric notations on the sides
####################### Functions #########################
def reset_board():
global buttons
skin = ("helvetica", piece_size)
buttons = []
set_buttons(skin)
# TODO: Need to find a new font
def change_skin_to_arial():
global buttons
skin = ("serif", piece_size)
buttons = []
set_buttons(skin)
def set_buttons(skin):
for i in range(8):
buttons.append([])
for j in range(8):
buttons[i].append(Button(window, text=board[i][j]))
buttons[i][j].configure(command= lambda i=i, j=j: piece_control(i, j))
buttons[i][j].config(height=cell_size, width=cell_size, font=skin)
buttons[i][j].grid(row=i, column=j, padx=0, pady=0, sticky="nsew")
refresh_board()
def refresh_board():
for i in range(8):
for j in range(8):
if (i+j)%2 == 0:
buttons[i][j].config(image=white_block, compound="center")
else:
buttons[i][j].config(image=gray_block, compound="center")
def convert_coordinates(i1, j1, i2, j2):
x1 = "abcdefgh"[j1]
x2 = "abcdefgh"[j2]
y1 = "87654321"[i1]
y2 = "87654321"[i2]
return x1, y1, x2, y2
def scan_game_over():
white_king, black_king = False, False
for i in range(8):
for j in range(8):
if board[i][j] == "♔":
white_king = True
elif board[i][j] == "♚":
black_king = True
if not white_king:
messagebox.showinfo("Info", "Black Wins")
elif not black_king:
messagebox.showinfo("Info", "White Wins")
def end_turn(piece, coordinates, motion):
global piece_selected, turn, select_memory
x1, y1, x2, y2 = convert_coordinates(select_memory['coordinates']['x'], select_memory['coordinates']['y'], coordinates['x'], coordinates['y'])
log = f"{select_memory['piece']} {x1}{y1} {motion} {piece} {x2}{y2}"
if select_memory["piece"] == "♙" and select_memory["coordinates"]["x"] == 1 and coordinates["x"] == 0:
promote("white")
elif select_memory["piece"] == "♟" and select_memory["coordinates"]["x"] == 6 and coordinates["x"] == 7:
promote("black")
log_history.append(log)
update_log()
piece_selected = False
select_memory = {"piece": None, "coordinates": None}
if turn == "white":
turn = "black"
menu_bar.delete("White's Turn")
menu_bar.add_cascade(label="Black's Turn")
else:
turn = "white"
menu_bar.delete("Black's Turn")
menu_bar.add_cascade(label="White's Turn")
refresh_board()
scan_game_over()
def promote(c):
global select_memory, promotion_window
small_font = ("helvetica", 20)
promotion_window = Tk()
promotion_window.title("Select")
promotion_window.geometry("207x55")
promotion_window.resizable(False, False)
if c == "white":
Button(promotion_window, text="♘", font=small_font,
command=lambda color=c, p="♘", root=promotion_window: promote_to(color, p, root)).grid(row=0, column=1)
Button(promotion_window, text="♗", font=small_font,
command=lambda color=c, p="♗", root=promotion_window: promote_to(color, p, root)).grid(row=0, column=2)
Button(promotion_window, text="♖", font=small_font,
command=lambda color=c, p="♖", root=promotion_window: promote_to(color, p, root)).grid(row=0, column=3)
Button(promotion_window, text="♕", font=small_font,
command=lambda color=c, p="♕", root=promotion_window: promote_to(color, p, root)).grid(row=0, column=4)
elif c == "black":
Button(promotion_window, text="♞", font=small_font,
command=lambda color=c, p="♞", root=promotion_window: promote_to(color, p, root)).grid(row=0, column=1)
Button(promotion_window, text="♝", font=small_font,
command=lambda color=c, p="♝", root=promotion_window: promote_to(color, p, root)).grid(row=0, column=2)
Button(promotion_window, text="♜", font=small_font,
command=lambda color=c, p="♜", root=promotion_window: promote_to(color, p, root)).grid(row=0, column=3)
Button(promotion_window, text="♛", font=small_font,
command=lambda color=c, p="♛", root=promotion_window: promote_to(color, p, root)).grid(row=0, column=4)
def promote_to(color, p, root):
global piece, coordinates, select_memory
tile["text"] = p
board[coordinates["x"]][coordinates["y"]] = p
promoted_to = p
if color == "white":
log_history.append(f"♙ ⚜ {promoted_to}")
elif color == "black":
log_history.append(f"♟ ⚜ {promoted_to}")
update_log()
root.destroy()
def compare(i, j, max):
if max:
if i >= j:
return i
else:
return j
else:
if i >= j:
return j
else:
return i
# TODO: Get rid of repetitiveness
def knight(i, j, text):
if i >= 2:
if j >= 1: # 11:00 direction
if buttons[i - 2][j - 1]["text"] == " ":
buttons[i - 2][j - 1]["image"] = move_block
elif buttons[i - 2][j - 1]["text"] in text:
buttons[i - 2][j - 1]["image"] = capture_block
if j <= 6: # 1:00 direction
if buttons[i - 2][j + 1]["text"] == " ":
buttons[i - 2][j + 1]["image"] = move_block
elif buttons[i - 2][j + 1]["text"] in text:
buttons[i - 2][j + 1]["image"] = capture_block
if i <= 5:
if j >= 1: # 7:00 direction
if buttons[i + 2][j - 1]["text"] == " ":
buttons[i + 2][j - 1]["image"] = move_block
elif buttons[i + 2][j - 1]["text"] in text:
buttons[i + 2][j - 1]["image"] = capture_block
if j <= 6: # 5:00 direction
if buttons[i + 2][j + 1]["text"] == " ":
buttons[i + 2][j + 1]["image"] = move_block
elif buttons[i + 2][j + 1]["text"] in text:
buttons[i + 2][j + 1]["image"] = capture_block
if j >= 2:
if i >= 1: # 11:00 direction
if buttons[i - 1][j - 2]["text"] == " ":
buttons[i - 1][j - 2]["image"] = move_block
elif buttons[i - 1][j - 2]["text"] in text:
buttons[i - 1][j - 2]["image"] = capture_block
if i <= 6: # 8:00 direction
if buttons[i + 1][j - 2]["text"] == " ":
buttons[i + 1][j - 2]["image"] = move_block
elif buttons[i + 1][j - 2]["text"] in text:
buttons[i + 1][j - 2]["image"] = capture_block
if j <= 5:
if i >= 1: # 2:00 direction
if buttons[i - 1][j + 2]["text"] == " ":
buttons[i - 1][j + 2]["image"] = move_block
elif buttons[i - 1][j + 2]["text"] in text:
buttons[i - 1][j + 2]["image"] = capture_block
if i <= 6: # 4:00 direction
if buttons[i + 1][j + 2]["text"] == " ":
buttons[i + 1][j + 2]["image"] = move_block
elif buttons[i + 1][j + 2]["text"] in text:
buttons[i + 1][j + 2]["image"] = capture_block
def bishop(i, j, text):
if i >= 1 and j >= 1: # 10:30 direction
for shift in range(1, compare(i, j, False) + 1):
if buttons[i - shift][j - shift]["text"] == " ":
buttons[i - shift][j - shift]["image"] = move_block
elif buttons[i - shift][j - shift]["text"] in text:
buttons[i - shift][j - shift]["image"] = capture_block
break
else:
break
if i >= 1 and j <= 6: # 1:30 direction
for shift in range(1, compare(i, 7 - j, False) + 1):
if buttons[i - shift][j + shift]["text"] == " ":
buttons[i - shift][j + shift]["image"] = move_block
elif buttons[i - shift][j + shift]["text"] in text:
buttons[i - shift][j + shift]["image"] = capture_block
break
else:
break
if i <= 6 and j >= 1: # 7:30 direction
for shift in range(1, compare(7 - i, j, False) + 1):
if buttons[i + shift][j - shift]["text"] == " ":
buttons[i + shift][j - shift]["image"] = move_block
elif buttons[i + shift][j - shift]["text"] in text:
buttons[i + shift][j - shift]["image"] = capture_block
break
else:
break
if i <= 6 and j <= 6: # 4:30 direction
for shift in range(1, compare(7 - i, 7 - j, False) + 1):
if buttons[i + shift][j + shift]["text"] == " ":
buttons[i + shift][j + shift]["image"] = move_block
elif buttons[i + shift][j + shift]["text"] in text:
buttons[i + shift][j + shift]["image"] = capture_block
break
else:
break
def rook(i, j, text):
if j >= 1: # 9:00 direction
for shift in range(1, j+1):
if buttons[i][j - shift]["text"] == " ":
buttons[i][j - shift]["image"] = move_block
elif buttons[i][j - shift]["text"] in text:
buttons[i][j - shift]["image"] = capture_block
break
else:
break
if i >= 1: # 12:00 direction
for shift in range(1, i+1):
if buttons[i - shift][j]["text"] == " ":
buttons[i - shift][j]["image"] = move_block
elif buttons[i - shift][j]["text"] in text:
buttons[i - shift][j]["image"] = capture_block
break
else:
break
if j <= 6: # 3:00 direction
for shift in range(1, 8-j):
if buttons[i][j + shift]["text"] == " ":
buttons[i][j + shift]["image"] = move_block
elif buttons[i][j + shift]["text"] in text:
buttons[i][j + shift]["image"] = capture_block
break
else:
break
if i <= 6: # 6:00 direction
for shift in range(1, 8-i):
if buttons[i + shift][j]["text"] == " ":
buttons[i + shift][j]["image"] = move_block
elif buttons[i + shift][j]["text"] in text:
buttons[i + shift][j]["image"] = capture_block
break
else:
break
def king(i, j, text):
if i >= 1 and j >= 1: # 10:30 direction
if buttons[i - 1][j - 1]["text"] == " ":
buttons[i - 1][j - 1]["image"] = move_block
elif buttons[i - 1][j - 1]["text"] in text:
buttons[i - 1][j - 1]["image"] = capture_block
if i >= 1 and j <= 6: # 1:30 direction
if buttons[i - 1][j + 1]["text"] == " ":
buttons[i - 1][j + 1]["image"] = move_block
elif buttons[i - 1][j + 1]["text"] in text:
buttons[i - 1][j + 1]["image"] = capture_block
if i <= 6 and j >= 1: # 7:30 direction
if buttons[i + 1][j - 1]["text"] == " ":
buttons[i + 1][j - 1]["image"] = move_block
elif buttons[i + 1][j - 1]["text"] in text:
buttons[i + 1][j - 1]["image"] = capture_block
if i <= 6 and j <= 6: # 4:30 direction
if buttons[i + 1][j + 1]["text"] == " ":
buttons[i + 1][j + 1]["image"] = move_block
elif buttons[i + 1][j + 1]["text"] in text:
buttons[i + 1][j + 1]["image"] = capture_block
if j >= 1: # 9:00 direction
if buttons[i][j - 1]["text"] == " ":
buttons[i][j - 1]["image"] = move_block
elif buttons[i][j - 1]["text"] in text:
buttons[i][j - 1]["image"] = capture_block
if i >= 1: # 12:00 direction
if buttons[i - 1][j]["text"] == " ":
buttons[i - 1][j]["image"] = move_block
elif buttons[i - 1][j]["text"] in text:
buttons[i - 1][j]["image"] = capture_block
if j <= 6: # 3:00 direction
if buttons[i][j + 1]["text"] == " ":
buttons[i][j + 1]["image"] = move_block
elif buttons[i][j + 1]["text"] in text:
buttons[i][j + 1]["image"] = capture_block
if i <= 6: # 6:00 direction
if buttons[i + 1][j]["text"] == " ":
buttons[i + 1][j]["image"] = move_block
elif buttons[i + 1][j]["text"] in text:
buttons[i + 1][j]["image"] = capture_block
def select_piece(i, j):
global piece_selected, select_memory, tile
piece_selected = True
tile["image"] = sel_block
select_memory = {"piece": piece, "coordinates": coordinates}
# TODO:En passant capture
# TODO:Castling, Check and Checkmate features.
if piece == "♙": # White Pawn
if i == 6 and buttons[i - 1][j]["text"] == " ": # Two space movement at first move
buttons[i - 1][j]["image"] = move_block
if buttons[i - 2][j]["text"] == " ":
buttons[i - 2][j]["image"] = move_block
elif buttons[i - 1][j]["text"] == " " and i != 0: # Single space movement for every other situation
buttons[i - 1][j]["image"] = move_block
if j != 0 and buttons[i - 1][j - 1]["text"] in "♟♞♝♜♛♚": # Capture
buttons[i - 1][j - 1]["image"] = capture_block
if j != 7 and buttons[i - 1][j + 1]["text"] in "♟♞♝♜♛♚":
buttons[i - 1][j + 1]["image"] = capture_block
elif piece == "♟": # Black Pawn
if i == 1 and buttons[i+1][j]["text"] == " ": # Two space movement at first move
buttons[i + 1][j]["image"] = move_block
if buttons[i + 2][j]["text"] == " ":
buttons[i + 2][j]["image"] = move_block
elif buttons[i + 1][j]["text"] == " " and i != 7: # Single space movement for every other situation
buttons[i + 1][j]["image"] = move_block
if j != 0 and buttons[i + 1][j - 1]["text"] in "♙♘♗♖♕♔": # Capture
buttons[i + 1][j - 1]["image"] = capture_block
if j != 7 and buttons[i + 1][j + 1]["text"] in "♙♘♗♖♕♔":
buttons[i + 1][j + 1]["image"] = capture_block
elif piece == "♘": # White Knight
knight(i, j, "♟♞♝♜♛♚")
elif piece == "♞": # Black Knight
knight(i, j, "♙♘♗♖♕♔")
elif piece == "♗": # White Bishop
bishop(i, j, "♟♞♝♜♛♚")
elif piece == "♝": # Black Bishop
bishop(i, j, "♙♘♗♖♕♔")
elif piece == "♖": # White Rook
rook(i, j, "♟♞♝♜♛♚")
elif piece == "♜": # Black Rook
rook(i, j, "♙♘♗♖♕♔")
elif piece == "♕": # White Queen
bishop(i, j, "♟♞♝♜♛♚")
rook(i, j, "♟♞♝♜♛♚")
elif piece == "♛": # Black Queen
bishop(i, j, "♙♘♗♖♕♔")
rook(i, j, "♙♘♗♖♕♔")
elif piece == "♔": # White King
king(i, j, "♟♞♝♜♛♚")
elif piece == "♚": # Black King
king(i, j, "♙♘♗♖♕♔")
def piece_control(i, j):
global piece_selected, turn, select_memory, piece, coordinates, tile
coordinates = {"x":i, "y":j}
tile = buttons[i][j]
piece = tile["text"]
if piece in "♙♘♗♖♕♔":
color = "white"
elif piece in "♟♞♝♜♛♚":
color = "black"
else:
color = None
# If a piece is newly selected and its the person's turn.
if piece != " " and not piece_selected and color == turn: # Selecting a piece
select_piece(i, j)
# Movement
elif tile["image"] == "pyimage5":
tile["text"] = select_memory["piece"]
board[i][j] = select_memory["piece"]
buttons[select_memory["coordinates"]["x"]][select_memory["coordinates"]["y"]]["text"] = " "
board[select_memory["coordinates"]["x"]][select_memory["coordinates"]["y"]] = " "
end_turn(piece, coordinates, "→")
# Cancellation
elif coordinates == select_memory["coordinates"]:
piece_selected = False
select_memory = {"piece": None, "coordinates": None}
refresh_board()
# coordinates != select_memory["coordinates"] is implied
# Choosing a different piece.
elif piece != " " and color == turn:
piece_selected = False
select_memory = {"piece": None, "coordinates": None}
refresh_board()
select_piece(i, j)
# Capture
if tile["image"] == "pyimage6":
tile["text"] = select_memory["piece"]
board[i][j] = select_memory["piece"]
buttons[select_memory["coordinates"]["x"]][select_memory["coordinates"]["y"]]["text"] = " "
board[select_memory["coordinates"]["x"]][select_memory["coordinates"]["y"]] = " "
end_turn(piece, coordinates, "⚔") # Print the capture too
##################### Menu Bar #####################
menu_bar = Menu(window)
def show_log():
global log_list
log_window = Tk()
log_window.title("Log History")
log_window.geometry("250x300")
scrollbar = Scrollbar(log_window, orient=VERTICAL)
log_list = Listbox(log_window, yscrollcommand=scrollbar.set)
log_list.config(font=("Helvetica", 20))
log_list.config(height=300, width=250)
scrollbar.config(command=log_list.yview)
scrollbar.pack(side=RIGHT, fill=Y)
log_list.pack(side=LEFT, fill=BOTH, expand=1)
scrollbar.pack(side=RIGHT, fill=BOTH)
log_list.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=log_list.yview)
update_log()
mainloop()
def update_log():
try:
log_list.delete(0, END)
for l in log_history:
log_list.insert(END, l)
log_list.see("end")
except:
pass
skin_menu = Menu(menu_bar, tearoff=0)
skin_menu.add_command(label="Default", command=reset_board)
skin_menu.add_command(label="Arial", command=change_skin_to_arial)
overall_menu = Menu(menu_bar, tearoff=0)
overall_menu.add_cascade(label="Show Log", command=show_log)
overall_menu.add_cascade(label="Skin Settings", menu=skin_menu)
overall_menu.add_separator()
# TODO: Save & Quit feature
overall_menu.add_cascade(label="Quit", command=window.destroy)
menu_bar.add_cascade(label="☰", menu=overall_menu)
menu_bar.add_cascade(label="White's Turn")
window.config(menu=menu_bar)
# Equivalent to set_buttons()
reset_board()
for k in range(8):
alphabet = "abcdefgh"[k]
num = "87654321"[k]
Label(window, text=alphabet, font=("helvetica", 20)).grid(row=8, column=k)
Label(window, text=num, font=("helvetica", 20)).grid(row=k, column=8)
window.mainloop()