コード例 #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
コード例 #2
0
def test_place_tile_remove_other_player() -> None:
    # Black kills red by driving them off the edge of the board
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 2), Port.RightBottom))
    )
    bs = bs.with_live_players(
        bs.live_players.set("black", (BoardPosition(4, 2), Port.LeftTop))
    )
    bs = bs.with_tile(index_to_tile(2), BoardPosition(2, 0))
    bs = bs.with_tile(index_to_tile(2), BoardPosition(2, 1))
    bs = bs.with_tile(index_to_tile(3), BoardPosition(2, 2))

    bs = bs.with_tile(index_to_tile(2), BoardPosition(4, 0))
    bs = bs.with_tile(index_to_tile(2), BoardPosition(4, 1))
    bs = bs.with_tile(index_to_tile(32).rotate(), BoardPosition(4, 2))

    r = rc.is_move_suicidal(bs, IntermediateMove(index_to_tile(2), "red"))
    assert r.is_ok()
    assert r.value()
    move = IntermediateMove(index_to_tile(2), "black")
    r = rc.is_move_suicidal(bs, move)
    assert r.is_ok()
    assert not r.value()
    assert rc.validate_move(bs, [index_to_tile(2), index_to_tile(3)], move).is_ok()
    board = Board(bs)
    assert board.intermediate_move(move).is_ok()

    assert "red" not in board.live_players
    assert board.live_players["black"] == (BoardPosition(x=2, y=2), 6)
    bs = bs.with_tile(index_to_tile(2), BoardPosition(4, 2))
コード例 #3
0
def main() -> None:
    """
    Connect to the server at host:port via TCP to render a board state to the GUI via a GraphicalPlayerObserver
    """
    json_stream = StdinStdoutJSONStream()

    state_pats_json_r = json_stream.receive_message()
    board = setup_board(state_pats_json_r.assert_value())

    turn_pat_json = json_stream.receive_message().assert_value()
    act_pat = ActionPat.from_json(turn_pat_json[0]  # pylint: disable=unsubscriptable-object
                                  )
    offered_tiles: List[Tile] = [
        index_to_tile(tile_idx) for tile_idx in turn_pat_json[1:]  # pylint: disable=unsubscriptable-object
    ]
    move = IntermediateMove(
        tile_pattern_to_tile(act_pat.tile_pat.tile_index,
                             act_pat.tile_pat.rotation_angle),
        act_pat.player,
    )
    rule_checker = RuleChecker()
    r = rule_checker.validate_move(board.get_board_state(), offered_tiles,
                                   move)
    if r.is_error():
        json_stream.send_message("cheating")
    else:
        json_stream.send_message("legal")

    if DEBUG:
        board.get_board_state().debug_display_board()
        r = board.intermediate_move(move)
        if r.is_error():
            print("Failed to render #2")
        else:
            board.get_board_state().debug_display_board()
コード例 #4
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"
    )
コード例 #5
0
def test_is_move_suicidal_inexistent() -> None:
    rc = RuleChecker()
    bs = BoardState()

    r = rc.is_move_suicidal(bs, IntermediateMove(index_to_tile(22), "red"))
    assert r.is_error()
    assert r.error() == "player red is not alive thus the move cannot be suicidal"
