Пример #1
0
    def test_board_equivalence(self):
        size = 9
        cache = BitBoardCache("../cache/9-magics", size=9, win_chain_length=5, force_build_win_checks=False)
        bitboard = BitBoard(cache, size=9, win_chain_length=5)

        normal_board = Board(size=9, win_chain_length=5)

        fboard_1 = FeatureBoard_v1_1(bitboard)
        fboard_2 = FeatureBoard_v1_1(normal_board)

        for i in range(1000):
            if np.random.rand() < 0.5:
                normal_board.make_random_move()
                bitboard.move(normal_board.get_last_move())
                if normal_board.game_over():
                    break
                fboard_1.move(normal_board.get_last_move())
                fboard_2.move(normal_board.get_last_move())
            elif normal_board.get_last_move():
                normal_board.unmove()
                bitboard.unmove()
                fboard_1.unmove()
                fboard_2.unmove()
            self.verify_sync(normal_board, fboard_2)
            self.verify_sync(bitboard, fboard_1)
Пример #2
0
 def test_mass_play(self):
     for i in range(1000):
         board = Board(size=7, win_chain_length=4)
         board.make_random_move()
         if board.game_over():
             self.assertTrue(board.game_won() or board.game_assume_drawn())
         if board.game_assume_drawn():
             self.assertTrue(board.game_over())
Пример #3
0
    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())
Пример #4
0
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')
Пример #5
0
        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=25)

    board = Board(size=SIZE, win_chain_length=5)

    # randomize the board a bit
    for j in range(random.randint(0, int(SIZE * 2.5))):
        board.make_random_move()

    print(board.guide_print())

    while True:
        if board.get_player_to_move() == Board.FIRST_PLAYER:
            inp = input("Input your move (i.e. \"3 5\"): ")
            if len(inp.split(' ')) != 2:
                print('Incorrect number of coordinates, please try again!')
                continue
            x, y = inp.split(' ')
            try:
                x = int(x)
                y = int(y)
            except:
                print('Please input Numbers!')