-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.py
372 lines (325 loc) · 11.8 KB
/
chess.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
from random import choice
from time import sleep
from copy import deepcopy
pygame = None
pygame_imported = False
window = None
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 255, 0)
GREEN = (0, 0, 255)
def import_pygame():
global pygame_imported, window, pygame
pygame_imported = True
import pygame as pg
pg.init()
window = pg.display.set_mode((500, 500))
pg.display.set_caption('Chess game')
pygame = pg
class ChessPosition:
board_size = 8
general_id = 0
def __init__(self, b=None):
self.board = b if b is not None else self.get_default_board()
self.active_team = WHITE
self.id = self.general_id
self.general_id += 1
def play_random_game(self, depth=10, draw=True):
move = None
for i in range(depth):
m = self.make_random_move()
if i == 0:
move = m
if draw:
self.display()
self.draw_valid_moves()
sleep(1)
return move
def play_montecarlo(self, draw=True):
while not self.gameover():
move = self.montecarlo(500)
self.move(move)
if draw:
print("drawing")
self.display()
# self.draw_valid_moves()
# sleep(1)
print("game over")
sleep(10)
def play_minimax(self, draw=False):
while True:
val, move = self.minimax(5)
self.move(move)
if draw:
self.display()
def gameover(self):
king_count = 0
for row in self.board:
for square in row:
if square is not None and type(square) is King:
king_count += 1
return king_count < 2
def score(self):
num = 0
for row in self.board:
for square in row:
if square is not None:
num += square.val if square.team == WHITE else -square.val
return num
def montecarlo(self, quantity):
scores = {}
moves = {}
for game in range(quantity):
new_board = self.copy()
m = new_board.play_random_game(depth=5, draw=False)
s = new_board.score()
string = str(m)
s *= 1 if self.active_team == WHITE else -1
if string in scores:
scores[string] += s
else:
scores[string] = s
moves[string] = m
del new_board
top_score = max(scores.values())
for key in scores:
if scores[key] == top_score:
return moves[key]
def minimax(self, ply_left, alpha=-1000, beta=1000): # copyed from Connect Four, need to check
if ply_left <= 0 or self.gameover():
return self.score(), None
if self.active_team == WHITE: # MAX
current_max = -100000
best_play = -1
for move in self.get_all_moves():
modified = self.copy()
m = deepcopy(move)
m[0].board = modified
modified.move(m)
value, m = modified.minimax(ply_left - 1, alpha, beta)
if value > current_max:
current_max = value
move[0].board = self
best_play = move
if alpha < current_max:
alpha = current_max
if beta <= alpha:
break
return current_max, best_play
else: # MINI
current_min = 100000
best_play = None
for move in self.get_all_moves():
modified = self.copy()
m = deepcopy(move)
m[0].board = modified
modified.move(m)
value, m = modified.minimax(ply_left - 1, alpha, beta)
if value < current_min:
current_min = value
best_play = move
if beta > current_min:
beta = current_min
if beta <= alpha:
break
return current_min, best_play
def make_random_move(self):
all_moves = self.get_all_moves()
move = choice(all_moves)
p, pos = move
move_copy = deepcopy(p), pos
self.move(move)
return move_copy
def switch_team(self):
self.active_team = WHITE if self.active_team == BLACK else BLACK
def get_default_board(self):
board = []
base_row = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook]
for r in range(self.board_size):
row = []
for c in range(self.board_size):
if r == 0: row.append(base_row[c]((r, c), BLACK, self))
elif r == 1: row.append(Pawn((r, c), BLACK, self))
elif r == 6: row.append(Pawn((r, c), WHITE, self))
elif r == 7: row.append(base_row[c]((r, c), WHITE, self))
else: row.append(None)
board.append(row)
return board
def get_all_moves(self):
moves = []
for row in self.board:
for square in row:
if square != None:
moves.extend(square.get_all_valid_moves())
return moves
def display(self):
global window
if not pygame_imported:
import_pygame()
step = min(window.get_size()) / self.board_size
for row in range(self.board_size):
for col in range(self.board_size):
x, y = col * step, row * step
pygame.draw.rect(window, WHITE if (row+col)%2==0 else BLACK, (x, y, step, step))
piece = self.board[row][col]
if piece is not None:
piece.draw(x, y)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
def draw_valid_moves(self):
if not pygame_imported:
import_pygame()
for piece, moveTo in self.get_all_moves():
pygame.draw.line(window, GREEN, self.get_square_center(*piece.pos), self.get_square_center(*moveTo))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
def on_board(self, r, c):
return 0 <= r < self.board_size and 0 <= c < self.board_size
def screen_to_square(self):
step = min(window.get_size()) / self.board_size
def get_square_center(self, r, c):
step = min(window.get_size()) / self.board_size
return (c+0.5) * step, (r+0.5) * step
def move(self, move):
piece, moveTo = move
r1, c1 = piece.pos
self.board[r1][c1] = None
r2, c2 = moveTo
self.board[r2][c2] = piece
piece.pos = moveTo
self.switch_team()
def copy(self):
new = deepcopy(self)
for r in range(self.board_size):
for c in range(self.board_size):
val = new.board[r][c]
if val is not None:
new.board[r][c] = type(val)(val.pos, val.team, new)
return new
def __repr__(self):
return f"Board: {self.id}"
class ChessPiece:
val = 0
def __init__(self, pos, team, board):
self.board = board
self.pos = pos
self.team = team
def draw(self, x, y):
x, y = round(x), round(y)
# pygame.draw.circle(window, RED, (x, y), 5)
message = type(self).__name__
text_color = RED if self.team == WHITE else BLUE # (0, 255, 0)
text_font = 'Comic Sans MS'
text_size = 25
font = pygame.font.SysFont(text_font, text_size)
score_surface = font.render(message, False, text_color)
window.blit(score_surface, (x, y+10))
def get_all_valid_moves(self):
return []
def __repr__(self):
return f"{'white' if self.team == WHITE else 'black'} {type(self).__name__} @ {self.pos}"
class Pawn(ChessPiece):
val = 1
def get_all_valid_moves(self):
if self.board.active_team != self.team: return []
moves = []
r, c = self.pos
start = 6 if self.team == WHITE else 1
front = r-1 if self.team == WHITE else r+1
front2 = r-2 if self.team == WHITE else r+2
# normal push
if self.board.on_board(front, c) and self.board.board[front][c] is None:
moves.append((self, (front, c)))
# double push
if r == start and self.board.board[front2][c] is None:
moves.append((self, (front2, c)))
# taking
if self.board.on_board(front, c-1):
piece = self.board.board[front][c-1]
if piece is not None and piece.team != self.team:
moves.append((self, (front, c-1)))
if self.board.on_board(front, c + 1):
piece = self.board.board[front][c + 1]
if piece is not None and piece.team != self.team:
moves.append((self, (front, c + 1)))
return moves
class Knight(ChessPiece):
val = 3
transforms = [(2, 1), (1, 2), (-2, 1), (-1, 2), (2, -1), (1, -2), (-2, -1), (-1, -2)]
def get_all_valid_moves(self):
if self.board.active_team != self.team: return []
moves = []
r, c = self.pos
for dc, dr in self.transforms:
new_r, new_c = r+dr, c+dc
if self.board.on_board(new_r, new_c):
piece = self.board.board[new_r][new_c]
if piece is None or piece.team != self.team:
moves.append((self, (new_r, new_c)))
return moves
class Bishop(ChessPiece):
val = 3
def get_all_valid_moves(self):
if self.board.active_team != self.team: return []
r, c = self.pos
moves = []
for dr in [-1, 1]:
for dc in [-1, 1]:
new_r, new_c = r + dr, c + dc
while self.board.on_board(new_r, new_c):
piece = self.board.board[new_r][new_c]
if piece is None or piece.team != self.team:
moves.append((self, (new_r, new_c)))
new_r += dr
new_c += dc
if piece is not None:
break
return moves
class Rook(ChessPiece):
val = 5
transforms = [(1, 0), (-1, 0), (0, 1), (0, -1)]
def get_all_valid_moves(self):
if self.board.active_team != self.team: return []
r, c = self.pos
moves = []
for dr, dc in Rook.transforms:
new_r, new_c = r + dr, c + dc
while self.board.on_board(new_r, new_c):
piece = self.board.board[new_r][new_c]
if piece is None or piece.team != self.team:
moves.append((self, (new_r, new_c)))
new_r += dr
new_c += dc
if piece is not None:
break
return moves
class Queen(ChessPiece):
val = 9
def get_all_valid_moves(self):
if self.board.active_team != self.team: return []
moves = []
moves.extend(Rook.get_all_valid_moves(self))
moves.extend(Bishop.get_all_valid_moves(self))
return moves
class King(ChessPiece):
val = 200
def get_all_valid_moves(self):
if self.board.active_team != self.team: return []
r, c = self.pos
moves = []
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
new_r, new_c = r + dr, c + dc
if self.board.on_board(new_r, new_c):
piece = self.board.board[new_r][new_c]
if piece is None or piece.team != self.team:
moves.append((self, (new_r, new_c)))
return moves
if __name__ == '__main__':
c = ChessPosition()
c.play_minimax(True)