コード例 #1
0
    def test_get_possible_moves_overwrite_on_other_player(self):
        board = Board("""\
        2
        1
        0 0
        3 3
        2 0 0
        0 2 0
        0 0 1
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE, use_overwrite=True)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_ONE, EMPTY, EMPTY],
                           [EMPTY, PLAYER_ONE, EMPTY],
                           [EMPTY, EMPTY, PLAYER_ONE]])
        self.assertEqual(0, next_game_states[0].player_overwrites[PLAYER_ONE])

        def test_get_possible_moves_can_not_move_on_other_player(self):
            board = Board("""\
            2
            0
            0 0
            3 3
            2 0 0
            0 2 0
            0 0 1
            """)
            game = GameState(board)

            next_game_states = game.get_possible_moves_for_player(PLAYER_ONE, use_overwrite=True)
            self.assertEqual(0, len(next_game_states))
コード例 #2
0
    def test_get_possible_moves_three_player_choice(self):
        board = Board("""\
        3
        0
        0 0
        3 3
        2 1 3
        0 2 0
        0 c 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(3, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_TWO, PLAYER_ONE, PLAYER_THREE],
                           [EMPTY, PLAYER_ONE, EMPTY],
                           [EMPTY, PLAYER_ONE, EMPTY]])
        self.assert_board(next_game_states[1].board,
                          [[PLAYER_ONE, PLAYER_TWO, PLAYER_THREE],
                           [EMPTY, PLAYER_TWO, EMPTY],
                           [EMPTY, PLAYER_TWO, EMPTY]])
        self.assert_board(next_game_states[2].board,
                          [[PLAYER_TWO, PLAYER_THREE, PLAYER_ONE],
                           [EMPTY, PLAYER_THREE, EMPTY],
                           [EMPTY, PLAYER_THREE, EMPTY]])
