def initialize_standard_board(self): self.draw_board() # create and place 32 pieces for i in range(0, 8): self.place_piece([i, 1], Pawn(1)) self.place_piece([i, 6], Pawn(0)) self.place_piece([0, 0], Rook(1)) self.place_piece([7, 0], Rook(1)) self.place_piece([0, 7], Rook(0)) self.place_piece([7, 7], Rook(0)) self.place_piece([4, 0], King(1)) self.place_piece([4, 7], King(0)) self.place_piece([3, 0], Queen(1)) self.place_piece([3, 7], Queen(0)) self.place_piece([2, 0], Bishop(1)) self.place_piece([5, 0], Bishop(1)) self.place_piece([2, 7], Bishop(0)) self.place_piece([5, 7], Bishop(0)) self.place_piece([1, 0], Knight(1)) self.place_piece([6, 0], Knight(1)) self.place_piece([1, 7], Knight(0)) self.place_piece([6, 7], Knight(0))
def __init__(self): self.currentPlayer = "White" self.tiles = dict() self.moveCounter = 1 self.prevBoard = None for x in range(64): self.tiles[x] = (Tile(x, NullPiece())) for x in range(8, 16): pass self.tiles[x] = (Tile(x, Pawn("White", x))) for x in range(48, 56): self.tiles[x] = (Tile(x, Pawn("Black", x))) self.tiles[0] = (Tile(0, Rook("White", 0))) self.tiles[1] = (Tile(1, Knight("White", 1))) self.tiles[2] = (Tile(2, Bishop("White", 2))) self.tiles[3] = (Tile(3, Queen("White", 3))) self.tiles[4] = (Tile(4, King("White", 4))) self.tiles[5] = (Tile(5, Bishop("White", 5))) self.tiles[6] = (Tile(6, Knight("White", 6))) self.tiles[7] = (Tile(7, Rook("White", 7))) self.tiles[56] = (Tile(56, Rook("Black", 56))) self.tiles[57] = (Tile(57, Knight("Black", 57))) self.tiles[58] = (Tile(58, Bishop("Black", 58))) self.tiles[59] = (Tile(59, Queen("Black", 59))) self.tiles[60] = (Tile(60, King("Black", 60))) self.tiles[61] = (Tile(61, Bishop("Black", 61))) self.tiles[62] = (Tile(62, Knight("Black", 62))) self.tiles[63] = (Tile(63, Rook("Black", 63)))
def promotion_message(self, color): if color == self.BLACK: color = 0 else: color = 1 knight = Knight(color) bishop = Bishop(color) rook = Rook(color) queen = Queen(color) tenth = self.size / 10 pygame.draw.rect( self.screen, self.WHITE, [tenth, tenth, 8 * tenth, 2 * tenth]) # draw outer background pygame.draw.rect(self.screen, self.BLACK, [tenth, tenth, 8 * tenth, 2 * tenth], 5) # draw outer background knight.draw(self.screen, 1.5 * tenth, 1.5 * tenth, tenth) pygame.draw.rect(self.screen, self.BLACK, [1.5 * tenth, 1.5 * tenth, tenth, tenth], 3) bishop.draw(self.screen, 3.5 * tenth, 1.5 * tenth, tenth) pygame.draw.rect(self.screen, self.BLACK, [3.5 * tenth, 1.5 * tenth, tenth, tenth], 3) rook.draw(self.screen, 5.5 * tenth, 1.5 * tenth, tenth) pygame.draw.rect(self.screen, self.BLACK, [5.5 * tenth, 1.5 * tenth, tenth, tenth], 3) queen.draw(self.screen, 7.5 * tenth, 1.5 * tenth, tenth) pygame.draw.rect(self.screen, self.BLACK, [7.5 * tenth, 1.5 * tenth, tenth, tenth], 3)
def load_board_from_file_path(self, path): """ Load a chess board from .txt file on the local computer. :param path: The path to the .txt file. :return: None """ count = 0 with open(path) as board_loaded: for line in board_loaded: for char in line: if char != '|' and char != '\n': if char == '-': self.game_tiles[count] = NoPiece(count) count += 1 if char == 'R': self.game_tiles[count] = Rook("Black", count) count += 1 if char == 'N': self.game_tiles[count] = Knight("Black", count) count += 1 if char == 'B': self.game_tiles[count] = Bishop("Black", count) count += 1 if char == 'Q': self.game_tiles[count] = Queen("Black", count) count += 1 if char == 'K': self.game_tiles[count] = King("Black", count) count += 1 if char == 'P': self.game_tiles[count] = Pawn("Black", count) count += 1 if char == 'r': self.game_tiles[count] = Rook("White", count) count += 1 if char == 'n': self.game_tiles[count] = Knight("White", count) count += 1 if char == 'b': self.game_tiles[count] = Bishop("White", count) count += 1 if char == 'q': self.game_tiles[count] = Queen("White", count) count += 1 if char == 'k': self.game_tiles[count] = King("White", count) count += 1 if char == 'p': self.game_tiles[count] = Pawn("White", count) count += 1 self.original_game_tiles = self.game_tiles.copy() print("Done copying from file")
def create_pieces_for_new_game(self): """ instantiates class object for each piece type sets piece in corresponding start position white occupies rows 1 and 2 black occupies rows 7 and 8 """ # maybe we don't even want to do this - what we really want to do is assign pieces as occupants of corresponding squares # Haven't decided if a piece needs to know about position - for now we include but I may remove in the future white_pieces = dict( white_rook_1=Rook('White'), white_knight_1=Knight('White'), white_bishop_1=Bishop('White'), white_queen=Queen('White'), white_king=King('White'), white_bishop_2=Bishop('White'), white_knight_2=Knight('White'), white_rook_2=Rook('White'), white_pawn_1=Pawn('White'), white_pawn_2=Pawn('White'), white_pawn_3=Pawn('White'), white_pawn_4=Pawn('White'), white_pawn_5=Pawn('White'), white_pawn_6=Pawn('White'), white_pawn_7=Pawn('White'), white_pawn_8=Pawn('White'), ) black_pieces = dict( black_rook_1=Rook('Black'), black_knight_1=Knight('Black'), black_bishop_1=Bishop('Black'), black_queen=Queen('Black'), black_king=King('Black'), black_bishop_2=Bishop('Black'), black_knight_2=Knight('Black'), black_rook_2=Rook('Black'), black_pawn_1=Pawn('Black'), black_pawn_2=Pawn('Black'), black_pawn_3=Pawn('Black'), black_pawn_4=Pawn('Black'), black_pawn_5=Pawn('Black'), black_pawn_6=Pawn('Black'), black_pawn_7=Pawn('Black'), black_pawn_8=Pawn('Black'), ) self.white_pieces = white_pieces self.black_pieces = black_pieces
def test_queen_capture(): """Tests if the queen can capture """ playerOnePieces = set() playerTwoPieces = set() board = [[None] * 8 for i in range(8)] color = "BLACK" myPiece = Queen(board, color, playerOnePieces, 2, 'A') playerOnePieces.update({myPiece}) enemyPiece = Pawn(board, "WHITE", playerTwoPieces, 4, 'D') playerTwoPieces.update({enemyPiece}) possibleRow = {i: i - 1 for i in range(1, 9)} possibleCol = {chr(i): i - ord('A') for i in range(ord('A'), ord('A') + 8)} moveOne = "2A" moveTwo = "4A" board[possibleRow[int(moveOne[0])]][possibleCol[moveOne[1]]].move( int(moveTwo[0]), moveTwo[1]) moveOne = "4A" moveTwo = "4D" # Now I make a move board[possibleRow[int(moveOne[0])]][possibleCol[moveOne[1]]].move( int(moveTwo[0]), moveTwo[1]) assert board[possibleRow[int(moveTwo[0])] ][possibleCol[moveTwo[1]]] == myPiece assert len(playerTwoPieces) == 0
def initPlayerTwo(self): """This populates the playerTwoPieces (white) empty set with the player two's pieces. It also adds those pieces to the board """ color = "WHITE" group = self.playerTwoPieces self.playerTwoPieces.update({ Pawn(self.board, color, group, 7, 'A', self), Pawn(self.board, color, group, 7, 'B', self), Pawn(self.board, color, group, 7, 'C', self), Pawn(self.board, color, group, 7, 'D', self), Pawn(self.board, color, group, 7, 'E', self), Pawn(self.board, color, group, 7, 'F', self), Pawn(self.board, color, group, 7, 'G', self), Pawn(self.board, color, group, 7, 'H', self) }) #Add rank 1 pieces self.playerTwoPieces.update({ Rook(self.board, color, group, 8, 'A', self), Knight(self.board, color, group, 8, 'B', self), Bishop(self.board, color, group, 8, 'C', self), Queen(self.board, color, group, 8, 'D', self), King(self.board, color, group, 8, 'E', self), Bishop(self.board, color, group, 8, 'F', self), Knight(self.board, color, group, 8, 'G', self), Rook(self.board, color, group, 8, 'H', self) })
def reset(self): row = lambda colour, row: [ Rook(self.screen, colour, (0, row), self._board), Horse(self.screen, colour, (1, row), self._board), Bishop(self.screen, colour, (2, row), self._board), King(self.screen, colour, (3, row), self._board), Queen(self.screen, colour, (4, row), self._board), Bishop(self.screen, colour, (5, row), self._board), Horse(self.screen, colour, (6, row), self._board), Rook(self.screen, colour, (7, row), self._board) ] self._board[0] = row(Colour.white, 0) self._board[1] = [ Pawn(self.screen, Colour.white, (i, 1), self._board) for i in range(self.size) ] self._board[len(self._board) - 2] = [ Pawn(self.screen, Colour.black, (i, len(self._board) - 2), self._board) for i in range(self.size) ] self._board[len(self._board) - 1] = row(Colour.black, len(self._board) - 1)
def initPlayerOne(self, ): """This populates the playerOnePieces (black) empty set with the player one's pieces. It also adds those pieces to the board """ #add pawns #(self,self.board,color,group,row,col) color = "BLACK" group = self.playerOnePieces self.playerOnePieces.update({ Pawn(self.board, color, group, 2, 'A', self), Pawn(self.board, color, group, 2, 'B', self), Pawn(self.board, color, group, 2, 'C', self), Pawn(self.board, color, group, 2, 'D', self), Pawn(self.board, color, group, 2, 'E', self), Pawn(self.board, color, group, 2, 'F', self), Pawn(self.board, color, group, 2, 'G', self), Pawn(self.board, color, group, 2, 'H', self) }) #Add rank 1 pieces self.playerOnePieces.update({ Rook(self.board, color, group, 1, 'A', self), Knight(self.board, color, group, 1, 'B', self), Bishop(self.board, color, group, 1, 'C', self), Queen(self.board, color, group, 1, 'D', self), King(self.board, color, group, 1, 'E', self), Bishop(self.board, color, group, 1, 'F', self), Knight(self.board, color, group, 1, 'G', self), Rook(self.board, color, group, 1, 'H', self) })
def test7(): #--Starting position for a white queen at d3 #Enemy pieces at d6, g6 #Blocked by own piece at h3, d2, b1, e2 Q = Queen(Color.White) P = Pawn(Color.White) p = Pawn(Color.Black) gameBoard = Board() place_piece(gameBoard, "d3", Q) place_piece(gameBoard, "h3", P) place_piece(gameBoard, "d2", P) place_piece(gameBoard, "b1", P) place_piece(gameBoard, "e2", P) place_piece(gameBoard, "d6", p) place_piece(gameBoard, "g6", p) translate(Q.legalMoves(gameBoard, False)) display.print_board(gameBoard.board)
def initialize(self): team = Team(self.team) team.canMove = False self.Side.append(Rook(-1, -1, team)) self.Side.append(Knight(-1, -1, team)) self.Side.append(Bishop(-1, -1, team)) self.Side.append(King(-1, -1, team)) self.Side.append(Queen(-1, -1, team)) self.Side.append(Bishop(-1, -1, team)) self.Side.append(Knight(-1, -1, team)) self.Side.append(Rook(-1, -1, team)) for x in range(0, 8): self.Side.append(Pawn(-1, -1, team))
def __init__(self): self.game_tiles.insert(0, Rook("Black", 0)) self.game_tiles.insert(1, Knight("Black", 1)) self.game_tiles.insert(2, Bishop("Black", 2)) self.game_tiles.insert(3, Queen("Black", 3)) self.game_tiles.insert(4, King("Black", 4)) self.game_tiles.insert(5, Bishop("Black", 5)) self.game_tiles.insert(6, Knight("Black", 6)) self.game_tiles.insert(7, Rook("Black", 7)) for i in range(8, 16): self.game_tiles.insert(i, Pawn("Black", i)) for i in range(16, 48): self.game_tiles.insert(i, NoPiece(i)) for i in range(48, 56): self.game_tiles.insert(i, Pawn("White", i)) self.game_tiles.insert(56, Rook("White", 56)) self.game_tiles.insert(57, Knight("White", 57)) self.game_tiles.insert(58, Bishop("White", 58)) self.game_tiles.insert(59, Queen("White", 59)) self.game_tiles.insert(60, King("White", 60)) self.game_tiles.insert(61, Bishop("White", 61)) self.game_tiles.insert(62, Knight("White", 62)) self.game_tiles.insert(63, Rook("White", 63)) self.original_game_tiles = self.game_tiles.copy()
def update_figures(self, board): for i in range(8): for j in range(8): type_of_piece = board[i][j][1] color = board[i][j][0] if type_of_piece == "q": self.add_figure(Queen(j, i, color, type_of_piece)) if type_of_piece == "r": self.add_figure(Rook(j, i, color, type_of_piece)) if type_of_piece == "b": self.add_figure(Bishop(j, i, color, type_of_piece)) if type_of_piece == "k": self.add_figure(Knight(j, i, color, type_of_piece)) if type_of_piece == "p": self.add_figure(Pawn(j, i, color, type_of_piece)) if type_of_piece == "W": self.add_figure(King(j, i, color, type_of_piece))
def promote(self, newpiece): x = self.x y = self.y if "Queen" is newpiece.name: self.x = -1 Queen(self.board, y, x, self.color) return True if "Rook" is newpiece.name: self.x = -1 Rook(self.board, y, x, self.color) return True if "Bishop" is newpiece.name: self.x = -1 Bishop(self.board, y, x, self.color) return True if "Knight" is newpiece.name: self.x = -1 Knight(self.board, y, x, self.color) return True return False
def initPlayerTwo(playerTwoPieces,board): """This populates the playerTwoPieces (white) empty set with the player two's pieces. It also adds those pieces to the board :param playerTwoPieces: An empty set representing player two's pieces :type playerTwoePieces: set :param board: A 2d list consisting of an 8 by 8 None objects and any prepopulated pieces :type board: [list] """ color="WHITE" group=playerTwoPieces playerTwoPieces.update({Pawn(board,color,group,7,'A'),Pawn(board,color,group,7,'B'),Pawn(board,color,group,7,'C'),Pawn(board,color,group,7,'D'),Pawn(board,color,group,7,'E'),Pawn(board,color,group,7,'F'),Pawn(board,color,group,7,'G'),Pawn(board,color,group,7,'H')}) #Add rank 1 pieces playerTwoPieces.update({Rook(board,color,group,8,'A'),Knight(board,color,group,8,'B'),Bishop(board,color,group,8,'C'),Queen(board,color,group,8,'D'),King(board,color,group,8,'E'),Bishop(board,color,group,8,'F'),Knight(board,color,group,8,'G'),Rook(board,color,group,8,'H')})
def game(self): done = True promotion = False new_game_screen = False selection = [] while done is True: pygame.display.flip() for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = False # exit while loop if event.type == pygame.MOUSEBUTTONDOWN and new_game_screen: if pygame.mouse.get_pressed( )[0]: # left mouse button pressed pos = pygame.mouse.get_pos() x = self.tenth # yes - new game box if 3 * x <= pos[ 0] <= 4.5 * x: #and 7 * x <= pos[1] <= 8 * x: self.board.clear_board() self.board.initialize_standard_board() pygame.display.flip() self.board.white_turn = True # if white won, it should be their turn again self.game() # make a new game # no - new game box if 5.5 * x <= pos[0] <= 7 * x and 7 * x <= pos[ 1] <= 8 * x: done = False # exit while loop if event.type == pygame.MOUSEBUTTONDOWN and promotion: # promotion dialogue if pygame.mouse.get_pressed( )[0]: # left mouse button pressed pos = pygame.mouse.get_pos() # Each box for each piece selection, if they don't press on the box, try again x = self.tenth if 1.5 * x <= pos[0] < 2.5 * x and 1.5 * x <= pos[ 1] <= 2.5 * x: knight = Knight(color) self.board.place_piece(square, knight) self.board.deselect_board() pygame.display.flip() promotion = False elif 3.5 * x <= pos[0] < 4.5 * x and 1.5 * x <= pos[ 1] <= 2.5 * x: bishop = Bishop(color) self.board.place_piece(square, bishop) self.board.deselect_board() pygame.display.flip() promotion = False elif 5.5 * x <= pos[0] < 6.5 * x and 1.5 * x <= pos[ 1] <= 2.5 * x: rook = Rook(color) self.board.place_piece(square, rook) self.board.deselect_board() pygame.display.flip() promotion = False elif 7.5 * x <= pos[0] < 8.5 * x and 1.5 * x <= pos[ 1] <= 2.5 * x: queen = Queen(color) self.board.place_piece(square, queen) self.board.deselect_board() pygame.display.flip() promotion = False if event.type == pygame.MOUSEBUTTONDOWN: # normal move if pygame.mouse.get_pressed( )[0]: # left mouse button pressed pos = pygame.mouse.get_pos() square = self.board.get_square(pos) selection.append(square) self.board.select_piece(square) pygame.display.flip() if len(selection) == 2: output = self.board.player_move(selection[0], selection[1]) selection = [] if output[0] == "Promotion": color = output[1] square = output[2] pygame.display.flip() promotion = True if output == "New_Game": self.board.new_game_message() pygame.display.flip() new_game_screen = True self.board.print_move_list()
# node.toString() x = getBestMove(node, 2) newBoard = x[0] if newBoard.currentPlayer == "White": newBoard.currentPlayer = "Black" newBoard.prevBoard.currentPlayer = "White" else: newBoard.currentPlayer = "White" newBoard.prevBoard.currentPlayer = "Black" return newBoard #testing # b = BoardEvaluator(b) b = Board() b.tiles[39] = (Tile(39, Queen("White", 39))) b.tiles[36] = (Tile(36, Pawn("Black", 36))) b.tiles[52] = (Tile(52, NullPiece())) b.tiles[53] = (Tile(53, NullPiece())) b.tiles[21] = (Tile(21, Knight("White", 21))) b.tiles[6] = (Tile(6, NullPiece())) b.currentPlayer = "Black" # newBoard.printBoard() #resultNode = Node(b, True) #a = getBestMove(resultNode, 1) #x = 0 #for node in nodes.keys(): #node.position.printBoard()
def initPlayerOne(playerOnePieces,board): """This populates the playerOnePieces (black) empty set with the player one's pieces. It also adds those pieces to the board :param playerOnePieces: An empty set representing player one's pieces :type playerOnePieces: set :param board: A 2d list consisting of an 8 by 8 None objects and any prepopulated pieces :type board: [list] """ #add pawns #(self,board,color,group,row,col) color="BLACK" group=playerOnePieces playerOnePieces.update({Pawn(board,color,group,2,'A'),Pawn(board,color,group,2,'B'),Pawn(board,color,group,2,'C'),Pawn(board,color,group,2,'D'),Pawn(board,color,group,2,'E'),Pawn(board,color,group,2,'F'),Pawn(board,color,group,2,'G'),Pawn(board,color,group,2,'H')}) #Add rank 1 pieces playerOnePieces.update({Rook(board,color,group,1,'A'),Knight(board,color,group,1,'B'),Bishop(board,color,group,1,'C'),Queen(board,color,group,1,'D'),King(board,color,group,1,'E'),Bishop(board,color,group,1,'F'),Knight(board,color,group,1,'G'),Rook(board,color,group,1,'H')})
def makeMove(self): specialMove = False boardCopy = copy.deepcopy(self.board) prevPosition = self.piece.position boardCopy.tiles[self.piece.position] = Tile(self.piece.position, NullPiece()) enPassantHold = self.piece.position #after first move, castling isn't allowed if self.piece.toString().lower() == "k" or self.piece.toString().lower( ) == "r": self.piece.moved = True # deal with a queened pawn if self.piece.toString().lower() == "p": if self.piece.color == "White" and 56 <= self.coordinate < 64: boardCopy.tiles[self.coordinate] = Tile( self.coordinate, Queen("White", self.coordinate)) specialMove = True elif self.piece.color == "Black" and 0 <= self.coordinate < 7: boardCopy.tiles[self.coordinate] = Tile( self.coordinate, Queen("Black", self.coordinate)) specialMove = True #deal with en passant if self.piece.toString().lower() == "p": if self.piece.color == "White" and 32 <= enPassantHold < 39: if self.coordinate - enPassantHold != 8: if boardCopy.tiles[ self.coordinate].pieceOnTile.toString() == "-": boardCopy.tiles[self.coordinate - 8] = Tile( self.coordinate, NullPiece()) boardCopy.tiles[self.coordinate] = Tile( self.coordinate, self.piece) specialMove = True elif self.piece.color == "Black" and 24 <= enPassantHold < 31: if self.coordinate - enPassantHold != -8: if boardCopy.tiles[ self.coordinate].pieceOnTile.toString() == "-": boardCopy.tiles[self.coordinate + 8] = Tile( self.coordinate, NullPiece()) boardCopy.tiles[self.coordinate] = Tile( self.coordinate, self.piece) specialMove = True #deal with castling if self.piece.toString() == "k": if prevPosition == 4 and self.coordinate == 2: boardCopy.tiles[self.coordinate] = Tile( self.coordinate, King("White", self.coordinate)) boardCopy.tiles[0] = Tile(self.coordinate, NullPiece()) boardCopy.tiles[3] = Tile(self.coordinate, Rook("White", 3)) specialMove = True if prevPosition == 4 and self.coordinate == 6: boardCopy.tiles[self.coordinate] = Tile( self.coordinate, King("White", self.coordinate)) boardCopy.tiles[7] = Tile(self.coordinate, NullPiece()) boardCopy.tiles[5] = Tile(self.coordinate, Rook("White", 5)) specialMove = True if self.piece.toString() == "K": if prevPosition == 60 and self.coordinate == 58: boardCopy.tiles[self.coordinate] = Tile( self.coordinate, King("Black", self.coordinate)) boardCopy.tiles[56] = Tile(self.coordinate, NullPiece()) boardCopy.tiles[59] = Tile(self.coordinate, Rook("Black", 59)) specialMove = True if prevPosition == 60 and self.coordinate == 62: boardCopy.tiles[self.coordinate] = Tile( self.coordinate, King("Black", self.coordinate)) boardCopy.tiles[63] = Tile(self.coordinate, NullPiece()) boardCopy.tiles[61] = Tile(self.coordinate, Rook("Black", 61)) specialMove = True if not specialMove: self.piece.position = self.coordinate boardCopy.tiles[self.coordinate] = Tile(self.coordinate, self.piece) friendlyKing = None for tile in boardCopy.tiles.values(): if (tile.pieceOnTile.toString() == "K" and boardCopy.currentPlayer == "Black") or (tile.pieceOnTile.toString() == "k" and boardCopy.currentPlayer == "White"): friendlyKing = tile.pieceOnTile break if friendlyKing.inCheck(boardCopy): return False return boardCopy