def createBoard(self): for x in range(64): self.gameTiles[x] = Tile(x, nullPiece.NullPiece()) self.gameTiles[0] = Tile(0, rook.Rook("Black", 0)) self.gameTiles[1] = Tile(1, knight.Knight("Black", 1)) self.gameTiles[2] = Tile(2, bishop.Bishop("Black", 2)) self.gameTiles[3] = Tile(3, queen.Queen("Black", 3)) self.gameTiles[4] = Tile(4, king.King("Black", 4)) self.gameTiles[5] = Tile(5, bishop.Bishop("Black", 5)) self.gameTiles[6] = Tile(6, knight.Knight("Black", 6)) self.gameTiles[7] = Tile(7, rook.Rook("Black", 7)) self.gameTiles[8] = Tile(8, pawn.Pawn("Black", 8)) self.gameTiles[9] = Tile(9, pawn.Pawn("Black", 9)) self.gameTiles[10] = Tile(10, pawn.Pawn("Black", 10)) self.gameTiles[11] = Tile(11, pawn.Pawn("Black", 11)) self.gameTiles[12] = Tile(12, pawn.Pawn("Black", 12)) self.gameTiles[13] = Tile(13, pawn.Pawn("Black", 13)) self.gameTiles[14] = Tile(14, pawn.Pawn("Black", 14)) self.gameTiles[15] = Tile(15, pawn.Pawn("Black", 15)) # self.gameTiles[25] = Tile(25, pawn.Pawn("White", 25)) # #self.gameTiles[18] = Tile(18, pawn.Pawn("Black", 18)) # self.gameTiles[26] = Tile(26, pawn.Pawn("Black", 26)) # self.gameTiles[24] = Tile(24, pawn.Pawn("Black", 24)) self.gameTiles[48] = Tile(48, pawn.Pawn("White", 48)) self.gameTiles[49] = Tile(49, pawn.Pawn("White", 49)) self.gameTiles[50] = Tile(50, pawn.Pawn("White", 50)) self.gameTiles[51] = Tile(51, pawn.Pawn("White", 51)) self.gameTiles[52] = Tile(52, pawn.Pawn("White", 52)) self.gameTiles[53] = Tile(53, pawn.Pawn("White", 53)) self.gameTiles[54] = Tile(54, pawn.Pawn("White", 54)) self.gameTiles[55] = Tile(55, pawn.Pawn("White", 55)) self.gameTiles[56] = Tile(56, rook.Rook("White", 56)) self.gameTiles[57] = Tile(57, knight.Knight("White", 57)) self.gameTiles[58] = Tile(58, bishop.Bishop("White", 58)) self.gameTiles[59] = Tile(59, queen.Queen("White", 59)) self.gameTiles[60] = Tile(60, king.King("White", 60)) self.gameTiles[61] = Tile(61, bishop.Bishop("White", 61)) self.gameTiles[62] = Tile(62, knight.Knight("White", 62)) self.gameTiles[63] = Tile(63, rook.Rook("White", 63))
def createNewBoard(self): newBoard = Board() gameTiles = {} # IF THERE WAS AN ENPASSANT PAWN AND WE USED A PAWN TO ATTACK IT'S SQUARE # SET A MARKED FOR IT FOR IT TO BE SKIPPED enpassLocation = None if self.movedPiece.toString() == 'P': if not self.board.enPassPawn == None: if self.destination == self.board.enPassPawnBehind: enpassLocation = self.board.enPassPawn.position elif self.movedPiece.toString() == 'p': if not self.board.enPassPawn == None: if self.destination == self.board.enPassPawnBehind: enpassLocation = self.board.enPassPawn.position # FILL NEW BOARD WITH EVERYTHING EXCEPT DESTINATION, CURRENT LOCATION AND ENPASS LOCATION # ELSE FILL IT WITH A NULL PIECE FOR NOW for tile in range(64): if not tile == self.movedPiece.position and not tile == self.destination and not tile == enpassLocation: gameTiles[tile] = self.board.gameTiles[tile] else: gameTiles[tile] = Tile(tile, nullPiece.NullPiece()) # IF WE CASTLED, REPLACE THE ROOK WITH A NULL AND MOVE IT TO CORRECT POSITION if self.movedPiece.toString() == 'K' and self.movedPiece.firstMove: if self.destination == 2: if self.board.gameTiles[0].pieceOnTile.toString() == "R" \ and self.board.gameTiles[0].pieceOnTile.firstMove: gameTiles[0] = Tile(0, nullPiece.NullPiece()) gameTiles[3] = Tile(3, rook.Rook("Black", 3)) elif self.destination == 6: if self.board.gameTiles[7].pieceOnTile.toString() == "R" \ and self.board.gameTiles[7].pieceOnTile.firstMove: gameTiles[7] = Tile(7, nullPiece.NullPiece()) gameTiles[5] = Tile(5, rook.Rook("Black", 5)) # CASTLE FOR WHITE elif self.movedPiece.toString() == 'k': if self.destination == 58: if self.board.gameTiles[56].pieceOnTile.toString() == "r" \ and self.board.gameTiles[56].pieceOnTile.firstMove: gameTiles[56] = Tile(56, nullPiece.NullPiece()) gameTiles[59] = Tile(59, rook.Rook("White", 59)) elif self.destination == 62: if self.board.gameTiles[63].pieceOnTile.toString() == "r" \ and self.board.gameTiles[56].pieceOnTile.firstMove: gameTiles[63] = Tile(63, nullPiece.NullPiece()) gameTiles[61] = Tile(61, rook.Rook("White", 61)) # FINALLY CREATE A COPY OF MOVED PIECE AND ASSIGN IT TO ITS LOCATION updatePiece = copy.copy(self.movedPiece) updatePiece.firstMove = False updatePiece.position = self.destination gameTiles[self.destination] = Tile(self.destination, updatePiece) # THEN ASSIGN NEW BOARD'S TILES AS NEW TILES newBoard.gameTiles = gameTiles # IT WAS A PAWN JUMP MOVE, ASSIGN IT TO BE THE ENPASS PAWN AND SET IT'S LOCATION BEHIND IT if self.movedPiece.toString() == 'P': if self.movedPiece.position + 16 == self.destination: newBoard.enPassPawn = updatePiece newBoard.enPassPawnBehind = self.movedPiece.position + 8 elif self.movedPiece.toString() == 'p': if self.movedPiece.position - 16 == self.destination: newBoard.enPassPawn = updatePiece newBoard.enPassPawnBehind = self.movedPiece.position - 8 # IF IT WAS A PAWN MOVE AND IT REACHED THE LAST RANK, REPLACE IT WAS A QUEEN if self.movedPiece.toString() == 'P': if self.destination in self.movedPiece.eighthRow: newBoard.gameTiles[self.destination] = Tile( self.destination, queen.Queen("Black", self.destination)) elif self.movedPiece.toString() == 'p': if self.destination in self.movedPiece.firstRow: newBoard.gameTiles[self.destination] = Tile( self.destination, queen.Queen("White", self.destination)) # SWITCH CURRENT PLAYER OF THE BOARD newBoard.currentPlayer = self.board.currentPlayer if newBoard.currentPlayer == "White": newBoard.currentPlayer = "Black" elif newBoard.currentPlayer == "Black": newBoard.currentPlayer = "White" # WITH THE NEW BOARD CREATED, CHECK IF THE OPPONENT DID A VALID MOVE good = self.checkChecks(newBoard) if not good: return False mate = self.checkCheckmateOrStalemate(newBoard, newBoard.currentPlayer) if mate: return False return newBoard
def createNewBoard(self): newBoard = Board() gameTiles = {} # TODO CHECK if enpassant attack enpassLocation = None if self.movedPiece.toString() == 'P': if not self.board.enPassPawn == None: if self.destination == self.board.enPassPawnBehind: #print("b enpassatk") enpassLocation = self.board.enPassPawn.position elif self.movedPiece.toString() == 'p': if not self.board.enPassPawn == None: if self.destination == self.board.enPassPawnBehind: #print("w enpassatk") enpassLocation = self.board.enPassPawn.position for tile in range(64): #print(tile) if not tile == self.movedPiece.position and not tile == self.destination and not tile == enpassLocation: gameTiles[tile] = self.board.gameTiles[tile] else: gameTiles[tile] = Tile(tile, nullPiece.NullPiece()) # TODO Check if it is castle move if self.movedPiece.toString() == 'K' and self.movedPiece.firstMove: if self.destination == 2: if self.board.gameTiles[0].pieceOnTile.toString( ) == "R" and self.board.gameTiles[0].pieceOnTile.firstMove: #print('b qc') gameTiles[0] = Tile(0, nullPiece.NullPiece()) gameTiles[3] = Tile(3, rook.Rook("Black", 3)) elif self.destination == 6: if self.board.gameTiles[7].pieceOnTile.toString( ) == "R" and self.board.gameTiles[7].pieceOnTile.firstMove: #print('b kc') gameTiles[7] = Tile(7, nullPiece.NullPiece()) gameTiles[5] = Tile(5, rook.Rook("Black", 5)) elif self.movedPiece.toString() == 'k': if self.destination == 58: if self.board.gameTiles[56].pieceOnTile.toString( ) == "r" and self.board.gameTiles[56].pieceOnTile.firstMove: #print('w qc') gameTiles[56] = Tile(56, nullPiece.NullPiece()) gameTiles[59] = Tile(59, rook.Rook("White", 59)) elif self.destination == 62: if self.board.gameTiles[63].pieceOnTile.toString( ) == "r" and self.board.gameTiles[56].pieceOnTile.firstMove: #print('w kc') gameTiles[63] = Tile(63, nullPiece.NullPiece()) gameTiles[61] = Tile(61, rook.Rook("White", 61)) updatePiece = copy.copy(self.movedPiece) updatePiece.firstMove = False updatePiece.position = self.destination gameTiles[self.destination] = Tile(self.destination, updatePiece) newBoard.gameTiles = gameTiles # TODO if pawn was jump, set it to enpassant pawn if self.movedPiece.toString() == 'P': if self.movedPiece.position + 16 == self.destination: #print("Black j") newBoard.enPassPawn = updatePiece newBoard.enPassPawnBehind = self.movedPiece.position + 8 elif self.movedPiece.toString() == 'p': if self.movedPiece.position - 16 == self.destination: #print("White j") newBoard.enPassPawn = updatePiece newBoard.enPassPawnBehind = self.movedPiece.position - 8 #TODO add promotion if self.movedPiece.toString() == 'P': if self.destination in self.movedPiece.eighthRow: print("Promote") newBoard.gameTiles[self.destination] = Tile( self.destination, queen.Queen("Black", self.destination)) elif self.movedPiece.toString() == 'p': if self.destination in self.movedPiece.firstRow: print("Promote") newBoard.gameTiles[self.destination] = Tile( self.destination, queen.Queen("White", self.destination)) # Switch players newBoard.currentPlayer = self.board.currentPlayer if newBoard.currentPlayer == "White": newBoard.currentPlayer = "Black" elif newBoard.currentPlayer == "Black": newBoard.currentPlayer = "White" # TODO Check checks good = self.checkChecks(newBoard) if not good: # print("InvalidMove") # return self.board return False return newBoard