Example #1
0
def test_validate_initial_move_allowed() -> None:
    rc = RuleChecker()
    bs = BoardState()

    r = rc.validate_initial_move(
        bs,
        [index_to_tile(1), index_to_tile(2), index_to_tile(3)],
        InitialMove(BoardPosition(2, 0), index_to_tile(2), Port.BottomLeft, "red"),
    )
    assert r.is_ok()
    assert bs == BoardState()  # board state is unchanged

    r = rc.validate_initial_move(
        bs,
        [index_to_tile(1), index_to_tile(2), index_to_tile(3)],
        InitialMove(BoardPosition(0, 2), index_to_tile(2), Port.BottomLeft, "red"),
    )
    assert r.is_ok()
    assert bs == BoardState()  # board state is unchanged

    r = rc.validate_initial_move(
        bs,
        [index_to_tile(2), index_to_tile(3), index_to_tile(4)],
        InitialMove(BoardPosition(9, 2), index_to_tile(2), Port.BottomLeft, "white"),
    )
    assert r.is_ok()
    assert bs == BoardState()  # board state is unchanged
Example #2
0
def test_validate_initial_move_facing_edge() -> None:
    rc = RuleChecker()
    bs = BoardState()

    r = rc.validate_initial_move(
        bs,
        [index_to_tile(1), index_to_tile(2), index_to_tile(3)],
        InitialMove(BoardPosition(0, 3), index_to_tile(2), Port.LeftTop, "red"),
    )
    assert r.is_error()
    assert (
        r.error()
        == "cannot make an initial move at position BoardPosition(x=0, y=3), port 7 since it does not face the interior of the board"
    )

    r = rc.validate_initial_move(
        bs,
        [index_to_tile(1), index_to_tile(2), index_to_tile(3)],
        InitialMove(BoardPosition(3, 0), index_to_tile(2), Port.TopRight, "red"),
    )
    assert r.is_error()
    assert (
        r.error()
        == "cannot make an initial move at position BoardPosition(x=3, y=0), port 1 since it does not face the interior of the board"
    )
Example #3
0
def test_validate_initial_move_middle() -> None:
    rc = RuleChecker()
    bs = BoardState()

    r = rc.validate_initial_move(
        bs,
        [index_to_tile(1), index_to_tile(2), index_to_tile(3)],
        InitialMove(BoardPosition(2, 3), index_to_tile(2), Port.BottomLeft, "red"),
    )
    assert r.is_error()
    assert (
        r.error()
        == "cannot make an initial move at position BoardPosition(x=2, y=3) since it is not on the edge"
    )
Example #4
0
def test_wrong_number_tile_choices() -> None:
    rc = RuleChecker()
    bs = BoardState()

    r = rc.validate_initial_move(
        bs,
        [index_to_tile(1)],
        InitialMove(BoardPosition(0, 2), index_to_tile(1), Port.BottomRight, "red"),
    )
    assert r.is_error()
    assert r.error() == "cannot validate move with 1 tile choices (expected 3)"
    r = rc.validate_initial_move(
        bs,
        [index_to_tile(1), index_to_tile(2), index_to_tile(3), index_to_tile(4)],
        InitialMove(BoardPosition(0, 2), index_to_tile(1), Port.BottomRight, "red"),
    )
    assert r.is_error()
    assert r.error() == "cannot validate move with 4 tile choices (expected 3)"

    r = rc.validate_move(
        bs, [index_to_tile(1)], IntermediateMove(index_to_tile(1), "red")
    )
    assert r.is_error()
    assert (
        r.error()
        == "cannot validate move with 1 tile choices (expected 2)"
    )
    r = rc.validate_move(
        bs,
        [index_to_tile(1), index_to_tile(3), index_to_tile(2)],
        IntermediateMove(index_to_tile(1), "red"),
    )
    assert r.is_error()
    assert (
        r.error()
        == "cannot validate move with 3 tile choices (expected 2)"
    )
Example #5
0
def test_validate_initial_move_on_top_of() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_tile(index_to_tile(5), BoardPosition(0, 2))
    r = rc.validate_initial_move(
        bs,
        [index_to_tile(1), index_to_tile(2), index_to_tile(3)],
        InitialMove(BoardPosition(0, 2), index_to_tile(2), Port.BottomLeft, "red"),
    )
    assert r.is_error()
    assert (
        r.error()
        == "cannot place tile at position BoardPosition(x=0, y=2) since there is already a tile at that position"
    )
Example #6
0
def test_validate_initial_move_not_in_choices() -> None:
    rc = RuleChecker()
    bs = BoardState()

    r = rc.validate_initial_move(
        bs,
        [index_to_tile(1), index_to_tile(3), index_to_tile(4)],
        InitialMove(BoardPosition(0, 2), index_to_tile(2), Port.BottomLeft, "red"),
    )
    assert r.is_error()
    assert (
        r.error()
        == "tile Tile(idx=2, edges=[(0, 5), (1, 4), (2, 7), (3, 6)]) is not in the list of tiles [Tile(idx=1, edges=[(0, 4), (1, 5), (2, 6), (3, 7)]), Tile(idx=3, edges=[(0, 4), (1, 3), (2, 6), (5, 7)]), Tile(idx=4, edges=[(0, 7), (1, 2), (3, 4), (5, 6)])] the player was given"
    )
    assert bs == BoardState()  # board state is unchanged
