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 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 __setupChessboard(self): lineup = [Rook,Knight,Bishop,Queen,King,Bishop,Knight,Rook] for i in range(0,8): self.__chessboard[1,i] = Pawn(self.__black) self.__chessboard[6,i] = Pawn(self.__white) self.__chessboard[0,i] = lineup[i](self.__black) self.__chessboard[7,i] = lineup[i](self.__white)
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 test2(): #--Starting position for white pawn in f2- #Black pawns in both of white's attacking postion P = Pawn(Color.White) p = Pawn(Color.Black) gameBoard = Board(); place_piece(gameBoard, "f2", P) place_piece(gameBoard, "e3", p) place_piece(gameBoard, "g3", p) #print "Legal Moves:", p.legalMoves(point, gameBoard) translate(P.legalMoves(gameBoard, False)) display.print_board(gameBoard.board)
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 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 test1(): #--Starting position for black pawn in b7-- #white pawns in both attacking directions p = Pawn(Color.Black) P = Pawn(Color.White) gameBoard = Board(); place_piece(gameBoard, "b7", p) place_piece(gameBoard, "a6", P) place_piece(gameBoard, "c6", P) #print "Legal Moves:", p.legalMoves(point, gameBoard) translate(p.legalMoves(gameBoard, False)) display.print_board(gameBoard.board)
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 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 test_king_capture(): """Tests if the king can capture """ playerOnePieces = set() playerTwoPieces = set() board = [[None] * 8 for i in range(8)] color = "BLACK" myPiece = King(board, color, playerOnePieces, 2, 'A') playerOnePieces.update({myPiece}) enemyPiece = Pawn(board, "WHITE", playerTwoPieces, 3, 'B') 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 = "3A" board[possibleRow[int(moveOne[0])]][possibleCol[moveOne[1]]].move( int(moveTwo[0]), moveTwo[1]) moveOne = "3A" moveTwo = "3B" # 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 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 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 test_pawn_move(): """Tests if the pawn can move """ playerOnePieces = set() board = [[None] * 8 for i in range(8)] color = "BLACK" myPiece = Pawn(board, color, playerOnePieces, 2, 'A') playerOnePieces.update({myPiece}) 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 = "3A" # 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
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))
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 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