def test_serialization_returns_correct_serialization_for_boards():
    p1 = Player.ONE
    p1_row = PlayerRow.get_new_player_row()
    p2 = Player.TWO
    p2_row = PlayerRow.get_new_player_row()
    board = Board({p1: p1_row, p2: p2_row})
    assert to_serializable(board) == {
        to_serializable(p1): to_serializable(p1_row),
        to_serializable(p2): to_serializable(p2_row),
    }
def test_get_new_game_board_provides_new_player_rows():
    new_board = get_new_board()
    assert Player.ONE in new_board.keys() and Player.TWO in new_board.keys()
    assert all([
        player_row == PlayerRow.get_new_player_row()
        for player_row in new_board.values()
    ])
def test_player_row_provides_convenience_function_for_new_row():
    assert hasattr(PlayerRow, "get_new_player_row")
    new_player_row = PlayerRow.get_new_player_row()
    assert new_player_row.bins == [
        NUMBER_OF_STARTING_PIECES for _ in range(NUMBER_OF_BINS)
    ]
    assert new_player_row.goal == 0
def test_who_gets_next_turn_returns_correct_player(player, opponent):
    # player makes optimal first game move to get another turn
    prior_board = Board({
        player: PlayerRow(bins=[4, 4, 4, 4, 4, 4], goal=0),
        opponent: PlayerRow(bins=[4, 4, 4, 4, 4, 4], goal=0),
    })
    turn = Turn(player, 3)
    new_board = Board({
        player: PlayerRow(bins=[5, 5, 5, 0, 4, 4], goal=1),
        opponent: PlayerRow.get_new_player_row(),
    })
    assert who_gets_next_turn(prior_board, turn, new_board) == player

    # player makes a move to steal pieces from opponent
    prior_board = Board({
        player: PlayerRow(bins=[0, 10, 0, 0, 0, 2], goal=12),
        opponent: PlayerRow(bins=[1, 6, 7, 1, 2, 7], goal=3),
    })
    turn = Turn(player, 5)
    new_board = Board({
        player: PlayerRow(bins=[0, 10, 0, 0, 1, 0], goal=14),
        opponent: PlayerRow(bins=[1, 6, 7, 0, 2, 7], goal=3),
    })
    assert who_gets_next_turn(prior_board, turn, new_board) == opponent

    # player makes double-wrap move to steal pieces from opponent
    prior_board = Board({
        player: PlayerRow(bins=[0, 10, 0, 0, 0, 2], goal=12),
        opponent: PlayerRow(bins=[1, 6, 7, 1, 2, 7], goal=3),
    })
    turn = Turn(player, 1)
    new_board = Board({
        player: PlayerRow(bins=[1, 0, 0, 0, 0, 3], goal=17),
        opponent: PlayerRow(bins=[2, 7, 8, 2, 0, 8], goal=3),
    })
    assert who_gets_next_turn(prior_board, turn, new_board) == opponent

    # player makes double-wrap move and ends in goal
    prior_board = Board({
        player: PlayerRow(bins=[14, 0, 0, 0, 2, 0], goal=10),
        opponent: PlayerRow(bins=[1, 6, 4, 0, 1, 7], goal=3),
    })
    turn = Turn(player, 0)
    new_board = Board({
        player: PlayerRow(bins=[1, 1, 1, 1, 3, 1], goal=12),
        opponent: PlayerRow(bins=[2, 7, 5, 1, 2, 8], goal=3),
    })
    assert who_gets_next_turn(prior_board, turn, new_board) == player
def test_serialize_returns_type_error_for_unregistered_type():
    class NewType:
        pass

    with pytest.raises(TypeError):
        to_serializable(NewType())


@pytest.mark.parametrize(
    "serializable,serialized",
    [
        (None, None),
        (Player.ONE, "one"),
        (Player.TWO, "two"),
        (
            PlayerRow.get_new_player_row(),
            {
                "bins": PlayerRow.get_new_player_row().bins,
                "goal": PlayerRow.get_new_player_row().goal,
            },
        ),
        (Turn(Player.ONE, 4), {
            "player": "one",
            "selected_bin": 4
        }),
        (Turn(Player.TWO, 2), {
            "player": "two",
            "selected_bin": 2
        }),
    ],
)
def test_player_row_cannot_add_non_positive_pieces_to_goal():
    player_row = PlayerRow.get_new_player_row()
    with pytest.raises(ValueError):
        player_row.add_pieces_to_goal(-1)
    with pytest.raises(ValueError):
        player_row.add_pieces_to_goal(0)
def test_player_row_provides_nice_representation():
    new_player_row = PlayerRow.get_new_player_row()
    assert repr(new_player_row) == "[  0 |  4,  4,  4,  4,  4,  4 ]"

    player_row = PlayerRow([12, 0, 1, 9, 10, 4], goal=10)
    assert repr(player_row) == "[ 10 | 12,  0,  1,  9, 10,  4 ]"
def test_player_row_add_pieces_to_bin_must_be_correct_bin_selection():
    new_player_row = PlayerRow.get_new_player_row()
    with pytest.raises(ValueError):
        new_player_row.add_piece_in_bin(-1)
    with pytest.raises(ValueError):
        new_player_row.add_piece_in_bin(NUMBER_OF_BINS)
            PlayerRow([4, 4, 4, 5, 5, 5], 0),
        ),
        (
            1,
            PlayerRow([5, 0, 4, 4, 4, 4], 1),
            PlayerRow([4, 4, 4, 4, 5, 5], 0),
        ),
        (
            2,
            PlayerRow([5, 5, 0, 4, 4, 4], 1),
            PlayerRow([4, 4, 4, 4, 4, 5], 0),
        ),
        (
            3,
            PlayerRow([5, 5, 5, 0, 4, 4], 1),
            PlayerRow.get_new_player_row(),
        ),
        (
            4,
            PlayerRow([5, 5, 5, 5, 0, 4], 0),
            PlayerRow.get_new_player_row(),
        ),
        (
            5,
            PlayerRow([4, 5, 5, 5, 5, 0], 0),
            PlayerRow.get_new_player_row(),
        ),
    ],
)
def test_first_moves_on_a_new_game_board(selected_bin, players_result_row,
                                         opponents_result_row):
Example #10
0
def test_even_goal_stealing_shedding_requires_opponent_row():
    strategy = EvenGoalStealAndPiecesOnOtherSideStrategy()
    with pytest.raises(ValueError):
        strategy.choose_bin(player_row=PlayerRow.get_new_player_row())