-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.py
More file actions
479 lines (400 loc) · 14.7 KB
/
Copy pathchess.py
File metadata and controls
479 lines (400 loc) · 14.7 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
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
from copy import deepcopy
INF = float('inf')
class Color:
WHITE = "w"
BLACK = "b"
class Piece:
KING = "K"
QUEEN = "Q"
ROOK = "R"
BISHOP = "B"
KNIGHT = "N"
PAWN = "P"
def __init__(self, p, color):
self.type = p
self.color = color
def __str__(self):
if self.color == Color.BLACK:
return self.type.lower() + ' '
return self.type + ' '
def __eq__(self, other):
return str(self) == str(other)
class Board:
BLANK = "- "
def __init__(self):
self.board = [[Board.BLANK for i in range(8)]
for j in range(8)] # board content
self.load_default()
def load_default(self):
for i in range(8):
self.board[1][i] = Piece(Piece.PAWN, Color.WHITE)
self.board[6][i] = Piece(Piece.PAWN, Color.BLACK)
for info in [[Color.WHITE, 0], [Color.BLACK, 7]]:
color, row = info
self.board[row][0] = Piece(Piece.ROOK, color)
self.board[row][7] = Piece(Piece.ROOK, color)
self.board[row][1] = Piece(Piece.KNIGHT, color)
self.board[row][6] = Piece(Piece.KNIGHT, color)
self.board[row][2] = Piece(Piece.BISHOP, color)
self.board[row][5] = Piece(Piece.BISHOP, color)
self.board[row][3] = Piece(Piece.QUEEN, color)
self.board[row][4] = Piece(Piece.KING, color)
def __str__(self):
s = ""
tmp = []
for row in self.board:
tmp.append("".join([str(p) for p in row]) + "\n")
return "\n".join(tmp[::-1])
def over(self):
white_king = Piece(Piece.KING, Color.WHITE)
black_king = Piece(Piece.KING, Color.BLACK)
white_king_alive = False
black_king_alive = False
for i in range(8):
for j in range(8):
if self.board[i][j] == white_king:
white_king_alive = True
if self.board[i][j] == black_king:
black_king_alive = True
if white_king_alive and black_king_alive:
return False, None
if white_king_alive:
return True, Color.WHITE
if black_king_alive:
return True, Color.BLACK
def get_pieces(self):
"""get current pieces on board"""
total = {
Color.WHITE: {
Piece.KING: [],
Piece.QUEEN: [],
Piece.BISHOP: [],
Piece.KNIGHT: [],
Piece.ROOK: [],
Piece.PAWN: []
},
Color.BLACK: {
Piece.KING: [],
Piece.QUEEN: [],
Piece.BISHOP: [],
Piece.KNIGHT: [],
Piece.ROOK: [],
Piece.PAWN: []
}
}
# pieces = {Piece.KING: [], Piece.QUEEN: [], Piece.BISHOP: [], Piece.KNIGHT: [], Piece.ROOK: [], Piece.PAWN: []}
for i in range(8):
for j in range(8):
p = self.board[i][j]
if type(p) is Piece:
color = p.color
p_type = p.type
total[color][p_type].append([p, (i, j)])
return total
def apply(self, move):
a, b = move
ai, aj = a
bi, bj = b
piece = self.board[ai][aj]
self.board[ai][aj] = Board.BLANK
self.board[bi][bj] = piece
def get_opponent(player):
return Color.BLACK if player == Color.WHITE else Color.WHITE
def successors(game):
"""
get the possible move and next game state from current
"""
player = game.current_player
pieces = game.board.get_pieces()
my_pieces = pieces[player]
result = []
result.extend(pawn_successors(my_pieces[Piece.PAWN], game))
result.extend(queen_successors(my_pieces[Piece.QUEEN], game))
result.extend(knight_successors(my_pieces[Piece.KNIGHT], game))
result.extend(bishop_successors(my_pieces[Piece.BISHOP], game))
result.extend(rook_successors(my_pieces[Piece.ROOK], game))
result.extend(king_successors(my_pieces[Piece.KING], game))
return result
def pawn_successors(pieces, game):
"""
check pawn of white and black separately
"""
result = []
for (p, pos) in pieces:
if p.color == Color.WHITE:
white_pawn_successors(p, pos, game, result)
else:
black_pawn_successors(p, pos, game, result)
return result
def white_pawn_successors(piece, pos, game, result):
"""
for pawn, if it's at beginning position,then it's can move one step or two
and possible to eat the piece of corner
"""
board = game.board.board
i, j = pos
if i < 7:
# move forward one step
p = board[i + 1][j]
if type(p) is not Piece:
new_game = deepcopy(game)
new_game.board.board[i][j] = Board.BLANK
new_game.board.board[i + 1][j] = piece
result.append([[pos, (i + 1, j)], new_game])
if i == 2:
# move forward two step
p = board[i + 2][j]
if type(p) is not Piece:
new_game = deepcopy(game)
new_game.board.board[i][j] = Board.BLANK
new_game.board.board[i + 2][j] = piece
result.append([[pos, (i + 2, j)], new_game])
# try eat
for n_pos in [(i + 1, j - 1), (i + 1, j + 1)]:
if not ok_p(n_pos):
continue
pi, pj = n_pos
p = board[pi][pj]
if type(p) is Piece:
if p.color != piece.color:
new_game = deepcopy(game)
new_game.board.board[i][j] = Board.BLANK
new_game.board.board[pi][pj] = piece
result.append([[pos, n_pos], new_game])
return result
def black_pawn_successors(piece, pos, game, result):
"""
for pawn, if it's at beginning position,then it's can move one step or two
and possible to eat the piece of corner
"""
board = game.board.board
i, j = pos
if i > 0:
# move forward one step
p = board[i - 1][j]
if type(p) is not Piece:
new_game = deepcopy(game)
new_game.board.board[i][j] = Board.BLANK
new_game.board.board[i - 1][j] = piece
result.append([[pos, (i - 1, j)], new_game])
if i == 6:
# move forward two step
p = board[i - 2][j]
if type(p) is not Piece:
new_game = deepcopy(game)
new_game.board.board[i][j] = Board.BLANK
new_game.board.board[i - 2][j] = piece
result.append([[pos, (i - 2, j)], new_game])
# try eat
for n_pos in [(i - 1, j - 1), (i - 1, j + 1)]:
if not ok_p(n_pos):
continue
pi, pj = n_pos
p = board[pi][pj]
if type(p) is Piece:
if p.color != piece.color:
new_game = deepcopy(game)
new_game.board.board[i][j] = Board.BLANK
new_game.board.board[pi][pj] = piece
result.append([[pos, n_pos], new_game])
return result
def knight_successors(pieces, game):
"""eight possible next position"""
result = []
for (p, pos) in pieces:
i, j = pos
next_pos = [(i + 2, j + 1), (i + 1, j + 2), (i - 2, j - 1),
(i - 1, j - 2), (i - 2, j + 1), (i - 1, j + 2),
(i + 2, j - 1), (i + 1, j - 2)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result, stop=False)
return result
def queen_successors(pieces, game):
# """queen can go eight direction"""
result = []
for (p, pos) in pieces:
i, j = pos
next_pos = [(d, j) for d in range(i + 1, 8)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(d, j) for d in range(0, i)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i, d) for d in range(j + 1, 8)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i, d) for d in range(0, j)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i + k, j + k) for k in range(1, 8)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i + k, j - k) for k in range(1, 8)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i - k, j - k) for k in range(1, 8)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i - k, j + k) for k in range(1, 8)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result)
return result
def ok_p(p):
# check if the position is legal
i, j = p
return 0 <= i < 8 and 0 <= j < 8
def bishop_successors(pieces, game):
"""bishop can go four direction (diag)"""
result = []
for (p, pos) in pieces:
i, j = pos
next_pos = [(i + k, j + k) for k in range(1, 8)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i + k, j - k) for k in range(1, 8)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i - k, j - k) for k in range(1, 8)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i - k, j + k) for k in range(1, 8)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result)
return result
def rook_successors(pieces, game):
"""rook can go four direction"""
result = []
for (p, pos) in pieces:
i, j = pos
next_pos = [(d, j) for d in range(i + 1, 8)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(d, j) for d in range(0, i)][::-1]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i, d) for d in range(j + 1, 8)]
insert_possible(next_pos, pos, p, game, result)
next_pos = [(i, d) for d in range(0, j)][::-1]
insert_possible(next_pos, pos, p, game, result)
return result
def king_successors(pieces, game):
"""four possible next position"""
result = []
for (p, pos) in pieces:
i, j = pos
next_pos = [(i, j + 1), (i + 1, j), (i, j - 1), (i - 1, j)]
next_pos = [p for p in next_pos if ok_p(p)]
insert_possible(next_pos, pos, p, game, result, stop=False)
return result
def insert_possible(pos_list, pos, piece, game, result, stop=True):
"""for a list of position to check if can place the piece, if stop, then when meet a piece the check stop"""
board = game.board.board
oi, oj = pos
for next_pos in pos_list:
i, j = next_pos
p = board[i][j]
if type(p) is not Piece:
new_game = deepcopy(game)
new_game.board.board[i][j] = piece
new_game.board.board[oi][oj] = Board.BLANK
result.append([[pos, next_pos], new_game])
else:
if p.color != piece.color:
new_game = deepcopy(game)
new_game.board.board[i][j] = piece
new_game.board.board[oi][oj] = Board.BLANK
result.append([[pos, next_pos], new_game])
if stop:
break
def evaluate(game, current_player):
player = current_player
pieces = game.board.get_pieces()
my_pieces = pieces[player]
oppo_pieces = pieces[get_opponent(player)]
return score(my_pieces) - score(oppo_pieces)
def score(pieces):
s = 0
s += 1 * len(pieces[Piece.PAWN])
s += 30 * len(pieces[Piece.KNIGHT])
s += 50 * len(pieces[Piece.ROOK])
s += 40 * len(pieces[Piece.BISHOP])
s += 160 * len(pieces[Piece.QUEEN])
s += 10000 * len(pieces[Piece.KING])
return s
class Game:
def __init__(self, board, current_player):
self.board = board
self.current_player = current_player
self.winner = ""
def over(self):
over, winner = self.board.over()
self.winner = winner
return over
def apply(self, move):
self.board.apply(move)
self.current_player = Color.BLACK if self.current_player == Color.WHITE else Color.WHITE
class BasePlayer:
def __init__(self, color):
self.color = color
def get_move(self, board):
raise Exception("not implement error")
class MinimaxPlayer(BasePlayer):
def __init__(self, color, depth):
super().__init__(color)
self.player = color
self.depth = depth
def get_move(self, game):
# minimax search with alpha-beta cut
depth = self.depth
best_score = -INF
beta = INF
best_action = None
for (move, next_state) in successors(game):
v = self.min_value(next_state, best_score, beta, depth - 1)
if v > best_score:
best_score = v
best_action = move
return best_action
def max_value(self, game, alpha, beta, depth):
if depth <= 0 or game.over():
return evaluate(game, self.player)
v = -INF
for (move, next_state) in successors(game):
v = max(v, self.min_value(next_state, alpha, beta, depth - 1))
if v >= beta:
return v
alpha = max(alpha, v)
return v
def min_value(self, game, alpha, beta, depth):
if depth <= 0 or game.over():
return evaluate(game, self.player)
v = INF
for (move, next_state) in successors(game):
v = min(v, self.max_value(next_state, alpha, beta, depth - 1))
if v <= alpha:
return v
beta = min(beta, v)
return v
def to_pos(p):
col, row = p[0], p[1]
return (int(row) - 1, "abcdefgh".index(col))
class HumanPlayer(BasePlayer):
def get_move(self, board):
move = input("input your move (pos,pos, eg, a7,a6):")
p1, p2 = move.split(",")
return to_pos(p1), to_pos(p2) # trans to array index
def run():
depth = 4 # control depth of search tree
current_player = Color.WHITE
game = Game(Board(), current_player)
white_player = MinimaxPlayer(Color.WHITE, depth) # ai player
#black_player = MinimaxPlayer(Color.BLACK, depth)
black_player = HumanPlayer(Color.BLACK) # human input player
while not game.over():
# game turn
if game.current_player == Color.WHITE:
move = white_player.get_move(game)
else:
move = black_player.get_move(game)
game.apply(move)
# print(move)
print(game.board)
# current_player = Color.BLACK if current_player == Color.WHITE else Color.WHITE
print("game over")
print("winner:", game.winner)
if __name__ == "__main__":
run()