-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.py
492 lines (325 loc) · 12.5 KB
/
game.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
import chess
import chess.svg
import chess.polyglot
# Below are the two-dimentional arrays to guid the bot to choosing a proper move for each chess piece.
pawntable = [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10,-20,-20, 10, 10, 5,
5, -5,-10, 0, 0,-10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 5, 10, 25, 25, 10, 5, 5,
10, 10, 20, 30, 30, 20, 10, 10,
50, 50, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0]
knightstable = [
-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 5, 5, 0,-20,-40,
-30, 5, 10, 15, 15, 10, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 10, 15, 15, 10, 0,-30,
-40,-20, 0, 0, 0, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50]
bishopstable = [
-20,-10,-10,-10,-10,-10,-10,-20,
-10, 5, 0, 0, 0, 0, 5,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 0, 0, 0, 0, 0, 0,-10,
-20,-10,-10,-10,-10,-10,-10,-20]
rookstable = [
0, 0, 0, 5, 5, 0, 0, 0,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0]
queenstable = [
-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 5, 5, 5, 5, 5, 0,-10,
0, 0, 5, 5, 5, 5, 0, -5,
-5, 0, 5, 5, 5, 5, 0, -5,
-10, 0, 5, 5, 5, 5, 0,-10,
-10, 0, 0, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20]
kingstable = [
20, 30, 10, 0, 0, 10, 30, 20,
20, 20, 0, 0, 0, 0, 20, 20,
-10,-20,-20,-20,-20,-20,-20,-10,
-20,-30,-30,-40,-40,-30,-30,-20,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30]
# To view the chess board.
# board = chess.Board()
# print(board)
movehistory = []
# Below are four functions used by the bot to evaluate and choose a proper move.
def evaluate_board(board):
if board.is_checkmate():
if board.turn:
return -9999
else:
return 9999
if board.is_stalemate():
return 0
if board.is_insufficient_material():
return 0
wp = len(board.pieces(chess.PAWN, chess.WHITE))
bp = len(board.pieces(chess.PAWN, chess.BLACK))
wn = len(board.pieces(chess.KNIGHT, chess.WHITE))
bn = len(board.pieces(chess.KNIGHT, chess.BLACK))
wb = len(board.pieces(chess.BISHOP, chess.WHITE))
bb = len(board.pieces(chess.BISHOP, chess.BLACK))
wr = len(board.pieces(chess.ROOK, chess.WHITE))
br = len(board.pieces(chess.ROOK, chess.BLACK))
wq = len(board.pieces(chess.QUEEN, chess.WHITE))
bq = len(board.pieces(chess.QUEEN, chess.BLACK))
material = 100*(wp-bp) + 320*(wn-bn) + 330*(wb-bb) + 500*(wr-br) + 900*(wq-bq)
pawnsq = sum([pawntable[i] for i in board.pieces(chess.PAWN, chess.WHITE)])
pawnsq = pawnsq + sum([-pawntable[chess.square_mirror(i)] for i in board.pieces(chess.PAWN, chess.BLACK)])
knightsq = sum([knightstable[i] for i in board.pieces(chess.KNIGHT, chess.WHITE)])
knightsq = knightsq + sum([-knightstable[chess.square_mirror(i)] for i in board.pieces(chess.KNIGHT, chess.BLACK)])
bishopsq = sum([bishopstable[i] for i in board.pieces(chess.BISHOP, chess.WHITE)])
bishopsq = bishopsq + sum([-bishopstable[chess.square_mirror(i)] for i in board.pieces(chess.BISHOP, chess.BLACK)])
rooksq = sum([rookstable[i] for i in board.pieces(chess.ROOK, chess.WHITE)])
rooksq = rooksq + sum([-rookstable[chess.square_mirror(i)] for i in board.pieces(chess.ROOK, chess.BLACK)])
queensq = sum([queenstable[i] for i in board.pieces(chess.QUEEN, chess.WHITE)])
queensq = queensq + sum([-queenstable[chess.square_mirror(i)] for i in board.pieces(chess.QUEEN, chess.BLACK)])
kingsq = sum([kingstable[i] for i in board.pieces(chess.KING, chess.WHITE)])
kingsq = kingsq + sum([-kingstable[chess.square_mirror(i)] for i in board.pieces(chess.KING, chess.BLACK)])
eval = material + pawnsq + knightsq + bishopsq + rooksq + queensq + kingsq
if board.turn:
return eval
else:
return -eval
# this function to add the numbers and letters around the board and it prints the board as well
def print_board(board):
string = str(board)
string_list = string.split("\n")
counter = 1
print("\na b c d e f g h")
print("---------------")
for array in string_list:
array += " |" + str(9- counter)
string_list[counter - 1] = array
counter += 1
print(array)
print("---------------")
print("a b c d e f g h \n")
def alphabeta(alpha, beta, depthleft, board):
bestscore = -9999
if( depthleft == 0 ):
return quiesce( alpha, beta, board )
for move in board.legal_moves:
board.push(move)
score = -alphabeta( -beta, -alpha, depthleft - 1 , board)
board.pop()
if( score >= beta ):
return score
if( score > bestscore ):
bestscore = score
if( score > alpha ):
alpha = score
return bestscore
def quiesce(alpha, beta, board):
stand_pat = evaluate_board(board)
if( stand_pat >= beta ):
return beta
if( alpha < stand_pat ):
alpha = stand_pat
for move in board.legal_moves:
if board.is_capture(move):
board.push(move)
score = -quiesce( -beta, -alpha, board )
board.pop()
if( score >= beta ):
return beta
if( score > alpha ):
alpha = score
return alpha
def selectmove(depth, board):
try:
move = chess.polyglot.MemoryMappedReader("bookfish.bin").weighted_choice(board).move()
movehistory.append(move)
return move
except:
bestMove = chess.Move.null()
bestValue = -99999
alpha = -100000
beta = 100000
for move in board.legal_moves:
board.push(move)
boardValue = -alphabeta(-beta, -alpha, depth-1, board)
if boardValue > bestValue:
bestValue = boardValue
bestMove = move
if boardValue > alpha:
alpha = boardValue
board.pop()
movehistory.append(bestMove)
return bestMove
# Functions below are used to add extra properties for the user side of the game.
def checking(board):
"""A function to check whether the game has stopping condition or not."""
if board.is_checkmate():
if board.turn:
return "Black won"
else:
return "White won"
if board.is_stalemate():
return "Draw"
if board.is_insufficient_material():
return "Draw"
return "Continue"
def game_state(check, board):
"""A function to check whether the game has stopping condition or not."""
if check == "Continue":
print_board(board)
elif check != "Continue":
print_board(board)
print(checking(board))
print("Game Over.")
return "break"
def hints_for_attack():
""" given a square, gets possible attack moves of a piece"""
board = chess.Board()
square_name = input("Enter square name: ").lower()
square_number = chess.SQUARE_NAMES.index(square_name)
squares = board.attacks(square_number)
return chess.svg.board(board, squares=squares, size=350)
def is_piece_under_attack(board):
"""At a certain point player might want to know a piece of his is under attack by oppenent,
the user shall enter square name, then function would get the piece index and the color piece,
uses built-in method (is_under_attack) to check if any of his pieces is in danger and returns a boolean"""
square = input('Enter square to check: ').lower()
square_number = chess.SQUARE_NAMES.index(square)
square_color = board.color_at(square_number)
# Checks if user input is valid or no
if square_color == None:
print("Square is empty")
else:
is_under_attack = board.is_attacked_by(not square_color, square_number)
if is_under_attack == True:
print(f'{square} is attacked!! Watch out!!!!')
attackers_answer = input("Do you want to know which pieces are attacking you? (y/n) \n")
if attackers_answer == 'y':
print(attackers(square, board))
else:
print(f'{square} is safe! :) \n')
def attackers(square, board):
"""At a certain point a player might want to know opponent's pieces that can attack of a piece of his,
the user shall enter square name, then function would get the piece index and the color piece,
uses built-in method (attackers) and returns a sqaure list"""
square_number = chess.SQUARE_NAMES.index(square)
square_color = board.color_at(square_number)
attackers_list = board.attackers(not square_color, square_number)
return attackers_list
def test_move(move, board):
if move == 'hint':
return True
try:
move_not_str = chess.Move.from_uci(move)
if move_not_str in board.legal_moves:
return True
else:
return False
except:
return False
# Functions below are to run the game in different modes.
def bot_vs_bot():
""" Bot vs. Bot Mode for testing."""
board = chess.Board()
move = 'd2d4'
board.push_san(move)
for seq in range(2):
bot1 = selectmove(3)
board.push(bot1)
print("Bot 1 move: ", bot1)
bot2 = selectmove(3)
board.push(bot2)
print("Bot 2 move: ", bot2)
def user_vs_bot():
""" User vs. Bot Mode."""
board = chess.Board()
# first = input("Would you like to start? (y/n) ")
first = input("Choose your pieces (white or black) (w/b) ")
if first == 'w':
player_no = 'Player'
else:
player_no = 'Bot'
mistakes = 0
while True:
if player_no == 'Player':
print("Would like to know who if you are under attack input 'hint'.")
move = input(f'{player_no} move: ')
test = test_move(move, board)
if test == True:
if move == 'hint':
is_piece_under_attack(board)
else:
board.push_san(move)
check = checking(board)
game_state(check, board)
player_no = "Bot"
if check != "Continue":
break
else:
mistakes += 1
print("You have entered an invalid move, try again.")
if mistakes >= 2:
legal_moves = []
for move in board.legal_moves:
legal_moves.append(str(move))
print("Valid moves: ", legal_moves)
elif player_no == 'Bot':
bot_move = selectmove(3, board)
board.push(bot_move)
print("Bot move: ", bot_move)
print_board(board)
player_no = "Player"
def user_vs_user():
""" User vs. User Mode."""
board = chess.Board()
player_no = 'Player #1'
mistakes = 0
while True:
print("If you want a hint on attacking input 'hint'.")
move = input(f'{player_no} move: ')
test = test_move(move, board)
if test == True:
if move == 'hint':
is_piece_under_attack(board)
else:
board.push_san(move)
check = checking(board)
game_state(check, board)
if player_no == "Player #1":
player_no = "Player #2"
else:
player_no = "Player #1"
if check != "Continue":
break
else:
mistakes += 1
print("You have entered an invalid move, try again.")
if mistakes >= 2:
legal_moves = []
for move in board.legal_moves:
legal_moves.append(str(move))
print("Valid moves: ", legal_moves)
# Main function to run whole game.
def game():
print("Welcome to Py Gambit!")
mode = input("Which mode would you like to play? Against computer(1) or another player(2)? ")
if mode == '1':
user_vs_bot()
if mode == '2':
user_vs_user()
game()