def generator(Q=0.5, pattern=None):

    if pattern is None:
        pattern = (11, 11, 9)

    width, height, mines = pattern

    center = int(width / 2)
    center = (center, center)

    width, height, mines = pattern

    board = minesweeper.MineSweeperBord(width, height, mines)

    while True:
        if board.touchReview(center):
            break

        board.restart()

    for local in board.getDarkLocal():
        dataset = board.getArea(local)
        __ = visibility(dataset)

        if __ >= Q:
            (area, mask) = dataset
            yield (__, area, mask)
    def gen(self, pattern=None, boardCount=1, catchINRound=3):

        if pattern is None:
            pattern = (24, 24, 99)

        self.pattern = pattern
        width, height, __ = self.pattern
        board = minesweeper.MineSweeperBord(*self.pattern)

        self.Q = list()
        self.ans = list()
        self.mask = list()
        self.area = list()

        for __ in range(boardCount):
            while True:
                board.restart()
                if board.touchReview((int(width / 2), int(height / 2))):
                    break

            while True:
                candiate = list()
                for local in board.getDarkLocal():

                    (area, mask) = board.getArea(local)

                    candiate.append(
                        [local, visibility((area, mask)), area, mask])

                candiate = sorted(candiate, key=lambda x: x[1],
                                  reverse=True)[:catchINRound]

                if len(candiate) > 0:
                    for (__, Q, area, mask) in candiate:
                        self.Q.append(Q)
                        self.ans.append(area[(2, 2)])
                        self.area.append(area)
                        self.mask.append(mask)

                    (local, __, __, __) = candiate[0]
                    if board.peeking(local) == minesweeper.__TOKEN_MINE__:
                        board.markMine(local)
                    else:
                        board.touchReview(local)

                else:
                    break

        return self
Exemplo n.º 3
0
def main(TARGET_MODEL, MODEL_DEFINE):

    TestingModel = __import__(MODEL_DEFINE[:-3])
    TARGET_MODEL = os.path.abspath(TARGET_MODEL)
    TARGET_DIR = os.path.dirname(TARGET_MODEL) + '/'
    TARGET_CHECKPOINT_NAME = TARGET_MODEL[:-5]

    print('TARGET MODEL           : {}'.format(TARGET_MODEL))
    print('TARGET_DIR             : {}'.format(TARGET_DIR))
    print('TARGET_CHECKPOINT_NAME : {}'.format(TARGET_CHECKPOINT_NAME))

    targetModel = tf.train.import_meta_graph(TARGET_MODEL)
    board = minesweeper.MineSweeperBord(BOARD_WIDTH, BOARD_HEIGHT, BOARD_MINES)
    FLAG_RESTART = WAIT_N_CYCLE

    with tf.Session() as sess:

        targetModel.restore(sess, TARGET_CHECKPOINT_NAME)
        graph = tf.get_default_graph()

        HYPE = graph.get_tensor_by_name('LAST/HYPE:0')
        X0 = graph.get_tensor_by_name('INPUT:0')
        predictor = tf.argmax(HYPE, 1)

        counter = list()

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # restart board & counter
            if FLAG_RESTART >= WAIT_N_CYCLE:
                FLAG_RESTART = 0

                board.restart()
                counter = list()

                while True:
                    if board.touchReview(
                        (int(BOARD_WIDTH / 2), int(BOARD_HEIGHT / 2))):
                        break

                    board.restart()

            drawBoard(board.game(True))

            # collecting candiate
            candiate = list()
            for local in board.getDarkLocal():

                item = list()
                item.append(local)

                (area, mask) = board.getArea(local)
                item.append(dataset.visibility((area, mask)))
                item.append(area)
                item.append(mask)

                candiate.append(item)

            # if having any candite
            if len(candiate) > 0:

                # select most likely solvable candiate (sorting by Q(visibility))
                candiate = sorted(candiate, key=lambda x: x[1],
                                  reverse=True)[:1]

                (local, Q, area, mask) = candiate[0]

                # encode area for model to solving
                T = np.copy(area)
                T[mask ==
                  minesweeper.__TOKEN_MASK__] = minesweeper.__TOKEN_MASK__
                T[mask ==
                  minesweeper.__TOKEN_MARK__] = minesweeper.__TOKEN_MARK__
                candiate[0].append(TestingModel.encodeArea(T))

                # solving candiate
                prediction = sess.run(
                    predictor, {X0: list(map(lambda x: x[4], candiate))})
                rawValue = sess.run(HYPE,
                                    {X0: list(map(lambda x: x[4], candiate))})

                prediction = prediction[0]
                (local, Q) = candiate[0][:2]
                (Y, N) = rawValue[0]

                print('solving #{:4d} ,Y = {:.3f}, N = {:.3f}'.format(
                    len(counter) + 1, Y, N))
                if prediction == 0:
                    drawView(local, Q, (255, 0, 0))
                elif prediction == 1:
                    drawView(local, Q, (0, 255, 0))

                # check prediction is correct
                if prediction == 0 and board.peeking(
                        local) != minesweeper.__TOKEN_MINE__:
                    print('failse positive. Q = {:.2f}'.format(Q))
                    counter.append(SolvingResult.INCORRECT)
                elif prediction == 1 and board.peeking(
                        local) == minesweeper.__TOKEN_MINE__:
                    print('failse negative. Q = {:.2f}'.format(Q))
                    counter.append(SolvingResult.INCORRECT)
                else:
                    counter.append(SolvingResult.CORRECT)

                # progress in game
                if board.peeking(local) == minesweeper.__TOKEN_MINE__:
                    board.markMine(local)
                else:
                    board.touchReview(local)

            else:
                if FLAG_RESTART == 0:
                    AC_RATE = float(counter.count(
                        SolvingResult.CORRECT)) / len(counter)
                    print('     AC RATE = {:.2f}%'.format(100.0 * AC_RATE))
                    # print('SOLVING RATE = {:.8f}%'.format(100.0 * math.pow(0.5, counter.count(SolvingResult.INCORRECT))))

                FLAG_RESTART += 1

            # process all drawing & wait
            print()
            pygame.display.flip()
            pygame.time.wait(WAIT_BETWEEN_CYCLE)