Пример #1
0
def test_generate_first_move_no_valid_moves() -> None:
    # No possible first moves
    second_s = SecondS()
    bs = BoardState()

    bs = bs.with_tile(index_to_tile(0), BoardPosition(2, 0))
    bs = bs.with_tile(index_to_tile(1), BoardPosition(3, 0))
    bs = bs.with_tile(index_to_tile(2), BoardPosition(5, 0))
    bs = bs.with_tile(index_to_tile(3), BoardPosition(7, 0))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(9, 0))
    bs = bs.with_tile(index_to_tile(5), BoardPosition(9, 2))
    bs = bs.with_tile(index_to_tile(6), BoardPosition(9, 4))
    bs = bs.with_tile(index_to_tile(7), BoardPosition(9, 6))
    bs = bs.with_tile(index_to_tile(8), BoardPosition(9, 8))
    bs = bs.with_tile(index_to_tile(9), BoardPosition(8, 9))
    bs = bs.with_tile(index_to_tile(10), BoardPosition(6, 9))
    bs = bs.with_tile(index_to_tile(11), BoardPosition(4, 9))
    bs = bs.with_tile(index_to_tile(12), BoardPosition(2, 9))
    bs = bs.with_tile(index_to_tile(13), BoardPosition(0, 9))
    bs = bs.with_tile(index_to_tile(14), BoardPosition(0, 7))
    bs = bs.with_tile(index_to_tile(15), BoardPosition(0, 5))
    bs = bs.with_tile(index_to_tile(16), BoardPosition(0, 3))
    bs = bs.with_tile(index_to_tile(17), BoardPosition(0, 1))

    r = second_s.generate_first_move(
        [index_to_tile(22),
         index_to_tile(23),
         index_to_tile(4)], bs)
    assert r.is_error()
    assert r.error() == "Failed to find a valid initial move!"
Пример #2
0
def test_generate_first_move_0_0() -> None:
    # The first move is placing a tile at 0,0 since there are tiles blocking the other positions
    second_s = SecondS()
    bs = BoardState()

    bs = bs.with_tile(index_to_tile(0), BoardPosition(2, 0))
    bs = bs.with_tile(index_to_tile(1), BoardPosition(3, 0))
    bs = bs.with_tile(index_to_tile(2), BoardPosition(5, 0))
    bs = bs.with_tile(index_to_tile(3), BoardPosition(7, 0))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(9, 0))
    bs = bs.with_tile(index_to_tile(5), BoardPosition(9, 2))
    bs = bs.with_tile(index_to_tile(6), BoardPosition(9, 4))
    bs = bs.with_tile(index_to_tile(7), BoardPosition(9, 6))
    bs = bs.with_tile(index_to_tile(8), BoardPosition(9, 8))
    bs = bs.with_tile(index_to_tile(9), BoardPosition(8, 9))
    bs = bs.with_tile(index_to_tile(10), BoardPosition(6, 9))
    bs = bs.with_tile(index_to_tile(11), BoardPosition(4, 9))
    bs = bs.with_tile(index_to_tile(12), BoardPosition(2, 9))
    bs = bs.with_tile(index_to_tile(13), BoardPosition(0, 9))
    bs = bs.with_tile(index_to_tile(14), BoardPosition(0, 7))
    bs = bs.with_tile(index_to_tile(15), BoardPosition(0, 5))
    bs = bs.with_tile(index_to_tile(16), BoardPosition(0, 3))
    bs = bs.with_tile(index_to_tile(17), BoardPosition(0, 2))

    r = second_s.generate_first_move(
        [index_to_tile(22),
         index_to_tile(23),
         index_to_tile(4)], bs)
    assert r.assert_value() == (
        BoardPosition(x=0, y=0),
        index_to_tile(4),
        Port.RightTop,
    )
Пример #3
0
def test_incorrect_number_tiles_given() -> None:
    second_s = SecondS()
    bs = BoardState()

    r = second_s.generate_move(
        [index_to_tile(1),
         index_to_tile(2),
         index_to_tile(3)], bs)
    assert r.is_error()
    assert r.error() == "Strategy.generate_move given 3 (expected 2)"
    r2 = second_s.generate_first_move([index_to_tile(1), index_to_tile(2)], bs)
    assert r2.is_error()
    assert r2.error() == "Strategy.generate_first_move given 2 (expected 3)"
Пример #4
0
def test_generate_first_move_1_0() -> None:
    # The first move is just placing a tile at 0,1
    second_s = SecondS()
    bs = BoardState()

    r = second_s.generate_first_move(
        [index_to_tile(22),
         index_to_tile(23),
         index_to_tile(24)], bs)
    assert r.assert_value() == (
        BoardPosition(x=1, y=0),
        index_to_tile(24),
        Port.RightTop,
    )
Пример #5
0
def test_generate_first_move_9_2() -> None:
    # The first move is placing a tile at 9,2 since there are tiles blocking the other positions
    second_s = SecondS()
    bs = BoardState()

    bs = bs.with_tile(index_to_tile(0), BoardPosition(1, 0))
    bs = bs.with_tile(index_to_tile(1), BoardPosition(3, 0))
    bs = bs.with_tile(index_to_tile(2), BoardPosition(5, 0))
    bs = bs.with_tile(index_to_tile(3), BoardPosition(7, 0))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(9, 0))

    r = second_s.generate_first_move(
        [index_to_tile(22),
         index_to_tile(23),
         index_to_tile(4)], bs)
    assert r.assert_value() == (BoardPosition(x=9, y=2), index_to_tile(4),
                                Port.TopLeft)
