Exemplo n.º 1
0
    def test_white_pawn_cannot_move_if_piece_in_front():

        # Arrange
        board = Board.empty()
        pawn = Pawn(Player.WHITE)
        pawn_square = Square.at(4, 4)
        board.set_piece(pawn_square, pawn)

        obstructing_square = Square.at(5, 4)
        obstruction = Pawn(Player.BLACK)
        board.set_piece(obstructing_square, obstruction)

        # Act
        moves = pawn.get_available_moves(board)

        # Assert
        assert len(moves) == 0
    def test_king_cannot_move_if_piece_in_front():
        # Arrange
        board = Board.empty()
        king = King(Player.WHITE)
        king_square = Square.at(4, 4)
        board.set_piece(king_square, king)

        friend = Pawn(Player.WHITE)
        friend_square = Square.at(5, 5)
        board.set_piece(friend_square, friend)

        friend2 = Pawn(Player.WHITE)
        friendly_square2 = Square.at(3, 4)
        board.set_piece(friendly_square2, friend2)

        enemy = Pawn(Player.BLACK)
        enemy_square = Square.at(5, 4)
        board.set_piece(enemy_square, enemy)

        # Act
        moves = king.get_available_moves(board)

        # Assert
        print(moves)
        assert Square.at(5, 5) not in moves
        assert Square.at(3, 4) not in moves
        assert Square.at(5, 4) in moves
Exemplo n.º 3
0
    def test_white_pawns_cannot_move_diagonally_except_to_capture():

        # Arrange
        board = Board.empty()
        pawn = Pawn(Player.WHITE)
        pawn_square = Square.at(3, 4)
        board.set_piece(pawn_square, pawn)

        friendly = Pawn(Player.WHITE)
        friendly_square = Square.at(4, 5)
        board.set_piece(friendly_square, friendly)

        # Act
        moves = pawn.get_available_moves(board)

        # Assert
        assert Square.at(4, 3) not in moves
        assert Square.at(4, 5) not in moves
    def handle_click(row, col):

        nonlocal window, board, from_square, to_squares
        clicked_piece = board.get_piece(Square.at(row, col))

        # If making an allowed move, then make it
        if from_square is not None and any(s.row == row and s.col == col for s in to_squares):
            board.get_piece(from_square).move_to(board, Square.at(row, col))
            from_square, to_squares = None, []

        # If clicking on a piece whose turn it is, get its allowed moves
        elif clicked_piece is not None and clicked_piece.player == board.current_player:
            from_square = Square.at(row, col)
            to_squares = clicked_piece.get_available_moves(board)

        # Otherwise reset everthing to default
        else:
            from_square, to_squares = None, []
Exemplo n.º 5
0
    def get_lateral(self, squarelist, square, board, is_king=False):
        config = [(square.row + 1, BOARD_MAX + 1, 1, True),
                  (square.row - 1, BOARD_MIN - 1, -1, True),
                  (square.col + 1, BOARD_MAX + 1, 1, False),
                  (square.col - 1, BOARD_MIN - 1, -1, False)]

        for c in config:
            for n in range(c[0], c[1], c[2]):
                obstruction = self.maybe_add_square(
                    squarelist=squarelist,
                    square=Square.at(n, square.col) if c[3] else Square.at(
                        square.row, n),
                    board=board,
                    empty=True,
                    takeable=True)

                if obstruction or is_king:
                    break
Exemplo n.º 6
0
    def test_black_pawns_can_capture_diagonally():

        # Arrange
        board = Board.empty()
        pawn = Pawn(Player.BLACK)
        pawn_square = Square.at(3, 4)
        board.set_piece(pawn_square, pawn)

        friendly = Pawn(Player.BLACK)
        friendly_square = Square.at(2, 5)
        board.set_piece(friendly_square, friendly)

        # Act
        moves = pawn.get_available_moves(board)

        # Assert
        assert Square.at(2, 3) not in moves
        assert Square.at(2, 5) not in moves
    def test_white_king_checked_by_black_rook():
        # Arrange
        board = Board.empty()
        king = King(Player.WHITE)
        king_square = Square.at(2, 3)
        board.set_piece(king_square, king)

        enemy = Rook(Player.BLACK)
        enemy_square = Square.at(2, 2)
        board.set_piece(enemy_square, enemy)

        # Act

        # check if current square is in check?
        check = king.is_in_check(board)

        # Assert
        assert check is True
Exemplo n.º 8
0
 def find_piece(self, piece_to_find):
     """
     Searches for the given piece on the board and returns its square.
     """
     for row in range(BOARD_SIZE):
         for col in range(BOARD_SIZE):
             if self.board[row][col] is piece_to_find:
                 return Square.at(row, col)
     raise Exception('The supplied piece is not on the board')
