Exemple #1
0
def test_clear_pieces_after_creations_empties_pieces():
    """ Tests clear_pieces. """
    board = Board(create_random_maze())
    for _ in range(8):
        board.create_piece()
    assert len(board.pieces) == 8
    board.clear_pieces()
    assert not board.pieces
def test_computer_player_random_algorith_when_piece_is_pushed_out(post_move, post_shift, time_sleep):
    """ Tests case where piece is positioned on a shift location, so that it is pushed out.
    Runs computation 100 times. Push-out expectation rate is 1/12.
    Probability that no push-out takes place in 100 runs is negligible
    .start() is patched so that the compute method runs sequentially.
    This test recreates a bug, where the pushed-out piece is not updated correctly, leading
    to exceptions thrown when computer makes a move.
    """
    card_factory = MazeCardFactory()
    board = Board(create_maze(MAZE_STRING, card_factory), leftover_card=card_factory.create_instance("NE", 0))
    piece = board.create_piece()
    piece.maze_card = board.maze[BoardLocation(3, 6)]
    game = Mock()
    game.get_enabled_shift_locations.return_value = board.shift_locations
    game.board = board
    computer_player = ComputerPlayer(library_binding_factory=Mock(), move_url="move-url", shift_url="shift-url",
                                     game=game, identifier=9, board=board, piece=piece)

    for _ in range(100):
        shift_action, move_location = computer_player.random_actions()
        shift_location, _ = shift_action
        allowed_coordinates = [(3, 6)]
        if shift_location == BoardLocation(3, 6):
            allowed_coordinates = [(3, 5)]
        elif shift_location == BoardLocation(3, 0):
            allowed_coordinates = [(3, 0)]
        allowed_moves = {BoardLocation(*coordinates) for coordinates in allowed_coordinates}
        assert move_location in allowed_moves
Exemple #3
0
def test_shift_updates_pieces_on_pushed_out_card():
    """ Tests shift """
    board = Board()
    piece = board.create_piece()
    pushed_card = board.leftover_card
    piece.maze_card = board.maze[BoardLocation(0, 3)]
    board.shift(BoardLocation(board.maze.maze_size - 1, 3), 90)
    assert piece.maze_card == pushed_card
Exemple #4
0
def test_create_piece_assigns_pieces_consecutive_unique_indices():
    """ Tests create_piece. Adds four pieces, removes first two, adds one, removes third, adds two,
    and checks that all pieces have consecutive unique index """
    board = Board(create_random_maze())
    pieces = [
        board.create_piece(),
        board.create_piece(),
        board.create_piece(),
        board.create_piece()
    ]
    board.remove_piece(pieces[0])
    board.remove_piece(pieces[1])
    pieces[0] = board.create_piece()
    board.remove_piece(pieces[3])
    pieces[1] = board.create_piece()
    pieces[3] = board.create_piece()
    assert set([0, 1, 2,
                3]) == set(map(lambda piece: piece.piece_index, pieces))
Exemple #5
0
def test_move_raises_error_on_unreachable_location():
    """ Tests move validation """
    maze_card_factory = MazeCardFactory()
    board = Board(maze=create_maze(MAZE_STRING, maze_card_factory),
                  leftover_card=maze_card_factory.create_random_maze_card())
    piece = board.create_piece()
    piece.maze_card = board.maze[BoardLocation(0, 1)]
    with pytest.raises(MoveUnreachableException):
        board.move(piece, BoardLocation(0, 0))
Exemple #6
0
def test_move_new_objective_locations_after_reaching_location():
    """ Tests new objective generation after reaching one """
    maze_card_factory = MazeCardFactory()
    maze = create_maze(MAZE_STRING, maze_card_factory)
    objective_maze_card = maze[BoardLocation(1, 6)]
    board = Board(maze=maze,
                  leftover_card=maze_card_factory.create_random_maze_card(),
                  objective_maze_card=objective_maze_card)
    piece = board.create_piece()
    piece.maze_card = maze[BoardLocation(0, 6)]
    board.move(piece, BoardLocation(1, 6))
    _assert_all_piece_and_objective_location_different(board)
Exemple #7
0
def test_move_updates_players_maze_card_correctly():
    """ Tests move
    Instead of calling init_board(), the board is built manually, and
    the player's position is set manually as well, so that
    randomness is eliminated for testing """
    maze_card_factory = MazeCardFactory()
    board = Board(maze=create_maze(MAZE_STRING, maze_card_factory),
                  leftover_card=maze_card_factory.create_random_maze_card())
    piece = board.create_piece()
    piece.maze_card = board.maze[BoardLocation(0, 1)]
    board.move(piece, BoardLocation(0, 2))
    assert board.maze[BoardLocation(0, 2)] == piece.maze_card
Exemple #8
0
def test_create_piece_sets_all_pieces_on_corners():
    """ Tests create_piece """
    board = Board(create_random_maze())

    def is_corner(location):
        return (location.row in [
            0, board.maze.maze_size - 1
        ]) and (location.column in [0, board.maze.maze_size - 1])

    for _ in range(8):
        piece = board.create_piece()
        piece_location = board.maze.maze_card_location(piece.maze_card)
        assert is_corner(piece_location)
Exemple #9
0
def test_after_series_of_creates_and_removes_no_corners_empty():
    """ Tests create_piece. Adds three pieces, removes two, adds three,
    and checks that all pieces are on a different corner """
    board = Board(create_random_maze())
    piece1 = board.create_piece()
    piece2 = board.create_piece()
    board.create_piece()
    board.remove_piece(piece1)
    board.remove_piece(piece2)
    board.create_piece()
    board.create_piece()
    board.create_piece()
    assert len(board.pieces) == 4
    piece_cards = {piece.maze_card for piece in board.pieces}
    assert len(piece_cards) == 4
def test_computer_player_calls_start_on_compute_method(post_move, post_shift, time_sleep):
    """ Tests that the computer player calls start() one its computation method.
    """
    card_factory = MazeCardFactory()
    board = Board(create_maze(MAZE_STRING, card_factory), leftover_card=card_factory.create_instance("NE", 0))
    piece = board.create_piece()
    game = Mock()
    type(game).identifier = PropertyMock(return_value=7)
    game.get_enabled_shift_locations.return_value = board.shift_locations
    mock_method = Mock()
    mock_method.start = Mock()
    mock_method.shift_action = BoardLocation(0, 1), 90
    mock_method.move_action = board.maze.maze_card_location(piece.maze_card)
    mock_method_factory = Mock()
    mock_method_factory.return_value = mock_method
    player = ComputerPlayer(library_binding_factory=mock_method_factory, move_url="move-url", shift_url="shift-url",
                            game=game, identifier=9, board=board, piece=piece)
    player.run()
    mock_method.start.assert_called_once()
    post_shift.assert_called_once()
    post_move.assert_called_once()
Exemple #11
0
def test_remove_piece_after_create_piece():
    """ Tests remove_piece """
    board = Board(create_random_maze())
    piece = board.create_piece()
    board.remove_piece(piece)
    assert not board.pieces
Exemple #12
0
def test_move_raises_error_on_invalid_location():
    """ Tests move validation """
    board = Board()
    piece = board.create_piece()
    with pytest.raises(InvalidLocationException):
        board.move(piece, BoardLocation(-1, -1))