コード例 #3
0
    def test_get_possible_moves_boese01(self):
        board = Board("""\
        2
        0
        0 0
        8 11
        - - - - - - 0 - - - - 
        - - - - - - 2 - - - - 
        - 2 2 2 2 2 2 2 1 0 0 
        - - - - - - 2 - - - - 
        - - - - - - 2 - - - - 
        - - - - - - 2 - - - - 
        - - - - - - 2 - - - - 
        - - - - - - 2 - - - - 
        1 2 6 <-> 6 7 4
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, EMPTY, EMPTY],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE]])
コード例 #4
0
    def test_get_possible_moves_boese07(self):
        board = Board("""\
        2
        1
        0 0
        8 11
        - - - - - - 1 - - - - 
        - - - - - - 2 - - - - 
        - 2 2 2 2 2 2 2 1 1 0 
        - - - - - - 2 - - - - 
        - - - - - - 2 - - - - 
        - - - - - - 2 - - - - 
        - - - - - - 2 - - - - 
        - - - - - - 2 - - - - 
        1 2 6 <-> 6 7 4
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_on_position((6, 2), player=PLAYER_ONE, use_overwrite=True)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, PLAYER_TWO, PLAYER_TWO, PLAYER_TWO, PLAYER_TWO, PLAYER_TWO, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, EMPTY],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, PLAYER_TWO, HOLE, HOLE, HOLE, HOLE]])
コード例 #5
0
    def test_get_possible_moves_boese_lost_trace(self):
        board = Board("""\
        2
        0
        0 0
        8 9
        - - - - - - - - -
        - - 2 - - - 2 - -
        - 2 2 2 0 2 2 2 -
        - - 2 - - - 2 - -
        - - 2 - - - 2 - -
        - - 2 - - - 2 - -
        - - 1 - - - 1 - -
        - - - - - - - - -
        7 2 2 <-> 2 1 0
        1 2 6 <-> 6 1 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_on_position((4, 2), player=PLAYER_ONE, use_overwrite=True)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE],
                           [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE],
                           [HOLE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, HOLE],
                           [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE],
                           [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE],
                           [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE],
                           [HOLE, HOLE, PLAYER_ONE, HOLE, HOLE, HOLE, PLAYER_ONE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE, HOLE]])
コード例 #6
0
        def test_get_possible_moves_can_not_move_on_other_player(self):
            board = Board("""\
            2
            0
            0 0
            3 3
            2 0 0
            0 2 0
            0 0 1
            """)
            game = GameState(board)

            next_game_states = game.get_possible_moves_for_player(PLAYER_ONE, use_overwrite=True)
            self.assertEqual(0, len(next_game_states))
コード例 #7
0
    def test_get_possible_moves_expansion_only_with_overwrite(self):
        board = Board("""\
        2
        0
        0 0
        3 3
        0 0 0
        0 x 0
        0 0 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(Field.PLAYER_ONE, use_overwrite=True)
        self.assertEqual(0, len(next_game_states))
コード例 #8
0
    def run(self):
        self.client.start()
        self.game_state = GameState(self.client.board)

        if self.game_start_callback:
            self.game_start_callback(self.game_state, self.client.player)

        self._game_loop()
        self.logger.info("Game Ended")

        if self.game_end_callback:
            self.game_end_callback(self.game_state)

        self.client.stop()
コード例 #9
0
    def test_get_possible_moves_must_capture_minimum_one_enemy(self):
        board = Board("""\
        2
        1
        0 0
        3 3
        1 0 0
        0 0 0
        0 0 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(0, len(next_game_states))
コード例 #10
0
    def test_get_possible_moves_boese02(self):
        board = Board("""\
        2
        0
        0 0
        2 10
        2 2 2 0 2 2 2 2 2 2 
        - - - - - 1 2 0 - - 
        0 0 6 <-> 9 0 2
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_on_position((3, 0), player=PLAYER_ONE)
        self.assertEqual(0, len(next_game_states))
コード例 #11
0
    def test_normal_scoring(self):
        board = Board("""\
        2
        1
        0 0
        3 3
        1 0 0
        0 0 0
        0 0 0
        """)
        game = GameState(board)
        scores = game.calculate_scores()

        self.assertEqual(scores[PLAYER_ONE], 1.0)
        self.assertEqual(scores[PLAYER_TWO], -1.0)
コード例 #12
0
    def test_get_possible_moves_eight_player_choice(self):
        board = Board("""\
        8
        0
        0 0
        3 3
        2 1 3
        4 2 5
        6 c 7
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(8, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_TWO, PLAYER_ONE, PLAYER_THREE],
                           [PLAYER_FOUR, PLAYER_ONE, PLAYER_FIVE],
                           [PLAYER_SIX, PLAYER_ONE, PLAYER_SEVEN]])
        self.assert_board(next_game_states[1].board,
                          [[PLAYER_ONE, PLAYER_TWO, PLAYER_THREE],
                           [PLAYER_FOUR, PLAYER_TWO, PLAYER_FIVE],
                           [PLAYER_SIX, PLAYER_TWO, PLAYER_SEVEN]])
        self.assert_board(next_game_states[2].board,
                          [[PLAYER_TWO, PLAYER_THREE, PLAYER_ONE],
                           [PLAYER_FOUR, PLAYER_THREE, PLAYER_FIVE],
                           [PLAYER_SIX, PLAYER_THREE, PLAYER_SEVEN]])
        self.assert_board(next_game_states[3].board,
                          [[PLAYER_TWO, PLAYER_FOUR, PLAYER_THREE],
                           [PLAYER_ONE, PLAYER_FOUR, PLAYER_FIVE],
                           [PLAYER_SIX, PLAYER_FOUR, PLAYER_SEVEN]])
        self.assert_board(next_game_states[4].board,
                          [[PLAYER_TWO, PLAYER_FIVE, PLAYER_THREE],
                           [PLAYER_FOUR, PLAYER_FIVE, PLAYER_ONE],
                           [PLAYER_SIX, PLAYER_FIVE, PLAYER_SEVEN]])
        self.assert_board(next_game_states[5].board,
                          [[PLAYER_TWO, PLAYER_SIX, PLAYER_THREE],
                           [PLAYER_FOUR, PLAYER_SIX, PLAYER_FIVE],
                           [PLAYER_ONE, PLAYER_SIX, PLAYER_SEVEN]])
        self.assert_board(next_game_states[6].board,
                          [[PLAYER_TWO, PLAYER_SEVEN, PLAYER_THREE],
                           [PLAYER_FOUR, PLAYER_SEVEN, PLAYER_FIVE],
                           [PLAYER_SIX, PLAYER_SEVEN, PLAYER_ONE]])
        self.assert_board(next_game_states[7].board,
                          [[PLAYER_TWO, PLAYER_EIGHT, PLAYER_THREE],
                           [PLAYER_FOUR, PLAYER_EIGHT, PLAYER_FIVE],
                           [PLAYER_SIX, PLAYER_EIGHT, PLAYER_SEVEN]])
コード例 #13
0
    def __init__(self,
                 board,
                 time,
                 depth,
                 port=DEFAULT_PORT,
                 group_to_player=None):
        super().__init__()
        self.logger = logging.getLogger("Server ({})".format(port))
        self.game = GameState(board)
        self.server = BasicServer(board, port)
        self.time = time * 1000
        self.depth = depth
        self.group_to_player = group_to_player

        self.times = dict()
        for player in self.game.players:
            self.times[player] = 0
コード例 #14
0
    def test_get_possible_moves_boese03(self):
        board = Board("""\
        2
        0
        0 0
        2 10
        2 2 2 1 0 2 2 2 2 2 
        - - - - 0 1 2 0 - -
        0 0 6 <-> 9 0 2
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_on_position((4, 0), player=PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE, PLAYER_ONE],
                           [HOLE, HOLE, HOLE, HOLE, EMPTY, PLAYER_ONE, PLAYER_TWO, EMPTY, HOLE, HOLE]])
コード例 #15
0
    def test_get_possible_moves_walk_multipath(self):
        board = Board("""\
        2
        0
        0 0
        3 3
        1 0 0
        0 2 2
        0 0 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_ONE, EMPTY, EMPTY],
                           [EMPTY, PLAYER_ONE, PLAYER_TWO],
                           [EMPTY, EMPTY, PLAYER_ONE]])
コード例 #16
0
    def test_get_possible_moves_basic_inversion(self):
        board = Board("""\
        2
        0
        0 0
        3 3
        0 0 1
        0 2 0
        i 0 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[EMPTY, EMPTY, PLAYER_TWO],
                           [EMPTY, PLAYER_TWO, EMPTY],
                           [PLAYER_TWO, EMPTY, EMPTY]])
