-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessGame.py
383 lines (347 loc) · 15.4 KB
/
ChessGame.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
import pygame as pg
class Move:
def __init__(self, square1, square2, board):
self.start_row = square1[0]
self.start_col = square1[1]
self.end_row = square2[0]
self.end_col = square2[1]
self.moved_piece = board[self.start_row][self.start_col]
self.captured_piece = board[self.end_row][self.end_col]
def __eq__(self, other):
if self.start_row == other.start_row and self.start_col == other.start_col and self.end_row == other.end_row and self.end_col == other.end_col and self.moved_piece == other.moved_piece and self.captured_piece == other.captured_piece:
return True
return False
class GameState:
def __init__(self):
self.white_turn = True
self.move_log = []
self.white_king_loc = (7, 4)
self.black_king_loc = (0, 4)
self.check_mate = False
self.stale_mate = False
self.move_made = False
self.board = [
["br", "bn", "bb", "bq", "bk", "bb", "bn", "br"],
["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
["wr", "wn", "wb", "wq", "wk", "wb", "wn", "wr"],
]
def make_move(self, move):
self.board[move.start_row][move.start_col] = "--"
self.board[move.end_row][move.end_col] = move.moved_piece
self.move_log.append(move)
self.white_turn = not self.white_turn
if move.moved_piece == "wk":
self.white_king_loc = (move.end_row, move.end_col)
elif move.moved_piece == "bk":
self.black_king_loc = (move.end_row, move.end_col)
def undo_move(self):
if len(self.move_log) > 0:
move = self.move_log.pop()
self.board[move.start_row][move.start_col] = move.moved_piece
self.board[move.end_row][move.end_col] = move.captured_piece
self.white_turn = not self.white_turn
if move.moved_piece == "wk":
self.white_king_loc = (move.start_row, move.start_col)
elif move.moved_piece == "bk":
self.black_king_loc = (move.start_row, move.start_col)
def undo_move_twice(self):
if len(self.move_log) > 1:
move1 = self.move_log.pop()
move2 = self.move_log.pop()
self.board[move1.start_row][move1.start_col] = move1.moved_piece
self.board[move1.end_row][move1.end_col] = move1.captured_piece
self.board[move2.start_row][move2.start_col] = move2.moved_piece
self.board[move2.end_row][move2.end_col] = move2.captured_piece
if move1.moved_piece == "wk":
self.white_king_loc = (move1.start_row, move1.start_col)
elif move1.moved_piece == "bk":
self.black_king_loc = (move1.start_row, move1.start_col)
if move2.moved_piece == "wk":
self.white_king_loc = (move2.start_row, move2.start_col)
elif move2.moved_piece == "bk":
self.black_king_loc = (move2.start_row, move2.start_col)
def all_possible_moves(self):
moves = []
for row in range(8):
for col in range(8):
color = self.board[row][col][0]
if color == "w":
enemy = "b"
else:
enemy = "w"
if (color == "w" and self.white_turn) or (color == "b" and (not self.white_turn)):
piece = self.board[row][col][1]
if piece == "p":
self.all_possible_pawn_moves(row, col, moves)
elif piece == "r":
self.all_possible_rook_moves(row, col, enemy, moves)
elif piece == "n":
self.all_possible_knight_moves(row, col, enemy, moves)
elif piece == "b":
self.all_possible_bishop_moves(row, col, enemy, moves)
elif piece == "q":
self.all_possible_queen_moves(row, col, enemy, moves)
elif piece == "k":
self.all_possible_king_moves(row, col, enemy, moves)
return moves
def all_possible_pawn_moves(self, row, col, moves):
if self.white_turn:
if self.board[row - 1][col] == "--":
moves.append(Move((row, col), (row - 1, col), self.board))
if row == 6 and self.board[row - 2][col] == "--":
moves.append(Move((row, col), (row - 2, col), self.board))
if col != 0:
if self.board[row - 1][col - 1][0] == "b":
moves.append(Move((row, col), (row - 1, col - 1), self.board))
if col != 7:
if self.board[row - 1][col + 1][0] == "b":
moves.append(Move((row, col), (row - 1, col + 1), self.board))
else:
if self.board[row + 1][col] == "--":
moves.append(Move((row, col), (row + 1, col), self.board))
if row == 1 and self.board[row + 2][col] == "--":
moves.append(Move((row, col), (row + 2, col), self.board))
if col != 0:
if self.board[row + 1][col - 1][0] == "w":
moves.append(Move((row, col), (row + 1, col - 1), self.board))
if col != 7:
if self.board[row + 1][col + 1][0] == "w":
moves.append(Move((row, col), (row + 1, col + 1), self.board))
def all_possible_rook_moves(self, row, col, enemy, moves):
directions = ((1, 0), (0, 1), (-1, 0), (0, -1))
for direction in directions:
for i in range(1, 8):
row2 = row + i * direction[0]
col2 = col + i * direction[1]
if 0 <= row2 <= 7 and 0 <= col2 <= 7:
piece = self.board[row2][col2]
if piece == "--":
moves.append(Move((row, col), (row2, col2), self.board))
elif piece[0] == enemy:
moves.append(Move((row, col), (row2, col2), self.board))
break
else:
break
else:
break
def all_possible_knight_moves(self, row, col, enemy, moves):
directions = ((2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2))
for direction in directions:
row2 = row + direction[0]
col2 = col + direction[1]
if 0 <= row2 <= 7 and 0 <= col2 <= 7:
piece = self.board[row2][col2]
if piece == "--" or piece[0] == enemy:
moves.append(Move((row, col), (row2, col2), self.board))
def all_possible_bishop_moves(self, row, col, enemy, moves):
directions = ((1, 1), (-1, 1), (-1, -1), (1, -1))
for direction in directions:
for i in range(1, 8):
row2 = row + i * direction[0]
col2 = col + i * direction[1]
if 0 <= row2 <= 7 and 0 <= col2 <= 7:
piece = self.board[row2][col2]
if piece == "--":
moves.append(Move((row, col), (row2, col2), self.board))
elif piece[0] == enemy:
moves.append(Move((row, col), (row2, col2), self.board))
break
else:
break
else:
break
def all_possible_queen_moves(self, row, col, enemy, moves):
self.all_possible_rook_moves(row, col, enemy, moves)
self.all_possible_bishop_moves(row, col, enemy, moves)
def all_possible_king_moves(self, row, col, enemy, moves):
directions = ((1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1))
for direction in directions:
row2 = row + direction[0]
col2 = col + direction[1]
if 0 <= row2 <= 7 and 0 <= col2 <= 7:
piece = self.board[row2][col2]
if piece == "--" or piece[0] == enemy:
moves.append(Move((row, col), (row2, col2), self.board))
def all_legal_moves(self):
legal_moves = self.all_possible_moves()
for i in range(len(legal_moves) - 1, -1, -1):
self.make_move(legal_moves[i])
self.white_turn = not self.white_turn
if self.in_check():
legal_moves.remove(legal_moves[i])
self.white_turn = not self.white_turn
self.undo_move()
if len(legal_moves) == 0:
if self.in_check():
self.check_mate = True
else:
self.stale_mate = True
else:
self.check_mate = False
self.stale_mate = False
return legal_moves
def in_check(self):
if self.white_turn:
return self.square_attacked(self.white_king_loc[0], self.white_king_loc[1])
else:
return self.square_attacked(self.black_king_loc[0], self.black_king_loc[1])
def square_attacked(self, row, col):
self.white_turn = not self.white_turn
moves = self.all_possible_moves()
self.white_turn = not self.white_turn
for move in moves:
if move.end_row == row and move.end_col == col:
return True
return False
def evaluation(self):
sum = 0
pieces_dic = {"p":100, "n":320, "b":330, "r":500, "q":900, "k":20000}
if self.check_mate:
if self.white_turn:
return -20000
else:
return 20000
if self.stale_mate:
return 0
for row in range(8):
for col in range(8):
piece = self.board[row][col][1]
color = self.board[row][col][0]
if piece != "-":
if color == "w":
sum = sum + pieces_dic[piece]
else:
sum = sum - pieces_dic[piece]
return sum
def load_images():
pieces = ["wp", "wr", "wn", "wb", "wq", "wk", "bp", "br", "bn", "bb", "bq", "bk"]
for piece in pieces:
Images_Dic[piece] = pg.transform.scale(pg.image.load("images/" + piece + ".png"), (Square_Size, Square_Size))
def draw_game_state(screen, game_state, valid, selected_square):
draw_board(screen)
draw_highlight(screen, game_state, valid, selected_square)
draw_pieces(screen, game_state.board)
def draw_board(screen):
for row in range(8):
for col in range(8):
if (row + col) % 2 == 0:
color = pg.Color("white")
else:
color = pg.Color("brown")
pg.draw.rect(screen, color, pg.Rect(col*Square_Size, row*Square_Size, Square_Size, Square_Size))
def draw_highlight(screen, game_state, valid, selected_square):
if selected_square != ():
row, col = selected_square
if game_state.board[row][col][0] == "w":
s = pg.Surface((Square_Size, Square_Size))
s.set_alpha(100)
s.fill(pg.Color("green"))
screen.blit(s, (col*Square_Size, row*Square_Size))
s.fill(pg.Color("yellow"))
for move in valid:
if move.start_row == row and move.start_col == col:
screen.blit(s, (move.end_col*Square_Size, move.end_row*Square_Size))
def draw_pieces(screen, board):
for row in range(8):
for col in range(8):
piece = board[row][col]
if piece != "--":
screen.blit(Images_Dic[piece], pg.Rect(col*Square_Size, row*Square_Size, Square_Size, Square_Size))
def optimal_move(game_state):
depth = 3
if game_state.white_turn:
best = minimax(game_state, depth, float("-inf"), float("inf"), True)
else:
best = minimax(game_state, depth, float("-inf"), float("inf"), False)
return best[1]
def minimax(game_state, depth, alpha, beta, is_maximizing):
valid = game_state.all_legal_moves()
if depth == 0 or game_state.check_mate or game_state.stale_mate:
return game_state.evaluation(), None
if is_maximizing:
best_move = None
for move in valid:
game_state.make_move(move)
(temp_score, temp_move) = minimax(game_state, depth - 1, alpha, beta, False)
game_state.undo_move()
if temp_score > alpha:
alpha = temp_score
best_move = move
if alpha >= beta:
break
return alpha, best_move
else:
best_move = None
for move in valid:
game_state.make_move(move)
(temp_score, temp_move) = minimax(game_state, depth - 1, alpha, beta, True)
game_state.undo_move()
if temp_score < beta:
beta = temp_score
best_move = move
if alpha >= beta:
break
return beta, best_move
def main():
pg.init()
screen = pg.display.set_mode((Screen_Size, Screen_Size))
screen.fill(pg.Color("white"))
game_state = GameState()
load_images()
selected_square = ()
selections = []
valid = game_state.all_legal_moves()
run = True
while run:
for e in pg.event.get():
if e.type == pg.QUIT:
run = False
elif e.type == pg.KEYDOWN:
if e.key == pg.K_u:
game_state.undo_move_twice()
game_state.move_made = True
selected_square = ()
selections = []
break
if game_state.white_turn:
if e.type == pg.MOUSEBUTTONDOWN:
loc = pg.mouse.get_pos()
col = loc[0] // Square_Size
row = loc[1] // Square_Size
if len(selections) == 0:
if game_state.board[row][col][0] == "w":
selected_square = (row, col)
selections.append(selected_square)
else:
selected_square = (row, col)
move1 = Move(selections[0], selected_square, game_state.board)
if move1 in valid:
game_state.make_move(move1)
game_state.move_made = True
selected_square = ()
selections = []
else:
if move1.captured_piece == "--" or move1.captured_piece[0] == "b" or (move1.start_row == move1.end_row and move1.start_col == move1.end_col):
selected_square = ()
selections = []
else:
selections[0] = selected_square
else:
best = optimal_move(game_state)
game_state.make_move(best)
game_state.move_made = True
if game_state.move_made:
valid = game_state.all_legal_moves()
game_state.move_made = False
draw_game_state(screen, game_state, valid, selected_square)
pg.display.flip()
Screen_Size = 552
Square_Size = Screen_Size // 8
FPS = 15
Images_Dic = {}
main()