コード例 #6
0
def test_validate_place_tile_loop_someone_else() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 0), Port.RightBottom))
    )
    bs = bs.with_live_players(
        bs.live_players.set("white", (BoardPosition(4, 0), Port.LeftTop))
    )
    bs = bs.with_tile(index_to_tile(4), BoardPosition(2, 0))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(2, 1))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(3, 1))
    bs = bs.with_tile(index_to_tile(2), BoardPosition(4, 0))

    # The move is suicidal for red but not for white
    r = rc.is_move_suicidal(bs, IntermediateMove(index_to_tile(13).rotate(), "white"))
    assert r.is_ok()
    assert not r.value()
    r = rc.move_creates_loop(bs, IntermediateMove(index_to_tile(13).rotate(), "red"))
    assert r.is_ok()
    assert r.value()
    # But it does create a loop no matter who places it
    r = rc.move_creates_loop(bs, IntermediateMove(index_to_tile(13).rotate(), "white"))
    assert r.is_ok()
    assert r.value()
    r = rc.move_creates_loop(bs, IntermediateMove(index_to_tile(13).rotate(), "red"))
    assert r.is_ok()
    assert r.value()
    # And thus it is illegal
    r = rc.is_move_illegal(bs, IntermediateMove(index_to_tile(13).rotate(), "white"))
    assert r.is_ok()
    assert r.value()
    r = rc.is_move_illegal(bs, IntermediateMove(index_to_tile(13).rotate(), "red"))
    assert r.is_ok()
    assert r.value()

    # Red and white are both not allowed to place the move since it causes a loop
    r2 = rc.validate_move(
        bs,
        [index_to_tile(13).rotate().rotate(), index_to_tile(6)],
        IntermediateMove(index_to_tile(13).rotate(), "white"),
    )
    assert r2.is_error()
    assert (
        r2.error()
        == "player chose a loopy move when this does not create a loop: Tile(idx=13, edges=[(0, 7), (1, 2), (3, 5), (4, 6)])"
    )
    r2 = rc.validate_move(
        bs,
        [index_to_tile(13).rotate().rotate(), index_to_tile(6)],
        IntermediateMove(index_to_tile(13).rotate(), "red"),
    )
    assert r2.is_error()
    assert (
        r2.error()
        == "player chose a loopy move when this does not create a loop: Tile(idx=13, edges=[(0, 7), (1, 2), (3, 5), (4, 6)])"
    )
コード例 #7
0
def test_validate_place_tile_inexistent_player() -> None:
    rc = RuleChecker()
    bs = BoardState()

    r = rc.validate_move(
        bs,
        [index_to_tile(1), index_to_tile(2)],
        IntermediateMove(index_to_tile(1), "red"),
    )
    assert r.is_error()
    assert r.error() == "cannot place a tile for player red since they are not alive"
コード例 #8
0
def test_is_move_suicidal_walk_off_edge() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 0), Port.RightTop))
    )
    bs = bs.with_tile(index_to_tile(2), BoardPosition(2, 0))
    r = rc.is_move_suicidal(bs, IntermediateMove(index_to_tile(4), "red"))
    assert r.is_ok()
    assert r.value()
コード例 #9
0
def test_is_move_suicidal_loop() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 0), Port.RightBottom))
    )
    bs = bs.with_tile(index_to_tile(4), BoardPosition(2, 0))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(2, 1))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(3, 1))
    r = rc.move_creates_loop(bs, IntermediateMove(index_to_tile(4), "red"))
    assert r.is_ok()
    assert r.value()
コード例 #10
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"
    )
コード例 #11
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
コード例 #12
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"
    )
コード例 #13
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])
コード例 #14
0
 def _initialize_ref(self) -> Referee:
     """
     Sets up a ref to run a game.
     """
     ref = Referee()
     ref.set_rule_checker(RuleChecker())
     ref.set_tile_iterator(deterministic_tile_iterator())
     return ref
コード例 #15
0
def test_validate_place_tile_required_suicide() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 0), Port.RightTop))
    )
    bs = bs.with_tile(index_to_tile(2), BoardPosition(2, 0))
    r = rc.is_move_suicidal(bs, IntermediateMove(index_to_tile(4), "red"))
    assert r.is_ok()
    assert r.value()
    r2 = rc.validate_move(
        bs,
        [index_to_tile(4), index_to_tile(4)],
        IntermediateMove(index_to_tile(4).rotate(), "red"),
    )
    assert r2.is_ok()
コード例 #16
0
def test_validate_place_tile_not_in_choices() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 0), Port.TopRight))
    )
    r = rc.validate_move(
        bs,
        [index_to_tile(2), index_to_tile(3)],
        IntermediateMove(index_to_tile(1), "red"),
    )
    assert r.is_error()
    assert (
        r.error()
        == "tile Tile(idx=1, edges=[(0, 4), (1, 5), (2, 6), (3, 7)]) is not in the list of tiles [Tile(idx=2, edges=[(0, 5), (1, 4), (2, 7), (3, 6)]), Tile(idx=3, edges=[(0, 4), (1, 3), (2, 6), (5, 7)])] the player was given"
    )
