-
Notifications
You must be signed in to change notification settings - Fork 8
/
chess.py
882 lines (847 loc) · 41.9 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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
from copy import deepcopy
"""
Game Engine for playing chess in the console
"""
class Chess:
"""
Input: EPD - string representing the EPD hash you want to start the game with
(Default='rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -') [OPTIONAL]
Description: Chess initail variables
Output: None
"""
def __init__(self, EPD='rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -'):
self.x = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] #Board x representation
self.y = ['8', '7', '6', '5', '4', '3', '2', '1'] #Board y representation
self.notation = {'p':1, 'n':2, 'b':3, 'r':4, 'q':5, 'k':6} #Map of notation to part number
self.parts = {1:'Pawn', 2:'Knight', 3:'Bishop', 4:'Rook', 5:'Queen', 6:'King'} #Map of number to part
self.c_escape = {} #Possible check escapes
self.reset(EPD=EPD) #Reset game board and state
"""
Input: EPD - string representing the EPD hash you want to start the game with
(Default='rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -') [OPTIONAL]
Description: reset game board to desired EPD hash
Output: None
"""
def reset(self, EPD='rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -'):
self.log = [] #Game log
self.init_pos = EPD #Inital position
self.EPD_table = {} #EPD hashtable
self.p_move = 1 #Current players move white = 1 black = -1
self.castling = [1, 1, 1, 1] #Castling control
self.en_passant = None #En passant control
self.prev_move = None #Previous move
self.board = [[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]] #Generate empty chess board
self.load_EPD(EPD) #Load in game starting position
"""
Input: None
Description: display the current game state
Output: None
"""
def display(self):
result = ' a b c d e f g h \n ----------------\n'
for c, y in enumerate(self.board):
result += f'{8-c}|'
for x in y:
if x != 0:
n = getattr(Chess, self.parts[int(x) if x > 0 else int(x)*(-1)])().notation.lower() if x < 0 else getattr(Chess, self.parts[int(x) if x > 0 else int(x)*(-1)])().notation.upper()
if n == '':
n = 'p' if x < 0 else 'P'
result += n
else:
result += '.'
result += ' '
result += f'|{8-c}\n'
result += ' ----------------\n a b c d e f g h\n'
print(result)
"""
Input: cord - string representing the game board cordinate you want to convert
Description: convert board string cordinate to a matrix index of the cordinate
Output: tuple of x,y cordinates for game board matrix
"""
def board_2_array(self, cord):
cord = list(cord)
if len(cord) == 2 and str(cord[0]).lower() in self.x and str(cord[1]) in self.y:
return self.x.index(str(cord[0]).lower()), self.y.index(str(cord[1]))
else:
return None
"""
Input: None
Description: represent current game state as a EPD hash
Output: string representing the current game in EPD hash format
"""
def EPD_hash(self):
result = ''
for i, rank in enumerate(self.board):
e_count = 0
for square in rank:
if square == 0:
e_count += 1
else:
if e_count > 0:
result += str(e_count)
e_count = 0
p_name = self.parts[int(square) if square > 0 else int(square)*(-1)] #Get name of part
p_notation = getattr(Chess, p_name)().notation
if p_notation == '':
p_notation = 'p'
if square < 0:
p_notation = str(p_notation).lower()
else:
p_notation = str(p_notation).upper()
result += p_notation
if e_count > 0:
result += str(e_count)
if i < 7:
result += '/'
if self.p_move == -1:
result += ' w'
else:
result += ' b'
result += ' '
if sum(self.castling) == 0:
result += '-'
else:
if self.castling[0] == 1:
result += 'K'
if self.castling[1] == 1:
result += 'Q'
if self.castling[2] == 1:
result += 'k'
if self.castling[3] == 1:
result += 'q'
result += ' '
if self.en_passant == None:
result += '-'
else:
result += f'{self.x[self.en_passant[0]]}{self.y[self.en_passant[1]]}'
return result
"""
Input: EPD - string representing the current game in EPD hash format
Description: update game state to requirements in supplied EPD hash
Output: boolean representing the outcome of the function
"""
def load_EPD(self, EPD):
data = EPD.split(' ')
if len(data) == 4:
for x, rank in enumerate(data[0].split('/')):
y = 0
for p in rank:
if p.isdigit():
for i in range(int(p)):
self.board[x][y] = 0
y += 1
else:
self.board[x][y] = self.notation[str(p).lower()]*(-1) if str(p).islower() else self.notation[str(p).lower()]
y += 1
self.p_move = 1 if data[1] == 'w' else -1
if 'K' in data[2]:
self.castling[0] = 1
else:
self.castling[0] = 0
if 'Q' in data[2]:
self.castling[1] = 1
else:
self.castling[1] = 0
if 'k' in data[2]:
self.castling[2] = 1
else:
self.castling[2] = 0
if 'q' in data[2]:
self.castling[3] = 1
else:
self.castling[3] = 0
self.en_passant = None if data[3] == '-' else self.board_2_array(data[3])
return True
else:
return False
"""
Input: part - integer representing the peice that was moved
cur_cord - string representing the current cordinate of the peice
next_pos - string representing the next cordinate of the peice
n_part - string representing the new part pawn became, used for pawn promotion (Default = None) [OPTIONAL]
Description: log player move using chess notation (English)
Output: None
"""
def log_move(self, part, cur_cord, next_cord, cur_pos, next_pos, n_part=None):
#to remove ambiguity where multiple pieces could make the move add starting identifier after piece notation ex Rab8
if part == 6*self.p_move and next_pos[0]-cur_pos[0] == 2:
move = '0-0'
elif part == 6*self.p_move and next_pos[0]-cur_pos[0] == -2:
move = '0-0-0'
elif part == 1*self.p_move and n_part != None:
move = f'{str(next_cord).lower()}={str(n_part).upper()}'
else:
p_name = self.parts[int(part) if part > 0 else int(part)*(-1)] #Get name of part
move = str(getattr(Chess, p_name)().notation).upper() #Get part notation
if self.board[next_pos[1]][next_pos[0]] != 0 or (next_pos == self.en_passant and (part == 1 or part == -1)): #Check if there is a capture
move += 'x' if move != '' else str(cur_cord)[0] + 'x'
move += str(next_cord).lower()
self.log.append(move)
"""
Input: cur_cord - string representing the current cordinate of the peice
next_pos - string representing the next cordinate of the peice
Description: move peice on game board
Output: boolean representing the state of the function
"""
def move(self, cur_pos, next_pos):
cp = self.board_2_array(cur_pos)
np = self.board_2_array(next_pos)
if self.valid_move(cp, np) == True:
part = self.board[cp[1]][cp[0]]
if np == self.en_passant and (part == 1 or part == -1):
self.board[self.en_passant[1]-(self.p_move*(-1))][self.en_passant[0]] = 0
self.log_move(part, cur_pos, next_pos, cp, np)
self.prev_move = self.board
if (part == 1 and np[1] == 4) or (part == -1 and np[1] == 3):
self.en_passant = (np[0], np[1]+1) if part == 1 else (np[0], np[1]-1)
elif part == 6*self.p_move and np[0]-cp[0] == 2:
self.board[np[1]][np[0]-1] = 4*self.p_move
self.board[np[1]][np[0]+1] = 0
elif part == 6*self.p_move and np[0]-cp[0] == -2:
self.board[np[1]][np[0]+1] = 4*self.p_move
self.board[np[1]][np[0]-2] = 0
else:
self.en_passant = None
if part == 6*self.p_move:
if self.p_move == 1:
self.castling[0] = 0
self.castling[1] = 0
else:
self.castling[2] = 0
self.castling[3] = 0
elif part == 4*self.p_move:
if self.p_move == 1:
if cp == (0, 7):
self.castling[1] = 0
else:
self.castling[0] = 0
else:
if cp == (0, 0):
self.castling[3] = 0
else:
self.castling[2] = 0
self.board[cp[1]][cp[0]] = 0
self.board[np[1]][np[0]] = part
hash = self.EPD_hash()
if hash in self.EPD_table:
self.EPD_table[hash] += 1
else:
self.EPD_table[hash] = 1
#self.p_move = self.p_move * (-1)
return True
return False
"""
Input: cur_cord - string representing the current cordinate of the peice
next_pos - string representing the next cordinate of the peice
Description: determine if player move is valid game move
Output: boolean representing the state of the player move
"""
def valid_move(self, cur_pos, next_pos):
if cur_pos != None and next_pos != None:
part = self.board[cur_pos[1]][cur_pos[0]]
if part * self.p_move > 0 and part != 0:
p_name = self.parts[int(part) if part > 0 else int(part)*(-1)] #Get name of part
v_moves = getattr(Chess, p_name).movement(self, self.p_move, cur_pos, capture=True)
#print(v_moves)
if len(self.log) > 0 and '+' in self.log[-1]:
v_moves = [m for m in v_moves if cur_pos in self.c_escape and m in self.c_escape[cur_pos]]
if next_pos in v_moves:
return True
return False
"""
Input: capture - boolean representing control of if you do not allow moves past peice capture (Default=True) [OPTIONAL]
Description: determine all possible board moves for current game state
Output: dictionary containing all possible moves by peice on the board
"""
def possible_board_moves(self, capture=True):
moves = {}
for y, row in enumerate(self.board):
for x, part in enumerate(row):
if part != 0:
p_name = self.parts[int(part) if part > 0 else int(part)*(-1)] #Get name of part
p_colour = 1 if part > 0 else -1
v_moves = getattr(Chess, p_name).movement(self, p_colour, [x, y], capture=capture)
if len(self.log) > 0 and '+' in self.log[-1]:
v_moves = [m for m in v_moves if (x, y) in self.c_escape and m in self.c_escape[(x, y)]]
moves[f'{str(self.x[x]).upper() if p_colour > 0 else str(self.x[x]).lower()}{self.y[y]}'] = v_moves
return moves
"""
Input: moves - dictionary containing all possible moves for current game state
check - boolean representing if check has been found or not (Default=False) [OPTIONAL]
Description: determine if the current game state results in a check mate or not
Output: list representing current state of the game
"""
def is_checkmate(self, moves):
self.c_escape = {}
k_pos = () #King position
p_blocks = [] #Possible blocks
u_moves = {} #User potential moves
#Sort all possible moves
for p, a in moves.items():
pos = self.board_2_array(p)
if (str(p[0]).isupper() and self.p_move == -1) or (str(p[0]).islower() and self.p_move == 1):
if self.board[pos[1]][pos[0]] == self.King().value * (self.p_move*(-1)):
k_pos = (pos, a)
else:
for m in a:
if (pos, m) not in p_blocks:
p_blocks.append((pos, m))
else:
if pos not in u_moves:
u_moves[pos] = a
p_moves = [m for a in u_moves.values() for m in a]
#Check if checkmate is in posible moves
if len(k_pos) > 0 and k_pos[0] not in p_moves:
return [0, 0, 0]
elif len(k_pos) == 0:
for y, row in enumerate(self.board):
if self.King().value * (self.p_move*(-1)) in row:
k_pos = ((row.index(self.King().value*(self.p_move*(-1))), y), [])
break
if len(k_pos) > 0 and k_pos[0] in p_moves:
for m in p_blocks:
i_game = deepcopy(self)
i_game.p_move = i_game.p_move * (-1)
i_game.move(f'{self.x[m[0][0]]}{self.y[m[0][1]]}', f'{self.x[m[1][0]]}{self.y[m[1][1]]}') #Move king
i_game.p_move = i_game.p_move * (-1)
i_moves = i_game.possible_board_moves(capture=True) #Imaginary moves
if True not in [True for k in i_moves if k_pos[0] in i_moves[k]]: #Check if moved king still in check
#if len(self.log) > 0 and self.log[-1][-1] is not '+':
#self.log[-1] += '+' #Check
#print(m,f'{self.x[m[0][0]]}{self.y[m[0][1]]}', f'{self.x[m[1][0]]}{self.y[m[1][1]]}')
if m[0] in self.c_escape:
self.c_escape[m[0]].append(m[1])
else:
self.c_escape[m[0]] = [m[1]]
#return [0, 0, 0]
for m in k_pos[1]:
if m not in p_moves:
i_game = deepcopy(self)
i_game.p_move = i_game.p_move * (-1)
i_game.move(f'{self.x[k_pos[0][0]]}{self.y[k_pos[0][1]]}', f'{self.x[m[0]]}{self.y[m[1]]}') #Move king
i_game.p_move = i_game.p_move * (-1)
i_moves = i_game.possible_board_moves(capture=True) #Imaginary moves
if True not in [True for k in i_moves if m in i_moves[k]]: #Check if moved king still in check
#if len(self.log) > 0 and self.log[-1][-1] is not '+':
#self.log[-1] += '+' #Check
#print(m)
#print(k_pos[0],m,f'{self.x[k_pos[0][0]]}{self.y[k_pos[0][1]]}', f'{self.x[m[0]]}{self.y[m[1]]}')
if k_pos[0] in self.c_escape:
self.c_escape[k_pos[0]].append(m)
else:
self.c_escape[k_pos[0]] = [m]
#return [0, 0, 0]
if len(self.c_escape) > 0:
self.log[-1] += '+' #Check
return [0, 0, 0]
elif self.p_move == -1:
self.log[-1] += '#'
return [0, 0, 1] #Black wins
else:
self.log[-1] += '#'
return [1, 0, 0] #White wins
else:
return [1, 0, 0] if self.p_move == 1 else [0, 0, 1]
"""
Input: n_part - string representing the new part you want to promote your pawn to (Default=None) [OPTIONAL]
Description: update game board with new part for pawn promotion
Output: boolean representing the state of the function
"""
def pawn_promotion(self, n_part=None):
if n_part == None:
while True:
n_part = input('\nPawn Promotion - What peice would you like to switch too:\n\n*Queen[q]\n*Bishop[b]\n*Knight[n]\n*Rook[r]\n')
if str(n_part).lower() not in ['q', 'b', 'n', 'r', 'queen', 'bishop', 'knight', 'rook']:
print('\nInvalid Option')
else:
break
if len(n_part) > 1:
n_part = getattr(Chess, str(n_part).capitalize())().notation
part = self.notation[str(n_part).lower()]*self.p_move
pos = self.board_2_array(self.log[-1].replace('+', '').split('x')[-1])
if pos != None:
self.board[pos[1]][pos[0]] = part
self.log[-1] += f'={str(n_part).upper()}'
return True
else:
return False
"""
Input: moves - dictionary containing all possible moves for current game state
choice - string representing if you want a draw or not (Default=None) (Choices=['y','yes','n','no']) [OPTIONAL]
Description: check current state of the game for the fifty move rule
Output: boolean representing the state of the function
"""
def fifty_move_rule(self, moves, choice=None):
if len(self.log) > 100:
for m in self.log[-100:]:
if 'x' in m or m[0].islower():
return False
else:
return False
if choice == None:
while True:
choice = input('Fifty move rule - do you want to claim a draw? [Y/N]')
if choice.lower() == 'y' or choice.lower() == 'yes' or choice.lower() == '1':
return True
elif choice.lower() == 'n' or choice.lower() == 'no' or choice.lower() == '0':
return False
print('Unsupported answer')
if choice.lower() == 'y' or choice.lower() == 'yes' or choice.lower() == '1':
return True
elif choice.lower() == 'n' or choice.lower() == 'no' or choice.lower() == '0':
return False
"""
Input: moves - dictionary containing all possible moves for current game state
Description: check current state of the game for the seventy five move rule
Output: boolean representing the state of the function
"""
def seventy_five_move_rule(self, moves):
if len(self.log) > 150:
for m in self.log[-150:]:
if 'x' in m or m[0].islower():
return False
else:
return False
return True
"""
Input: hash - string representing the game state you want to check for in game EPD hash table
Description: check current state of the game for the three fold rule
Output: boolean representing the state of the function
"""
def three_fold_rule(self, hash):
if hash in self.EPD_table:
if self.EPD_table[hash] == 3:
while True:
choice = input('Three fold rule - do you want to claim a draw? [Y/N]')
if choice.lower() == 'y' or choice.lower() == 'yes' or choice.lower() == '1':
return True
elif choice.lower() == 'n' or choice.lower() == 'no' or choice.lower() == '0':
return False
print('Unsupported answer')
return False
"""
Input: hash - string representing the game state you want to check for in game EPD hash table
Description: check current state of the game for the five fold rule
Output: boolean representing the state of the function
"""
def five_fold_rule(self, hash):
if hash in self.EPD_table:
if self.EPD_table[hash] >= 5:
return True
return False
"""
Input: moves - dictionary containing all possible moves for current game state
Description: check to see if the current state is a dead position
Output: boolean representing the state of the function
"""
def is_dead_position(self, moves):
#King and bishop against king and bishop with both bishops on squares of the same colour
a_pieces = []
for y in self.board:
for x in y:
if x != 0:
a_pieces.append(x)
if len(a_pieces) > 4:
return False
if len(a_pieces) == 2 and -6 in a_pieces and 6 in a_pieces:
return True
elif len(a_pieces) == 3 and ((-6 in a_pieces and 3 in a_pieces and 6 in a_pieces) or (-6 in a_pieces and -3 in a_pieces and 6 in a_pieces)):
return True
elif len(a_pieces) == 3 and ((-6 in a_pieces and 2 in a_pieces and 6 in a_pieces) or (-6 in a_pieces and -2 in a_pieces and 6 in a_pieces)):
return True
return False
"""
Input: moves - dictionary containing all possible moves for current game state
Description: check to see if the current state is a stalemate
Output: boolean representing the state of the function
"""
def is_stalemate(self, moves):
if False not in [False for p, a in moves.items() if len(a) > 0 and ((self.p_move == 1 and str(p[0]).isupper()) or (self.p_move == -1 and str(p[0]).islower()))]:
return True
return False
def is_draw(self, moves, hash):
if self.is_stalemate(moves) == True:
return True
elif self.is_dead_position(moves) == True:
return True
elif self.seventy_five_move_rule(moves) == True:
return True
elif self.five_fold_rule(hash) == True:
return True
elif self.fifty_move_rule(moves) == True:
return True
elif self.three_fold_rule(hash) == True:
return True
return False
"""
Input: None
Description: check to see if it's the end of the game
Output: list containing the state of the game
"""
def is_end(self):
w_king = False
b_king = False
for y, row in enumerate(self.board):
for x, peice in enumerate(row):
if self.board[y][x] == self.King().value * (-1):
b_king = True
elif self.board[y][x] == self.King().value:
w_king = True
if w_king == False and b_king == False:
return [0, 1, 0]
elif w_king == False:
return [0, 0, 1]
elif b_king == False:
return [1, 0, 0]
moves = self.possible_board_moves(capture=True)
check_mate = self.is_checkmate(moves)
hash = self.EPD_hash()
if sum(check_mate) > 0:
return check_mate
elif self.is_draw(moves, hash) == True:
return [0, 1, 0]
return [0, 0, 0]
"""
Input: hash - string representing the game state you want to check for in game EPD hash table
Description: check current state of the game
Output: boolean representing the state of the game or string representing additional action needed
"""
def check_state(self, hash):
if len(self.log) > 0 and self.p_move == 1 and (self.log[-1][0].isupper() == False or self.log[-1][0] == 'P') and True in [True for l in self.log[-1] if l == '8']:
return 'PP' #Pawn promotion
elif len(self.log) > 0 and self.p_move == -1 and (self.log[-1][0].isupper() == False or self.log[-1][0] == 'P') and True in [True for l in self.log[-1] if l == '1']:
return 'PP' #Pawn promotion
elif hash in self.EPD_table and self.EPD_table[hash] == 3:
return '3F' #3 Fold
elif len(self.log) > 100:
for m in self.log[-100:]:
if 'x' in m or m[0].islower():
return None
return '50M' #50 move
else:
return None
"""
Chess peice object for the king
"""
class King:
"""
Input: None
Description: King initail variables
Output: None
"""
def __init__(self):
self.value = 6 #Numerical value of piece
self.notation = 'K' #Chess notation
"""
Input: player - integer representing which player the peice belongs to
pos - tuple containing the current position of the peice
capture - boolean representing control of if you do not allow moves past peice capture (Default=True) [OPTIONAL]
Description: show possible moves for peice
Output: list of possible moves for the peice
"""
def movement(game, player, pos, capture=True):
result = []
if pos[1]+1 >= 0 and pos[1]+1 <= 7 and pos[0] >= 0 and pos[0] <= 7 and (game.board[pos[1]+1][pos[0]]*player < 0 or game.board[pos[1]+1][pos[0]] == 0):
result.append((pos[0], pos[1]+1))
if pos[1]-1 >= 0 and pos[1]-1 <= 7 and pos[0] >= 0 and pos[0] <= 7 and (game.board[pos[1]-1][pos[0]]*player < 0 or game.board[pos[1]-1][pos[0]] == 0):
result.append((pos[0], pos[1]-1))
if pos[1] >= 0 and pos[1] <= 7 and pos[0]+1 >= 0 and pos[0]+1 <= 7 and (game.board[pos[1]][pos[0]+1]*player < 0 or game.board[pos[1]][pos[0]+1] == 0):
result.append((pos[0]+1, pos[1]))
if pos[1] >= 0 and pos[1] <= 7 and pos[0]-1 >= 0 and pos[0]-1 <= 7 and (game.board[pos[1]][pos[0]-1]*player < 0 or game.board[pos[1]][pos[0]-1] == 0):
result.append((pos[0]-1, pos[1]))
if pos[1]+1 >= 0 and pos[1]+1 <= 7 and pos[0]+1 >= 0 and pos[0]+1 <= 7 and (game.board[pos[1]+1][pos[0]+1]*player < 0 or game.board[pos[1]+1][pos[0]+1] == 0):
result.append((pos[0]+1, pos[1]+1))
if pos[1]+1 >= 0 and pos[1]+1 <= 7 and pos[0]-1 >= 0 and pos[0]-1 <= 7 and (game.board[pos[1]+1][pos[0]-1]*player < 0 or game.board[pos[1]+1][pos[0]-1] == 0):
result.append((pos[0]-1, pos[1]+1))
if pos[1]-1 >= 0 and pos[1]-1 <= 7 and pos[0]+1 >= 0 and pos[0]+1 <= 7 and (game.board[pos[1]-1][pos[0]+1]*player < 0 or game.board[pos[1]-1][pos[0]+1] == 0):
result.append((pos[0]+1, pos[1]-1))
if pos[1]-1 >= 0 and pos[1]-1 <= 7 and pos[0]-1 >= 0 and pos[0]-1 <= 7 and (game.board[pos[1]-1][pos[0]-1]*player < 0 or game.board[pos[1]-1][pos[0]-1] == 0):
result.append((pos[0]-1, pos[1]-1))
if (pos == (4, 7) or pos == (4, 0)) and game.board[pos[1]][pos[0]+1] == 0 and game.board[pos[1]][pos[0]+2] == 0 and ((game.castling[0] == 1 and game.p_move == 1) or (game.castling[2] == 1 and game.p_move == -1)):
result.append((pos[0]+2, pos[1]))
if (pos == (4, 7) or pos == (4, 0)) and game.board[pos[1]][pos[0]-1] == 0 and game.board[pos[1]][pos[0]-2] == 0 and ((game.castling[1] == 1 and game.p_move == 1) or (game.castling[3] == 1 and game.p_move == -1)):
result.append((pos[0]-2, pos[1]))
return result
"""
Chess peice object for the queen
"""
class Queen:
"""
Input: None
Description: Queen initail variables
Output: None
"""
def __init__(self):
self.value = 5 #Numerical value of piece
self.notation = 'Q' #Chess notation
"""
Input: player - integer representing which player the peice belongs to
pos - tuple containing the current position of the peice
capture - boolean representing control of if you do not allow moves past peice capture (Default=True) [OPTIONAL]
Description: show possible moves for peice
Output: list of possible moves for the peice
"""
def movement(game, player, pos, capture=True):
result = []
check = [True, True, True, True, True, True, True, True]
for c in range(1, 8, 1):
if pos[1]+c >= 0 and pos[1]+c <= 7 and pos[0] >= 0 and pos[0] <= 7 and (game.board[pos[1]+c][pos[0]]*player < 0 or game.board[pos[1]+c][pos[0]] == 0) and check[0] == True:
result.append((pos[0], pos[1]+c))
if game.board[pos[1]+c][pos[0]]*player < 0 and capture == True:
check[0] = False
else:
check[0] = False
if pos[1]-c >= 0 and pos[1]-c <= 7 and pos[0] >= 0 and pos[0] <= 7 and (game.board[pos[1]-c][pos[0]]*player < 0 or game.board[pos[1]-c][pos[0]] == 0) and check[1] == True:
result.append((pos[0], pos[1]-c))
if game.board[pos[1]-c][pos[0]]*player < 0 and capture == True:
check[1] = False
else:
check[1] = False
if pos[1] >= 0 and pos[1] <= 7 and pos[0]+c >= 0 and pos[0]+c <= 7 and (game.board[pos[1]][pos[0]+c]*player < 0 or game.board[pos[1]][pos[0]+c] == 0) and check[2] == True:
result.append((pos[0]+c, pos[1]))
if game.board[pos[1]][pos[0]+c]*player < 0 and capture == True:
check[2] = False
else:
check[2] = False
if pos[1] >= 0 and pos[1] <= 7 and pos[0]-c >= 0 and pos[0]-c <= 7 and (game.board[pos[1]][pos[0]-c]*player < 0 or game.board[pos[1]][pos[0]-c] == 0) and check[3] == True:
result.append((pos[0]-c, pos[1]))
if game.board[pos[1]][pos[0]-c]*player < 0 and capture == True:
check[3] = False
else:
check[3] = False
if pos[1]+c >= 0 and pos[1]+c <= 7 and pos[0]+c >= 0 and pos[0]+c <= 7 and (game.board[pos[1]+c][pos[0]+c]*player < 0 or game.board[pos[1]+c][pos[0]+c] == 0) and check[4] == True:
result.append((pos[0]+c, pos[1]+c))
if game.board[pos[1]+c][pos[0]+c]*player < 0 and capture == True:
check[4] = False
else:
check[4] = False
if pos[1]+c >= 0 and pos[1]+c <= 7 and pos[0]-c >= 0 and pos[0]-c <= 7 and (game.board[pos[1]+c][pos[0]-c]*player < 0 or game.board[pos[1]+c][pos[0]-c] == 0) and check[5] == True:
result.append((pos[0]-c, pos[1]+c))
if game.board[pos[1]+c][pos[0]-c]*player < 0 and capture == True:
check[5] = False
else:
check[5] = False
if pos[1]-c >= 0 and pos[1]-c <= 7 and pos[0]+c >= 0 and pos[0]+c <= 7 and (game.board[pos[1]-c][pos[0]+c]*player < 0 or game.board[pos[1]-c][pos[0]+c] == 0) and check[6] == True:
result.append((pos[0]+c, pos[1]-c))
if game.board[pos[1]-c][pos[0]+c]*player < 0 and capture == True:
check[6] = False
else:
check[6] = False
if pos[1]-c >= 0 and pos[1]-c <= 7 and pos[0]-c >= 0 and pos[0]-c <= 7 and (game.board[pos[1]-c][pos[0]-c]*player < 0 or game.board[pos[1]-c][pos[0]-c] == 0) and check[7] == True:
result.append((pos[0]-c, pos[1]-c))
if game.board[pos[1]-c][pos[0]-c]*player < 0 and capture == True:
check[7] = False
else:
check[7] = False
if True not in check:
break
return result
"""
Chess peice object for the rook
"""
class Rook:
"""
Input: None
Description: Rook initail variables
Output: None
"""
def __init__(self):
self.value = 4 #Numerical value of piece
self.notation = 'R' #Chess notation
"""
Input: player - integer representing which player the peice belongs to
pos - tuple containing the current position of the peice
capture - boolean representing control of if you do not allow moves past peice capture (Default=True) [OPTIONAL]
Description: show possible moves for peice
Output: list of possible moves for the peice
"""
def movement(game, player, pos, capture=True):
result = []
check = [True, True, True, True]
for c in range(1, 8, 1):
if pos[1]+c >= 0 and pos[1]+c <= 7 and pos[0] >= 0 and pos[0] <= 7 and (game.board[pos[1]+c][pos[0]]*player < 0 or game.board[pos[1]+c][pos[0]] == 0) and check[0] == True:
result.append((pos[0], pos[1]+c))
if game.board[pos[1]+c][pos[0]]*player < 0 and capture == True:
check[0] = False
else:
check[0] = False
if pos[1]-c >= 0 and pos[1]-c <= 7 and pos[0] >= 0 and pos[0] <= 7 and (game.board[pos[1]-c][pos[0]]*player < 0 or game.board[pos[1]-c][pos[0]] == 0) and check[1] == True:
result.append((pos[0], pos[1]-c))
if game.board[pos[1]-c][pos[0]]*player < 0 and capture == True:
check[1] = False
else:
check[1] = False
if pos[1] >= 0 and pos[1] <= 7 and pos[0]+c >= 0 and pos[0]+c <= 7 and (game.board[pos[1]][pos[0]+c]*player < 0 or game.board[pos[1]][pos[0]+c] == 0) and check[2] == True:
result.append((pos[0]+c, pos[1]))
if game.board[pos[1]][pos[0]+c]*player < 0 and capture == True:
check[2] = False
else:
check[2] = False
if pos[1] >= 0 and pos[1] <= 7 and pos[0]-c >= 0 and pos[0]-c <= 7 and (game.board[pos[1]][pos[0]-c]*player < 0 or game.board[pos[1]][pos[0]-c] == 0) and check[3] == True:
result.append((pos[0]-c, pos[1]))
if game.board[pos[1]][pos[0]-c]*player < 0 and capture == True:
check[3] = False
else:
check[3] = False
if True not in check:
break
return result
"""
Chess peice object for the bishop
"""
class Bishop:
"""
Input: None
Description: Bishop initail variables
Output: None
"""
def __init__(self):
self.value = 3 #Numerical value of piece
self.notation = 'B' #Chess notation
"""
Input: player - integer representing which player the peice belongs to
pos - tuple containing the current position of the peice
capture - boolean representing control of if you do not allow moves past peice capture (Default=True) [OPTIONAL]
Description: show possible moves for peice
Output: list of possible moves for the peice
"""
def movement(game, player, pos, capture=True):
result = []
check = [True, True, True, True]
for c in range(1, 8, 1):
if pos[1]+c >= 0 and pos[1]+c <= 7 and pos[0]+c >= 0 and pos[0]+c <= 7 and (game.board[pos[1]+c][pos[0]+c]*player < 0 or game.board[pos[1]+c][pos[0]+c] == 0) and check[0] == True:
result.append((pos[0]+c, pos[1]+c))
if game.board[pos[1]+c][pos[0]+c]*player < 0 and capture == True:
check[0] = False
else:
check[0] = False
if pos[1]+c >= 0 and pos[1]+c <= 7 and pos[0]-c >= 0 and pos[0]-c <= 7 and (game.board[pos[1]+c][pos[0]-c]*player < 0 or game.board[pos[1]+c][pos[0]-c] == 0) and check[1] == True:
result.append((pos[0]-c, pos[1]+c))
if game.board[pos[1]+c][pos[0]-c]*player < 0 and capture == True:
check[1] = False
else:
check[1] = False
if pos[1]-c >= 0 and pos[1]-c <= 7 and pos[0]+c >= 0 and pos[0]+c <= 7 and (game.board[pos[1]-c][pos[0]+c]*player < 0 or game.board[pos[1]-c][pos[0]+c] == 0) and check[2] == True:
result.append((pos[0]+c, pos[1]-c))
if game.board[pos[1]-c][pos[0]+c]*player < 0 and capture == True:
check[2] = False
else:
check[2] = False
if pos[1]-c >= 0 and pos[1]-c <= 7 and pos[0]-c >= 0 and pos[0]-c <= 7 and (game.board[pos[1]-c][pos[0]-c]*player < 0 or game.board[pos[1]-c][pos[0]-c] == 0) and check[3] == True:
result.append((pos[0]-c, pos[1]-c))
if game.board[pos[1]-c][pos[0]-c]*player < 0 and capture == True:
check[3] = False
else:
check[3] = False
if True not in check:
break
return result
"""
Chess peice object for the knight
"""
class Knight:
"""
Input: None
Description: Knight initail variables
Output: None
"""
def __init__(self):
self.value = 2 #Numerical value of piece
self.notation = 'N' #Chess notation
"""
Input: player - integer representing which player the peice belongs to
pos - tuple containing the current position of the peice
capture - boolean representing control of if you do not allow moves past peice capture (Default=True) [OPTIONAL]
Description: show possible moves for peice
Output: list of possible moves for the peice
"""
def movement(game, player, pos, capture=True):
result = []
for i in [-1, 1]:
if pos[0]-i >= 0 and pos[0]-i <= 7 and pos[1]-(2*i) >= 0 and pos[1]-(2*i) <= 7 and (game.board[pos[1]-(2*i)][pos[0]-i]*player < 0 or game.board[pos[1]-(2*i)][pos[0]-i] == 0):
result.append((pos[0]-i, pos[1]-(2*i)))
if pos[0]+i >= 0 and pos[0]+i <= 7 and pos[1]-(2*i) >= 0 and pos[1]-(2*i) <= 7 and (game.board[pos[1]-(2*i)][pos[0]+i]*player < 0 or game.board[pos[1]-(2*i)][pos[0]+i] == 0):
result.append((pos[0]+i, pos[1]-(2*i)))
if pos[0]-(2*i) >= 0 and pos[0]-(2*i) <= 7 and pos[1]-i >= 0 and pos[1]-i <= 7 and (game.board[pos[1]-i][pos[0]-(2*i)]*player < 0 or game.board[pos[1]-i][pos[0]-(2*i)] == 0):
result.append((pos[0]-(2*i), pos[1]-i))
if pos[0]-(2*i) >= 0 and pos[0]-(2*i) <= 7 and pos[1]+i >= 0 and pos[1]+i <= 7 and (game.board[pos[1]+i][pos[0]-(2*i)]*player < 0 or game.board[pos[1]+i][pos[0]-(2*i)] == 0):
result.append((pos[0]-(2*i), pos[1]+i))
return result
"""
Chess peice object for the pawn
"""
class Pawn:
"""
Input: None
Description: Pawn initail variables
Output: None
"""
def __init__(self):
self.value = 1 #Numerical value of piece
self.notation = '' #Chess notation
"""
Input: player - integer representing which player the peice belongs to
pos - tuple containing the current position of the peice
capture - boolean representing control of if you do not allow moves past peice capture (Default=True) [OPTIONAL]
Description: show possible moves for peice
Output: list of possible moves for the peice
"""
def movement(game, player, pos, capture=True):
result = []
init = 1 if player < 0 else 6
amt = 1 if pos[1] != init else 2
for i in range(amt):
if pos[1]-((i+1)*player) >= 0 and pos[1]-((i+1)*player) <= 7 and game.board[pos[1]-((i+1)*player)][pos[0]] == 0:
result.append((pos[0], pos[1]-((i+1)*player)))
else:
break
if pos[1]-player <= 7 and pos[1]-player >= 0 and pos[0]+1 <= 7 and pos[0]+1 >= 0 and game.board[pos[1]-player][pos[0]+1]*player < 0:
result.append((pos[0]+1, pos[1]-player))
if pos[1]-player >= 0 and pos[1]-player <= 7 and pos[0]-1 >= 0 and pos[0]-1 <= 7 and game.board[pos[1]-player][pos[0]-1]*player < 0:
result.append((pos[0]-1, pos[1]-player))
if pos[1]-player <= 7 and pos[1]-player >= 0 and pos[0]+1 <= 7 and pos[0]+1 >= 0 and (pos[0]+1, pos[1]-player) == game.en_passant:
result.append((pos[0]+1, pos[1]-player))
if pos[1]-player >= 0 and pos[1]-player <= 7 and pos[0]-1 >= 0 and pos[0]-1 <= 7 and (pos[0]-1, pos[1]-player) == game.en_passant:
result.append((pos[0]-1, pos[1]-player))
return result
if __name__ == '__main__':
#chess_game = Chess(EPD='4kb2/rpp1p3/6p1/6Np/3Q1B2/4P2b/PPP2PPP/RN1R2K1 w - -')
chess_game = Chess(EPD='1b4k1/Q7/p2np1/P1P2p2/1P3P2/1R5R/q6P/5rK1 b - -')
#chess_game = Chess()
while True:
if chess_game.p_move == 1:
print('\nWhites Turn [UPPER CASE]\n')
else:
print('\nBlacks Turn [LOWER CASE]\n')
chess_game.display()
cur = input('What piece do you want to move?\n')
next = input('Where do you want to move the piece to?\n')
if chess_game.move(cur, next) == False:
if len(chess_game.log) > 0 and '+' in chess_game.log[-1]:
print('Invalid move, you are in check')
else:
print('Invalid move')
else:
state = chess_game.is_end()
if sum(state) > 0:
print('\n*********************\n GAME OVER\n*********************\n')
chess_game.display()
print('Game Log:\n---------\n')
print(f'INITIAL POSITION = {chess_game.init_pos}')
print(f'MOVES = {chess_game.log}')
print('\nGame Result:\n------------\n')
if state == [0, 0, 1]:
print('BLACK WINS\n')
elif state == [1, 0, 0]:
print('WHITE WINS\n')
else:
print('TIE GAME\n')
break
chess_game.p_move = chess_game.p_move * (-1)