Example #7
0
def test_validate_initial_move_double_play() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(5, 0), Port.RightBottom))
    )
    copied = deepcopy(bs)
    r = rc.validate_initial_move(
        copied,
        [index_to_tile(1), index_to_tile(2), index_to_tile(3)],
        InitialMove(BoardPosition(0, 2), index_to_tile(2), Port.BottomLeft, "red"),
    )
    assert r.is_error()
    assert (
        r.error() == "cannot place player red since the player is already on the board"
    )
    assert bs == copied  # board state is unchanged
Example #8
0
def test_allow_collision() -> None:
    rc = RuleChecker()
    b = Board()

    move = InitialMove(BoardPosition(0, 0), index_to_tile(4), Port.BottomRight, "red")
    assert rc.validate_initial_move(
        b.get_board_state(),
        [index_to_tile(4), index_to_tile(5), index_to_tile(6)],
        move,
    ).is_ok()
    r = b.initial_move(move)
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(0, 0)) == index_to_tile(4)
    assert b.live_players["red"] == (BoardPosition(0, 0), Port.BottomRight)

    move = InitialMove(BoardPosition(2, 0), index_to_tile(22), Port.LeftBottom, "green")
    assert rc.validate_initial_move(
        b.get_board_state(),
        [index_to_tile(22), index_to_tile(5), index_to_tile(6)],
        move,
    ).is_ok()
    r = b.initial_move(move)
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(2, 0)) == index_to_tile(22)
    assert b.live_players["green"] == (BoardPosition(2, 0), Port.LeftBottom)

    move = InitialMove(BoardPosition(4, 0), index_to_tile(22), Port.LeftBottom, "blue")
    assert rc.validate_initial_move(
        b.get_board_state(),
        [index_to_tile(22), index_to_tile(5), index_to_tile(6)],
        move,
    ).is_ok()
    r = b.initial_move(move)
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(4, 0)) == index_to_tile(22)
    assert b.live_players["blue"] == (BoardPosition(4, 0), Port.LeftBottom)

    move = InitialMove(BoardPosition(6, 0), index_to_tile(22), Port.LeftBottom, "white")
    assert rc.validate_initial_move(
        b.get_board_state(),
        [index_to_tile(22), index_to_tile(5), index_to_tile(6)],
        move,
    ).is_ok()
    r = b.initial_move(move)
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(6, 0)) == index_to_tile(22)
    assert b.live_players["white"] == (BoardPosition(6, 0), Port.LeftBottom)

    move = InitialMove(BoardPosition(8, 0), index_to_tile(22), Port.LeftBottom, "black")
    assert rc.validate_initial_move(
        b.get_board_state(),
        [index_to_tile(22), index_to_tile(5), index_to_tile(6)],
        move,
    ).is_ok()
    r = b.initial_move(move)
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(8, 0)) == index_to_tile(22)
    assert b.live_players["black"] == (BoardPosition(8, 0), Port.LeftBottom)

    intermediate_move = IntermediateMove(index_to_tile(22), "black")
    assert rc.validate_move(
        b.get_board_state(), [index_to_tile(22), index_to_tile(5)], intermediate_move
    ).is_ok()
    r = b.intermediate_move(intermediate_move)
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(7, 0)) == index_to_tile(22)

    intermediate_move = IntermediateMove(index_to_tile(22), "black")
    assert rc.validate_move(
        b.get_board_state(), [index_to_tile(22), index_to_tile(5)], intermediate_move
    ).is_ok()
    r = b.intermediate_move(intermediate_move)
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(5, 0)) == index_to_tile(22)

    intermediate_move = IntermediateMove(index_to_tile(22), "black")
    assert rc.validate_move(
        b.get_board_state(), [index_to_tile(22), index_to_tile(5)], intermediate_move
    ).is_ok()
    r = b.intermediate_move(intermediate_move)
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(3, 0)) == index_to_tile(22)

    intermediate_move = IntermediateMove(index_to_tile(22), "black")
    assert rc.validate_move(
        b.get_board_state(), [index_to_tile(22), index_to_tile(5)], intermediate_move
    ).is_ok()
    r = b.intermediate_move(intermediate_move)
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(1, 0)) == index_to_tile(22)

    assert len(b._board_state.live_players) == 5
    assert len(set(b._board_state.live_players.values())) == 1
    assert b.live_players == pmap(
        {
            "black": (BoardPosition(x=0, y=0), 4),
            "blue": (BoardPosition(x=0, y=0), 4),
            "green": (BoardPosition(x=0, y=0), 4),
            "red": (BoardPosition(x=0, y=0), 4),
            "white": (BoardPosition(x=0, y=0), 4),
        }
    )