def setBoard(self): self.board = [[None for i in range(8)] for i in range(8)] for piece in self.board[1]: self.board[1][self.board[1].index(piece)] = Pawn( [1, self.board[1].index(piece)], "black") for piece in self.board[6]: self.board[6][self.board[6].index(piece)] = Pawn( [6, self.board[6].index(piece)], "white") self.board[0][0] = Rook([0, 0], "black") self.board[0][7] = Rook([0, 7], "black") self.board[0][1] = Knight([0, 1], "black") # black knight self.board[0][6] = Knight([0, 6], "black") # black knight self.board[0][2] = Bishop([0, 2], "black") # black bishop self.board[0][5] = Bishop([0, 5], "black") # black bishop self.board[0][3] = Queen([0, 3], "black") # black king self.board[0][4] = King([0, 4], "black") # black queen self.board[7][0] = Rook([7, 0], "white") self.board[7][7] = Rook([7, 7], "white") self.board[7][1] = Knight([7, 1], "white") # white knight self.board[7][6] = Knight([7, 6], "white") # white knight self.board[7][2] = Bishop([7, 2], "white") # white bishop self.board[7][5] = Bishop([7, 5], "white") # white bishop self.board[7][3] = Queen([7, 3], "white") # white king self.board[7][4] = King([7, 4], "white") # white queen
def setBoard(self): backRank, nextRank = 0, 1 if(self.color == "black"): backRank = 7 nextRank = 6 for piece in self.piecesNameList: if(piece == "r"): self.board[backRank][0] = Rook(self.color, (0, backRank)) self.board[backRank][7] = Rook(self.color, (7, backRank)) elif(piece == "n"): self.board[backRank][1] = Knight(self.color, (1, backRank)) self.board[backRank][6] = Knight(self.color, (6, backRank)) elif(piece == "b"): self.board[backRank][2] = Bishop(self.color, (2, backRank)) self.board[backRank][5] = Bishop(self.color, (5, backRank)) elif(piece == "q"): self.board[backRank][3] = Queen(self.color, (3, backRank)) elif(piece == "k"): self.board[backRank][4] = King(self.color, (4, backRank)) else: for file in range(0, 8): self.board[nextRank][file] = Pawn(self.color, (file, nextRank))
def create_pieces(self): header = 'XABCDEFGHX' for i in range(self.size): self.board[0][i] = EmptyField('None', [0, i], header[i]) self.board[self.size - 1][i] = EmptyField('None', [i, self.size - 1], header[i]) for i in range(1, self.size - 1, 1): self.board[i][0] = EmptyField('None', [i, 0], (self.size - 1) - i) self.board[i][self.size - 1] = EmptyField('None', [i, self.size - 1], (self.size - 1) - i) self.board[1][1] = Rook('B', [1, 1]) self.board[1][8] = Rook('B', [1, 8]) self.board[8][1] = Rook('W', [8, 1]) self.board[8][8] = Rook('W', [8, 8]) self.board[1][2] = Knight('B', [1, 2]) self.board[1][7] = Knight('B', [1, 7]) self.board[8][2] = Knight('W', [8, 2]) self.board[8][7] = Knight('W', [8, 8]) self.board[1][3] = Bishop('B', [1, 3]) self.board[1][6] = Bishop('B', [1, 6]) self.board[8][3] = Bishop('W', [8, 3]) self.board[8][6] = Bishop('W', [8, 6]) self.board[1][4] = Queen('B', [1, 4]) self.board[8][4] = Queen('W', [8, 4]) self.board[1][5] = King('B', [1, 5]) self.board[8][5] = King('W', [8, 5]) for i in range(1, self.size - 1, 1): self.board[2][i] = Pawn('B', [2, i]) self.board[7][i] = Pawn('W', [7, i])
def __init__(self): self.num_rows = 8 self.num_cols = 8 self.board = np.array([ [ Rook(0, 0, 'WHITE'), Knight(1, 0, 'WHITE'), Bishop(2, 0, 'WHITE'), King(3, 0, 'WHITE'), Queen(4, 0, 'WHITE'), Bishop(5, 0, 'WHITE'), Knight(6, 0, 'WHITE'), Rook(7, 0, 'WHITE') ], [Pawn(i, 1, 'WHITE') for i in range(self.num_cols)], [None for _ in range(self.num_cols)], [None for _ in range(self.num_cols)], [None for _ in range(self.num_cols)], [None for _ in range(self.num_cols)], [Pawn(i, 6, 'BLACK') for i in range(self.num_cols)], [ Rook(0, 7, 'BLACK'), Knight(1, 7, 'BLACK'), Bishop(2, 7, 'BLACK'), King(3, 7, 'BLACK'), Queen(4, 7, 'BLACK'), Bishop(5, 7, 'BLACK'), Knight(6, 7, 'BLACK'), Rook(7, 7, 'BLACK') ], ])
def init_pieces(self): # Init the pieces # black self.board[0][0].piece = Rook(0, 0, 'black', self) self.board[0][7].piece = Rook(0, 7, 'black', self) self.board[0][1].piece = Knight(0, 1, 'black', self) self.board[0][6].piece = Knight(0, 6, 'black', self) self.board[0][2].piece = Bishop(0, 2, 'black', self) self.board[0][5].piece = Bishop(0, 5, 'black', self) self.board[0][3].piece = Queen(0, 3, 'black', self) self.board[0][4].piece = King(0, 4, 'black', self) # white self.board[7][0].piece = Rook(7, 0, 'white', self) self.board[7][7].piece = Rook(7, 7, 'white', self) self.board[7][1].piece = Knight(7, 1, 'white', self) self.board[7][6].piece = Knight(7, 6, 'white', self) self.board[7][2].piece = Bishop(7, 2, 'white', self) self.board[7][5].piece = Bishop(7, 5, 'white', self) # self.board[4][2].piece = Bishop(4, 2, 'white', self) self.board[7][3].piece = Queen(7, 3, 'white', self) # self.board[3][7].piece = Queen(3, 7, 'white', self) self.board[7][4].piece = King(7, 4, 'white', self) for j in range(8): self.board[1][j].piece = Pawn(1, j, 'black', self) self.board[6][j].piece = Pawn(6, j, 'white', self)
def read_piece_on_square(driver, pos): class_on_square = read_class_of_square(driver, pos, "piece") if class_on_square is None: return None else: if "wr" in class_on_square: return Rook(1, pos.X, pos.Y, True) # hack autoset the id to 1 if "wn" in class_on_square: return Knight(1, pos.X, pos.Y, True) # hack autoset the id to 1 if "wb" in class_on_square: return Bishop(1, pos.X, pos.Y, True) # hack autoset the id to 1 if "wq" in class_on_square: return Queen(1, pos.X, pos.Y, True) # hack autoset the id to 1 if "wk" in class_on_square: return King(1, pos.X, pos.Y, True) # hack autoset the id to 1 if "wp" in class_on_square: return Pawn(1, pos.X, pos.Y, True) # hack autoset the id to 1 if "br" in class_on_square: return Rook(1, pos.X, pos.Y, False) # hack autoset the id to 1 if "bn" in class_on_square: return Knight(1, pos.X, pos.Y, False) # hack autoset the id to 1 if "bb" in class_on_square: return Bishop(1, pos.X, pos.Y, False) # hack autoset the id to 1 if "bq" in class_on_square: return Queen(1, pos.X, pos.Y, False) # hack autoset the id to 1 if "bk" in class_on_square: return King(1, pos.X, pos.Y, False) # hack autoset the id to 1 if "bp" in class_on_square: return Pawn(1, pos.X, pos.Y, False) # hack autoset the id to 1
def __init__(self, screen): self.screen = screen self.is_whites_turn = True self.board = [[0 for j in range(NO_OF_COLS)] for i in range(NO_OF_ROWS)] self.board[0][0] = Rook(screen, 0, 0, "black") self.board[0][1] = Knight(screen, 0, 1, "black") self.board[0][2] = Bishop(screen, 0, 2, "black") self.board[0][3] = Queen(screen, 0, 3, "black") self.board[0][4] = King(screen, 0, 4, "black") self.board[0][5] = Bishop(screen, 0, 5, "black") self.board[0][6] = Knight(screen, 0, 6, "black") self.board[0][7] = Rook(screen, 0, 7, "black") self.board[7][0] = Rook(screen, 7, 0, "white") self.board[7][1] = Knight(screen, 7, 1, "white") self.board[7][2] = Bishop(screen, 7, 2, "white") self.board[7][3] = Queen(screen, 7, 3, "white") self.board[7][4] = King(screen, 7, 4, "white") self.board[7][5] = Bishop(screen, 7, 5, "white") self.board[7][6] = Knight(screen, 7, 6, "white") self.board[7][7] = Rook(screen, 7, 7, "white") for j in range(NO_OF_COLS): self.board[1][j] = Pawn(screen, 1, j, "black") for j in range(NO_OF_COLS): self.board[6][j] = Pawn(screen, 6, j, "white") self.to_highlight = []
def init_all_pieces(self, is_captured=False, p_start_pos=False, k_start_pos=False): '''Initializes all chess pieces''' # Piece attributes self.pieces = { 'w_pawns': [Pawn(i + 1, is_captured=is_captured, start_pos=p_start_pos) for i in range(8)], 'b_pawns': [Pawn(i + 1, colour='Black', is_captured=is_captured, start_pos=p_start_pos) for i in range(8)], 'w_pieces': [ Rook(1, is_captured=is_captured), Knight(1, is_captured=is_captured), Bishop(1, is_captured=is_captured), Queen(is_captured=is_captured), King(is_captured=is_captured, start_pos=k_start_pos), Bishop(2, is_captured=is_captured), Knight(2, is_captured=is_captured), Rook(2, is_captured=is_captured), ], 'b_pieces': [ Rook(1, colour='Black', is_captured=is_captured), Knight(1, colour='Black', is_captured=is_captured), Bishop(1, colour='Black', is_captured=is_captured), Queen(colour='Black', is_captured=is_captured), King(colour='Black', is_captured=is_captured, start_pos=k_start_pos), Bishop(2, colour='Black', is_captured=is_captured), Knight(2, colour='Black', is_captured=is_captured), Rook(2, colour='Black', is_captured=is_captured), ] }
class TestBishop(TestPiece): """ Testing a Bishop piece functionality with a board set to it. """ def setUp(self): """ setup the test with 1 Bishop on a board. :return: None. """ self.piece = Bishop() self.piece.board = Board(4, 4, [self.piece]) def test_get_moves(self): """ test getting the available moves of a Bishop piece. :return: None. """ self.setUp() moves = self.piece.get_moves() moves_should_be = [(1, 1), (2, 2), (3, 3)] self.assertEqual(moves, moves_should_be) self.piece.move(2, 2) moves = self.piece.get_moves() moves_should_be = [(3, 1), (1, 3), (0, 0), (1, 1), (3, 3)] self.assertEqual(moves, moves_should_be)
def _setup_board(self): self.parent.title = 'Chess' self.parent.geometry('600x600') tk.Grid.rowconfigure(self.parent, 0, weight=1) tk.Grid.columnconfigure(self.parent, 0, weight=1) self.frame.grid(row=0, column=0, sticky='nsew') grid = tk.Frame(self.frame) grid.grid(sticky='nsew', column=0, row=7, columnspan=2) tk.Grid.rowconfigure(self.frame, 7, weight=1) tk.Grid.columnconfigure(self.frame, 0, weight=1) player1_pieces = [] player2_pieces = [] # create pawns for i in range(8): player1_pieces.append( WhitePawn(img_path=IMAGE_PATH.format('P'), x=i, y=1)) player2_pieces.append( BlackPawn(img_path=IMAGE_PATH.format('p'), x=i, y=6)) # create others player1_pieces.append(Rook(img_path=IMAGE_PATH.format('R'), x=0, y=0)) player1_pieces.append(Rook(img_path=IMAGE_PATH.format('R'), x=7, y=0)) player1_pieces.append(Knight(img_path=IMAGE_PATH.format('N'), x=1, y=0)) player1_pieces.append(Knight(img_path=IMAGE_PATH.format('N'), x=6, y=0)) player1_pieces.append(Bishop(img_path=IMAGE_PATH.format('B'), x=2, y=0)) player1_pieces.append(Bishop(img_path=IMAGE_PATH.format('B'), x=5, y=0)) player2_pieces.append(Rook(img_path=IMAGE_PATH.format('r'), x=0, y=7)) player2_pieces.append(Rook(img_path=IMAGE_PATH.format('r'), x=7, y=7)) player2_pieces.append(Knight(img_path=IMAGE_PATH.format('n'), x=1, y=7)) player2_pieces.append(Knight(img_path=IMAGE_PATH.format('n'), x=6, y=7)) player2_pieces.append(Bishop(img_path=IMAGE_PATH.format('b'), x=2, y=7)) player2_pieces.append(Bishop(img_path=IMAGE_PATH.format('b'), x=5, y=7)) # create king + queens player1_pieces.append(King(img_path=IMAGE_PATH.format('K'), x=3, y=0)) player1_pieces.append(Queen(img_path=IMAGE_PATH.format('Q'), x=4, y=0)) player2_pieces.append(King(img_path=IMAGE_PATH.format('k'), x=4, y=7)) player2_pieces.append(Queen(img_path=IMAGE_PATH.format('q'), x=3, y=7)) self.players[0].pieces = player1_pieces self.players[1].pieces = player2_pieces self.add_pieces_to_board() for x in range(8): tk.Grid.columnconfigure(self.frame, x, weight=1) for y in range(8): tk.Grid.rowconfigure(self.frame, y, weight=1)
def CreateNewBoard(cls, fen=None): if fen: raise NotImplementedError() else: pieces = [Pawn((i, 1), "white") for i in range(8) ] + [Pawn((i, 6), "black") for i in range(8)] pieces.extend([ Rook((0, 0), "white"), Rook((7, 0), "white"), Rook((0, 7), "black"), Rook((7, 7), "black") ]) pieces.extend([ Knight((1, 0), "white"), Knight((6, 0), "white"), Knight((1, 7), "black"), Knight((6, 7), "black") ]) pieces.extend([ Bishop((2, 0), "white"), Bishop((5, 0), "white"), Bishop((2, 7), "black"), Bishop((5, 7), "black") ]) pieces.extend([King((4, 0), "white"), King((4, 7), "black")]) pieces.extend([Queen((3, 0), "white"), Queen((3, 7), "black")]) return cls(pieces, "white")
def __init__(self): self.spaceSize = 50 self.blackRow = 0 self.whiteRow = 7 self.whiteKing = King(self.whiteRow,4,'white') self.blackKing = King(self.blackRow,4,'black') #Set up the board self.blackPieces =[Rook(self.blackRow,0,'black'), Knight(self.blackRow,1,'black'), Bishop(self.blackRow,2,'black'), Queen(self.blackRow,3,'black'), self.blackKing, Bishop(self.blackRow,5,'black'), Knight(self.blackRow,6,'black'), Rook(self.blackRow,self.whiteRow,'black')] self.whitePieces = [Rook(self.whiteRow,0,'white'), Knight(self.whiteRow,1,'white'), Bishop(self.whiteRow,2,'white'), Queen(self.whiteRow,3,'white'), self.whiteKing, Bishop(self.whiteRow,5,'white'), Knight(self.whiteRow,6,'white'), Rook(self.whiteRow,self.whiteRow,'white')] self.board = [ [], [], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [], [] ] setPawns(self.board) setPieces(self.board,self.blackPieces,self.whitePieces) dBoard = [x[:] for x in self.board] self.positions = [[dBoard,0]]
def init(self): """Initialize board pieces.""" self._pieces = { BLACK: PList([]), WHITE: PList([]), } self._playing_color = WHITE for color in [BLACK, WHITE]: for pawn_n in range(8): x = 1 if color is TOP_COLOR else 6 y = pawn_n pawn = Pawn(x, y, color) self._add_piece(pawn) lign = 0 if color is TOP_COLOR else 7 self._add_piece(Bishop(lign, 2, color)) self._add_piece(Bishop(lign, 5, color)) self._add_piece(Knight(lign, 1, color)) self._add_piece(Knight(lign, 6, color)) self._add_piece(Rook(lign, 0, color)) self._add_piece(Rook(lign, 7, color)) if TOP_COLOR is BLACK: self._add_piece(Queen(lign, 3, color)) self._add_piece(King(lign, 4, color)) else: self._add_piece(King(lign, 3, color)) self._add_piece(Queen(lign, 4, color))
def newPieces(self): self.wPieces = [] self.bPieces = [] self.empty = [] for i in range(8): self.wPieces.append(Pawn('w', (i, 1), 10 + i)) self.bPieces.append(Pawn('b', (i, 6), 10 + i)) for i in [0, 7]: self.wPieces.append(Rook('w', (i, 0), 20 + i)) self.bPieces.append(Rook('b', (i, 7), 20 + i)) for i in [1, 6]: self.wPieces.append(Knight('w', (i, 0), 30 + i)) self.bPieces.append(Knight('b', (i, 7), 30 + i)) for i in [2, 5]: self.wPieces.append(Bishop('w', (i, 0), 40 + i)) self.bPieces.append(Bishop('b', (i, 7), 40 + i)) self.wPieces.append(Queen('w', (3, 0), 50)) self.bPieces.append(Queen('b', (3, 7), 50)) self.wPieces.append(King('w', (4, 0), 60)) self.bPieces.append(King('b', (4, 7), 60)) for i in range(8): self.empty.append(Empty((i, 2))) self.empty.append(Empty((i, 3))) self.empty.append(Empty((i, 4))) self.empty.append(Empty((i, 5))) self.allPieces = self.wPieces + self.bPieces + self.empty
def test_bishop_valid_moves(): board = Board() bishop = Bishop(1, 3, 'b') bishop.valid_moves(board) actual = bishop.move_list expected = [[0, 2], [0, 4], [2, 4], [3, 5], [4, 6], [5, 7], [2, 2], [3, 1], [4, 0]] assert actual == expected
def test_bishop_valid_moves_two(): board = Board() bishop = Bishop(4, 4, 'b') bishop.valid_moves(board) actual = bishop.move_list expected = [[3, 3], [2, 2], [1, 1], [0, 0], [3, 5], [2, 6], [1, 7], [5, 5], [6, 6], [7, 7], [5, 3], [6, 2], [7, 1]] assert actual == expected
def __init__(self, color, player=1): self.name, y = ('player1', 1) if player == 1 else ('player2', 8) self.color = color self.pawns = [Pawn(color, (x, (2 if player == 1 else 7))) for x in range(1, 9)] self.king = King(color, (5, y)) self.pieces = [Rook(color, (1, y)), Rook(color, (8, y)), Knight(color, (2, y)), Knight(color, (7, y)), Bishop(color, (3, y)), Bishop(color, (6, y)), Queen(color, (4, y)), self.king] + self.pawns self.turn = False
def test_bishop_attacking(): board = Board() bishop = Bishop(4, 3, 'w') board.board[4][3] = bishop board.board[5][4] = Pawn(5, 4, 'b') bishop.valid_moves(board) actual = bishop.attack_list expected = [[5, 4]] assert actual == expected
def __init__(self, side='w'): self.side = side # side closes to you aka the color you are playing if self.side == 'w': self.opponent = 'b' else: self.opponent = 'w' self.white_to_move = True self.moves = [] # log of moves self.board = np.full((DIM, DIM), Null(), dtype=object) self.board[0][0] = Rook((0, 0), self.opponent) self.board[0][1] = Knight((0, 1), self.opponent) self.board[0][2] = Bishop((0, 2), self.opponent) self.board[0][5] = Bishop((0, 5), self.opponent) self.board[0][6] = Knight((0, 6), self.opponent) self.board[0][7] = Rook((0, 7), self.opponent) self.board[1][0] = Pawn((1, 0), self.opponent) self.board[1][1] = Pawn((1, 1), self.opponent) self.board[1][2] = Pawn((1, 2), self.opponent) self.board[1][3] = Pawn((1, 3), self.opponent) self.board[1][4] = Pawn((1, 4), self.opponent) self.board[1][5] = Pawn((1, 5), self.opponent) self.board[1][6] = Pawn((1, 6), self.opponent) self.board[1][7] = Pawn((1, 7), self.opponent) self.board[6][0] = Pawn((6, 0), self.side) self.board[6][1] = Pawn((6, 1), self.side) self.board[6][2] = Pawn((6, 2), self.side) self.board[6][3] = Pawn((6, 3), self.side) self.board[6][4] = Pawn((6, 4), self.side) self.board[6][5] = Pawn((6, 5), self.side) self.board[6][6] = Pawn((6, 6), self.side) self.board[6][7] = Pawn((6, 7), self.side) self.board[7][0] = Rook((7, 0), self.side) self.board[7][1] = Knight((7, 1), self.side) self.board[7][2] = Bishop((7, 2), self.side) self.board[7][5] = Bishop((7, 5), self.side) self.board[7][6] = Knight((7, 6), self.side) self.board[7][7] = Rook((7, 7), self.side) # flip the King/Queen depending on the side if self.side == 'w': qcol = 3 kcol = 4 else: qcol = 4 kcol = 3 self.board[0][qcol] = Queen((0, 3), self.opponent) self.board[0][kcol] = King((0, 4), self.opponent) self.board[7][qcol] = Queen((7, 3), self.side) self.board[7][kcol] = King((7, 4), self.side)
def __init__(self, rows, columns): self.rows = rows self.columns = columns self.ready = False self.last = None self.copy = True self.board = [[0 for x in range(8)] for _ in range(rows)] self.board[0][0] = Rook(0, 0, "black") self.board[0][1] = Knight(0, 1, "black") self.board[0][2] = Bishop(0, 2, "black") self.board[0][3] = Queen(0, 3, "blackwhite") self.board[0][4] = King(0, 4, "black") self.board[0][5] = Bishop(0, 5, "black") self.board[0][6] = Knight(0, 6, "black") self.board[0][7] = Rook(0, 7, "black") self.board[1][0] = Pawn(1, 0, "black") self.board[1][1] = Pawn(1, 1, "black") self.board[1][2] = Pawn(1, 2, "black") self.board[1][3] = Pawn(1, 3, "black") self.board[1][4] = Pawn(1, 4, "black") self.board[1][5] = Pawn(1, 5, "black") self.board[1][6] = Pawn(1, 6, "black") self.board[1][7] = Pawn(1, 7, "black") self.board[7][0] = Rook(7, 0, "white") self.board[7][1] = Knight(7, 1, "white") self.board[7][2] = Bishop(7, 2, "white") self.board[7][3] = Queen(7, 3, "white") self.board[7][4] = King(7, 4, "white") self.board[7][5] = Bishop(7, 5, "white") self.board[7][6] = Knight(7, 6, "white") self.board[7][7] = Rook(7, 7, "white") self.board[6][0] = Pawn(6, 0, "white") self.board[6][1] = Pawn(6, 1, "white") self.board[6][2] = Pawn(6, 2, "white") self.board[6][3] = Pawn(6, 3, "white") self.board[6][4] = Pawn(6, 4, "white") self.board[6][5] = Pawn(6, 5, "white") self.board[6][6] = Pawn(6, 6, "white") self.board[6][7] = Pawn(6, 7, "white") self.p1Name = "Player 1" self.p2Name = "Player 2" self.turn = "white" self.time1 = 900 self.time2 = 900 self.storedTime1 = 0 self.storedTime2 = 0 self.winner = None self.startTime = time.time()
def test_bishop_no_moves_after_attacking(): board = Board() bishop = Bishop(4, 3, 'w') board.board[4][3] = bishop board.board[5][4] = Pawn(5, 4, 'b') bishop.valid_moves(board) actual = bishop.move_list expected = [[3, 2], [2, 1], [1, 0], [3, 4], [2, 5], [1, 6], [0, 7], [5, 2], [6, 1], [7, 0]] assert actual == expected
def set_initial_state(self): ''' Creates and initializes the board ''' self.board = [[Rook(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Rook(0)], [Knight(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Knight(0)], [Bishop(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Bishop(0)], [Queen(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Queen(0)], [King(1), Pawn(1), 0, 0, 0, 0, Pawn(0), King(0)], [Bishop(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Bishop(0)], [Knight(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Knight(0)], [Rook(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Rook(0)]]
def test_attacks_1(self): """ test method for Bishop.attacks_pos method """ bishop = Bishop(5, 5) self.assertTrue(bishop.attacks_pos(1, 1)) self.assertTrue(bishop.attacks_pos(2, 8)) self.assertTrue(bishop.attacks_pos(7, 3)) self.assertFalse(bishop.attacks_pos(0, 5)) self.assertFalse(bishop.attacks_pos(5, 0)) self.assertFalse(bishop.attacks_pos(5, 4)) self.assertFalse(bishop.attacks_pos(5, 6))
def populate_board(self): """ Initialize board with positions of pieces. """ self.board = [ [ Rook(owner='white', position=(0, 0)), Knight(owner='white', position=(1, 0)), Bishop(owner='white', position=(2, 0)), Queen(owner='white', position=(3, 0)), King(owner='white', position=(4, 0)), Bishop(owner='white', position=(5, 0)), Knight(owner='white', position=(6, 0)), Rook(owner='white', position=(7, 0)), ], [Pawn(owner='white', position=(i, 1)) for i in range(8)], *[[None] * 8 for _ in range(4)], [Pawn(owner='black', position=(i, 6)) for i in range(8)], [ Rook(owner='black', position=(0, 7)), Knight(owner='black', position=(1, 7)), Bishop(owner='black', position=(2, 7)), Queen(owner='black', position=(3, 7)), King(owner='black', position=(4, 7)), Bishop(owner='black', position=(5, 7)), Knight(owner='black', position=(6, 7)), Rook(owner='black', position=(7, 7)), ], ] self.char_board = [] for row in self.board: disp_row = [] for piece in row: if piece: disp_row.append(piece.cli_characterset) else: disp_row.append(None) self.char_board.append(disp_row) for i, row in enumerate(self.board): self.pieces_in_play['white'].extend( [piece for piece in row if i < 2]) self.pieces_in_play['black'].extend( [piece for piece in row if i > 5]) # Add reference to the kings for both players self.w_king = self.board[0][4] self.b_king = self.board[7][4]
def create_pieces(self): for player, f_row, b_row in zip([self.white, self.black], [1, 6], [0, 7]): #pawns for col in self.cols: p = Pawn(player) if player == self.black: p.direction = -1 player.add_piece(p) self.grid[col][f_row].add_piece(p) #rook for col in ['a', 'h']: p = Rook(player) player.add_piece(p) self.grid[col][b_row].add_piece(p) #knight for col in ['b', 'g']: p = Knight(player) player.add_piece(p) self.grid[col][b_row].add_piece(p) #bishop for col in ['c', 'f']: p = Bishop(player) player.add_piece(p) self.grid[col][b_row].add_piece(p) #queen king for col, piece in zip(['d', 'e'], [Queen(player), King(player)]): player.add_piece(piece) self.grid[col][b_row].add_piece(piece) if isinstance(piece, King): player.king = piece
def promote(self, piece): done = False # freeze normal execution (in main), wait for player to decide which piece take while not done: pressed, events = Interface.run() # display everything Board.display() self.menu.display() self.ChessGame.display() self.pmenu.display() self.pmenu.react_events(events, pressed) if self.pmenu.state == 'done': done = True piece_name = self.pmenu.piece_name self.pmenu.state = 'wait' # create new piece if piece_name == 'queen': new_piece = Queen(piece.coord, piece.color) elif piece_name == 'bishop': new_piece = Bishop(piece.coord, piece.color) elif piece_name == 'rock': new_piece = Rock(piece.coord, piece.color) elif piece_name == 'knight': new_piece = Knight(piece.coord, piece.color) # remove pawn self.player.pieces.remove(piece) # add new piece self.player.pieces.append(new_piece) Interface.add_resizable_objs([new_piece])
def setUp(self): """ setup the test with 1 Bishop on a board. :return: None. """ self.piece = Bishop() self.piece.board = Board(4, 4, [self.piece])
def create_pieces(self, config): self.pieces = [] self.king = None for pdict in config: if pdict['name'] == 'pawn': pawn = Pawn(pdict['coord'], pdict['c']) self.pieces.append(pawn) elif pdict['name'] == 'bishop': bishop = Bishop(pdict['coord'], pdict['c']) self.pieces.append(bishop) elif pdict['name'] == 'queen': queen = Queen(pdict['coord'], pdict['c']) self.pieces.append(queen) elif pdict['name'] == 'rock': rock = Rock(pdict['coord'], pdict['c']) self.pieces.append(rock) elif pdict['name'] == 'knight': knight = Knight(pdict['coord'], pdict['c']) self.pieces.append(knight) elif pdict['name'] == 'king': king = King(pdict['coord'], pdict['c']) self.pieces.append(king) self.king = king # get player's color self.color = self.king.color
def promotePawn(self, coord, promoteTo): """ Promote pawn to desired piece. TODO: GUI for suggesting which piece """ self.array[coord[0], coord[1]] = Queen([coord[0], coord[1]], self.array[coord[0], coord[1]].colour, self) if promoteTo.lower() == "queen": self.array[coord[0], coord[1]] = Queen([coord[0], coord[1]], self.array[coord[0], coord[1]].colour, self) elif promoteTo.lower() == "rook": self.array[coord[0], coord[1]] = Rook([coord[0], coord[1]], self.array[coord[0], coord[1]].colour, self) elif promoteTo.lower() == "knight": self.array[coord[0], coord[1]] = Knight([coord[0], coord[1]], self.array[coord[0], coord[1]].colour, self) elif promoteTo.lower() == "bishop": self.array[coord[0], coord[1]] = Bishop([coord[0], coord[1]], self.array[coord[0], coord[1]].colour, self)
def promote(self): from pieces import Queen, Bishop, Knight, Rook, Empty while (True): newPiece = input( 'what piece would you like to promote your pawn to? [q = Queen, b = Bishop, n = Knight, r = rook] ' ) if (newPiece == 'q'): self.promotePiece = Queen(self.piece.color, self.dest.position, 1000 + self.promoteID) break elif (newPiece == 'b'): self.promotePiece = Bishop(self.piece.color, self.dest.position, 1000 + self.promoteID) break elif (newPiece == 'n'): self.promotePiece = Knight(self.piece.color, self.dest.position, 1000 + self.promoteID) break elif (newPiece == 'r'): self.promotePiece = Rook(self.piece.color, self.dest.position, 1000 + self.promoteID) break else: print('invalid input, try again...') position = self.piece.position self.board.allPieces.remove(self.piece) self.board.allPieces.remove(self.dest) self.board.allPieces.append(self.promotePiece) self.board.allPieces.append(Empty(position)) self.board.allocatePieces()
def unpack(packed_board): board = Board() board.clear() board.moves = eval(packed_board["moves"]) board.can_castle = eval(packed_board["can_castle"]) b = packed_board["board"] board.player_to_move = packed_board['player_to_move'] m = board.matrix for i in range(8): for j in range(8): square = b[i][j] if square == None: m[i][j] = 0 else: color = (square["color"] == "white") * 2 - 1 row = square["pos_row"] column = square["pos_col"] if square["type"] == "rook": Rook(color, row, column, board) elif square["type"] == "knight": Knight(color, row, column, board) elif square["type"] == "bishop": Bishop(color, row, column, board) elif square["type"] == "queen": Queen(color, row, column, board) elif square["type"] == "pawn": Pawn(color, row, column, board) elif square["type"] == "king": kk = King(color, row, column, board) if color == 1: board.white_king = kk else: board.black_king = kk return board
def makePiece(pieceString, x, y, color): """This method returns a piece in a given x, y, and color Args: x - x coordinate - integer y - y coordinate - integer color - White, Black Returns: Piece object """ if (pieceString == "King"): piece = King() elif (pieceString == "Queen"): piece = Queen() elif (pieceString == "Rook"): piece = Rook() elif (pieceString == "Bishop"): piece = Bishop() elif (pieceString == "Pawn"): piece = Pawn() elif (pieceString == "Knight"): piece = Knight() piece.setX(x) piece.setY(y) if (color == "White"): piece.setColor(0) if (color == "Black"): piece.setColor(1) return piece
def get_legal_moves(self) -> List[Coordinate]: """Define Queen legal moves by combining the legal moves of a Rook and a Bishop in its position""" legal_moves: List[Coordinate] = [] if not self.active: return legal_moves # Determine legal coordinate destinations legal_moves = Rook.get_legal_moves(self) + Bishop.get_legal_moves(self) return legal_moves