Pacman minimax算法评估功能

时间:2019-02-11 04:10:08

标签: java minimax pacman

我正在为pacman开发minimax AI,我正在寻找一点帮助,首先要确保我在正确的总体方向上,其次是改善评估功能。

从状态中,我可以获取所有以前的状态,点位置,幻影位置,pacmans位置。点数和时间存储在游戏中。目前,我已经获得了评估函数的返回值:是否食用了一个点,已收集了多少个点以及最近的幽灵的位置(最多7个正方形)。

我遇到的主要问题是,随着游戏的进行,吃豆人只是坐在一个角落,在两个动作之间跳跃,直到鬼魂出现之前它什么也不会做。我目前认为解决此问题的一种好方法是找到最近的点,然后从比分中添加或减去该位置。如果有人有什么好主意,我很想听听他们的想法。

谢谢

public Move chooseMove(Game game) {
    return findBestMove(game);
}

private int maxvalue(Game game, int depth) {
    State currentState = game.getCurrentState();
    if(depth == DEPTH_LIMIT || game.isFinal(currentState)) {
        return evaluateMove(currentState);
    }
    else {
        int bestScore = Integer.MIN_VALUE;
        List<Move> legalMoves = game.getLegalPacManMoves();

        for(Move move: legalMoves) {
            //Go to next state
            State nextState = game.getNextState(currentState, move);
            game.setCurrentState(nextState);

            bestScore = Math.max(minvalue(game, depth + 1), bestScore);

            //Undo Move
            game.setCurrentState(currentState);
        }
        return bestScore;
    }
}

private int minvalue(Game game, int depth) {
    State currentState = game.getCurrentState();
    if(depth == DEPTH_LIMIT || game.isFinal(currentState)) {
        return evaluateMove(currentState);
    }
    else {
        int worstScore = Integer.MAX_VALUE;
        List<Move> legalMoves = game.getLegalPacManMoves();

        for(Move move: legalMoves) {
            //Go to next state
            State nextState = game.getNextState(currentState, move);
            game.setCurrentState(nextState);

            worstScore = Math.min(maxvalue(game, depth + 1), worstScore);

            //Undo Move
            game.setCurrentState(currentState);
        }
        return worstScore;
    }
}

private Move findBestMove(Game game) {
    int bestValue = Integer.MIN_VALUE;
    Move bestMove = Move.NONE;
    State currentState = game.getCurrentState();

    List<Move> legalMoves = game.getLegalPacManMoves();

    for(Move move : legalMoves) {
        //Go to next state
        State nextState = game.getNextState(currentState, move);
        game.setCurrentState(nextState);

        int moveValue = maxvalue(game, 0);

        //Undo Move
        game.setCurrentState(currentState);

        if(moveValue > bestValue) {
            bestValue = moveValue;
            bestMove = move;
        }
    }

    return bestMove;
}

private int evaluateMove(State state) {
    //Get previous state
    State previousState = state.getParent();

    //Was a dot eaten this state
    int dotEaten = (previousState.getDotLocations().size() - state.getDotLocations().size());

    int totalDots = state.getHistory().get(0).getDotLocations().size() - state.getDotLocations().size();

    return dotEaten + totalDots + getClosestGhost(state);
}

private int getClosestGhost(State state) {
    List<Location> ghosts = state.getGhostLocations();
    Location pacman = state.getPacManLocation();

    int closest = Integer.MAX_VALUE;

    for(Location ghost : ghosts) {
        int x = ghost.getX() - pacman.getX();
        x = Math.abs(x);

        int y = ghost.getY() - pacman.getY();
        y = Math.abs(y);

        int total = x + y;

        if(total < closest) {
            closest = total;
        }
    }

    if(closest > 7) {
        closest = 7;
    }

    return closest;
}

0 个答案:

没有答案