Exemplo n.º 9
0
    def get_available_moves(self, board):
        moves = []

        current_square = board.find_piece(self)

        for vertical,horizontal in [(1,1),(1,-1),(-1,1),(-1,-1)]:
            for i in range(1,8):
                if inBounds(current_square.row+(vertical*i)) and inBounds(current_square.col+(horizontal*i)):
                     square = Square.at(current_square.row+(vertical*i),current_square.col+(horizontal*i))
                     piece = board.get_piece(square)
                     if piece == None:
                         moves.append(square)
                     elif piece.player != self.player:
                         moves.append(square)
                         break
                     else:
                         break

        for direction in [1,-1]:
            if direction == 1: max = 8
            else: max = -1

            for vertical in range(current_square.row+direction,max,direction):
                square = Square.at(vertical,current_square.col)
                piece = board.get_piece(square)
                if piece == None:
                    moves.append(square)
                elif piece.player != self.player:
                    moves.append(square)
                    break
                else:
                    break
            for horizontal in range(current_square.col+direction,max,direction):
                square = Square.at(current_square.row,horizontal)
                piece = board.get_piece(square)
                if piece == None:
                    moves.append(square)
                elif piece.player != self.player:
                    moves.append(square)
                    break
                else:
                    break

        return moves
Exemplo n.º 10
0
 def prawn_capture(self, board, current_square, valid_moves, row_direction,
                   col_direction):
     next_square = Square.at(current_square.row + row_direction,
                             current_square.col + col_direction)
     if board.does_square_exist(
             next_square) and not board.is_square_empty(next_square):
         check_piece = board.get_piece(next_square)
         board.check_for_capture(self, check_piece, valid_moves,
                                 next_square)
     return valid_moves
Exemplo n.º 11
0
def test_new_board_has_white_pieces_at_bottom():

    # Arrange
    board = Board.at_starting_position()

    # Act
    piece = board.get_piece(Square.at(0, 0))

    # Assert
    assert piece.player == Player.WHITE
Exemplo n.º 12
0
    def get_available_moves(self, board):

        positionDelta = 0
        currentLoc = board.find_piece(self)

        if self.player == Player.WHITE:
            positionDelta = 1
        else:
            positionDelta = -1

        single_move = Square.at(currentLoc.row + positionDelta, currentLoc.col)
        double_move = Square.at(currentLoc.row + positionDelta * 2,
                                currentLoc.col)

        diagonal_move_1 = Square.at(currentLoc.row + positionDelta,
                                    currentLoc.col + positionDelta)
        diagonal_move_2 = Square.at(currentLoc.row + positionDelta,
                                    currentLoc.col - positionDelta)

        # Standard Forward moves
        moves = []
        if board.squareInBounds(single_move) and board.squareInBounds(
                double_move):

            if board.get_piece(single_move) is None:
                moves.append(single_move)

                if (board.get_piece(double_move) is None) and not self.moved:
                    moves.append(double_move)

        capturingMoves = []
        if board.squareInBounds(diagonal_move_1) and board.squareInBounds(
                diagonal_move_2):
            piece1 = board.get_piece(diagonal_move_1)
            piece2 = board.get_piece(diagonal_move_2)

            if piece1 is not None and piece1.player != self.player:
                capturingMoves.append(diagonal_move_1)

            if piece2 is not None and piece2.player != self.player:
                capturingMoves.append(diagonal_move_2)

        return moves + capturingMoves
Exemplo n.º 13
0
 def check_en_passant(self, board, candidate_square, direction):
     moves = []
     if board.last_move is not None:
         if candidate_square.is_on_board():
             if board.get_piece(candidate_square) == board.last_move[0]:
                 if board.last_move[2]:
                     moves.append(
                         Square.at(candidate_square.row + direction,
                                   candidate_square.col))
     return moves
Exemplo n.º 14
0
def test_new_board_has_black_pieces_at_top():

    # Arrange
    board = Board.at_starting_position()

    # Act
    piece = board.get_piece(Square.at(7, 0))

    # Assert
    assert piece.player == Player.BLACK
Exemplo n.º 15
0
        def test_black_king_can_castle():
            # Arrange
            board = Board.empty()
            king = King(Player.BLACK)
            square = Square.at(7, 4)
            board.set_piece(square, king)

            rook1 = Rook(Player.BLACK)
            board.set_piece(Square.at(7, 0), rook1)

            rook2 = Rook(Player.BLACK)
            board.set_piece(Square.at(7, 7), rook2)

            # Act
            moves = king.get_available_moves(board)

            # Assert
            assert Square.at(7, 2) in moves
            assert Square.at(7, 6) in moves
