Repository navigation
Expand file tree
/
Copy pathturts.py
More file actions
449 lines (350 loc) · 11.9 KB
/
Copy pathturts.py
File metadata and controls
449 lines (350 loc) · 11.9 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
443
444
445
446
447
import turtle
t = turtle.Turtle()
t.screen.setup(640, 640)
t.shape("square")
t.shapesize(1,1)
t.speed(0) # 1:slowest, 3:slow, 5:normal, 10:fast, 0:fastest
t.pu()
board = [["♖","♘","♗","♕","♔","♗","♘","♖"],
["♙","♙","♙","♙","♙","♙","♙","♙"],
["","","","","","","",""],
["","","","","","","",""],
["","","","","","","",""],
["","","","","","","",""],
["♟","♟","♟","♟","♟","♟","♟","♟"],
["♜","♞","♝","♛","♚","♝","♞","♜"]
]
white_pieces = ["♖","♘","♗","♕","♔","♙"]
black_pieces = ["♜","♞","♝","♛","♚","♟"]
screen = t.screen
last_clicked_square = None
white_turn = True
def restore_square(col,row):
x = -250 + col * 62
y = -250 + row * 62
t.goto(x,y)
t.color("chocolate" if (col + row) % 2 == 0 else "wheat")
t.stamp()
if board[row][col] != "":
t.goto(x,y - 21.5)
t.color("black")
t.write(board[row][col], align="center", font=("Arial", 25, "normal"))
def highlight_square(col,row):
x = -250 + col * 62
y = -250 + row * 62
t.goto(x,y)
t.color("light blue")
t.stamp()
t.goto(x,y - 21.5)
t.color("black")
t.write(board[row][col], align="center", font=("Arial", 25, "normal"))
def is_legal_move(from_col, from_row, to_col, to_row, check_white):
if check_white == True:
piece_set = white_pieces
else:
piece_set = black_pieces
if board[to_row][to_col] in piece_set:
return False
piece = board[from_row][from_col]
if piece == "♙":
if to_col == from_col and to_row == from_row + 1 and board[to_row][to_col] == "":
return True
elif to_col == from_col and to_row == from_row + 2 and from_row == 1 and board[to_row][to_col] == "" and board[from_row + 1][from_col] == "":
return True
elif abs(to_col - from_col) == 1 and to_row == from_row + 1 and board[to_row][to_col] != "":
return True
else:
return False
if piece == "♟":
if to_col == from_col and to_row == from_row - 1 and board[to_row][to_col] == "":
return True
elif to_col == from_col and to_row == from_row - 2 and from_row == 6 and board[to_row][to_col] == "" and board[from_row - 1][from_col] == "":
return True
elif abs(to_col - from_col) == 1 and to_row == from_row - 1 and board[to_row][to_col] != "":
return True
else:
return False
if piece == "♞" or piece == "♘":
if abs(to_col - from_col) == 2 and abs(to_row - from_row) == 1:
return True
elif abs(to_col - from_col) == 1 and abs(to_row - from_row) == 2:
return True
else:
return False
if piece == "♚" or piece == "♔":
if abs(to_col - from_col) <= 1 and abs(to_row - from_row) <= 1:
return True
else:
return False
if piece == "♖" or piece == "♜":
if to_col == from_col or to_row == from_row:
# Horizontal Move (Row stays the same)
if to_row == from_row:
for c in range(min(from_col, to_col) + 1, max(from_col, to_col)):
if board[from_row][c] != "":
return False
return True
# Vertical Move (Column stays the same)
elif to_col == from_col:
for r in range(min(from_row, to_row) + 1, max(from_row, to_row)):
if board[r][from_col] != "":
return False
return True
else:
return False
if piece == "♗" or piece == "♝":
# 1. Is it a perfect diagonal line?
if abs(to_col - from_col) == abs(to_row - from_row):
# 2. Figure out the direction of the steps (1 for right/down, -1 for left/up)
if to_col > from_col:
col_step = 1
else:
col_step = -1
if to_row > from_row:
row_step = 1
else:
row_step = -1
# 3. Check every square along the path
distance = abs(to_col - from_col)
for i in range(1, distance):
c = from_col + (i * col_step)
r = from_row + (i * row_step)
if board[r][c] != "":
return False # Something is blocking the path!
return True # The path is clear!
else:
return False # It wasn't a diagonal move
if piece == "♛" or piece == "♕":
if to_col == from_col or to_row == from_row:
# Horizontal Move (Row stays the same)
if to_row == from_row:
for c in range(min(from_col, to_col) + 1, max(from_col, to_col)):
if board[from_row][c] != "":
return False
return True
# Vertical Move (Column stays the same)
elif to_col == from_col:
for r in range(min(from_row, to_row) + 1, max(from_row, to_row)):
if board[r][from_col] != "":
return False
return True
elif abs(to_col - from_col) == abs(to_row - from_row):
# 2. Figure out the direction of the steps (1 for right/down, -1 for left/up)
if to_col > from_col:
col_step = 1
else:
col_step = -1
if to_row > from_row:
row_step = 1
else:
row_step = -1
# 3. Check every square along the path
distance = abs(to_col - from_col)
for i in range(1, distance):
c = from_col + (i * col_step)
r = from_row + (i * row_step)
if board[r][c] != "":
return False # Something is blocking the path!
return True # The path is clear!
else:
return False
return True
def move_piece(from_col, from_row, to_col, to_row):
piece = board[from_row][from_col]
board[from_row][from_col] = ""
board[to_row][to_col] = piece
if piece == "♙" and to_row == 7:
board[to_row][to_col] = "♕"
if piece == "♟" and to_row == 0:
board[to_row][to_col] = "♛"
restore_square(from_col, from_row)
restore_square(to_col, to_row)
def is_square_under_attack(target_col, target_row, attacker_is_white):
for r in range(8):
for c in range(8):
piece = board[r][c]
if piece == "":
continue
if attacker_is_white == True and piece in black_pieces:
continue
if attacker_is_white == False and piece in white_pieces:
continue
if is_legal_move(c,r,target_col,target_row,attacker_is_white):
return True
return False
def has_legal_moves(attacker_is_white):
for r in range(8):
for c in range(8):
piece = board[r][c]
if piece == "":
continue
elif attacker_is_white and piece in black_pieces:
continue
elif not attacker_is_white and piece in white_pieces:
continue
for dest_row in range(8):
for dest_col in range(8):
if is_legal_move(c, r, dest_col, dest_row, attacker_is_white):
hypo_capt_piece = board[dest_row][dest_col]
board[r][c] = ""
board[dest_row][dest_col] = piece
if attacker_is_white:
target_king = "♔"
else:
target_king = "♚"
for kr in range(8):
for kc in range(8):
if board[kr][kc] == target_king:
king_row = kr
king_col = kc
king_in_check = is_square_under_attack(king_col, king_row, not attacker_is_white)
board[r][c] = piece
board[dest_row][dest_col] = hypo_capt_piece
if not king_in_check:
return True
return False
def on_click(x,y):
global last_clicked_square
global white_turn
col = int((x + 281) // 62)
row = int((y + 281) // 62)
if col < 0 or col > 7 or row < 0 or row > 7:
return
t.shapesize(3.1, 3.1)
if last_clicked_square != None:
piece_col, piece_row = last_clicked_square
if board[piece_row][piece_col] != "":
froms = piece_col, piece_row
tos = col, row
if froms == tos:
restore_square(piece_col, piece_row)
last_clicked_square = None
return
if is_legal_move(piece_col, piece_row, col, row, white_turn):
piece = board[piece_row][piece_col]
hypo_capt_piece = board[row][col]
board[piece_row][piece_col] = ""
if board[row][col] != "":
hypo_capt_piece = board[row][col]
board[row][col] = ""
board[row][col] = piece
if white_turn:
target_king = "♔"
else:
target_king = "♚"
for r in range(8):
for c in range(8):
square = board[r][c]
if square == target_king:
king_row = r
king_col = c
king_in_check = is_square_under_attack(king_col, king_row, not white_turn)
board[piece_row][piece_col] = piece
board[row][col] = hypo_capt_piece
if king_in_check:
return
else:
move_piece(piece_col, piece_row, col, row)
last_clicked_square = None
white_turn = not white_turn
if has_legal_moves(white_turn) == False:
print("CHEKMATETETTETET")
else:
return
print(col , row ) # Find a way so that the highlighted square goes back to its original state when another square is highlighted.
piece = board[row][col]
if white_turn == True and piece not in white_pieces:
return
if white_turn == False and piece not in black_pieces:
return
y = -250 + row * 62
x = -250 + col * 62
t.goto(x,y)
if last_clicked_square != None:
prev_col, prev_row = last_clicked_square
restore_square(prev_col, prev_row)
last_clicked_square = (col,row)
highlight_square(col,row)
def print_board_rim(): # Prints a big square that traces the board
t.hideturtle()
t.goto(-283,-283)
t.color("black")
t.pensize(2)
t.pd()
t.forward(500)
t.lt(90)
t.forward(500)
t.lt(90)
t.forward(500)
t.lt(90)
t.forward(500)
def print_squares():
for row in range(8): #Each time this runs, col runs 8 times. 8 * 8 = 64 squares!
y = -250 + row * 62
x = -250
for col in range(8):
if (col + row) % 2 == 0:
t.color("chocolate")
else:
t.color("wheat")
t.pu()
t.goto(x, y)
t.shapesize(3.1,3.1)
t.stamp()
x += 62
def print_coordinates():
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
for col in range(8):
x = -250 + col * 62
y = -310
t.goto(x,y)
t.color("black")
t.write(letters[col], align="center", font=("Arial", 10, "normal"))
for row in range(8):
x = 240
y = -250 + row * 62 - 7
t.goto(x,y)
t.color("black")
t.write(int(row + 1), align="center", font=("Arial", 10, "normal"))
def print_pieces():
# starting positions for black pieces
back_row_white = ["♖","♘","♗","♕","♔","♗","♘","♖"]
back_row_black = ["♜","♞","♝","♛","♚","♝","♞","♜"]
pawn_white = ["♙"]
pawn_black = ["♟"]
for col in range(8): # Whites Peices
x = -250 + col * 62
y = -271.5 + 0 * 62
piece = back_row_white[col]
t.goto(x,y)
t.color("black")
t.write(piece, align="center", font=("Arial", 25, "normal"))
for col in range(8): # Whites Pawns
x = -250 + col * 62
y = -271.5 + 1 * 62
piece = pawn_white[0]
t.goto(x,y)
t.color("black")
t.write(piece, align="center", font=("Arial", 25, "normal"))
for col in range(8): # Blacks Peices
x = -250 + col * 62
y = -271.5 + 7 * 62
piece = back_row_black[col]
t.goto(x,y)
t.color("black")
t.write(piece, align="center", font=("Arial", 25, "normal"))
for col in range(8): # Black's Pawns
x = -250 + col * 62
y = -271.5 + 6 * 62
piece = pawn_black[0]
t.goto(x,y)
t.color("black")
t.write(piece, align="center", font=("Arial", 25, "normal"))
screen.tracer(0)
print_board_rim()
print_squares()
print_pieces()
print_coordinates()
screen.update()
screen.onclick(on_click)
turtle.done()