Ejemplo n.º 1
0
    def test_black_knight_cannot_move_if_space_blocked_by_friendly_piece():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.BLACK)
        square = Square.at(3, 3)
        board.set_piece(square, knight)

        obstructing_square1 = Square.at(4, 5)
        obstructing_square2 = Square.at(4, 1)
        obstructing_square3 = Square.at(5, 4)
        obstructing_square4 = Square.at(5, 2)
        obstructing_square5 = Square.at(2, 5)
        obstructing_square6 = Square.at(2, 1)
        obstructing_square7 = Square.at(1, 4)
        obstructing_square8 = Square.at(1, 2)
        obstruction = Knight(Player.BLACK)
        board.set_piece(obstructing_square1, obstruction)
        board.set_piece(obstructing_square2, obstruction)
        board.set_piece(obstructing_square3, obstruction)
        board.set_piece(obstructing_square4, obstruction)
        board.set_piece(obstructing_square5, obstruction)
        board.set_piece(obstructing_square6, obstruction)
        board.set_piece(obstructing_square7, obstruction)
        board.set_piece(obstructing_square8, obstruction)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert len(moves) == 0
Ejemplo n.º 2
0
    def testKnightJumping():
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(3, 5)
        board.set_piece(square, knight)
        for row_delta in range(-1, 1):
            for col_delta in range(-1, 1):
                if row_delta != 0 or col_delta != 0:
                    board.set_piece(
                        Square.at(square.row + row_delta,
                                  square.col + col_delta), Pawn(Player.WHITE))

        moves = knight.get_available_moves(board)

        # Assert
        expected_moves = [
            Square.at(4, 7),
            Square.at(4, 3),
            Square.at(5, 6),
            Square.at(5, 4),
            Square.at(2, 7),
            Square.at(2, 3),
            Square.at(1, 6),
            Square.at(1, 4)
        ]
        assert len(moves) == len(expected_moves)
        assert set(moves) == set(expected_moves)
Ejemplo n.º 3
0
    def test_white_knights_can_move_to_all_unoccupied_squares():

        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(1, 4)
        board.set_piece(square, knight)
        enemy1 = Pawn(Player.BLACK)
        enemy1_square = Square.at(0, 6)
        board.set_piece(enemy1_square, enemy1)
        enemy2 = Pawn(Player.WHITE)
        enemy2_square = Square.at(2, 6)
        board.set_piece(enemy2_square, enemy2)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert set(moves) == set([
            Square.at(0, 6),
            Square.at(0, 2),
            Square.at(2, 2),
            Square.at(3, 3),
            Square.at(3, 5)
        ])
Ejemplo n.º 4
0
    def test_white_knight_can_move_diagonally():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(0, 1)
        board.set_piece(square, knight)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert Square.at(2, 2) in moves
Ejemplo n.º 5
0
    def test_black_knight_can_move_diagonally():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.BLACK)
        square = Square.at(7, 1)
        board.set_piece(square, knight)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert Square.at(5, 0) in moves
Ejemplo n.º 6
0
    def test_knight_can_move_in_an_l_left_up():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        knight_square = Square.at(4, 4)
        board.set_piece(knight_square, knight)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert Square.at(5, 2) in moves
Ejemplo n.º 7
0
    def test_knight_leave_the_board():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(7, 7)
        board.set_piece(square, knight)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        expected_moves = [Square.at(5, 6), Square.at(6, 5)]
        assert len(moves) == len(expected_moves)
        assert sorted(moves) == sorted(expected_moves)
Ejemplo n.º 8
0
    def test_knight_can_move_in_l_pattern(valid_destination):

        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(4, 4)
        board.set_piece(square, knight)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert Square.at(valid_destination[0], valid_destination[1]) in moves
        assert Square.at(valid_destination[1], valid_destination[0]) in moves
Ejemplo n.º 9
0
    def test_white_knight_cannot_move_at_top_of_board():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(3, 7)
        board.set_piece(square, knight)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert Square.at(2, 9) not in moves
        assert Square.at(4, 9) not in moves
        assert Square.at(5, 8) not in moves
        assert Square.at(1, 8) not in moves
Ejemplo n.º 10
0
    def test_knights_can_capture():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        knight_square = Square.at(4, 4)
        board.set_piece(knight_square, knight)

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

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert Square.at(6, 5) in moves
Ejemplo n.º 11
0
    def test_knight_cannot_move_to_friendly_square():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        knight_square = Square.at(4, 4)
        board.set_piece(knight_square, knight)

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

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert Square.at(5, 6) not in moves
Ejemplo n.º 12
0
    def testKnightFreindly():

        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(3, 5)
        board.set_piece(square, knight)

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

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert friendly_square not in moves
Ejemplo n.º 13
0
    def testKnightCapturing():

        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(3, 5)
        board.set_piece(square, knight)

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

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert enemy_square in moves
Ejemplo n.º 14
0
    def test_black_knights_can_move_in_l_shape():
        # Arrange
        board = Board.empty()
        knight = Knight(Player.BLACK)
        square = Square.at(3, 3)
        board.set_piece(square, knight)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert Square.at(4, 5) in moves
        assert Square.at(4, 1) in moves
        assert Square.at(5, 4) in moves
        assert Square.at(5, 2) in moves
        assert Square.at(2, 5) in moves
        assert Square.at(2, 1) in moves
        assert Square.at(1, 4) in moves
        assert Square.at(1, 2) in moves
Ejemplo n.º 15
0
    def test_white_knights_can_move_to_all_possible_squares():

        # Arrange
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(1, 4)
        board.set_piece(square, knight)

        # Act
        moves = knight.get_available_moves(board)

        # Assert
        assert set(moves) == set([
            Square.at(0, 6),
            Square.at(0, 2),
            Square.at(2, 6),
            Square.at(2, 2),
            Square.at(3, 3),
            Square.at(3, 5)
        ])
Ejemplo n.º 16
0
    def testKnightMovementShape():
        board = Board.empty()
        knight = Knight(Player.WHITE)
        square = Square.at(3, 5)
        board.set_piece(square, knight)

        moves = knight.get_available_moves(board)

        expected_moves = [
            Square.at(4, 7),
            Square.at(4, 3),
            Square.at(5, 6),
            Square.at(5, 4),
            Square.at(2, 7),
            Square.at(2, 3),
            Square.at(1, 6),
            Square.at(1, 4)
        ]
        assert len(moves) == len(expected_moves)
        assert set(moves) == set(expected_moves)
Ejemplo 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