Exemplo n.º 16
0
    def get_base_moves(self, board):
        base_moves = []
        square = board.find_piece(self)
        base_moves.append(Square.at(square.row - 1, square.col - 1))
        base_moves.append(Square.at(square.row - 1, square.col - 0))
        base_moves.append(Square.at(square.row - 1, square.col + 1))
        base_moves.append(Square.at(square.row - 0, square.col - 1))
        base_moves.append(Square.at(square.row - 0, square.col + 1))
        base_moves.append(Square.at(square.row + 1, square.col - 1))
        base_moves.append(Square.at(square.row + 1, square.col - 0))
        base_moves.append(Square.at(square.row + 1, square.col + 1))

        return base_moves
Exemplo n.º 17
0
    def test_knights_cannot_move_off_board():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        knight_square = Square.at(0, 0)
        board.set_piece(knight_square, knight)

        friendly_1 = Pawn(Player.WHITE)
        friendly_1_square = Square.at(2, 1)
        board.set_piece(friendly_1_square, friendly_1)

        friendly_2 = Pawn(Player.WHITE)
        friendly_2_square = Square.at(1, 2)
        board.set_piece(friendly_2_square, friendly_2)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert len(moves) == 0
Exemplo n.º 18
0
    def test_black_pawn_can_be_captured_by_en_pessant():
        # Arrange
        board = Board.empty()

        pawn1 = Pawn(Player.BLACK)  # MAKE BLACK PAWN
        starting_square1 = Square.at(6, 4)
        board.set_piece(starting_square1, pawn1)

        pawn2 = Pawn(Player.WHITE)  # MAKE WHITE PAWN
        starting_square2 = Square.at(4, 3)
        board.set_piece(starting_square2, pawn2)

        intermediate_square = Square.at(4, 4)  # move black pawn
        pawn1.move_to(board, intermediate_square)

        # Act
        moves = pawn2.get_available_moves(board)

        # Assert
        assert Square.at(5, 4) in moves
Exemplo n.º 19
0
    def test_rooks_cannot_move_off_the_board():
        # Arrange
        board = Board.empty()
        rook = Rook(Player.WHITE)
        rook_square = Square.at(0, 0)
        board.set_piece(rook_square, rook)

        friendly_1 = Pawn(Player.WHITE)
        friendly_1_square = Square.at(1, 0)
        board.set_piece(friendly_1_square, friendly_1)

        friendly_2 = Pawn(Player.WHITE)
        friendly_2_square = Square.at(0, 1)
        board.set_piece(friendly_2_square, friendly_2)

        # Act
        moves = rook.get_available_moves(board)

        # Assert
        assert len(moves) == 0
Exemplo n.º 20
0
 def get_enemy_locations(self, board):
     """
     Get all squares containing enemy pieces
     """
     enemy_pieces_squares = []
     for square_row in range(8):
         for square_col in range(8):
             selected_square = Square.at(square_row, square_col)
             if self.check_enemy_piece(board, selected_square):
                 enemy_pieces_squares.append(selected_square)
     return enemy_pieces_squares
Exemplo n.º 21
0
    def test_white_kings_can_move_to_unoccupied_neighbouring_squares_inside_board(
    ):

        # Arrange
        board = Board.empty()
        king = King(Player.WHITE)
        square = Square.at(7, 0)
        board.set_piece(square, king)
        enemy1 = Pawn(Player.BLACK)
        enemy1_square = Square.at(6, 0)
        board.set_piece(enemy1_square, enemy1)
        enemy2 = Pawn(Player.WHITE)
        enemy2_square = Square.at(6, 1)
        board.set_piece(enemy2_square, enemy2)

        # Act
        moves = king.get_available_moves(board)

        # Assert
        assert set(moves) == set([Square.at(6, 0), Square.at(7, 1)])
Exemplo n.º 22
0
    def get_available_moves(self, board):
        moves = []
        white = self.player == Player.WHITE
        pos = board.find_piece(self)

        # in front
        self.maybe_add_square(squarelist=moves,
                              square=Square.at(
                                  pos.row + 1 if white else pos.row - 1,
                                  pos.col),
                              board=board,
                              empty=True,
                              takeable=False)

        if len(moves) == 1 and (not self.moved):
            self.maybe_add_square(squarelist=moves,
                                  square=Square.at(
                                      pos.row + 2 if white else pos.row - 2,
                                      pos.col),
                                  board=board,
                                  empty=True,
                                  takeable=False)

        # diagonal
        self.maybe_add_square(squarelist=moves,
                              square=Square.at(
                                  pos.row + 1 if white else pos.row - 1,
                                  pos.col - 1),
                              board=board,
                              empty=False,
                              takeable=True)

        self.maybe_add_square(squarelist=moves,
                              square=Square.at(
                                  pos.row + 1 if white else pos.row - 1,
                                  pos.col + 1),
                              board=board,
                              empty=False,
                              takeable=True)

        return moves
