def __init__(self): self.board = [[], [], [], [], [], [], [], []] self.board[0].append(Rook(0, 0, "black")) self.board[1].append(Knight(1, 0, "black")) self.board[2].append(Bishop(2, 0, "black")) self.board[3].append(Queen(3, 0, "black")) self.board[4].append(King(4, 0, "black")) self.board[5].append(Bishop(5, 0, "black")) self.board[6].append(Knight(6, 0, "black")) self.board[7].append(Rook(7, 0, "black")) for i in range(8): self.board[i].append(Pawn(i, 1, "black")) for i in range(4): for j in range(8): self.board[j].append(NoPiece(i + 2, j)) for i in range(8): self.board[i].append(Pawn(i, 6, "white")) self.board[0].append(Rook(0, 7, "white")) self.board[1].append(Knight(1, 7, "white")) self.board[2].append(Bishop(2, 7, "white")) self.board[3].append(Queen(3, 7, "white")) self.board[4].append(King(4, 7, "white")) self.board[5].append(Bishop(5, 7, "white")) self.board[6].append(Knight(6, 7, "white")) self.board[7].append(Rook(7, 7, "white"))
def rook_take_test(): print("========================================") print("Performing the rook take test") print("========================================") board = Board() piece = Rook(Colour.WHITE) current_location = Location('a', 1) board.__add_piece__(piece, current_location) friend_piece = Pawn(Colour.WHITE) enemy_piece = Pawn(Colour.BLACK) board.__add_piece__(friend_piece, Location('a', 2)) board.__add_piece__(enemy_piece, Location('b', 1)) print("Added a Rook and 2 pawns to the board...") print(board) allowed_moves = piece.allowed_moves(current_location, board) print("For a {} {} starting at position {} the moves are:".format( piece.colour, piece.name, current_location)) print(allowed_moves) new_location = allowed_moves[0] board.move_piece(piece, current_location, new_location) current_location = new_location print("Moved the {} to position {}".format(piece.name, new_location)) print(board) print("For a {} {} at position {} the moves are:".format( piece.colour, piece.name, current_location)) print(piece.allowed_moves(current_location, board))
class RookTests(unittest.TestCase): def setUp(self): chess_coord_white = ChessCoord('F', '8') self.rook_white = Rook(chess_coord_white, white) chess_coord_black = ChessCoord('E', '3') self.rook_black = Rook(chess_coord_black, black) def test_constructor_white(self): self.failUnless(self.rook_white.letter == 'R') self.failUnless(self.rook_white.chess_coord == ChessCoord('F', '8')) self.failUnless(self.rook_white.grid_coord == GridCoord(5, 7)) self.failUnless(self.rook_white.colour == white) self.failUnless(self.rook_white.symbol == '♖') self.failUnless(util.compare_lists(self.rook_white.move_directions, directions.move_directions_rook())) def test_constructor_black(self): self.failUnless(self.rook_black.letter == 'R') self.failUnless(self.rook_black.chess_coord == ChessCoord('E', '3')) self.failUnless(self.rook_black.grid_coord == GridCoord(4, 2)) self.failUnless(self.rook_black.colour == black) self.failUnless(self.rook_black.symbol == '♜') self.failUnless(util.compare_lists(self.rook_black.move_directions, directions.move_directions_rook())) def test_white_rook_allowed_to_move_south7(self): pieces = [] self.failUnless(self.rook_white.inspect_move(pieces, ChessCoord('F', '1')).is_valid_move) def test_black_rook_allowed_to_move_west4(self): pieces = [] self.failUnless(self.rook_black.inspect_move(pieces, ChessCoord('A', '3')).is_valid_move)
def init_pieces(width): return [ Rook(0, 0, PieceColor.BLACK, width), Knight(0, 1, PieceColor.BLACK, width), Bishop(0, 2, PieceColor.BLACK, width), Queen(0, 3, PieceColor.BLACK, width), King(0, 4, PieceColor.BLACK, width), Bishop(0, 5, PieceColor.BLACK, width), Knight(0, 6, PieceColor.BLACK, width), Rook(0, 7, PieceColor.BLACK, width), Pawn(1, 0, PieceColor.BLACK, width), Pawn(1, 1, PieceColor.BLACK, width), Pawn(1, 2, PieceColor.BLACK, width), Pawn(1, 3, PieceColor.BLACK, width), Pawn(1, 4, PieceColor.BLACK, width), Pawn(1, 5, PieceColor.BLACK, width), Pawn(1, 6, PieceColor.BLACK, width), Pawn(1, 7, PieceColor.BLACK, width), Pawn(6, 0, PieceColor.WHITE, width), Pawn(6, 1, PieceColor.WHITE, width), Pawn(6, 2, PieceColor.WHITE, width), Pawn(6, 3, PieceColor.WHITE, width), Pawn(6, 4, PieceColor.WHITE, width), Pawn(6, 5, PieceColor.WHITE, width), Pawn(6, 6, PieceColor.WHITE, width), Pawn(6, 7, PieceColor.WHITE, width), Rook(7, 0, PieceColor.WHITE, width), Knight(7, 1, PieceColor.WHITE, width), Bishop(7, 2, PieceColor.WHITE, width), Queen(7, 3, PieceColor.WHITE, width), King(7, 4, PieceColor.WHITE, width), Bishop(7, 5, PieceColor.WHITE, width), Knight(7, 6, PieceColor.WHITE, width), Rook(7, 7, PieceColor.WHITE, width), ]
def test_rook_threat_squares_blocking_pieces(self): queen_white = Queen(ChessCoord('A', '4'), white) rook_white = Rook(ChessCoord('F', '4'), white) pawn_black_1 = Pawn(ChessCoord('F', '5'), black, go_south) pawn_black_2 = Pawn(ChessCoord('H', '4'), black, go_south) pieces = [queen_white, pawn_black_1, pawn_black_2, rook_white] rook_white.analyze_threats_on_board_for_new_move(pieces, ChessCoord('F', '4')) expected_squares_chess = [ ChessCoord('F', '3'), # south ChessCoord('F', '2'), ChessCoord('F', '1'), ChessCoord('B', '4'), # west ChessCoord('C', '4'), ChessCoord('D', '4'), ChessCoord('E', '4'), ChessCoord('G', '4'), # east ] expected_squares_grid = map(chess_coord_to_grid_coord, expected_squares_chess) self.failUnless(util.compare_lists(expected_squares_grid, rook_white.is_threat_to_these_squares))
def custom_piece_placement(self): color = "white" p = Pawn(color) n = Knight(color) r = Rook(color) q = Queen(color) b = Bishop(color) k = King(color) self.board[7][0].add_piece(r) self.board[4][0].add_piece(k) self.board[0][0].add_piece(r) self.board[0][1].add_piece(p) color = "black" p = Pawn(color) n = Knight(color) r = Rook(color) q = Queen(color) b = Bishop(color) k = King(color) self.board[2][3].add_piece(p) self.board[5][6].add_piece(p) self.board[6][7].add_piece(q) self.board[1][5].add_piece(p)
def place_pieces(self, color): column = 0 color_string = "black" if color == Color.WHITE: column = 7 color_string = "white" self.board[column][0] = Rook(color, column, 0, images[color_string + "_rook"]) self.board[column][1] = Knight(color, column, 1, images[color_string + "_knight"]) self.board[column][2] = Bishop(color, column, 2, images[color_string + "_bishop"]) self.board[column][3] = Queen(color, column, 3, images[color_string + "_queen"]) self.board[column][4] = King(color, column, 4, images[color_string + "_king"]) self.board[column][5] = Bishop(color, column, 5, images[color_string + "_bishop"]) self.board[column][6] = Knight(color, column, 6, images[color_string + "_knight"]) self.board[column][7] = Rook(color, column, 7, images[color_string + "_rook"]) if color == Color.BLACK: column = 1 else: column = 6 for i in range(0, NUM_SQUARES): self.board[column][i] = Pawn(color, column, i, images[color_string + "_pawn"])
def king_castling_check_test(): print("========================================") print("Performing the king castling check test") print("========================================") board = Board() piece = King(Colour.WHITE) current_location = Location('e', 1) board.__add_piece__(piece, current_location) board.__add_piece__(Rook(Colour.WHITE), Location('a', 1)) board.__add_piece__(Rook(Colour.WHITE), Location('h', 1)) board.__add_piece__(Rook(Colour.BLACK), Location('e', 2)) print(board) allowed_moves = piece.allowed_moves(current_location, board) print("For a {} {} starting at position {} the moves are:".format( piece.colour, piece.name, current_location)) print(allowed_moves) new_location = allowed_moves[0] board.move_piece(piece, current_location, new_location) current_location = new_location print("Moved the {} to position {}".format(piece.name, new_location)) print(board) print("For a {} {} at position {} the moves are:".format( piece.colour, piece.name, current_location)) print(piece.allowed_moves(current_location, board))
def loadPieces(self, n, p): if (p == 'K'): self.gameTiles[n] = Tile(n, King("Black", n)) elif (p == 'k'): self.gameTiles[n] = Tile(n, King("White", n)) elif (p == 'Q'): self.gameTiles[n] = Tile(n, Queen("Black", n)) elif (p == 'q'): self.gameTiles[n] = Tile(n, Queen("White", n)) elif (p == 'N'): self.gameTiles[n] = Tile(n, Knight("Black", n)) elif (p == 'n'): self.gameTiles[n] = Tile(n, Knight("White", n)) elif (p == 'B'): self.gameTiles[n] = Tile(n, Bishop("Black", n)) elif (p == 'b'): self.gameTiles[n] = Tile(n, Bishop("White", n)) elif (p == 'R'): self.gameTiles[n] = Tile(n, Rook("Black", n)) elif (p == 'r'): self.gameTiles[n] = Tile(n, Rook("White", n)) elif (p == 'P'): self.gameTiles[n] = Tile(n, Pawn("Black", n)) elif (p == 'p'): self.gameTiles[n] = Tile(n, Pawn("White", n)) else: self.gameTiles[n] = Tile(n, NullPiece())
def setupBoard(self): self.tiles.list = [ [ Rook(0, 0, self.tiles, 0), Knight(1, 0, self.tiles, 0), Bishop(2, 0, self.tiles, 0), Queen(3, 0, self.tiles, 0), self.kings[0], Bishop(5, 0, self.tiles, 0), Knight(6, 0, self.tiles, 0), Rook(7, 0, self.tiles, 0) ], [Pawn(x, 1, self.tiles, 0) for x in range(8)], ['', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', ''], [Pawn(x, 6, self.tiles, 1) for x in range(8)], [ Rook(0, 7, self.tiles, 1), Knight(1, 7, self.tiles, 1), Bishop(2, 7, self.tiles, 1), Queen(3, 7, self.tiles, 1), self.kings[1], Bishop(5, 7, self.tiles, 1), Knight(6, 7, self.tiles, 1), Rook(7, 7, self.tiles, 1) ], ]
def parse(matrix: List[List[str]]) -> List[List[Field]]: """ Parses matrix of strings to matrix of fields and pieces on chessboard. :param matrix: matrix of strings containing string representations of pieces and fields. :return: matrix of fields and chess pieces representing chessboard. """ chessboard = list() for i in range(len(matrix)): chessboard.append(list()) for j in range(len(matrix[i])): chessboard[i].append({ '--': Field(i, j), 'wW': King(i, j, Color.WHITE), 'bW': King(i, j, Color.BLACK), 'wq': Queen(i, j, Color.WHITE), 'bq': Queen(i, j, Color.BLACK), 'wb': Bishop(i, j, Color.WHITE), 'bb': Bishop(i, j, Color.BLACK), 'wk': Knight(i, j, Color.WHITE), 'bk': Knight(i, j, Color.BLACK), 'wr': Rook(i, j, Color.WHITE), 'br': Rook(i, j, Color.BLACK), 'wp': Pawn(i, j, Color.WHITE), 'bp': Pawn(i, j, Color.BLACK) }[matrix[i][j]]) return chessboard
def switchOutPawn(currPiece, screen): screen.blit(switchText, (75, 280)) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: if currPiece.color: return Queen(currPiece.x, currPiece.y, "images/whiteQueen.png", 1) else: return Queen(currPiece.x, currPiece.y, "images/blackQueen.png", 0) if event.key == pygame.K_r: if currPiece.color: return Rook(currPiece.x, currPiece.y, "images/whiteRook.png", 1) else: return Rook(currPiece.x, currPiece.y, "images/blackRook.png", 0) if event.key == pygame.K_b: if currPiece.color: return Bishop(currPiece.x, currPiece.y, "images/whiteBishop.png", 1) else: return Bishop(currPiece.x, currPiece.y, "images/blackBishop.png", 0) if event.key == pygame.K_k: if currPiece.color: return Knight(currPiece.x, currPiece.y, "images/whiteKnight.png", 1) else: return Knight(currPiece.x, currPiece.y, "images/blackKnight.png", 0)
def _populate_major_and_minor(self, row, color): self.grid[row][4] = King(self, [row, 4], color) self.grid[row][3] = Queen(self, [row, 3], color) self.grid[row][2] = Bishop(self, [row, 2], color) self.grid[row][5] = Bishop(self, [row, 5], color) self.grid[row][0] = Rook(self, [row, 0], color) self.grid[row][7] = Rook(self, [row, 7], color) self.grid[row][1] = Knight(self, [row, 1], color) self.grid[row][6] = Knight(self, [row, 6], color)
def __init__(self): self.state = [ [ Lance(p1), Knight(p1), Silver(p1), Gold(p1), King(p1), Gold(p1), Silver(p1), Knight(p1), Lance(p1) ], [None, Rook(p1), None, None, None, None, None, Bishop(p1), None], [ Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1), Pawn(p1) ], [None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None, None], [ Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2), Pawn(p2) ], [None, Bishop(p2), None, None, None, None, None, Rook(p2), None], [ Lance(p2), Knight(p2), Silver(p2), Gold(p2), King(p2), Gold(p2), Silver(p2), Knight(p2), Lance(p2) ], ]
def test_taking_king_not_allowed_further_away(self): c4_rook = Rook(ChessCoord('C', '4'), black) c1_king = King(ChessCoord('C', '1'), white) pieces = [c4_rook, c1_king] move_inspect_result = c4_rook.inspect_move(pieces, ChessCoord('C', '1')) self.failUnless(move_inspect_result == MoveInspectResult(False, True, [GridCoord(2, 2), GridCoord(2, 1)], c1_king))
def test_castling_should_not_be_possible_if_rook_has_moved(self): e1_king = King(ChessCoord('E', '1'), white) h1_rook = Rook(ChessCoord('H', '1'), white) h1_rook.update_coord(ChessCoord('H', '2')) pieces = [e1_king, h1_rook] inspect_move_result = e1_king.inspect_move(pieces, ChessCoord('G', '1')) self.failUnless(inspect_move_result == CastlingMoveInspectResult(False, False, True, False, None, ChessCoord('F', '1')))
def get_valid_moves(self, board): moves = [] diagonal_moves = Bishop.get_valid_moves( Bishop(self.row, self.col, self.color), board) straight_moves = Rook.get_valid_moves( Rook(self.row, self.col, self.color), board) moves.extend(diagonal_moves) moves.extend(straight_moves) return moves
def getLegalMoves(self, board): rookQ = Rook(self.color) rookQ.position = self.position legalRook = rookQ.getLegalMoves(board) bishopQ = Bishop(self.color) bishopQ.position = self.position legalBishop = bishopQ.getLegalMoves(board) self.LegalMovesList = [ legalRook[0] + legalBishop[0], legalRook[1] + legalBishop[1] ] return self.LegalMovesList
def create_board(self): # A chess board is constitued of 64 tiles for tile in range(64): self.gameTiles[tile] = Tile(tile, NullPiece()) # Placing the pieces on the board # Placing black pices # First Line self.gameTiles[0] = Tile(0, Rook("Black", 0)) self.gameTiles[1] = Tile(1, Knight("Black", 1)) self.gameTiles[2] = Tile(2, Bishop("Black", 2)) self.gameTiles[3] = Tile(3, Queen("Black", 3)) self.gameTiles[4] = Tile(4, King("Black", 4)) self.gameTiles[5] = Tile(5, Bishop("Black", 5)) self.gameTiles[6] = Tile(6, Knight("Black", 6)) self.gameTiles[7] = Tile(7, Rook("Black", 7)) self.gameTiles[8] = Tile(8, Pawn("Black", 8)) self.gameTiles[9] = Tile(9, Pawn("Black", 9)) # Second Line self.gameTiles[10] = Tile(10, Pawn("Black", 10)) self.gameTiles[11] = Tile(11, Pawn("Black", 11)) self.gameTiles[12] = Tile(12, Pawn("Black", 12)) self.gameTiles[13] = Tile(13, Pawn("Black", 13)) self.gameTiles[14] = Tile(14, Pawn("Black", 14)) self.gameTiles[15] = Tile(15, Pawn("Black", 15)) # Placing the white pieces # First line self.gameTiles[48] = Tile(48, Pawn("White", 48)) self.gameTiles[49] = Tile(49, Pawn("White", 49)) self.gameTiles[50] = Tile(50, Pawn("White", 50)) self.gameTiles[51] = Tile(51, Pawn("White", 51)) self.gameTiles[52] = Tile(52, Pawn("White", 52)) self.gameTiles[53] = Tile(53, Pawn("White", 53)) self.gameTiles[54] = Tile(54, Pawn("White", 54)) self.gameTiles[55] = Tile(55, Pawn("White", 55)) # Second line self.gameTiles[56] = Tile(56, Rook("White", 56)) self.gameTiles[57] = Tile(57, Knight("White", 57)) self.gameTiles[58] = Tile(58, Bishop("White", 58)) self.gameTiles[59] = Tile(59, Queen("White", 59)) self.gameTiles[60] = Tile(60, King("White", 60)) self.gameTiles[61] = Tile(61, Bishop("White", 61)) self.gameTiles[62] = Tile(62, Knight("White", 62)) self.gameTiles[63] = Tile(63, Rook("White", 63))
def createBoard(self): square_color = ["light", "dark"] x = 0 for i in range(64): self.squares[i] = Square(square_color[x % 2], i, nullPiece()) if i % 8 == 0 and i != 0: x += 2 else: x += 1 self.squares[0] = Square("light", 0, Rook("Black", 0)) self.squares[1] = Square("dark", 1, Knight("Black", 1)) self.squares[2] = Square("light", 2, Bishop("Black", 2)) self.squares[3] = Square("dark", 3, Queen("Black", 3)) self.squares[4] = Square("light", 4, self.black_king) self.squares[5] = Square("dark", 5, Bishop("Black", 5)) self.squares[6] = Square("light", 6, Knight("Black", 6)) self.squares[7] = Square("dark", 7, Rook("Black", 7)) self.squares[8] = Square("dark", 8, Pawn("Black", 8)) self.squares[9] = Square("light", 9, Pawn("Black", 9)) self.squares[10] = Square("dark", 10, Pawn("Black", 10)) self.squares[11] = Square("light", 11, Pawn("Black", 11)) self.squares[12] = Square("dark", 12, Pawn("Black", 12)) self.squares[13] = Square("light", 13, Pawn("Black", 13)) self.squares[14] = Square("dark", 14, Pawn("Black", 14)) self.squares[15] = Square("light", 15, Pawn("Black", 15)) self.squares[16] = Square("light", 16, nullPiece()) self.squares[24] = Square("dark", 24, nullPiece()) self.squares[32] = Square("light", 32, nullPiece()) self.squares[40] = Square("dark", 40, nullPiece()) self.squares[48] = Square("light", 48, Pawn("White", 48)) self.squares[49] = Square("dark", 49, Pawn("White", 49)) self.squares[50] = Square("light", 50, Pawn("White", 50)) self.squares[51] = Square("dark", 51, Pawn("White", 51)) self.squares[52] = Square("light", 52, Pawn("White", 52)) self.squares[53] = Square("dark", 53, Pawn("White", 53)) self.squares[54] = Square("light", 54, Pawn("White", 54)) self.squares[55] = Square("dark", 55, Pawn("White", 55)) self.squares[56] = Square("dark", 56, Rook("White", 56)) self.squares[57] = Square("light", 57, Knight("White", 57)) self.squares[58] = Square("dark", 58, Bishop("White", 58)) self.squares[59] = Square("light", 59, Queen("White", 59)) self.squares[60] = Square("dark", 60, self.white_king) self.squares[61] = Square("light", 61, Bishop("White", 61)) self.squares[62] = Square("dark", 62, Knight("White", 62)) self.squares[63] = Square("light", 63, Rook("White", 63))
def createBoard(self): for c in range(65): self.gameTiles.append(None) for tile in range(65): self.gameTiles[tile] = Tile(tile, NullPiece()) self.gameTiles[1] = Tile(1, Rook(1, "Black")) self.gameTiles[2] = Tile(2, Knight(2, "Black")) self.gameTiles[3] = Tile(3, Bishop(3, "Black")) self.gameTiles[4] = Tile(4, Queen(4, "Black")) self.gameTiles[5] = Tile(5, King(5, "Black")) self.gameTiles[6] = Tile(6, Bishop(6, "Black")) self.gameTiles[7] = Tile(7, Knight(7, "Black")) self.gameTiles[8] = Tile(8, Rook(8, "Black")) self.gameTiles[9] = Tile(9, Pawn(9, "Black")) self.gameTiles[10] = Tile(10, Pawn(10, "Black")) self.gameTiles[11] = Tile(11, Pawn(11, "Black")) self.gameTiles[12] = Tile(12, Pawn(12, "Black")) self.gameTiles[13] = Tile(13, Pawn(13, "Black")) self.gameTiles[14] = Tile(14, Pawn(14, "Black")) self.gameTiles[15] = Tile(15, Pawn(15, "Black")) self.gameTiles[16] = Tile(16, Pawn(16, "Black")) #self.gameTiles[35] = Tile(35, Bishop(35, "White")) #self.gameTiles[14] = Tile(14,Queen(14, "White")) self.gameTiles[49] = Tile(49, Pawn(49, "White")) self.gameTiles[50] = Tile(50, Pawn(50, "White")) self.gameTiles[51] = Tile(51, Pawn(51, "White")) self.gameTiles[52] = Tile(52, Pawn(52, "White")) self.gameTiles[53] = Tile(53, Pawn(53, "White")) self.gameTiles[54] = Tile(54, Pawn(54, "White")) self.gameTiles[55] = Tile(55, Pawn(55, "White")) self.gameTiles[56] = Tile(56, Pawn(56, "White")) self.gameTiles[57] = Tile(57, Rook(57, "White")) self.gameTiles[58] = Tile(58, Knight(58, "White")) self.gameTiles[59] = Tile(59, Bishop(59, "White")) self.gameTiles[60] = Tile(60, Queen(60, "White")) self.gameTiles[61] = Tile(61, King(61, "White")) self.gameTiles[62] = Tile(62, Bishop(62, "White")) self.gameTiles[63] = Tile(63, Knight(63, "White")) self.gameTiles[64] = Tile(64, Rook(64, "White"))
def valid_moves(self, board: Board) -> List[str]: valid = [] valid += Rook.valid_moves(self, board) valid += Bishop.valid_moves(self, board) return valid
def __init__(self): pygame.font.init() self.width = 400 self.height = 500 self.win = pygame.display.set_mode((self.width, self.height)) self.squares = [[0 for x in range(8)] for y in range(8)] self.square_width = 50 self.square_height = 50 self.white_pieces = [Pawn(0) for x in range(8)] + [Rook(0), Knight(0), Bishop(0), Queen(0), King(0), Bishop(0), Knight(0), Rook(0)] self.black_pieces = [Pawn(1) for x in range(8)] + [Rook(1), Knight(1), Bishop(1), Queen(1), King(1), Bishop(1), Knight(1), Rook(1)] self.current_player = 0 self.chosen = None self.big_font = pygame.font.SysFont('Comic Sans MS', 30, bold=True) self.small_font = pygame.font.SysFont('Comic Sans MS', 20, bold=True) pygame.display.set_caption("pyChess") pygame.display.set_icon(pygame.transform.scale(pygame.image.load("assets/pieces/black/black_king.png"), (32, 32)))
def test_rook(self): b = board.Board() b.set_piece((1, 1), board.Piece(board.PieceType.ROOK, True)) self.assertItemsEqual([(0, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7)], Rook.get_moves((1, 1), b[(1, 1)], b))
def promote(self, x, y, alliance, piece): if (piece == "Q"): self.board[x][y] = Tile(Queen(alliance, x, y)) if (piece == "B"): self.board[x][y] = Tile(Bishop(alliance, x, y)) if (piece == "R"): self.board[x][y] = Tile(Rook(alliance, x, y)) if (piece == "N"): self.board[x][y] = Tile(Knight(alliance, x, y))
def build_black_pieces(): black_pieces = [] # rooks black_pieces.append(Rook(BLACK, Coordinate(0, 0))) black_pieces.append(Rook(BLACK, Coordinate(7, 0))) # knights black_pieces.append(Knight(BLACK, Coordinate(1, 0))) black_pieces.append(Knight(BLACK, Coordinate(6, 0))) # bishops black_pieces.append(Bishop(BLACK, Coordinate(2, 0))) black_pieces.append(Bishop(BLACK, Coordinate(5, 0))) # queen black_pieces.append(Queen(BLACK, Coordinate(3, 0))) # king black_pieces.append(King(BLACK, Coordinate(4, 0))) # pawns for x in range(8): black_pieces.append(Pawn(BLACK, Coordinate(x, 1))) return black_pieces
def build_white_pieces(): white_pieces = [] # rooks white_pieces.append(Rook(WHITE, Coordinate(0, 7))) white_pieces.append(Rook(WHITE, Coordinate(7, 7))) # knights white_pieces.append(Knight(WHITE, Coordinate(1, 7))) white_pieces.append(Knight(WHITE, Coordinate(6, 7))) # bishops white_pieces.append(Bishop(WHITE, Coordinate(2, 7))) white_pieces.append(Bishop(WHITE, Coordinate(5, 7))) # queen white_pieces.append(Queen(WHITE, Coordinate(3, 7))) # king white_pieces.append(King(WHITE, Coordinate(4, 7))) # pawns for x in range(8): white_pieces.append(Pawn(WHITE, Coordinate(x, 6))) return white_pieces
def reset_pieces(self): # p2 = Pawn("black") # b.board[5][6].add_piece(p) # WHITE PAWNS color = "white" for i in range(Config.BOARD_SIZE): p = Pawn(color) self.board[i][6].add_piece(p) # Other white pieces pieces = [ Rook(color), Knight(color), Bishop(color), Queen(color), King(color), Bishop(color), Knight(color), Rook(color) ] for i in range(len(pieces)): self.board[i][7].add_piece(pieces[i]) # BLACK PAWNS color = "black" for i in range(Config.BOARD_SIZE): p = Pawn(color) self.board[i][1].add_piece(p) # Other black pieces pieces = [ Rook(color), Knight(color), Bishop(color), Queen(color), King(color), Bishop(color), Knight(color), Rook(color) ] for i in range(len(pieces)): self.board[i][0].add_piece(pieces[i])
def generate_moves_for_piece( color: int, position: tuple[int, int], board: list[list[Piece]], only_captures: bool = False) -> list[tuple[int, int]]: moves = Rook.generate_moves_for_piece(color, position, board, only_captures) moves.extend( Bishop.generate_moves_for_piece(color, position, board, only_captures)) return moves
def populate_board(self): # self.board[0][0] = Rook(0, 0, "black") # self.board[1][0] = Knight(1, 0, "black") # self.board[2][0] = Bishop(2, 0, "black") # self.board[4][0] = Queen(4, 0, "black") # self.board[3][0] = King(3, 0, "black") # self.board[5][0] = Bishop(5, 0, "black") # self.board[6][0] = Knight(6, 0, "black") # self.board[7][0] = Rook(7, 0, "black") # for i in range(8): # self.board[i][1] = Pawn(i, 1, "black") # self.board[i][6] = Pawn(i, 6, "white") # self.board[0][7] = Rook(0, 7, "white") # self.board[1][7] = Knight(1, 7, "white") # self.board[2][7] = Bishop(2, 7, "white") # self.board[4][7] = Queen(4, 7, "white") # self.board[3][7] = King(3, 7, "white") # self.board[5][7] = Bishop(5, 7, "white") # self.board[6][7] = Knight(6, 7, "white") # self.board[7][7] = Rook(7, 7, "white") 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, "black") 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") for i in range(8): self.board[1][i] = Pawn(1, i, "black") self.board[6][i] = Pawn(6, i, "white") 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")
def is_valid_move(self, board, startx, starty, endx, endy, evaluate_only=True): if self.color == "white": look_for_color = "black" else: look_for_color = "white" if (startx == endx or startx+1 == endx or startx-1 == endx) and (starty + 1== endy or starty - 1 == endy or starty==endy): if Bishop.is_valid_move(self, board, startx, starty, endx, endy) or Rook.is_valid_move(self, board, startx, starty, endx, endy): if not evaluate_only: self.moved = True if board.check_if_tile_under_attack(endx,endy,look_for_color): return False return True elif starty == endy and abs(endx-startx) == 2: # F*****G CASTLING if not self.moved: look_for_color = None if endx < startx: # LEFT # print("Castle to the left") if not board.check_if_tile_under_attack(startx, starty, look_for_color): if not board.check_if_tile_under_attack(startx - 1, starty, look_for_color) and board.is_tile_free(startx - 1, starty): if not board.check_if_tile_under_attack(startx - 2, starty,look_for_color) and board.is_tile_free(startx - 2,starty): if board.is_tile_free(startx-3,starty): other_piece = board.get_piece_at(0, starty) if other_piece is not None and not other_piece.moved and isinstance(other_piece, Rook): if not evaluate_only: board.board[startx - 4][starty].remove_piece() board.board[startx - 1][endy].add_piece(other_piece) if board.check_if_tile_under_attack(endx, endy, look_for_color): return False return True if startx < endx: # RIGHT # print("Castle to the right") if not board.check_if_tile_under_attack(startx,starty,look_for_color): if not board.check_if_tile_under_attack(startx+1,starty,look_for_color) and board.is_tile_free(startx+1,starty): if not board.check_if_tile_under_attack(startx+2,starty,look_for_color) and board.is_tile_free(startx+2,starty): other_piece = board.get_piece_at(7,starty) if other_piece is not None and not other_piece.moved and isinstance(other_piece,Rook): if not evaluate_only: print("Yes move it!") board.board[startx+3][starty].remove_piece() board.board[startx+1][endy].add_piece(other_piece) if board.check_if_tile_under_attack(endx, endy, look_for_color): return False return True else: return False
def roque_menor(self,cor_do_jogador): ut = util() cor_do_jogador = cor_do_jogador.upper() if cor_do_jogador == "WHITE": if (self.contagem_movimento_rei_brancas > 0) or (self.contagem_movimento_torre_rm_brancas > 0) or (ut.peca_ameacada(62,cor_do_jogador)) or (ut.peca_ameacada(63,cor_do_jogador)) or (type(self.gameTiles[62].pieceOnTile) is not NullPiece ) or (type(self.gameTiles[63].pieceOnTile) is not NullPiece) or (ut.xeque(cor_do_jogador)): return True else: self.gameTiles[63] = Tile(63, King(63, "White")) self.gameTiles[62] = Tile(62, Rook(62, "White")) self.gameTiles[61] = Tile(61, NullPiece()) self.gameTiles[64] = Tile(64, NullPiece()) return False if cor_do_jogador == 'BLACK': if (self.contagem_movimento_rei_pretas > 0) or (self.contagem_movimento_torre_rm_pretas > 0) or (ut.peca_ameacada(6,cor_do_jogador)) or (ut.peca_ameacada(7,cor_do_jogador)) or (type(self.gameTiles[6].pieceOnTile) is not NullPiece) or (type(self.gameTiles[7].pieceOnTile) is not NullPiece) or (ut.xeque(cor_do_jogador)): return True else: self.gameTiles[6] = Tile(6,Rook(6,"Black")) self.gameTiles[7] = Tile(7, King(7, "Black")) self.gameTiles[5] = Tile(5, NullPiece()) self.gameTiles[8] = Tile(8, NullPiece()) return False
def roque_maior(self,cor_do_jogador): ut = util() cor_do_jogador = cor_do_jogador.upper() if cor_do_jogador == "WHITE": if (self.contagem_movimento_rei_brancas > 0) or (self.contagem_movimento_torre_rma_brancas > 0) or (ut.peca_ameacada(60,cor_do_jogador)) or (ut.peca_ameacada(59,cor_do_jogador)) or (ut.peca_ameacada(58,cor_do_jogador)) or (type(self.gameTiles[60].pieceOnTile) is not NullPiece ) or (type(self.gameTiles[59].pieceOnTile) is not NullPiece) or (type(self.gameTiles[58].pieceOnTile) is not NullPiece) or (ut.xeque(cor_do_jogador)): return True else: self.gameTiles[59] = Tile(59, King(59, "White")) self.gameTiles[60] = Tile(60, Rook(60, "White")) self.gameTiles[61] = Tile(61, NullPiece()) self.gameTiles[57] = Tile(64, NullPiece()) return False if cor_do_jogador == 'BLACK': if (self.contagem_movimento_rei_pretas > 0) or (self.contagem_movimento_torre_rma_pretas > 0) or (ut.peca_ameacada(4,cor_do_jogador)) or (ut.peca_ameacada(3,cor_do_jogador)) or (ut.peca_ameacada(2,cor_do_jogador)) or (type(self.gameTiles[4].pieceOnTile) is not NullPiece) or (type(self.gameTiles[3].pieceOnTile) is not NullPiece) or (type(self.gameTiles[2].pieceOnTile) is not NullPiece) or (ut.xeque(cor_do_jogador)): return True else: self.gameTiles[4] = Tile(4,Rook(4,"Black")) self.gameTiles[3] = Tile(3, King(3, "Black")) self.gameTiles[5] = Tile(5, NullPiece()) self.gameTiles[1] = Tile(1, NullPiece()) return False
def __add_inital_pieces__(board, colour): first_row = 1 if colour == Colour.WHITE else 8 second_row = 2 if colour == Colour.WHITE else 7 board.__add_piece__(Rook(colour), Location('a', first_row)) board.__add_piece__(Knight(colour), Location('b', first_row)) board.__add_piece__(Bishop(colour), Location('c', first_row)) board.__add_piece__(Queen(colour), Location('d', first_row)) board.__add_piece__(King(colour), Location('e', first_row)) board.__add_piece__(Bishop(colour), Location('f', first_row)) board.__add_piece__(Knight(colour), Location('g', first_row)) board.__add_piece__(Rook(colour), Location('h', first_row)) board.__add_piece__(Pawn(colour), Location('a', second_row)) board.__add_piece__(Pawn(colour), Location('b', second_row)) board.__add_piece__(Pawn(colour), Location('c', second_row)) board.__add_piece__(Pawn(colour), Location('d', second_row)) board.__add_piece__(Pawn(colour), Location('e', second_row)) board.__add_piece__(Pawn(colour), Location('f', second_row)) board.__add_piece__(Pawn(colour), Location('g', second_row)) board.__add_piece__(Pawn(colour), Location('h', second_row))
def setUp(self): chess_coord_white = ChessCoord('F', '8') self.rook_white = Rook(chess_coord_white, white) chess_coord_black = ChessCoord('E', '3') self.rook_black = Rook(chess_coord_black, black)