def test_thoughtboard_root_win(self): trivial_board = Board(size=9, win_chain_length=5) trivial_board.set_to_one_move_from_win() tb = ThoughtBoard(trivial_board, FeatureBoard_v1_1) trivial_board.move(36) print(trivial_board.pprint()) move_list = MoveList((), []).append(36) self.assertTrue(trivial_board.game_won()) self.assertTrue(tb.game_over_after(move_list))
def test_export_parse(self): board = Board(size=9, win_chain_length=5) for i in range(10): board.make_random_move() parsed_board = Board.load(board.export()) self.assertEqual(board.pprint(lastmove_highlight=False), parsed_board.pprint(lastmove_highlight=False)) self.assertFalse(parsed_board.game_over()) self.assertFalse(parsed_board.game_won()) self.assertTrue(np.equal(parsed_board._matrix, board._matrix).all())
def test_rotator(self): rot = BoardTransform(size=9) for i in range(9 * 9): x, y = rot.index_to_coordinate(i) # print(x, y, i) self.assertEqual(i, rot.coordinate_to_index(x, y)) board = Board(size=9, win_chain_length=5) moves = [(0, 0), (2, 1), (5, 5), (4, 4)] for i, move in enumerate(moves): board.move_coord(move[0], move[1]) print(board.pprint()) index = rot.coordinate_to_index(move[0], move[1]) rotated_matrices = rot.get_rotated_matrices(board._matrix.reshape((board.get_size(), board.get_size(), 1))) self.assertFalse(np.equal(rotated_matrices[0], rotated_matrices[2]).all()) for point, mat in zip(rot.get_rotated_points(index), rotated_matrices): x, y = rot.index_to_coordinate(point) self.assertEqual(mat[x][y][0], board.get_player_last_move().value)
def run(): mind = pexp_mind.PExpMind(size=SIZE, init=False, channels=4) mind.load_net('../models/9_4_4') rs = RandomState(42) for i in range(50): board = Board(size=SIZE, win_chain_length=5, draw_point=50) print('Game', i) # randomize the board a bit for j in range(rs.randint(0, 10)): board.make_random_move() # board.move(2, 2) # board.move(0, 1) # board.move(2, 3) # board.move(2, 1) # board.move(2, 4) # board.move(3, 1) # board.move(4, 3) # board.move(3, 4) # board.move(6, 6) # board.move(4, 1) # board.move(0, 0) # board.move(1, 0) # board.move(0, 4) # board.move(2, 2) # board.move(0, 7) # board.move(3, 0) # board.move(1, 2) # board.move(3, 7) # board.move(1, 6) # board.move(4, 4) # board.move(1, 8) # board.move(4, 5) # board.move(2, 0) # board.move(4, 6) # board.move(2, 4) # board.move(4, 8) # board.move(2, 8) # board.move(5, 0) # board.move(3, 1) # board.move(5, 4) # board.move(3, 6) # board.move(5, 8) # board.move(4, 0) # board.move(6, 3) # board.move(4, 3) # board.move(7, 2) # board.move(4, 7) # board.move(7, 6) # board.move(6, 6) # board.move(8, 0) # board.move(8, 1) # board.move(8, 8) # board.move(8, 7) print(board) current_player = board.get_player_to_move() def expanding_p(depth, p): return np.logical_or.reduce([ np.logical_and(depth < 2, p > -6), np.logical_and(depth < 4, p > -4), np.logical_and(depth < 6, p > -4), np.logical_and(depth < np.inf, p > -3) ]) def permissive_expansion(depth): if depth < 2: return np.inf if depth < 8: return 5 return 3 mind.define_policies(expanding_p, permissive_expansion, convergence_count=5, alpha=0.2, q_exp_batch_size=SIZE**2, p_exp_batch_size=SIZE**3, required_depth=6, max_iters=20) while True: result = mind.make_move(board, as_player=current_player, epsilon=0.1, consistency_check=False, verbose=True) print(board.pprint()) if current_player == Board.FIRST_PLAYER: current_player = Board.SECOND_PLAYER else: current_player = Board.FIRST_PLAYER if result: break print('done')