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 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 promote(self, promoting_pieces_square, player_chosen_piece=None): #ask the player what piece they would like to promote to #return a new instantiation of that piece type new_piece = Queen(promoting_pieces_square.pieceOnTile.alliance, promoting_pieces_square.pieceOnTile.position) new_piece.has_moved = True return (new_piece)
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 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 __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 setUp(self): self.queen_white = Queen(ChessCoord('E', '6'), white) self.queen_black = Queen(ChessCoord('G', '5'), black) self.rook_white = Rook(ChessCoord('D', '1'), white) self.knight_white = Knight(ChessCoord('G', '1'), white) self.pieces = [self.queen_white, self.queen_black, self.rook_white, self.knight_white]
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 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 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 test_queen_get_possible_moves(self): matrix = PieceTest.initialize_empty_grid() queen = Queen('black', 'black_queen') positions = [(0, 0), (0, 7), (3, 4), (7, 0), (7, 7)] expectations = { (0, 0): [(0, 1, 'mov'), (0, 2, 'mov'), (0, 3, 'mov'), (0, 4, 'mov'), (0, 5, 'mov'), (0, 6, 'mov'), (0, 7, 'mov'), (1, 0, 'mov'), (2, 0, 'mov'), (3, 0, 'mov'), (4, 0, 'mov'), (5, 0, 'mov'), (6, 0, 'mov'), (7, 0, 'mov'), (1, 1, 'mov'), (2, 2, 'mov'), (3, 3, 'mov'), (4, 4, 'mov'), (5, 5, 'mov'), (6, 6, 'mov'), (7, 7, 'mov')], (0, 7): [(0, 6, 'mov'), (0, 5, 'mov'), (0, 4, 'mov'), (0, 3, 'mov'), (0, 2, 'mov'), (0, 1, 'mov'), (0, 0, 'mov'), (1, 7, 'mov'), (2, 7, 'mov'), (3, 7, 'mov'), (4, 7, 'mov'), (5, 7, 'mov'), (6, 7, 'mov'), (7, 7, 'mov'), (1, 6, 'mov'), (2, 5, 'mov'), (3, 4, 'mov'), (4, 3, 'mov'), (5, 2, 'mov'), (6, 1, 'mov'), (7, 0, 'mov')], (3, 4): [(3, 3, 'mov'), (3, 2, 'mov'), (3, 1, 'mov'), (3, 0, 'mov'), (3, 5, 'mov'), (3, 6, 'mov'), (3, 7, 'mov'), (2, 4, 'mov'), (1, 4, 'mov'), (0, 4, 'mov'), (4, 4, 'mov'), (5, 4, 'mov'), (6, 4, 'mov'), (7, 4, 'mov'), (2, 3, 'mov'), (1, 2, 'mov'), (0, 1, 'mov'), (2, 5, 'mov'), (1, 6, 'mov'), (0, 7, 'mov'), (4, 3, 'mov'), (5, 2, 'mov'), (6, 1, 'mov'), (7, 0, 'mov'), (4, 5, 'mov'), (5, 6, 'mov'), (6, 7, 'mov')], (7, 0): [(7, 1, 'mov'), (7, 2, 'mov'), (7, 3, 'mov'), (7, 4, 'mov'), (7, 5, 'mov'), (7, 6, 'mov'), (7, 7, 'mov'), (6, 0, 'mov'), (5, 0, 'mov'), (4, 0, 'mov'), (3, 0, 'mov'), (2, 0, 'mov'), (1, 0, 'mov'), (0, 0, 'mov'), (6, 1, 'mov'), (5, 2, 'mov'), (4, 3, 'mov'), (3, 4, 'mov'), (2, 5, 'mov'), (1, 6, 'mov'), (0, 7, 'mov')], (7, 7): [ (7, 6, 'mov'), (7, 5, 'mov'), (7, 4, 'mov'), (7, 3, 'mov'), (7, 2, 'mov'), (7, 1, 'mov'), (7, 0, 'mov'), (6, 7, 'mov'), (5, 7, 'mov'), (4, 7, 'mov'), (3, 7, 'mov'), (2, 7, 'mov'), (1, 7, 'mov'), (0, 7, 'mov'), (6, 6, 'mov'), (5, 5, 'mov'), (4, 4, 'mov'), (3, 3, 'mov'), (2, 2, 'mov'), (1, 1, 'mov'), (0, 0, 'mov') ] } for pos in positions: possible_moves = queen.get_possible_moves(pos, matrix) self.assertCountEqual(possible_moves, expectations[pos])
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 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 simulate_move(self, piece, board, game, move, color): target = board.get_piece(move[0], move[1]) board.prev_square = (piece.row, piece.col) board.piece = piece board.target = (move[0], move[1]) board.captured_piece = target # Castling if isinstance(piece, King) and isinstance( target, Rook) and piece.color == target.color: dangerous_squares = game.get_dangerous_squares() game.castle(piece, target, move[0], move[1], dangerous_squares, board) else: if target != 0 and target.color != color: board.board[move[0]][move[1]] = 0 board.move(piece, move[0], move[1]) # Promote the pawn if game.detect_promotion(piece): board.board[piece.row][piece.col] = Queen( piece.row, piece.col, piece.color) # If a rook or king moves, it loses castling privileges if isinstance(piece, (Rook, King)): piece.can_castle = False return board
def test_queen_threat_blocking_pieces(self): queen_white = Queen(ChessCoord('E', '6'), white) rook_white = Rook(ChessCoord('E', '7'), white) pawn_black = Pawn(ChessCoord('A', '1'), black, go_south) pieces = [queen_white, pawn_black, rook_white] queen_white.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '5')) expected_squares_chess = [ ChessCoord('E', '6'), # north # rest blocked ChessCoord('E', '4'), # south ChessCoord('E', '3'), ChessCoord('E', '2'), ChessCoord('E', '1'), ChessCoord('D', '5'), # east ChessCoord('C', '5'), ChessCoord('B', '5'), ChessCoord('A', '5'), ChessCoord('F', '5'), # west ChessCoord('G', '5'), ChessCoord('H', '5'), ChessCoord('D', '4'), # south west ChessCoord('C', '3'), ChessCoord('B', '2'), # blocked: ChessCoord('A', '1'), ChessCoord('D', '6'), # north west ChessCoord('C', '7'), ChessCoord('B', '8'), ChessCoord('F', '6'), # north east ChessCoord('G', '7'), ChessCoord('H', '8'), ChessCoord('F', '4'), # south east ChessCoord('G', '3'), ChessCoord('H', '2'), ] expected_squares_grid = map(chess_coord_to_grid_coord, expected_squares_chess) self.failUnless(util.compare_lists(expected_squares_grid, queen_white.is_threat_to_these_squares))
def test_queen_threat_empty_board(self): queen_white = Queen(ChessCoord('E', '6'), white) pieces = [queen_white] queen_white.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '5')) expected_squares_chess = [ ChessCoord('E', '6'), # north ChessCoord('E', '7'), ChessCoord('E', '8'), ChessCoord('E', '4'), # south ChessCoord('E', '3'), ChessCoord('E', '2'), ChessCoord('E', '1'), ChessCoord('D', '5'), # east ChessCoord('C', '5'), ChessCoord('B', '5'), ChessCoord('A', '5'), ChessCoord('F', '5'), # west ChessCoord('G', '5'), ChessCoord('H', '5'), ChessCoord('D', '4'), # south west ChessCoord('C', '3'), ChessCoord('B', '2'), ChessCoord('A', '1'), ChessCoord('D', '6'), # north west ChessCoord('C', '7'), ChessCoord('B', '8'), ChessCoord('F', '6'), # north east ChessCoord('G', '7'), ChessCoord('H', '8'), ChessCoord('F', '4'), # south east ChessCoord('G', '3'), ChessCoord('H', '2'), ] expected_squares_grid = map(chess_coord_to_grid_coord, expected_squares_chess) self.failUnless(util.compare_lists(expected_squares_grid, queen_white.is_threat_to_these_squares))
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 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 handle_piece_move(self, piece, destination): allowed = self.try_move(piece, destination) king = self.get_friendly_king() if allowed: king.put_outof_check() king.marked = False self.board[piece.x][piece.y] = 0 piece.move((destination[0], destination[1])) # queeening if isinstance(piece, Pawn) and (destination[0] == 0 or destination[0] == 7): piece = Queen(destination[0], destination[1], piece.player) self.board[destination[0]][destination[1]] = piece self.change_turn() else: piece.unselect() king.marked = True # to indicante why we couldn't take the move
class QueenTests(unittest.TestCase): def setUp(self): chess_coord_white = ChessCoord('C', '2') self.queen_white = Queen(chess_coord_white, white) chess_coord_black = ChessCoord('F', '5') self.queen_black = Queen(chess_coord_black, black) def test_constructor_white(self): self.failUnless(self.queen_white.letter == 'Q') self.failUnless(self.queen_white.chess_coord == ChessCoord('C', '2')) self.failUnless(self.queen_white.grid_coord == GridCoord(2, 1)) self.failUnless(self.queen_white.colour == white) self.failUnless(self.queen_white.symbol == '♕') self.failUnless(util.compare_lists(self.queen_white.move_directions, directions.move_directions_queen())) def test_constructor_black(self): self.failUnless(self.queen_black.letter == 'Q') self.failUnless(self.queen_black.chess_coord == ChessCoord('F', '5')) self.failUnless(self.queen_black.grid_coord == GridCoord(5, 4)) self.failUnless(self.queen_black.colour == black) self.failUnless(self.queen_black.symbol == '♛') self.failUnless(util.compare_lists(self.queen_black.move_directions, directions.move_directions_queen())) def test_white_queen_allowed_to_move_north2(self): pieces = [] self.failUnless(self.queen_white.inspect_move(pieces, ChessCoord('C', '4')).is_valid_move) def test_white_queen_should_not_be_allowed_move_north2_east1(self): pieces = [] self.failIf(self.queen_white.inspect_move(pieces, ChessCoord('D', '4')).is_valid_move) def test_white_queen_allowed_to_move_north3_east3(self): pieces = [] self.failUnless(self.queen_white.inspect_move(pieces, ChessCoord('F', '5')).is_valid_move) def test_white_queen_allowed_to_move_south1_west1(self): pieces = [] self.failUnless(self.queen_white.inspect_move(pieces, ChessCoord('B', '1')).is_valid_move)
def pawn_to_queen(self, screen, pawn): pawn_y = pawn.column pawn_x = pawn.row if pawn.color == Color.BLACK and pawn_y == NUM_SQUARES - 1: self.black_pieces.remove(pawn) erase_square(screen, pawn) self.board[pawn_y][pawn_x] = Queen(Color.BLACK, pawn_y, pawn_x, images["black_queen"]) self.black_pieces.append(self.board[pawn_y][pawn_x]) self.board[pawn_y][pawn_x].draw(screen, length) elif pawn.color == Color.WHITE and pawn_y == 0: self.white_pieces.remove(pawn) erase_square(screen, pawn) self.board[pawn_y][pawn_x] = Queen(Color.WHITE, pawn_y, pawn_x, images["white_queen"]) self.white_pieces.append(self.board[pawn_y][pawn_x]) self.board[pawn_y][pawn_x].draw(screen, length) pawn = self.board[pawn_y][pawn_x] return pawn
def test_queen(self): b = board.Board() b.set_piece((1, 1), board.Piece(board.PieceType.QUEEN, 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), (0, 0), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (0, 2), (2, 0)], Queen.get_moves((1, 1), b[(1, 1)], b))
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 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 createBoard(self): for rows in range(8): for cols in range(8): self.board[rows][cols] = Tile(nullPiece()) #Pawn create for cols in range(8): self.board[1][cols] = Tile(Pawn("B", 1, cols)) for cols in range(8): self.board[6][cols] = Tile(Pawn("W", 6, cols)) #create King self.board[0][4] = Tile(King("B", 0, 4)) self.board[7][4] = Tile(King("W", 7, 4)) # create Queen self.board[0][3] = Tile(Queen("B", 0, 3)) self.board[7][3] = Tile(Queen("W", 7, 3)) # create Rock self.board[0][0] = Tile(Rook("B", 0, 0)) self.board[0][7] = Tile(Rook("B", 0, 7)) self.board[7][0] = Tile(Rook("W", 7, 0)) self.board[7][7] = Tile(Rook("W", 7, 7)) # create Bishop self.board[0][2] = Tile(Bishop("B", 0, 2)) self.board[0][5] = Tile(Bishop("B", 0, 5)) self.board[7][2] = Tile(Bishop("W", 7, 2)) self.board[7][5] = Tile(Bishop("W", 7, 5)) # create Knight self.board[0][1] = Tile(Knight("B", 0, 1)) self.board[0][6] = Tile(Knight("B", 0, 6)) self.board[7][1] = Tile(Knight("W", 7, 1)) self.board[7][6] = Tile(Knight("W", 7, 6))
def createBoard(self): for square in range(64): self.squares[square] = Square(square, EmptyPiece()) self.squares[0] = Square(0, Rook("Black", 0)) self.squares[1] = Square(1, Knight("Black", 1)) self.squares[2] = Square(2, Bishop("Black", 2)) self.squares[3] = Square(3, Queen("Black", 3)) self.squares[4] = Square(4, King("Black", 4)) self.squares[5] = Square(5, Bishop("Black", 5)) self.squares[6] = Square(6, Knight("Black", 6)) self.squares[7] = Square(7, Rook("Black", 7)) self.squares[8] = Square(8, Pawn("Black", 8)) self.squares[9] = Square(9, Pawn("Black", 9)) self.squares[10] = Square(10, Pawn("Black", 10)) self.squares[11] = Square(11, Pawn("Black", 11)) self.squares[12] = Square(12, Pawn("Black", 12)) self.squares[13] = Square(13, Pawn("Black", 13)) self.squares[14] = Square(14, Pawn("Black", 14)) self.squares[15] = Square(15, Pawn("Black", 15)) self.squares[56] = Square(56, Rook("White", 56)) self.squares[57] = Square(57, Knight("White", 57)) self.squares[58] = Square(58, Bishop("White", 58)) self.squares[59] = Square(59, Queen("White", 59)) self.squares[60] = Square(60, King("White", 60)) self.squares[61] = Square(61, Bishop("White", 61)) self.squares[62] = Square(62, Knight("White", 62)) self.squares[63] = Square(63, Rook("White", 63)) self.squares[48] = Square(48, Pawn("White", 48)) self.squares[49] = Square(49, Pawn("White", 49)) self.squares[50] = Square(50, Pawn("White", 50)) self.squares[51] = Square(51, Pawn("White", 51)) self.squares[52] = Square(52, Pawn("White", 52)) self.squares[53] = Square(53, Pawn("White", 53)) self.squares[54] = Square(54, Pawn("White", 54)) self.squares[55] = Square(55, Pawn("White", 55))
def createPiece(self): chessboard[(6, 0)] = BlackPawn(6, 0, True) chessboard[(6, 1)] = BlackPawn(6, 1, True) chessboard[(6, 2)] = BlackPawn(6, 2, True) chessboard[(6, 3)] = BlackPawn(6, 3, True) chessboard[(6, 4)] = BlackPawn(6, 4, True) chessboard[(6, 5)] = BlackPawn(6, 5, True) chessboard[(6, 6)] = BlackPawn(6, 6, True) # chessboard[(6, 7)] = BlackPawn(6, 7, True) chessboard[(7, 0)] = Rook(7, 0, True) chessboard[(7, 1)] = Knight(7, 1, True) chessboard[(7, 2)] = Bishop(7, 2, True) chessboard[(7, 3)] = King(7, 3, True) self.white_king = chessboard[(7, 3)] chessboard[(7, 4)] = Queen(7, 4, True) chessboard[(7, 5)] = Bishop(7, 5, True) chessboard[(7, 6)] = Knight(7, 6, True) chessboard[(7, 7)] = Rook(7, 7, True) chessboard[(0, 0)] = Rook(0, 0, False) chessboard[(0, 1)] = Knight(0, 1, False) chessboard[(0, 2)] = Bishop(0, 2, False) chessboard[(0, 3)] = King(0, 3, False) self.black_king = chessboard[(0, 3)] chessboard[(0, 4)] = Queen(0, 4, False) chessboard[(0, 5)] = Bishop(0, 5, False) chessboard[(0, 6)] = Knight(0, 6, False) chessboard[(0, 7)] = Rook(0, 7, False) # chessboard[(1, 0)] = WhitePawn(1, 0, False) chessboard[(1, 1)] = WhitePawn(1, 1, False) chessboard[(1, 2)] = WhitePawn(1, 2, False) chessboard[(1, 3)] = WhitePawn(1, 3, False) chessboard[(1, 4)] = WhitePawn(1, 4, False) chessboard[(1, 5)] = WhitePawn(1, 5, False) chessboard[(1, 6)] = WhitePawn(1, 6, False) chessboard[(1, 7)] = WhitePawn(1, 7, False)
def basic_queen_test(): print("========================================") print("Performing the basic queen movement test") print("========================================") board = Board() piece = Queen(Colour.WHITE) current_location = Location('d', 5) board.__add_piece__(piece, current_location) 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 try_move(self, piece, move): board_copy = copy.deepcopy(self) board_copy.board[piece.x][piece.y] = 0 board_copy.board[move[0]][move[1]] = copy.deepcopy(piece) board_copy.board[move[0]][move[1]].move(move) # queeening if isinstance(board_copy.board[move[0]][move[1]], Pawn) and (move[0] == 0 or move[0] == 7): board_copy.board[move[0]][move[1]] = Queen(move[0], move[1], piece.player) king = board_copy.get_friendly_king() threats = king.get_threats(board_copy) if king.position in threats: return False return True
def __init__(self, colour, piece_type, file, rank): self.colour = colour self.file = file self.rank = rank self.type = piece_type if self.type == 'pawn': self.typemgr = Pawn(colour=colour, file=file, rank=rank) elif self.type == 'knight': self.typemgr = Knight(colour=colour, file=file, rank=rank) elif self.type == 'bishop': self.typemgr = Bishop(colour=colour, file=file, rank=rank) elif self.type == 'rook': self.typemgr = Rook(colour=colour, file=file, rank=rank) elif self.type == 'queen': self.typemgr = Queen(colour=colour, file=file, rank=rank) elif self.type == 'king': self.typemgr = King(colour=colour, file=file, rank=rank)
def __init__(self, board, turn, boards): self.turn = turn objBoard = [] kings = [] #Define a board filled with piece objects y_Pos = 0 for y in board: x_ObjBoard = [] x_Pos = 0 for x in y: if x != "": piece = Piece( x[0], x[1], (y_Pos, x_Pos), None ) #Why did I define it as (y,x)? I have no idea... But I regret doing so... deeply.... x_ObjBoard.append(piece) if x[1] == "R": movement = Rook(x[0], x[1], (y_Pos, x_Pos), x[2]) elif x[1] == "B": movement = Bishop(x[0], x[1], (y_Pos, x_Pos)) elif x[1] == "Q": movement = Queen(x[0], x[1], (y_Pos, x_Pos)) elif x[1] == "k": movement = Knight(x[0], x[1], (y_Pos, x_Pos)) elif x[1] == "P": movement = Pawn(x[0], x[1], (y_Pos, x_Pos), x[2]) elif x[1] == "K": movement = King(x[0], x[1], (y_Pos, x_Pos), x[2]) kings.append(piece) piece.setMove( movement ) #Append the unique movement code to the piece object else: x_ObjBoard.append("") x_Pos += 1 objBoard.append(x_ObjBoard) y_Pos += 1 self.promote = [False, False] #If a pawn is promoting (White,Black) self.colour = "R" self.boards = boards self.board = objBoard self.kings = kings #King objects stored for checking checks
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 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
class ThreatTests(unittest.TestCase): def setUp(self): self.queen_white = Queen(ChessCoord('E', '6'), white) self.queen_black = Queen(ChessCoord('G', '5'), black) self.rook_white = Rook(ChessCoord('D', '1'), white) self.knight_white = Knight(ChessCoord('G', '1'), white) self.pieces = [self.queen_white, self.queen_black, self.rook_white, self.knight_white] def test_queens_threatening_each_other(self): self.queen_white.analyze_threats_on_board_for_new_move(self.pieces, ChessCoord('E', '5')) self.failUnless(self.queen_black.is_threat_to_these_pieces == [self.queen_white, self.knight_white]) self.failUnless(self.queen_white.is_threat_to_these_pieces == [self.queen_black]) def test_queen_threatening_rook_but_not_vice_versa(self): self.queen_black.analyze_threats_on_board_for_new_move(self.pieces, ChessCoord('G', '4')) self.failUnless(self.queen_black.is_threat_to_these_pieces == [self.queen_white, self.rook_white, self.knight_white]) self.failUnless(self.queen_white.is_threat_to_these_pieces == [self.queen_black]) self.failUnless(self.rook_white.is_threat_to_these_pieces == []) def test_knight_threatening_queen_but_not_vice_versa(self): self.knight_white.analyze_threats_on_board_for_new_move(self.pieces, ChessCoord('F', '3')) self.failUnless(self.knight_white.is_threat_to_these_pieces == [self.queen_black]) self.failUnless(self.queen_black.is_threat_to_these_pieces == []) def test_knight_threatening_none(self): pieces = self.pieces + [Pawn(ChessCoord('C', '3'), black, go_north), Knight(ChessCoord('C', '1'), black), ] self.knight_white.update_coord(ChessCoord('A', '4')) self.knight_white.analyze_threats_on_board_for_new_move(pieces, ChessCoord('B', '2')) self.failUnless(self.knight_white.is_threat_to_these_pieces == []) def test_knight_threatening_none_with_all_pieces(self): pieces = copy.deepcopy(starting_pieces) g8_knight = select_piece(ChessCoord('G', '8'), pieces) e2_pawn = select_piece(ChessCoord('E', '2'), pieces) e2_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '4')) self.failUnless(g8_knight.is_threat_to_these_pieces == []) def test_king_only_threatens_for_one_step_with_all_pieces(self): pieces = copy.deepcopy(starting_pieces) e1_king = select_piece(ChessCoord('E', '1'), pieces) e1_king.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '6')) pawns_d7_e7_f7 = select_pieces([ChessCoord('D', '7'), ChessCoord('E', '7'), ChessCoord('F', '7')], pieces) self.failUnless(util.compare_lists(e1_king.is_threat_to_these_pieces, pawns_d7_e7_f7)) def test_pieces_threatens_none_from_start_position(self): pieces = copy.deepcopy(starting_pieces) map(lambda piece: piece.analyze_threats_on_board_for_new_move(pieces, piece.chess_coord ), pieces) all_threats = map(lambda piece: piece.is_threat_to_these_pieces, pieces) should_be_all_empty = filter(lambda threat_list: threat_list is not [], all_threats) should_be_all_empty_filter_away_empty = map(lambda sbae: sbae is not [], should_be_all_empty) self.failIf(False in should_be_all_empty_filter_away_empty) def test_pawn_threatens_none_from_start_position(self): pieces = copy.deepcopy(starting_pieces) b7_pawn = select_piece(ChessCoord('B', '7'), pieces) b7_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('B', '7')) self.failUnless(b7_pawn.is_threat_to_these_pieces == []) def test_pawn_threatens_appropriate_pieces_in_front_of_it(self): pieces = copy.deepcopy(starting_pieces) f2_pawn = select_piece(ChessCoord('F', '2'), pieces) f2_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('F', '6')) pawns_e7_g7 = select_pieces([ChessCoord('E', '7'), ChessCoord('G', '7')], pieces) self.failUnless(util.compare_lists(f2_pawn.is_threat_to_these_pieces, pawns_e7_g7)) def test_pawn_threatens_appropriate_pieces_with_en_passant_in_front_of_it(self): pieces = copy.deepcopy(starting_pieces) g7_pawn = select_piece(ChessCoord('G', '7'), pieces) g7_pawn.update_coord(ChessCoord('G', '5')) f2_pawn = select_piece(ChessCoord('F', '2'), pieces) f2_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('F', '5')) self.failUnless(util.compare_lists(f2_pawn.is_threat_to_these_pieces, [g7_pawn]))
def setUp(self): chess_coord_white = ChessCoord('C', '2') self.queen_white = Queen(chess_coord_white, white) chess_coord_black = ChessCoord('F', '5') self.queen_black = Queen(chess_coord_black, black)