def test_tora_init(self): joseki = {} joseki.update(MOUSE) joseki.update(BULL) joseki.update(RABBIT) joseki.update(SROSE) joseki.update(ROSEVILLE) joseki.update(FASTBOAT) joseki.update(CAT) joseki.update(TIGER) # no Measure _tora_ = _Tora_(Random()) key = _tora_.__class__.__name__ + str(os.getpid()) board = BitBoard() _tora_.next_move(c.black, board) self.assertEqual(_tora_.joseki, joseki) self.assertIsInstance(_tora_.base, Random) self.assertFalse(key in Measure.elp_time) # with Measure tora = Tora(Random()) key = tora.__class__.__name__ + str(os.getpid()) board = BitBoard() tora.next_move(c.black, board) self.assertEqual(tora.joseki, joseki) self.assertIsInstance(tora.base, Random) self.assertTrue(key in Measure.elp_time)
def test_usagi_init(self): joseki = {} joseki.update(MOUSE) joseki.update(BULL) joseki.update(TIGER) joseki.update(SROSE) joseki.update(ROSEVILLE) joseki.update(FASTBOAT) joseki.update(CAT) joseki.update(RABBIT) # no Measure _usagi_ = _Usagi_(Random()) key = _usagi_.__class__.__name__ + str(os.getpid()) board = BitBoard() _usagi_.next_move(c.black, board) self.assertEqual(_usagi_.joseki, joseki) self.assertIsInstance(_usagi_.base, Random) self.assertFalse(key in Measure.elp_time) # with Measure usagi = Usagi(Random()) key = usagi.__class__.__name__ + str(os.getpid()) board = BitBoard() usagi.next_move(c.black, board) self.assertEqual(usagi.joseki, joseki) self.assertIsInstance(usagi.base, Random) self.assertTrue(key in Measure.elp_time)
def test_neko_init(self): joseki = {} joseki.update(BULL) joseki.update(MOUSE) joseki.update(RABBIT) joseki.update(TIGER) joseki.update(SROSE) joseki.update(ROSEVILLE) joseki.update(FASTBOAT) joseki.update(SHEEP) joseki.update(CAT) # no Measure _neko_ = _Neko_(Random()) key = _neko_.__class__.__name__ + str(os.getpid()) board = BitBoard() _neko_.next_move(c.black, board) self.assertEqual(_neko_.joseki, joseki) self.assertIsInstance(_neko_.base, Random) self.assertFalse(key in Measure.elp_time) # with Measure neko = Neko(Random()) key = neko.__class__.__name__ + str(os.getpid()) board = BitBoard() neko.next_move(c.black, board) self.assertEqual(neko.joseki, joseki) self.assertIsInstance(neko.base, Random) self.assertTrue(key in Measure.elp_time)
def test_switch_init(self): turns = [10, 20, 30, 40, 50, 60] strategies = [Random(), Unselfish(), Greedy(), Random(), Unselfish(), Greedy()] switch = Switch( turns=turns, strategies=strategies, ) self.assertEqual(switch.turns, turns) self.assertEqual(switch.strategies, strategies)
def test_hitsuji_next_move(self): board = BitBoard() hitsuji = Hitsuji(Random()) patterns = [ # turn, move # --- 猫定石 --- (c.black, (5, 4)), (c.white, (3, 5)), (c.black, (2, 3)), (c.white, (3, 2)), (c.black, (4, 5)), (c.white, (5, 3)), (c.black, (4, 2)), (c.white, (5, 2)), (c.black, (2, 5)), (c.white, (5, 5)), (c.black, (6, 4)), (c.white, (6, 5)), (c.black, (2, 2)), (c.white, (7, 5)), (c.black, (6, 3)), (c.white, (7, 4)), (c.black, (7, 2)), (c.white, (2, 4)), ] for turn, expected in patterns: move = hitsuji.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
def test_neko_next_move(self): board = BitBoard() neko = Neko(Random()) patterns = [ # turn, move # --- 猫定石 --- (c.black, (5, 4)), (c.white, (3, 5)), (c.black, (2, 3)), (c.white, (3, 2)), (c.black, (2, 4)), (c.white, (5, 3)), (c.black, (4, 2)), (c.white, (5, 2)), (c.black, (4, 1)), (c.white, (2, 5)), (c.black, (4, 5)), (c.white, (5, 5)), (c.black, (3, 6)), (c.white, (2, 7)), ] for turn, expected in patterns: move = neko.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
def test_tora_next_move(self): board = BitBoard() tora = Tora(Random()) patterns = [ # turn, move # --- 虎定石 --- (c.black, (5, 4)), (c.white, (3, 5)), (c.black, (2, 2)), # --- ローズビル基本形 --- (c.white, (3, 2)), (c.black, (2, 3)), (c.white, (5, 3)), (c.black, (2, 4)), (c.white, (1, 2)), (c.black, (2, 1)), ] for turn, expected in patterns: move = tora.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
def test_ushi_next_move(self): board = BitBoard() ushi = Ushi(Random()) patterns = [ # turn, move # --- 牛定石 --- (c.black, (5, 4)), (c.white, (5, 5)), (c.black, (4, 5)), (c.white, (5, 3)), (c.black, (4, 2)), (c.white, (2, 4)), # --- 快速船基礎形 --- (c.black, (2, 3)), (c.white, (4, 6)), (c.black, (2, 5)), (c.white, (4, 1)), (c.black, (5, 2)), (c.white, (5, 1)), ] for turn, expected in patterns: move = ushi.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
def test_usagi_next_move(self): board = BitBoard() usagi = Usagi(Random()) patterns = [ # turn, move # --- 兎定石 --- (c.black, (5, 4)), (c.white, (3, 5)), (c.black, (2, 4)), (c.white, (5, 3)), (c.black, (4, 2)), # --- Sローズ基本形 --- (c.white, (2, 5)), (c.black, (3, 2)), (c.white, (5, 5)), (c.black, (4, 5)), (c.white, (3, 6)), (c.black, (6, 2)), (c.white, (2, 3)), ] for turn, expected in patterns: move = usagi.next_move(turn, board) board.put_disc(turn, *move) self.assertEqual(move, expected)
def test_random(self): random = Random() board = Board() legal_moves = board.get_legal_moves('black') self.assertTrue(random.next_move('black', board) in legal_moves)
def test_joseki_init(self): joseki = _Joseki_(Random()) self.assertEqual(joseki.joseki, {}) self.assertIsInstance(joseki.base, Random)
def __init__(self, remain=11, base=Random()): super().__init__(remain, base)
def test_simulator_init(self): # No Setting File players_info = { 'Unselfish': Unselfish(), 'Random': Random(), 'Greedy': Greedy(), 'SlowStarter': SlowStarter(), 'Table': Table(), } simulator = Simulator(players_info, 'No Setting File') self.assertEqual(simulator.matches, 10) self.assertEqual(simulator.board_size, 8) self.assertEqual(simulator.board_type, "bitboard") self.assertEqual(simulator.processes, 1) self.assertEqual(simulator.parallel, "player") self.assertEqual(simulator.random_opening, 8) for index, strategy in enumerate(players_info.keys()): self.assertEqual(simulator.black_players[index].name, strategy) self.assertIsInstance(simulator.black_players[index].strategy, RandomOpening) self.assertEqual(simulator.black_players[index].strategy.depth, 8) self.assertIsInstance(simulator.black_players[index].strategy.base, type(players_info[strategy])) self.assertEqual(simulator.white_players[index].name, strategy) self.assertIsInstance(simulator.white_players[index].strategy, RandomOpening) self.assertEqual(simulator.white_players[index].strategy.depth, 8) self.assertIsInstance(simulator.white_players[index].strategy.base, type(players_info[strategy])) self.assertEqual(simulator.game_results, []) self.assertEqual(simulator.total, []) self.assertEqual(simulator.result_ratio, {}) # Setting File1 json_file = './simulator_setting.json' simulator_setting = { "board_size": 4, "matches": 100, "processes": 2, "random_opening": 0, } with open(json_file, 'w') as f: json.dump(simulator_setting, f) players_info = { 'Unselfish': Unselfish(), 'Random': Random(), } simulator = Simulator(players_info, json_file) os.remove(json_file) self.assertEqual(simulator.matches, 100) self.assertEqual(simulator.board_size, 4) self.assertEqual(simulator.board_type, "bitboard") self.assertEqual(simulator.processes, 2) self.assertEqual(simulator.random_opening, 0) for index, strategy in enumerate(players_info.keys()): self.assertEqual(simulator.black_players[index].name, strategy) self.assertIsInstance(simulator.black_players[index].strategy, type(players_info[strategy])) self.assertEqual(simulator.white_players[index].name, strategy) self.assertIsInstance(simulator.white_players[index].strategy, type(players_info[strategy])) self.assertEqual(simulator.game_results, []) self.assertEqual(simulator.total, []) self.assertEqual(simulator.result_ratio, {}) # Setting File2 json_file = './simulator_setting.json' simulator_setting = { "board_size": 12, "board_type": 'board', "matches": 500, "processes": 1, "random_opening": 4, "player_names": [ "Random", "Table", ] } with open(json_file, 'w') as f: json.dump(simulator_setting, f) players_info = { 'Unselfish': Unselfish(), 'Random': Random(), 'Greedy': Greedy(), 'Table': Table(), } simulator = Simulator(players_info, json_file) os.remove(json_file) self.assertEqual(simulator.matches, 500) self.assertEqual(simulator.board_size, 12) self.assertEqual(simulator.board_type, "board") self.assertEqual(simulator.processes, 1) self.assertEqual(simulator.random_opening, 4) self.assertEqual(len(simulator.black_players), 2) self.assertEqual(len(simulator.white_players), 2) self.assertEqual(simulator.black_players[0].name, "Random") self.assertEqual(simulator.white_players[0].name, "Random") self.assertEqual(simulator.black_players[1].name, "Table") self.assertEqual(simulator.white_players[1].name, "Table") self.assertIsInstance(simulator.black_players[0].strategy.base, type(Random())) self.assertIsInstance(simulator.white_players[0].strategy.base, type(Random())) self.assertIsInstance(simulator.black_players[1].strategy.base, type(Table())) self.assertIsInstance(simulator.white_players[1].strategy.base, type(Table())) self.assertEqual(simulator.game_results, []) self.assertEqual(simulator.total, []) self.assertEqual(simulator.result_ratio, {})