pacman.controllers.examples.NearestPillPacManVS 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.PacmanController;
import pacman.game.Constants.DM;
import pacman.game.Game;
import pacman.game.GameView;
import java.awt.*;
import static pacman.game.Constants.GHOST;
import static pacman.game.Constants.MOVE;
/*
* Same as NearestPillPacMan but does some visuals to illustrate what can be done.
* Please note: the visuals are just to highlight different functionalities and may
* not make sense from a controller's point of view (i.e., they might not be useful)
* Comment/un-comment code below as desired (drawing all visuals would probably be too much).
*/
public final class NearestPillPacManVS extends PacmanController {
/* (non-Javadoc)
* @see pacman.controllers.Controller#getMove(pacman.game.Game, long)
*/
@SuppressWarnings("deprecation")
public MOVE getMove(Game game, long timeDue) {
int currentNodeIndex = game.getPacmanCurrentNodeIndex();
int[] activePills = game.getActivePillsIndices();
int[] activePowerPills = game.getActivePowerPillsIndices();
int[] targetNodeIndices = new int[activePills.length + activePowerPills.length];
for (int i = 0; i < activePills.length; i++)
targetNodeIndices[i] = activePills[i];
for (int i = 0; i < activePowerPills.length; i++)
targetNodeIndices[activePills.length + i] = activePowerPills[i];
int nearest = game.getClosestNodeIndexFromNodeIndex(currentNodeIndex, targetNodeIndices, DM.PATH);
//add the path that Ms Pac-Man is following
// GameView.addPoints(game,Color.GREEN,game.getShortestPath(game.getPacmanCurrentNodeIndex(),nearest));
//add the path from Ms Pac-Man to the first power pill
// GameView.addPoints(game,Color.CYAN,game.getShortestPath(game.getPacmanCurrentNodeIndex(),game.getPowerPillIndices()[0]));
// GameView.addPoints(game,Color.CYAN,game.getAStarPath(game.getPacmanCurrentNodeIndex(),game.getPowerPillIndices()[0]));
// GameView.addPoints(game,Color.YELLOW,game.getNonReversePath(game.getPacmanCurrentNodeIndex(),game.getPowerPillIndices()[0],game.getPacmanLastMoveMade()));
//add lines connecting Ms Pac-Man and the power pills
// for(int i=0;i 0) {
GameView.addPoints(game, Color.RED, game.getAStarPath(game.getGhostCurrentNodeIndex(GHOST.BLINKY), activePowerPills[0], MOVE.NEUTRAL));
GameView.addPoints(game, Color.YELLOW, game.getAStarPath(game.getGhostCurrentNodeIndex(GHOST.BLINKY), activePowerPills[0], game.getGhostLastMoveMade(GHOST.BLINKY)));
}
//add the path from Ghost 0 to the closest power pill
// if(game.getGhostLairTime(GHOST.BLINKY)==0 && activePowerPills.length>0)
// GameView.addPoints(game,Color.WHITE,game.getAStarPath(game.getGhostCurrentNodeIndex(GHOST.BLINKY),
// game.getClosestNodeIndexFromNodeIndex(game.getGhostCurrentNodeIndex(GHOST.BLINKY),activePowerPills,DM.PATH)));
//adds the paths the ghost would need to follow to reach Ms Pac-Man
// Color[] colors={Color.RED,Color.BLUE,Color.MAGENTA,Color.ORANGE};
//
// int index=0;
//
// for(GHOST ghostType : GHOST.values())
// if(game.getGhostLairTime(ghostType)==0)
// GameView.addPoints(game,colors[index++],game.getAStarPath(game.getGhostCurrentNodeIndex(ghostType),currentNodeIndex,game.getGhostLastMoveMade(ghostType)));
return game.getNextMoveTowardsTarget(game.getPacmanCurrentNodeIndex(), nearest, DM.PATH);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy