0

I want to implement PacMan game with minimax algorithm but I do not understand the meaning of algorithm fluently. I have written this code

public MOVE miniMax(Game game,Node[] nodes,int depth,Boolean pacMan){

        int value;
        MOVE thisMove;
        int bestValue;
        int score=0;

        EnumMap<MOVE, MOVE[]> possibleMoves = nodes[game.getPacmanCurrentNodeIndex()].allPossibleMoves;
        MOVE[] moves = possibleMoves.get(MOVE.NEUTRAL);

        if(depth == 0)
            score = evaluationFunction(game);
        if(pacMan){
            bestValue = -INF;
        for(int i=0;i<moves.length;i++){
            game.copy();
            game.updatePacMan(moves[i]);
            thisMove= miniMax(game,nodes,depth-1,Boolean.FALSE);
            //bestValue = Math.max(bestValue, value);

        }
        return thisMove;

    }else{
            bestValue = INF;
            for(int i=0;i<moves.length;i++){
                game.copy();
                game.updatePacMan(moves[i]);
                thisMove= miniMax(game,nodes,depth-1,Boolean.TRUE);
                //bestValue = Math.min(bestValue, value);
            }
            //return bestValue;
            return thisMove;
        }
    }

    public int evaluationFunction(Game game){

        return 0;
}

I have written this code considering the Wikipedia pseudo code but I have a problem that I do not know how can I calculate evaluation function as an integer and then decide to return a move,I should only return a move. And Is Evaluation function calculate for one move or choose one between all possible move for a node?

1 Answers1

0

First of all, minimax is adapted to turn based games such as chess. It is tempting to break down a continuous game (such as pacman) in discrete increments and apply this algorithm but you will find that you are trying to reach an equilibrium between two antagonistic objectives and might satisfy neither:

  • Small increments make for a better evaluation but will grow too fast in computational requirements (especially if you consider all the simultaneous moves of the ghosts)
  • Large increments make for a more manageable tree but will not provide the fine grained reactions required by such an arcade game

Still, it is an interesting problem, if only just to see the results.

The evaluation function is a heuristic function that attempts to estimate the strength of the current board state where a bigger score is better for the given player. This will be quite a challenge as there is no obvious way to estimate the strength of a position in pacman, but here are some ideas:

  • Being far away from ghosts is better if pacman is not invincible
  • Being close to ghosts is better if pacman is invincible
  • Having less regular dots left to collect on the board is better
  • Having more "invincible dots" still remaining on the board is better
  • Having more ghosts in prison is better

This of course is from the standpoint of pacman. For the ghosts it is the other way around. Fine-tuning the respective weights of these characteristics is one of the difficulties of successfully implementing minimax.

By the way, go straight for alpha-beta pruning or negascout (a bit trickier), it will make a world of difference in terms of performance with no loss in evaluation quality.

schmop
  • 1,440
  • 1
  • 12
  • 21