@@ -57,7 +57,7 @@ def isCheck(end, piece, board, opposite, canPassant, text, computer):
5757 moveList = computeAll (- piece , board , 0 , opposite , canPassant , computer )
5858 if kingCoord (- piece , board ) in moveList :
5959 inCheck = " White king in check" if - piece < 0 else " Black king in check"
60- addText (text , inCheck , 0 , 0 )
60+ addText (text , inCheck , 0 , 0 , 0 )
6161
6262
6363# computes the board position of the piece
@@ -80,14 +80,16 @@ def kingCoord(piece, board):
8080
8181# determine if move is legal during check or if it prevents check
8282def checkMove (piece , newY , newX , oldY , oldX , board , canPassant , computer ):
83- tempBoard = [ row [:] for row in board ]
84- tempBoard [oldY ][oldX ] = 0
85- tempBoard [newY ][newX ] = piece
86- moveList = computeAll (piece , tempBoard , 0 , 0 , canPassant , computer )
83+ newPiece = board [ newY ][ newX ]
84+ board [oldY ][oldX ] = 0
85+ board [newY ][newX ] = piece
86+ moveList = computeAll (piece , board , 0 , 0 , canPassant , computer )
8787
88- if (kingCoord (piece , tempBoard ) in moveList ):
88+ if (kingCoord (piece , board ) in moveList ):
89+ undo (board , piece , newPiece , newY , newX , oldY , oldX )
8990 return False
9091 else :
92+ undo (board , piece , newPiece , newY , newX , oldY , oldX )
9193 return True
9294
9395
@@ -231,7 +233,7 @@ def enPassantCapture(piece, board, newY, newX, oldY, oldX, isPawn, canPassant, t
231233 if isPawn and coord in canPassant and piece in {- 1 , 11 , - 11 , 1 } and spaceCheck (piece , board , newY , newX ) and newCoord == canPassant [0 ]:
232234 board [newY + add ][newX ] = 0
233235 if not noPrint :
234- addText (text , PIECE [piece ] + " en passant capture" , 0 , 0 )
236+ addText (text , PIECE [piece ] + " en passant capture" , 0 , 0 , 0 )
235237
236238
237239# determines if en passant is a possible move
@@ -387,14 +389,14 @@ def castle(piece, board, oldY, oldX, pSize, size, moveList, text, count):
387389 firstMove (piece , board , oldY , kingX )
388390 firstMove (rook , board , oldY , rookX )
389391 board [mY ][tempX ] = 0
390- count = addText (text , str (PIECE [piece ]) + str (side ), count , 0 )
392+ count = addText (text , str (PIECE [piece ]) + str (side ), count , 0 , 0 , 0 )
391393 return True , count
392394 return False , count
393395
394396
395397# evaluates if a button is pressed
396398# also dictates pawn promotion behaviour
397- def button (selection , info , promotedPiece , board , text , count , length ):
399+ def button (selection , info , promotedPiece , board , text , count , length , font ):
398400 pressed = pygame .mouse .get_pos ()[0 ] in range (info [0 ], info [2 ] + info [0 ]) and pygame .mouse .get_pos ()[1 ] in range (info [1 ], info [3 ] + info [1 ])
399401 pawn = "Black"
400402
@@ -408,8 +410,8 @@ def button(selection, info, promotedPiece, board, text, count, length):
408410 newPiece = - newPiece
409411 pawn = "White"
410412 board [y ][x ] = newPiece
411- addText (text , pawn + " pawn promoted to " , 0 , 0 )
412- addText (text , " " + str (PIECE [newPiece ]), 0 , length )
413+ addText (text , pawn + " pawn promoted to" , 0 , 0 , font )
414+ addText (text , " " + str (PIECE [newPiece ]), 0 , length , font )
413415
414416 if count == 0 :
415417 return pressed
@@ -421,29 +423,35 @@ def button(selection, info, promotedPiece, board, text, count, length):
421423def checkmate (king , board , canPassant , opposite , computer ):
422424 canMove = False
423425 opposite = 1 if (opposite == 0 and computer == None ) else 0
424-
425426 moveList = specificCompute (- king , board , canPassant , computer , opposite )[0 ]
426- tempBoard = [row [:] for row in board ]
427427
428428 # goes through each list in moveList
429429 for i , _ in enumerate (moveList ):
430430 n = moveList [i ][0 ]
431431 oldY , oldX = moveList [i ][1 ]
432- tempBoard = [row [:] for row in board ]
433- tempBoard [oldY ][oldX ] = 0
432+ board [oldY ][oldX ] = 0
434433
435434 # goes through each sublist
436435 for (newY , newX ) in moveList [i ][2 ]:
437- movedBoard = [ row [:] for row in tempBoard ]
438- if spaceCheck ( n , movedBoard , newY , newX ):
439- movedBoard [newY ][newX ] = n
440- tempMoveList = computeAll (king , movedBoard , 0 , opposite , canPassant , computer )
441- if kingCoord (king , movedBoard ) not in tempMoveList : # king not in check after move
436+ if spaceCheck ( n , board , newY , newX ):
437+ newPiece = board [ newY ][ newX ]
438+ board [newY ][newX ] = n
439+ tempMoveList = computeAll (king , board , 0 , opposite , canPassant , computer )
440+ if kingCoord (king , board ) not in tempMoveList : # king not in check after move
442441 canMove = True
442+ undo (board , 0 , newPiece , newY , newX , oldY , oldX )
443443 break
444+ undo (board , 0 , newPiece , newY , newX , oldY , oldX )
445+ board [oldY ][oldX ] = n
444446 return not canMove
445447
446448
449+ # undos the move to prevent deep copying the list
450+ def undo (board , oldPiece , newPiece , newY , newX , oldY , oldX ):
451+ board [newY ][newX ] = newPiece
452+ board [oldY ][oldX ] = oldPiece
453+
454+
447455# determines if the game has ended through checkmate or stalemate
448456def gameEnd (board , turn , pieceMoving , start , outline , canPassant , opposite , text , computer ):
449457 if outline or not start or pieceMoving :
@@ -471,29 +479,29 @@ def gameEnd(board, turn, pieceMoving, start, outline, canPassant, opposite, text
471479
472480 # stalemate cases
473481 if emptySpace == 62 : # king vs king
474- addText (text , "Stalemate: Insufficient material." , 0 , 0 )
475- addText (text , "Press restart or exit the game." , 0 , 0 )
482+ addText (text , "Stalemate: Insufficient material." , 0 , 0 , 0 )
483+ addText (text , "Press restart or exit the game." , 0 , 0 , 0 )
476484 return True
477485 elif emptySpace == 61 and ((len (bishop ) == 1 and knight == 0 ) or (knight == 1 and len (bishop ) == 0 )): # 1 bishop/knight vs king
478- addText (text , "Stalemate: Insufficient material." , 0 , 0 )
479- addText (text , "Press restart or exit the game." , 0 , 0 )
486+ addText (text , "Stalemate: Insufficient material." , 0 , 0 , 0 )
487+ addText (text , "Press restart or exit the game." , 0 , 0 , 0 )
480488 return True
481489 elif emptySpace == 60 and len (bishop ) == 2 : # 1 bishop vs 1 bishop, same colour
482490 b1 , b2 = bishop [0 ][0 ], bishop [1 ][0 ]
483491 i1 , i2 = (bishop [0 ][1 ] * 8 ) + bishop [0 ][2 ], (bishop [1 ][1 ] * 8 ) + bishop [1 ][2 ]
484492 y1 , y2 , = bishop [0 ][1 ], bishop [1 ][1 ]
485493 if b1 != b2 and y1 % 2 == i1 % 2 and y2 % 2 == i2 % 2 :
486- addText (text , "Stalemate: Insufficient material." , 0 , 0 )
487- addText (text , "Press restart or exit the game." , 0 , 0 )
494+ addText (text , "Stalemate: Insufficient material." , 0 , 0 , 0 )
495+ addText (text , "Press restart or exit the game." , 0 , 0 , 0 )
488496 return True
489497
490498 if checkmate (king , board , canPassant , opposite , computer ): # cannot move
491499 winner = {- 9 : "Black" , - 99 : "Black" , 99 : "White" , 9 : "White" }
492500 if kingCoord (king , board ) in moveList : # king in check
493- addText (text , "Checkmate: " + str (winner [king ]) + " won!" , 0 , 0 )
501+ addText (text , "Checkmate: " + str (winner [king ]) + " won!" , 0 , 0 , 0 )
494502 else : # no possible moves, stalemate
495- addText (text , "Stalemate: " + str (winner [- king ]) + " cannot move." , 0 , 0 )
496- addText (text , "Press restart or exit the game." , 0 , 0 )
503+ addText (text , "Stalemate: " + str (winner [- king ]) + " cannot move." , 0 , 0 , 0 )
504+ addText (text , "Press restart or exit the game." , 0 , 0 , 0 )
497505 return True
498506 else :
499507 return False
0 commit comments