コード例 #17
0
    def test_get_possible_moves_eight_player_inversion(self):
        board = Board("""\
        8
        0
        0 0
        3 3
        5 4 1
        6 2 3
        i 7 8
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_on_position((0, 2), player=PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_SIX, PLAYER_FIVE, PLAYER_TWO],
                           [PLAYER_SEVEN, PLAYER_TWO, PLAYER_FOUR],
                           [PLAYER_TWO, PLAYER_EIGHT, PLAYER_ONE]])
コード例 #18
0
    def test_get_possible_moves_five_player_inversion(self):
        board = Board("""\
        5
        0
        0 0
        3 3
        5 4 1
        0 2 3
        i 0 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_on_position((0, 2), player=PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_ONE, PLAYER_FIVE, PLAYER_TWO],
                           [EMPTY, PLAYER_TWO, PLAYER_FOUR],
                           [PLAYER_TWO, EMPTY, EMPTY]])
コード例 #19
0
    def test_get_possible_moves_walk_bottom_left(self):
        board = Board("""\
        2
        0
        0 0
        3 3
        0 0 1
        0 2 0
        0 0 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[EMPTY, EMPTY, PLAYER_ONE],
                           [EMPTY, PLAYER_ONE, EMPTY],
                           [PLAYER_ONE, EMPTY, EMPTY]])
コード例 #20
0
    def test_get_possible_moves_holes_stop_move(self):
        board = Board("""\
        2
        0
        0 0
        3 3
        1 0 0
        0 2 -
        0 0 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_ONE, EMPTY, EMPTY],
                           [EMPTY, PLAYER_ONE, HOLE],
                           [EMPTY, EMPTY, PLAYER_ONE]])
コード例 #21
0
    def test_get_possible_moves_walk_through_transition(self):
        board = Board("""\
        2
        0
        0 0
        3 3
        0 2 0
        0 1 0
        0 0 0
        1 2 4 <-> 1 0 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[EMPTY, PLAYER_ONE, EMPTY],
                           [EMPTY, PLAYER_ONE, EMPTY],
                           [EMPTY, PLAYER_ONE, EMPTY]])
コード例 #22
0
    def test_get_possible_moves_walk_through_loop_transition(self):
        board = Board("""\
        2
        0
        0 0
        1 9
        1 - 2 - 0 - 2 - 1
        0 0 6 <-> 8 0 2
        2 0 6 <-> 0 0 2
        4 0 6 <-> 2 0 2
        6 0 6 <-> 4 0 2
        8 0 6 <-> 6 0 2
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_ONE, HOLE, PLAYER_ONE, HOLE, PLAYER_ONE, HOLE, PLAYER_ONE, HOLE, PLAYER_ONE]])
コード例 #23
0
    def test_get_possible_moves_inversion_with_transition(self):
        board = Board("""\
        2
        0
        0 0
        3 3
        2 - 1
        2 2 1
        i - -
        0 0 0 <-> 0 1 6
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_TWO, HOLE, PLAYER_TWO],
                           [PLAYER_TWO, PLAYER_TWO, PLAYER_TWO],
                           [PLAYER_TWO, HOLE, HOLE]])
コード例 #24
0
    def test_get_possible_moves_capture_expansion_rows(self):
        board = Board("""\
        2
        0
        0 0
        3 3
        0 0 0
        0 x 0
        0 0 1
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE, use_overwrite=True)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_ONE, EMPTY, EMPTY],
                           [EMPTY, PLAYER_ONE, EMPTY],
                           [EMPTY, EMPTY, PLAYER_ONE]])
        self.assertEqual(0, next_game_states[0].player_overwrites[PLAYER_ONE])
コード例 #25
0
    def test_get_possible_moves_boese_reflect(self):
        board = Board("""\
        2
        1
        0 0
        9 4
        - - - - 
        - - - 2 
        - - - 2 
        - - - 2 
        - - - 1 
        - - - 0 
        - - - 0 
        - - - 0 
        - - - 0 
        3 1 0 <-> 3 1 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_on_position((3, 4), player=PLAYER_ONE, use_overwrite=True)
        self.assertEqual(0, len(next_game_states))
コード例 #26
0
    def test_get_possible_moves_bomb_two(self):
        board = Board("""\
        2
        0
        2 2
        3 3
        0 0 0
        - - 0
        0 0 0
        """)
        game = GameState(board)
        game.bomb_phase = True

        next_game_states = game.get_possible_bomb_move_on_position((0, 0), player=PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[HOLE, HOLE, HOLE],
                           [HOLE, HOLE, HOLE],
                           [EMPTY, EMPTY, EMPTY]])
        self.assertEqual(next_game_states[0].player_bombs[PLAYER_ONE], 1)
        self.assertEqual(next_game_states[0].player_bombs[PLAYER_TWO], 2)
コード例 #27
0
    def test_get_possible_moves_basic_choice(self):
        board = Board("""\
        2
        0
        0 0
        3 3
        2 1 0
        - 2 -
        0 c 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(2, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[PLAYER_TWO, PLAYER_ONE, EMPTY],
                           [HOLE, PLAYER_ONE, HOLE],
                           [EMPTY, PLAYER_ONE, EMPTY]])
        self.assert_board(next_game_states[1].board,
                          [[PLAYER_ONE, PLAYER_TWO, EMPTY],
                           [HOLE, PLAYER_TWO, HOLE],
                           [EMPTY, PLAYER_TWO, EMPTY]])
コード例 #28
0
    def test_get_possible_moves_boese04(self):
        board = Board("""\
        2
        0
        0 0
        4 3
        0 0 0 
        0 2 2 
        1 0 2 
        2 2 2 
        2 3 4 <-> 2 3 2
        0 2 5 <-> 0 3 6
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_on_position((2, 0), player=PLAYER_ONE)
        self.assertEqual(1, len(next_game_states))
        self.assert_board(next_game_states[0].board,
                          [[EMPTY, EMPTY, PLAYER_ONE],
                           [EMPTY, PLAYER_ONE, PLAYER_ONE],
                           [PLAYER_ONE, EMPTY, PLAYER_ONE],
                           [PLAYER_ONE, PLAYER_ONE, PLAYER_ONE]])
コード例 #29
0
    def test_get_possible_moves_bonus(self):
        board = Board("""\
        2
        1
        0 0
        3 3
        2 1 0
        - 2 -
        0 b 0
        """)
        game = GameState(board)

        next_game_states = game.get_possible_moves_for_player(PLAYER_ONE)
        self.assertEqual(2, len(next_game_states))

        self.assertEqual(next_game_states[0].player_bombs[PLAYER_ONE], 1)
        self.assertEqual(next_game_states[0].player_bombs[PLAYER_TWO], 0)
        self.assertEqual(next_game_states[0].player_overwrites[PLAYER_ONE], 1)
        self.assertEqual(next_game_states[0].player_overwrites[PLAYER_TWO], 1)

        self.assertEqual(next_game_states[1].player_bombs[PLAYER_ONE], 0)
        self.assertEqual(next_game_states[1].player_bombs[PLAYER_TWO], 0)
        self.assertEqual(next_game_states[1].player_overwrites[PLAYER_ONE], 2)
        self.assertEqual(next_game_states[1].player_overwrites[PLAYER_TWO], 1)
コード例 #30
0
class Client(threading.Thread):
    def __init__(self,
                 group,
                 find_move,
                 host=DEFAULT_HOST,
                 port=DEFAULT_PORT,
                 game_start_callback=None,
                 game_end_callback=None,
                 move_callback=None):
        super().__init__()
        self.logger = logging.getLogger("Client ({})".format(group))
        self.client = BasicClient(group, host, port)

        self.find_move = find_move
        self.game_start_callback = game_start_callback
        self.game_end_callback = game_end_callback
        self.move_callback = move_callback

        self.game_state = None

    def run(self):
        self.client.start()
        self.game_state = GameState(self.client.board)

        if self.game_start_callback:
            self.game_start_callback(self.game_state, self.client.player)

        self._game_loop()
        self.logger.info("Game Ended")

        if self.game_end_callback:
            self.game_end_callback(self.game_state)

        self.client.stop()

    def _game_loop(self):
        while True:
            message = self.client.read_message()
            if isinstance(message, EndPhaseTwoMessage):
                return
            elif isinstance(message, EndPhaseOneMessage):
                self.game_state.bomb_phase = True
                self.logger.info("Phase One Ended")
            elif isinstance(message, MoveRequestMessage):
                self.logger.info("Move Request from server ({}, {})".format(
                    message.time_limit, message.depth_limit))
                (player, pos, choice) = self.find_move(self.game_state,
                                                       message.time_limit,
                                                       message.depth_limit)
                self.logger.info("Answer: {}, {}".format(pos, choice))
                move_message = MoveResponseMessage(pos, choice)
                self.client.send_message(move_message)
            elif isinstance(message, DisqualificationMessage):
                self.logger.info("Player {} Disqualified!".format(
                    message.player))
                self.game_state.disqualify_player(message.player)
                if message.player == self.client.player:
                    self.logger.info(
                        "Client was disqualified, shutting down...")
                    return
            elif isinstance(message, MoveNotificationMessage):
                old_game_state = self.game_state
                self.game_state = self.game_state.execute_move(
                    message.player, message.pos, message.choice)

                if self.move_callback:
                    self.move_callback(
                        old_game_state,
                        (message.player, message.pos, message.choice),
                        self.game_state)