Пример #1
0
    def test_selector_w(self):
        board = BitBoard(8)
        board.put_disc('black', 3, 2)
        board.put_disc('white', 2, 4)
        board.put_disc('black', 1, 5)
        board.put_disc('white', 1, 4)
        board.put_disc('black', 2, 5)
        board.put_disc('white', 2, 6)
        board.put_disc('black', 1, 6)
        board.put_disc('white', 1, 7)
        selector = Selector_W(depth=2, limit=4)

        self.assertEqual(selector.depth, 2)
        self.assertEqual(selector.limit, 4)

        strategy = _AlphaBeta(evaluator=Evaluator_TPW())
        selector = Selector_W()

        self.assertEqual(selector.depth, 3)
        self.assertEqual(selector.limit, 3)

        moves = board.get_legal_moves('black')
        best_move, scores = strategy.get_best_move('black', board, moves, 4)
        moves = selector.select_moves('black', board,
                                      board.get_legal_moves('black'), scores,
                                      2)
        self.assertEqual(moves, [(0, 3), (2, 3), (0, 4), (5, 4), (0, 5),
                                 (4, 5), (5, 5), (0, 6), (0, 7), (2, 7)])

        moves = selector.select_moves('black', board,
                                      board.get_legal_moves('black'), scores,
                                      5)
        self.assertEqual(moves, [(2, 3), (0, 4), (5, 4), (0, 5), (4, 5),
                                 (5, 5), (0, 6), (0, 7), (2, 7)])
Пример #2
0
 def __init__(self, depth=4, evaluator=Evaluator_TPW()):
     super().__init__(depth, evaluator)
Пример #3
0
 def __init__(self, evaluator=Evaluator_TPW()):
     super().__init__(evaluator=evaluator)
Пример #4
0
    def test_simulator_multi_process_parallel_by_game(self):
        json_file = './simulator_setting2.json'
        simulator_setting = {
            "board_size": 4,
            "matches": 5,
            "processes": 2,
            "parallel": "game",
            "random_opening": 0,
        }
        with open(json_file, 'w') as f:
            json.dump(simulator_setting, f)

        players_info = {
            'AlphaBeta1': _AlphaBeta(depth=2, evaluator=Evaluator_TPW()),
            'AlphaBeta2': _AlphaBeta(depth=2, evaluator=Evaluator_TPW()),
        }
        simulator = Simulator(players_info, json_file)
        os.remove(json_file)

        with captured_stdout() as stdout:
            simulator.start()
            print(simulator)

        lines = stdout.getvalue().splitlines()
        self.assertEqual(lines[0], "processes 2")
        self.assertEqual(lines[1], "")
        self.assertEqual(lines[2], "Size : 4")
        self.assertEqual(
            lines[3],
            "                          | AlphaBeta1                AlphaBeta2               "
        )
        self.assertEqual(
            lines[4],
            "-------------------------------------------------------------------------------"
        )
        self.assertEqual(
            lines[5],
            "AlphaBeta1                | ------                     50.0%                    "
        )
        self.assertEqual(
            lines[6],
            "AlphaBeta2                |  50.0%                    ------                    "
        )
        self.assertEqual(
            lines[7],
            "-------------------------------------------------------------------------------"
        )
        self.assertEqual(lines[8], "")
        self.assertEqual(
            lines[9],
            "                          | Total  | Win   Lose  Draw  Match")
        self.assertEqual(
            lines[10],
            "------------------------------------------------------------")
        self.assertEqual(
            lines[11],
            "AlphaBeta1                |  50.0% |     5     5     0    10")
        self.assertEqual(
            lines[12],
            "AlphaBeta2                |  50.0% |     5     5     0    10")
        self.assertEqual(
            lines[13],
            "------------------------------------------------------------")