Esempio n. 1
0
def test_board_intermediate_move_loop_moves_no_one_else() -> None:
    # If player A places a tile that sends them into a loop and player B is next to the would be placed tile,
    # player B does not move
    b = Board()

    logging_observer = LoggingObserver()
    b.add_observer(logging_observer)

    # An initial move for green
    r = b.initial_move(
        InitialMove(BoardPosition(0, 1), index_to_tile(4), Port.RightBottom,
                    "green"))
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(0, 1)) == index_to_tile(4)
    assert b.live_players["green"] == (BoardPosition(0, 1), Port.RightBottom)

    # More moves for green to setup what we need to make a loop
    r = b.intermediate_move(IntermediateMove(index_to_tile(4), "green"))
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(1, 1)) == index_to_tile(4)
    assert b._board_state.live_players["green"] == (
        BoardPosition(1, 1),
        Port.BottomLeft,
    )

    r = b.intermediate_move(IntermediateMove(index_to_tile(4), "green"))
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(1, 2)) == index_to_tile(4)
    assert b._board_state.live_players["green"] == (BoardPosition(1, 2),
                                                    Port.LeftTop)

    # And set up white next to the where the would-be loop would be created
    assert b.place_tile_at_index_with_scissors(index_to_tile(4),
                                               BoardPosition(0, 3)).is_ok()
    b._board_state = b._board_state.with_live_players(
        b._board_state.live_players.set("white",
                                        (BoardPosition(0, 3), Port.TopRight)))

    # This tile placement will make a loop. Test that it is detected, the player is removed, and a message is broadcast
    # to all of the defined observers. According to assignment 6 the player should be removed but the tile
    # should not be on the board.
    assert logging_observer.all_messages() == []
    r = b.intermediate_move(IntermediateMove(index_to_tile(4), "green"))
    assert r.is_ok()
    assert "green" not in b.live_players
    assert b._board_state.get_tile(BoardPosition(0, 2)) is None
    assert logging_observer.all_messages() == [
        "entered_loop: green",
        "exited_board: green",
    ]

    # And white is still in the same place
    assert b._board_state.live_players["white"] == (BoardPosition(0, 3),
                                                    Port.TopRight)
Esempio n. 2
0
def test_board_initial_move() -> None:
    b = Board()

    logging_observer = LoggingObserver()
    b.add_observer(logging_observer)

    pos = BoardPosition(1, 1)
    r = b.initial_move(
        InitialMove(pos,
                    make_tiles()[0], Port.BottomRight, "red"))
    assert r.is_error()
    assert (
        r.error() ==
        f"cannot make an initial move at position {pos} since it is not on the edge"
    )

    pos = BoardPosition(3, 0)
    port = Port.TopLeft
    r = b.initial_move(InitialMove(pos, make_tiles()[0], port, "red"))
    assert r.is_error()
    assert (
        r.error() ==
        f"cannot make an initial move at position {pos}, port {port} since it does not face the interior of the "
        f"board")

    b.place_tile_at_index_with_scissors(make_tiles()[0], BoardPosition(2, 0))
    r = b.initial_move(
        InitialMove(BoardPosition(2, 0),
                    make_tiles()[1], Port.BottomRight, "blue"))
    assert r.is_error()
    assert (
        r.error() ==
        "cannot place tile at position BoardPosition(x=2, y=0) since there is already a tile at that position"
    )

    r = b.initial_move(
        InitialMove(BoardPosition(3, 0),
                    make_tiles()[1], Port.BottomRight, "blue"))
    assert r.is_error()
    assert (
        r.error() ==
        "cannot make an initial move at position BoardPosition(x=3, y=0) since the surrounding tiles are not all "
        "empty")

    r = b.initial_move(
        InitialMove(BoardPosition(1, 0),
                    make_tiles()[1], Port.BottomRight, "blue"))
    assert r.is_error()
    assert (
        r.error() ==
        "cannot make an initial move at position BoardPosition(x=1, y=0) since the surrounding tiles are not all "
        "empty")

    b._board_state = b._board_state.with_live_players(
        b._board_state.live_players.set(
            "red", (BoardPosition(2, 0), Port.BottomRight)))
    r = b.initial_move(
        InitialMove(BoardPosition(5, 0),
                    make_tiles()[1], Port.BottomRight, "red"))
    assert r.is_error()
    assert (r.error() ==
            "cannot place player red since the player is already on the board")

    r = b.initial_move(
        InitialMove(BoardPosition(5, 0),
                    make_tiles()[2], Port.BottomRight, "blue"))
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(5, 0)) == make_tiles()[2]
    assert b._board_state.live_players["blue"] == (
        BoardPosition(5, 0),
        Port.BottomRight,
    )

    r = b.initial_move(
        InitialMove(BoardPosition(9, 2),
                    make_tiles()[2], Port.TopLeft, "white"))
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(9, 2)) == make_tiles()[2]
    b._board_state = b._board_state.with_live_players(
        b._board_state.live_players.set("white",
                                        (BoardPosition(9, 2), Port.TopLeft)))

    r = b.initial_move(
        InitialMove(BoardPosition(9, 4),
                    make_tiles()[2], Port.TopLeft, "green"))
    assert r.is_ok()
    assert b._board_state.get_tile(BoardPosition(9, 4)) == make_tiles()[2]
    assert b._board_state.live_players["green"] == (BoardPosition(9, 4),
                                                    Port.TopLeft)

    assert logging_observer.all_messages() == []