pacman.controllers.examples.po.POPacMan 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.po;
import pacman.controllers.PacmanController;
import pacman.game.Game;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import static pacman.game.Constants.*;
/**
* Created by Piers on 15/02/2016.
*/
public class POPacMan extends PacmanController {
private static final int MIN_DISTANCE = 20;
private Random random = new Random();
@Override
public MOVE getMove(Game game, long timeDue) {
// Should always be possible as we are PacMan
int current = game.getPacmanCurrentNodeIndex();
// Strategy 1: Adjusted for PO
for (GHOST ghost : GHOST.values()) {
// If can't see these will be -1 so all fine there
if (game.getGhostEdibleTime(ghost) == 0 && game.getGhostLairTime(ghost) == 0) {
int ghostLocation = game.getGhostCurrentNodeIndex(ghost);
if (ghostLocation != -1) {
if (game.getShortestPathDistance(current, ghostLocation) < MIN_DISTANCE) {
return game.getNextMoveAwayFromTarget(current, ghostLocation, DM.PATH);
}
}
}
}
/// Strategy 2: Find nearest edible ghost and go after them
int minDistance = Integer.MAX_VALUE;
GHOST minGhost = null;
for (GHOST ghost : GHOST.values()) {
// If it is > 0 then it is visible so no more PO checks
if (game.getGhostEdibleTime(ghost) > 0) {
int distance = game.getShortestPathDistance(current, game.getGhostCurrentNodeIndex(ghost));
if (distance < minDistance) {
minDistance = distance;
minGhost = ghost;
}
}
}
if (minGhost != null) {
return game.getNextMoveTowardsTarget(current, game.getGhostCurrentNodeIndex(minGhost), DM.PATH);
}
// Strategy 3: Go after the pills and power pills that we can see
int[] pills = game.getPillIndices();
int[] powerPills = game.getPowerPillIndices();
ArrayList targets = new ArrayList();
for (int i = 0; i < pills.length; i++) {
//check which pills are available
Boolean pillStillAvailable = game.isPillStillAvailable(i);
if (pillStillAvailable != null) {
if (pillStillAvailable) {
targets.add(pills[i]);
}
}
}
for (int i = 0; i < powerPills.length; i++) { //check with power pills are available
Boolean pillStillAvailable = game.isPillStillAvailable(i);
if (pillStillAvailable != null) {
if (pillStillAvailable) {
targets.add(powerPills[i]);
}
}
}
if (!targets.isEmpty()) {
int[] targetsArray = new int[targets.size()]; //convert from ArrayList to array
for (int i = 0; i < targetsArray.length; i++) {
targetsArray[i] = targets.get(i);
}
//return the next direction once the closest target has been identified
return game.getNextMoveTowardsTarget(current, game.getClosestNodeIndexFromNodeIndex(current, targetsArray, DM.PATH), DM.PATH);
}
// Strategy 4: New PO strategy as now S3 can fail if nothing you can see
// Going to pick a random action here
MOVE[] moves = game.getPossibleMoves(current, game.getPacmanLastMoveMade());
if (moves.length > 0) {
return moves[random.nextInt(moves.length)];
}
// Must be possible to turn around
return game.getPacmanLastMoveMade().opposite();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy