All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pacman.controllers.examples.AggressiveGhosts Maven / Gradle / Ivy

There is a newer version: 2.0.1.0
Show newest version
package pacman.controllers.examples;

import pacman.controllers.Controller;
import pacman.game.Constants.GHOST;
import pacman.game.Constants.MOVE;
import pacman.game.Game;

import java.util.EnumMap;
import java.util.Random;

import static pacman.game.Constants.DM;

/*
 * The Class AggressiveGhosts.
 */
public final class AggressiveGhosts extends Controller> {
    private final static float CONSISTENCY = 1.0f;    //carry out intended move with this probability
    private Random rnd = new Random();
    private EnumMap myMoves = new EnumMap(GHOST.class);
    private MOVE[] moves = MOVE.values();

    /* (non-Javadoc)
     * @see pacman.controllers.Controller#getMove(pacman.game.Game, long)
     */
    @Override
    public EnumMap getMove(Game game, long timeDue) {
        myMoves.clear();

        for (GHOST ghost : GHOST.values())                //for each ghost
        {
            if (game.doesGhostRequireAction(ghost))        //if it requires an action
            {
                if (rnd.nextFloat() < CONSISTENCY)    //approach/retreat from the current node that Ms Pac-Man is at
                {
                    myMoves.put(ghost, game.getApproximateNextMoveTowardsTarget(game.getGhostCurrentNodeIndex(ghost),
                            game.getPacmanCurrentNodeIndex(), game.getGhostLastMoveMade(ghost), DM.PATH));
                } else                                    //else take a random action
                {
                    myMoves.put(ghost, moves[rnd.nextInt(moves.length)]);
                }
            }
        }

        return myMoves;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy