-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessMastah_05.py
More file actions
667 lines (516 loc) · 21.4 KB
/
ChessMastah_05.py
File metadata and controls
667 lines (516 loc) · 21.4 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
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
#!/usr/bin/env python
import os,sys,random
# Chessmastah, started Jan 2012 by Svein Arne Roed
#
# Available at https://sites.google.com/site/marsarsite/files or
# http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=31849
#
# Contact: svein+roed 'a+t' Gmail
#
# Version 0.5 hereby released :
#
# Castling
# En passant
# Choice between Knight and Queen when promoting a Pawn
# Game now ends in a draw if:
# * only kings are left
# * no possible moves (and isn't in check)
# * 50 consecutive moves without movement of a Pawn or a capture
# Various fixes and cleanup, thanks to cProfile - more efficient code
# Play against (random) computer
#
# "AI" not rewritten to classes yet, so computer is simply picking
# a random move.
#
# Thanks to python-forum.org's users Akavall and Micseydel
# for constructive feedback.
class Player(object):
allsquares = [(x, y) for x in range(8) for y in range(8)]
dullmoves = 0
def __init__(self, colour, nature, name):
self.colour = colour
self.nature = nature
self.name = name
self.can_castle_long_this_turn = False
self.can_castle_short_this_turn = False
self.playedturns = 0
def __str__(self):
if self.nature is 'AI':
return self.name+' ('+self.nature+')'+' as '+self.colour
else:
return self.name+' as '+self.colour
def set_opponent(self, opponent):
self.opponent = opponent
def getpieces(self, board):
return [pos for pos in board if board[pos].colour is self.colour]
def potentialtargets(self, playerspieces):
return [pos for pos in self.allsquares if pos not in playerspieces]
def kingpos(self, board):
for mine in self.getpieces(board):
if board[mine].piecename is 'k':
return mine
def validmoves(self, board):
self.set_castling_flags(board)
mypieces=self.getpieces(board)
for mine in mypieces:
for target in self.potentialtargets(mypieces):
if self.canmoveto(board, mine, target):
if not self.makesuscheck(mine, target, board):
yield (mine, target)
def set_castling_flags(self, board):
kingpos = self.kingpos(board)
if self.king_can_castle(board, kingpos):
if self.rook_can_castle_long(board, kingpos):
self.can_castle_long_this_turn = True
else:
self.can_castle_long_this_turn = False
if self.rook_can_castle_short(board, kingpos):
self.can_castle_short_this_turn = True
else:
self.can_castle_short_this_turn = False
else:
self.can_castle_long_this_turn = False
self.can_castle_short_this_turn = False
def king_can_castle(self, board, kingpos):
if board[kingpos].nrofmoves is 0 and not self.isincheck(board):
return True
def rook_can_castle_long(self, board, kingpos):
if self.longrook in board and board[self.longrook].nrofmoves is 0:
if self.hasclearpath(self.longrook, kingpos, board):
tmptarget = (kingpos[0],kingpos[1]-1)
if not self.makesuscheck(kingpos, tmptarget, board):
return True
def rook_can_castle_short(self, board, kingpos):
if self.shortrook in board and board[self.shortrook].nrofmoves is 0:
if self.hasclearpath(self.shortrook, kingpos, board):
tmptarget = (kingpos[0],kingpos[1]+1)
if not self.makesuscheck(kingpos, tmptarget, board):
return True
def getposition(self, move):
startcol = int(ord(move[0].lower())-97)
startrow = int(move[1])-1
targetcol = int(ord(move[2].lower())-97)
targetrow = int(move[3])-1
start = (startrow, startcol)
target = (targetrow, targetcol)
return start, target
def reacheddraw(self, board):
if not list(self.validmoves(board)) and not self.isincheck(board):
return True
if len(list(self.getpieces(board))) == \
len(list(self.opponent.getpieces(board))) == 1:
return True
if Player.dullmoves/2 == 50:
if self.nature is 'AI':
return True
else:
if raw_input("Call a draw? (yes/no) : ") in ['yes','y','Yes']:
return True
def ischeckmate(self, board):
if not list(self.validmoves(board)) and self.isincheck(board):
return True
def turn(self, board):
turnstring = "\n%s's turn," % self.name
warning = " *** Your King is in check *** "
if self.isincheck(board):
turnstring = turnstring + warning
return turnstring
def getmove(self, board):
print "\n"
while True:
# If player is computer, get a move from computer
if self.nature is 'AI':
#return aiengine.getAImove(self, board)
return random.choice(list(self.validmoves(board)))
else:
# Player is human, get a move from input
move=raw_input("\nMake a move : ")
if move == 'exit':
break
else:
start, target = self.getposition(move)
if (start, target) in self.validmoves(board):
return start, target
else:
raise IndexError
def makesuscheck(self, start, target, board):
# Make temporary move to test for check
self.domove(board, start, target)
retval = self.isincheck(board)
# Undo temporary move
self.unmove(board, start, target)
return retval
def isincheck(self, board):
kingpos = self.kingpos(board)
for enemy in self.opponent.getpieces(board):
if self.opponent.canmoveto(board, enemy, kingpos):
return True
def domove(self, board, start, target):
self.savedtargetpiece = None
if target in board:
self.savedtargetpiece = board[target]
board[target] = board[start]
board[target].position = target
del board[start]
board[target].nrofmoves += 1
if board[target].piecename is 'p' and not self.savedtargetpiece:
if abs(target[0]-start[0]) == 2:
board[target].turn_moved_twosquares = self.playedturns
elif abs(target[1]-start[1]) == abs(target[0]-start[0]) == 1:
# Pawn has done en passant, remove the victim
if self.colour is 'white':
passant_victim = (target[0]-1, target[1])
else:
passant_victim = (target[0]+1, target[1])
self.savedpawn = board[passant_victim]
del board[passant_victim]
if board[target].piecename is 'k':
if target[1]-start[1] == -2:
# King is castling long, move longrook
self.domove(board, self.longrook, self.longrook_target)
elif target[1]-start[1] == 2:
# King is castling short, move shortrook
self.domove(board, self.shortrook, self.shortrook_target)
def unmove(self, board, start, target):
board[start] = board[target]
board[start].position = start
if self.savedtargetpiece:
board[target] = self.savedtargetpiece
else:
del board[target]
board[start].nrofmoves -= 1
if board[start].piecename is 'p' and not self.savedtargetpiece:
if abs(target[0]-start[0]) == 2:
del board[start].turn_moved_twosquares
elif abs(target[1]-start[1]) == abs(target[0]-start[0]) == 1:
# We have moved back en passant Pawn, restore captured Pawn
if self.colour is 'white':
formerpos_passant_victim = (target[0]-1, target[1])
else:
formerpos_passant_victim = (target[0]+1, target[1])
board[formerpos_passant_victim] = self.savedpawn
if board[start].piecename is 'k':
if target[1]-start[1] == -2:
# King's castling long has been unmoved, move back longrook
self.unmove(board, self.longrook, self.longrook_target)
elif target[1]-start[1] == 2:
# King's castling short has been unmoved, move back shortrook
self.unmove(board, self.shortrook, self.shortrook_target)
def pawnpromotion(self, board, target):
if self.nature is 'AI':
# See if Knight makes opponent checkmate
board[target].promote('kn')
if self.opponent.ischeckmate(board):
return
else:
promoteto = 'q'
else:
promoteto = 'empty'
while promoteto.lower() not in ['kn','q']:
promoteto = \
raw_input("You may promote your pawn:\n[Kn]ight [Q]ueen : ")
board[target].promote(promoteto)
def hasclearpath(self, start, target, board):
startcol, startrow = start[1], start[0]
targetcol, targetrow = target[1], target[0]
if abs(startrow - targetrow) <= 1 and abs(startcol - targetcol) <= 1:
# The base case
return True
else:
if targetrow > startrow and targetcol == startcol:
# Straight down
tmpstart = (startrow+1,startcol)
elif targetrow < startrow and targetcol == startcol:
# Straight up
tmpstart = (startrow-1,startcol)
elif targetrow == startrow and targetcol > startcol:
# Straight right
tmpstart = (startrow,startcol+1)
elif targetrow == startrow and targetcol < startcol:
# Straight left
tmpstart = (startrow,startcol-1)
elif targetrow > startrow and targetcol > startcol:
# Diagonal down right
tmpstart = (startrow+1,startcol+1)
elif targetrow > startrow and targetcol < startcol:
# Diagonal down left
tmpstart = (startrow+1,startcol-1)
elif targetrow < startrow and targetcol > startcol:
# Diagonal up right
tmpstart = (startrow-1,startcol+1)
elif targetrow < startrow and targetcol < startcol:
# Diagonal up left
tmpstart = (startrow-1,startcol-1)
# If no pieces in the way, test next square
if tmpstart in board:
return False
else:
return self.hasclearpath(tmpstart, target, board)
def canmoveto(self, board, start, target):
startpiece = board[start].piecename.upper()
if startpiece == 'R' and not self.check_rook(start, target):
return False
elif startpiece == 'KN' and not self.check_knight(start, target):
return False
elif startpiece == 'P' and not self.check_pawn(start, target, board):
return False
elif startpiece == 'B' and not self.check_bishop(start, target):
return False
elif startpiece == 'Q' and not self.check_queen(start, target):
return False
elif startpiece == 'K' and not self.check_king(start, target):
return False
# Only the 'Knight' may jump over pieces
if startpiece in 'RPBQK':
if not self.hasclearpath(start, target, board):
return False
return True
def check_rook(self, start, target):
# Check for straight lines of movement(start/target on same axis)
if start[0] == target[0] or start[1] == target[1]:
return True
def check_knight(self, start, target):
# 'Knight' may move 2+1 in any direction and jump over pieces
if abs(target[0]-start[0]) == 2 and abs(target[1]-start[1]) == 1:
return True
elif abs(target[0]-start[0]) == 1 and abs(target[1]-start[1]) == 2:
return True
def check_pawn(self, start, target, board):
# Disable backwards and sideways movement
if 'white' in self.colour and target[0] < start[0]:
return False
elif 'black' in self.colour and target[0] > start[0]:
return False
if start[0] == target[0]:
return False
if target in board:
# Only attack if one square diagonaly away
if abs(target[1]-start[1]) == abs(target[0]-start[0]) == 1:
return True
else:
# Make peasants move only one forward (except first move)
if start[1] == target[1]:
# Normal one square move
if abs(target[0]-start[0]) == 1:
return True
# 1st exception to the rule, 2 square move first time
if board[start].nrofmoves is 0:
if abs(target[0]-start[0]) == 2:
return True
# 2nd exception to the rule, en passant
if start[0] == self.enpassantrow:
if abs(target[0]-start[0]) == 1:
if abs(target[1]-start[1]) == 1:
if target[1]-start[1] == -1:
passant_victim = (start[0], start[1]-1)
elif target[1]-start[1] == 1:
passant_victim = (start[0], start[1]+1)
if passant_victim in board and \
board[passant_victim].colour is not self.colour and \
board[passant_victim].piecename is 'p'and \
board[passant_victim].nrofmoves == 1 and \
board[passant_victim].turn_moved_twosquares == \
self.playedturns-1:
return True
def check_bishop(self, start, target):
# Check for non-horizontal/vertical and linear movement
if abs(target[1]-start[1]) == abs(target[0]-start[0]):
return True
def check_queen(self, start, target):
# Will be true if move can be done as Rook or Bishop
if self.check_rook(start, target) or self.check_bishop(start, target):
return True
def check_king(self, start, target):
# King can move one square in any direction
if abs(target[0]-start[0]) <= 1 and abs(target[1]-start[1]) <= 1:
return True
# ..except when castling
if self.can_castle_short_this_turn:
if target[1]-start[1] == 2 and start[0] == target[0]:
return True
if self.can_castle_long_this_turn:
if target[1]-start[1] == -2 and start[0] == target[0]:
return True
class Piece(object):
def __init__(self, piecename, position, player):
self.colour = player.colour
self.nature = player.nature
self.piecename = piecename
self.position = position
self.nrofmoves = 0
def __str__(self):
if self.colour is 'white':
if self.piecename is 'p':
return 'WP'
else:
return self.piecename.upper()
else:
return self.piecename
def canbepromoted(self):
if str(self.position[0]) in '07':
return True
def promote(self, to):
self.piecename = to.lower()
class Game(object):
def __init__(self, playera, playerb):
self.board = dict()
for player in [playera, playerb]:
if player.colour is 'white':
brow, frow = 0, 1
player.enpassantrow = 4
else:
brow, frow = 7, 6
player.enpassantrow = 3
player.longrook = (brow, 0)
player.longrook_target = \
(player.longrook[0], player.longrook[1]+3)
player.shortrook = (brow, 7)
player.shortrook_target = \
(player.shortrook[0], player.shortrook[1]-2)
[self.board.setdefault((frow,x), Piece('p', (frow,x), player)) \
for x in range(8)]
[self.board.setdefault((brow,x), Piece('r', (brow,x), player)) \
for x in [0,7]]
[self.board.setdefault((brow,x), Piece('kn',(brow,x), player)) \
for x in [1,6]]
[self.board.setdefault((brow,x), Piece('b', (brow,x), player)) \
for x in [2,5]]
self.board.setdefault((brow,3), Piece('q', (brow,3), player))
self.board.setdefault((brow,4), Piece('k', (brow,4), player))
def printboard(self):
topbottom=['*','a','b','c','d','e','f','g','h','*']
sides=['1','2','3','4','5','6','7','8']
tbspacer=' '*6
rowspacer=' '*5
cellspacer=' '*4
empty=' '*3
print
for field in topbottom:
print "%4s" % field,
print
print tbspacer+("_"*4+' ')*8
for row in range(8):
print(rowspacer+(('|'+cellspacer)*9))
print "%4s" % sides[row],('|'),
for col in range(8):
if (row, col) not in self.board:
print empty+'|',
else:
print "%2s" % self.board[(row, col)],('|'),
print "%2s" % sides[row],
print
print rowspacer+'|'+(("_"*4+'|')*8)
print
for field in topbottom:
print "%4s" % field,
print "\n"
def refreshscreen(self, player):
if player.colour is 'white':
playera, playerb = player, player.opponent
else:
playera, playerb = player.opponent, player
os.system('clear')
print " Now playing: %s vs %s" % (playera, playerb)
self.printboard()
def run(self, player):
self.refreshscreen(player)
while True:
print player.turn(self.board)
try:
start, target = player.getmove(self.board)
except (IndexError, ValueError):
self.refreshscreen(player)
print "\n\nPlease enter a valid move."
except TypeError:
# No start, target if user exit
break
else:
if target in self.board or self.board[start].piecename is 'p':
Player.dullmoves = 0
else:
Player.dullmoves += 1
player.domove(self.board, start, target)
player.playedturns += 1
# Check if there is a Pawn up for promotion
if self.board[target].piecename is 'p':
if self.board[target].canbepromoted():
player.pawnpromotion(self.board, target)
player = player.opponent
if player.reacheddraw(self.board):
return 1, player
elif player.ischeckmate(self.board):
return 2, player
else:
self.refreshscreen(player)
def end(self, player, result):
looser = player.name
winner = player.opponent.name
if result == 1:
endstring = "\n%s and %s reached a draw." % (winner, looser)
elif result == 2:
endstring = "\n%s put %s in checkmate." % (winner, looser)
os.system('clear')
self.printboard()
return endstring
def newgame():
os.system('clear')
print """
Welcome to Chessmastah, the fantastic console chess environment.
Please type in the name of the contestants.
If you want to play against the computer, leave one name blank
and press [Enter].
Or if you fancy, leave both names blank and watch the computer
duke it out with itself.
"""
playera, playerb = getplayers()
playera.set_opponent(playerb)
playerb.set_opponent(playera)
game = Game(playera, playerb)
infostring = \
"""
Very well, %s and %s, let's play.
Player A: %s (uppercase)
Player B: %s (lowercase)
(Use moves on form 'a2b3' or type 'exit' at any time.) """
print infostring % (playera.name, playerb.name, playera, playerb)
raw_input("\n\nPress [Enter] when ready")
# WHITE starts
player = playera
try:
result, player = game.run(player)
except TypeError:
# No result if user exit
pass
else:
print game.end(player, result)
raw_input("\n\nPress any key to continue")
def getplayers():
ainames = ['chesschick','foxysquare']
name1 = raw_input("\nPlayer A (white): ")
if not name1:
playera = Player('white', 'AI', ainames[0])
else:
playera = Player('white', 'human', name1)
name2 = raw_input("\nPlayer B (black): ")
if not name2:
playerb = Player('black', 'AI', ainames[1])
else:
playerb = Player('black', 'human', name2)
return playera, playerb
def main():
""" Kickstart everything. Display menu after game has ended. """
menu="""
Thanks for playing the Chessmastah, would you like to go again?
Press [Enter] to play again or type 'exit'. >> """
try:
while True:
newgame()
choice=raw_input(menu)
if choice == 'exit':
print "\nAs you wish. Welcome back!"
break
except KeyboardInterrupt:
sys.exit("\n\nOkok. Aborting.")
if __name__ == '__main__':
#cProfile.run('main()')
main()