Пример #6
0
def test_generate_move_no_valid_moves() -> None:
    # No possible moves, chooses first option without rotation
    second_s = SecondS()
    second_s.set_color(AllColors[0])
    second_s.set_rule_checker(RuleChecker())
    b = Board()
    assert b.initial_move(
        InitialMove(BoardPosition(1, 0), index_to_tile(34), Port.BottomRight,
                    second_s.color)).is_ok()

    tiles = [
        Tile(
            cast(
                List[Tuple[PortID, PortID]],
                [
                    tuple(Port.all()[i:i + 2])
                    for i in range(0, len(Port.all()), 2)
                ],
            )),
        Tile(
            cast(
                List[Tuple[PortID, PortID]],
                [
                    tuple(Port.all()[i:i + 2])
                    for i in range(0, len(Port.all()), 2)
                ],
            )),
    ]
    r = second_s.generate_move(tiles, b.get_board_state())
    assert id(r.assert_value()) == id(tiles[0])
Пример #7
0
def test_generate_first_move_5_0() -> None:
    # The first move is placing a tile at 5,0 since there are tiles blocking the other positions
    second_s = SecondS()
    bs = BoardState()

    bs = bs.with_tile(index_to_tile(0), BoardPosition(1, 0))
    bs = bs.with_tile(index_to_tile(0), BoardPosition(1, 0))
    bs = bs.with_tile(index_to_tile(1), BoardPosition(3, 0))

    r = second_s.generate_first_move(
        [index_to_tile(22),
         index_to_tile(23),
         index_to_tile(3)], bs)
    assert r.assert_value() == (
        BoardPosition(x=5, y=0),
        index_to_tile(3),
        Port.RightTop,
    )
Пример #8
0
def test_admin_by_function() -> None:
    tobs = TournamentObserver()
    admin = Administrator(SimpleBracketStrategy())
    admin.add_observer(tobs)

    pid_to_idx = {}
    for idx in range(20):
        pid_r = admin.add_player(Player(SecondS()))
        assert pid_r.is_ok()
        pid_to_idx[pid_r.value()] = idx
        time.sleep(0.1)
    
    admin.run_tournament()
Пример #9
0
def test_generate_first_move_4_9() -> None:
    # The first move is placing a tile at 4,9 since there are tiles blocking the other positions
    second_s = SecondS()
    bs = BoardState()

    bs = bs.with_tile(index_to_tile(0), BoardPosition(1, 0))
    bs = bs.with_tile(index_to_tile(1), BoardPosition(3, 0))
    bs = bs.with_tile(index_to_tile(2), BoardPosition(5, 0))
    bs = bs.with_tile(index_to_tile(3), BoardPosition(7, 0))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(9, 0))
    bs = bs.with_tile(index_to_tile(5), BoardPosition(9, 2))
    bs = bs.with_tile(index_to_tile(6), BoardPosition(9, 4))
    bs = bs.with_tile(index_to_tile(7), BoardPosition(9, 6))
    bs = bs.with_tile(index_to_tile(8), BoardPosition(9, 8))
    bs = bs.with_tile(index_to_tile(9), BoardPosition(8, 9))
    bs = bs.with_tile(index_to_tile(10), BoardPosition(6, 9))

    r = second_s.generate_first_move(
        [index_to_tile(22),
         index_to_tile(23),
         index_to_tile(4)], bs)
    assert r.assert_value() == (BoardPosition(x=4, y=9), index_to_tile(4),
                                Port.TopLeft)
Пример #10
0
def test_generate_move_takes_first_tile_no_rotation() -> None:
    # Test that the first tile is taken when valid
    second_s = SecondS()
    second_s.set_color(AllColors[0])
    second_s.set_rule_checker(RuleChecker())
    b = Board()
    b.initial_move(
        InitialMove(BoardPosition(4, 0), index_to_tile(34), Port.BottomRight,
                    second_s.color))

    tiles = [index_to_tile(6), index_to_tile(34)]
    r = second_s.generate_move(tiles, b.get_board_state())
    assert r.assert_value().edges == tiles[0].edges
Пример #11
0
def test_admin_10_second_s() -> None:

    # Assert that it runs in a reasonable amount of time
    with timeout(seconds=10):
        admin = Administrator(SimpleBracketStrategy())
        pid_to_idx = {}
        for idx in range(10):
            pid_r = admin.add_player(Player(SecondS()))
            assert pid_r.is_ok()
            pid_to_idx[pid_r.value()] = idx
        winners_r = admin.run_tournament()
        assert winners_r.is_ok()
        real_winners = winners_r.value()[0][0]
        assert len(real_winners)
        assert set([pid_to_idx[pid] for pid in real_winners]) == {1, 5}
Пример #12
0
def test_admin_5_second_s() -> None:
    admin = Administrator(SimpleBracketStrategy())
    pid_to_idx = {}
    players = []
    for idx in range(5):
        player = Mock(wraps=Player(SecondS()))
        players.append(player)
        pid_r = admin.add_player(player)
        assert pid_r.is_ok()
        pid_to_idx[pid_r.value()] = idx
    winners_r = admin.run_tournament()
    assert winners_r.is_ok()
    real_winners = winners_r.value()[0][0]
    assert set([pid_to_idx[pid] for pid in real_winners]) == {0, 2}
    assert players[0].notify_won_tournament.call_args_list == [call(True)]
    assert players[1].notify_won_tournament.call_args_list == [call(False)]
    assert players[2].notify_won_tournament.call_args_list == [call(True)]
    assert players[3].notify_won_tournament.call_args_list == [call(False)]
    assert players[4].notify_won_tournament.call_args_list == [call(False)]