示例#1
0
    def get_best_move_and_score_n_moves_ahead(self, board, n):
        max_score = -1
        best_move = None

        for move in [RIGHT, DOWN, LEFT, UP]:
            new_board = get_next_board(board, move)
            is_dud = new_board == board
            if is_dud:
                continue

            if n > 1:
                _, score = self.get_best_move_and_score_n_moves_ahead(
                    new_board, n - 1)
            else:
                score = self.score_board_state(new_board)

            if score > max_score and not is_dud:
                max_score = score
                best_move = move

        # look fewer steps if couldnt find a solution
        num_look_ahead = n - 2
        while max_score == -1 and num_look_ahead >= 0:
            best_move, max_score = self.get_best_move_and_score_n_moves_ahead(
                board, num_look_ahead)
            num_look_ahead -= 1

        return best_move, max_score
示例#2
0
文件: 2048-bot.py 项目: ymmn/2048-bot
    def get_best_move_and_score_n_moves_ahead(self, board, n):
        max_score = -1
        best_move = None

        for move in [RIGHT, DOWN, LEFT, UP]:
            new_board = get_next_board(board, move)
            is_dud = new_board == board
            if is_dud:
                continue

            if n > 1:
                _, score = self.get_best_move_and_score_n_moves_ahead(new_board, n - 1)
            else:
                score = self.score_board_state(new_board)

            if score > max_score and not is_dud:
                max_score = score
                best_move = move

        # look fewer steps if couldnt find a solution
        num_look_ahead = n - 2
        while max_score == -1 and num_look_ahead >= 0:
            best_move, max_score = self.get_best_move_and_score_n_moves_ahead(board, num_look_ahead)
            num_look_ahead -= 1

        return best_move, max_score
示例#3
0
    def get_move(self, board):
        attempt_seq = [RIGHT, DOWN, LEFT]
        random.shuffle(attempt_seq)
        attempt_seq.append(UP)

        for move in attempt_seq:
            if get_next_board(board, move) != board:
                return move
        return move
示例#4
0
文件: 2048-bot.py 项目: ymmn/2048-bot
    def get_move(self, board):
        attempt_seq = [RIGHT, DOWN, LEFT]
        random.shuffle(attempt_seq)
        attempt_seq.append(UP)

        for move in attempt_seq:
            if get_next_board(board, move) != board:
                return move
        return move