pacman.controllers.examples.AggressiveGhosts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pacman-main Show documentation
Show all versions of pacman-main Show documentation
The main code for Ms. Pac-Man Vs Ghosts
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