コード例 #17
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
コード例 #18
0
def test_both_moves_illegal() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_tile(index_to_tile(4), BoardPosition(2, 1))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(3, 1))
    bs = bs.with_tile(index_to_tile(24), BoardPosition(2, 0))
    bs = bs.with_tile(index_to_tile(22), BoardPosition(4, 0))
    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 0), Port.RightBottom)).set(
            "white", (BoardPosition(4, 0), Port.LeftBottom)
        )
    )
    move = IntermediateMove(index_to_tile(24), "white")
    r = rc.validate_move(bs, [index_to_tile(24), index_to_tile(24)], move)
    assert r.is_error()
    assert (
        r.error()
        == "player chose a loopy move when this does not create a loop: Tile(idx=24, edges=[(0, 7), (1, 2), (3, 6), (4, 5)])"
    )
コード例 #19
0
def test_validate_place_tile_unnecessary_loop() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 0), Port.RightBottom))
    )
    bs = bs.with_tile(index_to_tile(4), BoardPosition(2, 0))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(2, 1))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(3, 1))
    r = rc.move_creates_loop(bs, IntermediateMove(index_to_tile(4), "red"))
    assert r.is_ok()
    assert r.value()
    r = rc.move_creates_loop(bs, IntermediateMove(index_to_tile(5), "red"))
    assert r.is_ok()
    assert r.value()
    r = rc.is_move_suicidal(bs, IntermediateMove(index_to_tile(5).rotate(), "red"))
    assert r.is_ok()
    assert not r.value()

    r2 = rc.validate_move(
        bs,
        [index_to_tile(5), index_to_tile(4)],
        IntermediateMove(index_to_tile(4), "red"),
    )
    assert r2.is_error()
    assert (
        r2.error()
        == "player chose a loopy move when this does not create a loop: Tile(idx=5, edges=[(0, 7), (1, 5), (2, 6), (3, 4)])"
    )
コード例 #20
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
コード例 #21
0
 def __init__(self, name, color, connection, address):
     logging.basicConfig(filename="server.log",
                         level=logging.DEBUG,
                         format='\n%(asctime)s %(message)s')
     logging.info("Player proxy created for " + name + " with color " +
                  color)
     self.color = color
     self.name = name
     self.connection = connection
     self.address = address
     self.board = Board()
     self.rule_check = RuleChecker()
     self.converter = Converter()
     self.tile_dict = self.converter.generate_tile_dictionary()
コード例 #22
0
def test_validate_place_tile_unnecessary_suicide() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 0), Port.RightTop))
    )
    bs = bs.with_tile(index_to_tile(2), BoardPosition(2, 0))
    r = rc.is_move_suicidal(bs, IntermediateMove(index_to_tile(4), "red"))
    assert r.is_ok()
    assert r.value()
    r = rc.is_move_suicidal(bs, IntermediateMove(index_to_tile(3), "red"))
    assert r.is_ok()
    assert not r.value()
    r2 = rc.validate_move(
        bs,
        [index_to_tile(3), index_to_tile(4)],
        IntermediateMove(index_to_tile(4), "red"),
    )
    assert r2.is_error()
    assert (
        r2.error()
        == "player chose a suicidal move when this does not cause a suicide: Tile(idx=3, edges=[(0, 4), (1, 3), (2, 6), (5, 7)])"
    )
コード例 #23
0
def test_generate_move_needs_rotation() -> None:
    # Test that the first tile is rotated to a valid rotation when the second tile is invalid
    third_s = ThirdS()
    third_s.set_color(AllColors[0])
    third_s.set_rule_checker(RuleChecker())
    b = Board()
    b.initial_move(
        InitialMove(
            BoardPosition(9, 0), index_to_tile(34), Port.BottomRight, third_s.color
        )
    )

    tiles = [index_to_tile(11), index_to_tile(34)]
    r = third_s.generate_move(tiles, b.get_board_state())
    assert r.assert_value().edges == tiles[0].rotate().edges