Exemplo n.º 23
0
    def test_king_can_castle():
        # Arrange
        board = Board.empty()
        king = King(Player.WHITE)
        king_square = Square.at(0, 4)
        board.set_piece(king_square, king)

        friend1 = Rook(Player.WHITE)
        friend_square1 = Square.at(0, 7)
        board.set_piece(friend_square1, friend1)

        friend2 = Rook(Player.WHITE)
        friend_square2 = Square.at(0, 0)
        board.set_piece(friend_square2, friend2)

        # Act
        moves = king.get_available_moves(board)

        # Assert
        assert Square.at(0, 2) in moves
        assert Square.at(0, 6) in moves
Exemplo n.º 24
0
    def test_black_pawn_cannot_move_at_bottom_of_board():
        # Arrange
        board = Board.empty()
        pawn = Pawn(Player.BLACK)
        square = Square.at(0, 4)
        board.set_piece(square, pawn)

        # Act
        moves = pawn.get_available_moves(board)

        # Assert
        assert len(moves) == 0
Exemplo n.º 25
0
    def test_white_pawn_cannot_move_at_top_of_board():
        # Arrange
        board = Board.empty()
        pawn = Pawn(Player.WHITE)
        square = Square.at(7, 4)
        board.set_piece(square, pawn)

        # Act
        moves = pawn.get_available_moves(board)

        # Assert
        assert len(moves) == 0
Exemplo n.º 26
0
    def test_black_pawns_can_capture_diagonally():
        # Arrange
        board = Board.empty()
        pawn = Pawn(Player.BLACK)
        pawn_square = Square.at(3, 4)
        board.set_piece(pawn_square, pawn)

        enemy1 = Pawn(Player.WHITE)
        enemy1_square = Square.at(2, 5)
        board.set_piece(enemy1_square, enemy1)

        enemy2 = Pawn(Player.WHITE)
        enemy2_square = Square.at(2, 3)
        board.set_piece(enemy2_square, enemy2)

        # Act
        moves = pawn.get_available_moves(board)

        # Assert
        assert enemy1_square in moves
        assert enemy2_square in moves
Exemplo n.º 27
0
    def test_black_pawn_changes_to_queen_when_moved_to_top_of_board():
        # Arrange
        board = Board.empty()
        pawn = Pawn(Player.BLACK)
        square = Square.at(1, 4)
        board.set_piece(square, pawn)

        # Act
        moves = pawn.get_available_moves(board)

        # Assert
        assert len(moves) == 1
Exemplo n.º 28
0
 def get_available_moves(self, board):
     current_square = board.find_piece(self)
     row = current_square.row
     col = current_square.col
     available_moves = []
     x = [-2, -2, -1, -1, 1, 1, 2, 2]
     y = [1, -1, 2, -2, 2, -2, -1, 1]
     for i in range(0, len(x)):
         sq = Square.at(row + x[i], col + y[i])
         if self.can_move(sq, board):
             available_moves.append(sq)
     return available_moves
Exemplo n.º 29
0
 def get_available_moves(self, board):
     current_square = board.find_piece(self)
     row = current_square.row
     col = current_square.col
     available_moves = []
     row_dir = 1 if self.player == Player.WHITE else -1
     sq = Square.at(row + row_dir, col)
     if self.inside_board(sq, board) and board.get_piece(sq) == None:
         available_moves.append(sq)
         sq = Square.at(row + row_dir, col + 1)
         if self.inside_board(sq, board) and self.is_enemy(sq, board):
             available_moves.append(sq)
         sq = Square.at(row + row_dir, col - 1)
         if self.inside_board(sq, board) and self.is_enemy(sq, board):
             available_moves.append(sq)
         if self.has_moved == False:
             sq = Square.at(row + 2 * row_dir, col)
             if self.inside_board(sq,
                                  board) and board.get_piece(sq) == None:
                 available_moves.append(sq)
     return available_moves
Exemplo n.º 30
0
 def get_available_moves(self, board):
     current_square = board.find_piece(self)
     row = current_square.row
     col = current_square.col
     available_moves = []
     change = [-1, 0, 1]
     for row_change in change:
         for col_change in change:
             sq = Square.at(row + row_change, col + col_change)
             if self.can_move(sq, board):
                 available_moves.append(sq)
     return available_moves