コード例 #24
0
def test_run_game_without_init() -> None:
    ref = Referee()

    r = ref.run_game()
    assert r.is_error()
    assert r.error() == "must add players to this referee"
    assert ref.set_players(make_players(3)).is_ok()

    r = ref.run_game()
    assert r.is_error()
    assert r.error() == "must add a rule checker to this referee"
    ref.set_rule_checker(RuleChecker())

    r = ref.run_game()
    assert r.is_error()
    assert r.error() == "must add a tile iterator to this referee"
    ref.set_tile_iterator(deterministic_tile_iterator())

    r = ref.run_game()
    assert r.is_ok()
コード例 #25
0
ファイル: server.py プロジェクト: meganmelau/Tsuro
 def __init__(self, ip, port):
     self.ip = ip
     self.port = port
     self.player_proxies = []
     self.connections = {}
     self.board = Board()
     self.rule_checker = RuleChecker()
     self.referee = None
     self.colors = ["white", "black", "red", "green", "blue"]
     self.deck = Converter().generate_tile_dictionary()
     try:
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.socket.bind((ip, int(port)))
     except Exception as e:
         print("Failed Connection, shutting down.", e)
         return
     enough_players = self.wait_for_players()
     if enough_players:
         result = self.start_game()
         self.send_all_players(json.dumps(["game over", result]))
         print(result)
コード例 #26
0
def test_validate_place_tile_forced_loop_or_suicide() -> None:
    rc = RuleChecker()
    bs = BoardState()

    bs = bs.with_live_players(
        bs.live_players.set("red", (BoardPosition(2, 0), Port.RightBottom))
    )
    bs = bs.with_tile(index_to_tile(4), BoardPosition(2, 0))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(2, 1))
    bs = bs.with_tile(index_to_tile(4), BoardPosition(3, 1))
    r = rc.move_creates_loop(bs, IntermediateMove(index_to_tile(4), "red"))
    assert r.is_ok()
    assert r.value()
    r = rc.is_move_suicidal(bs, IntermediateMove(index_to_tile(7), "red"))
    assert r.is_ok()
    assert r.value()

    r2 = rc.validate_move(
        bs,
        [index_to_tile(7), index_to_tile(4)],
        IntermediateMove(index_to_tile(4), "red"),
    )
    assert r2.is_error()
    assert (
        r2.error()
        == "player chose a loopy move when this does not create a loop: Tile(idx=7, edges=[(0, 3), (1, 6), (2, 5), (4, 7)])"
    )

    r3 = rc.validate_move(
        bs,
        [index_to_tile(7), index_to_tile(4)],
        IntermediateMove(index_to_tile(7), "red"),
    )
    assert r3.is_error()
    assert (
        r3.error()
        == "player chose a suicidal move when this does not cause a suicide: Tile(idx=4, edges=[(0, 7), (1, 2), (3, 4), (5, 6)])"
    )
コード例 #27
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)"
    )
コード例 #28
0
 def __init__(self):
     self.rule_checker = RuleChecker()
コード例 #29
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),
        }
    )
コード例 #30
0
ファイル: xref.py プロジェクト: meganmelau/Tsuro
import sys
import inspect
current_dir = os.path.dirname(
    os.path.abspath(inspect.getfile(inspect.currentframe())))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)

import json
from Admin.referee import Referee
from Common.board import Board
import Common.tile
from Common.tile_conversion import Converter
from Player.player import DumbPlayer
from Common.rules import RuleChecker

color = ["white", "black", "red", "green", "blue"]
if __name__ == "__main__":
    input_content = sys.stdin.readline()
    json_array = json.loads(input_content)
    if len(json_array) > 5 or len(json_array) < 3:
        raise Exception("Must have 3 to 5 players.")
    # Construct players
    player_list = []
    for i in range(len(json_array)):
        player_list.append(DumbPlayer(json_array[i], color[i]))
    game_board = Board()
    rule_checker = RuleChecker()
    deck = Converter().generate_tile_dictionary()
    manager = Referee(player_list, game_board, rule_checker, deck)
    win_order, elimination = manager.run_game()
    print(json.dumps({"winners": win_order, "losers": elimination}))