pacman.controllers.examples.po.POCommGhosts 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.IndividualGhostController;
import pacman.controllers.MASController;
import pacman.game.Constants.DM;
import pacman.game.Constants.GHOST;
import pacman.game.Constants.MOVE;
import pacman.game.Game;
import pacman.game.comms.BasicMessage;
import pacman.game.comms.Message;
import pacman.game.comms.Messenger;
import java.util.EnumMap;
import java.util.Random;
/**
* Created by pwillic on 25/02/2016.
*/
public class POCommGhosts extends MASController {
public POCommGhosts() {
this(50);
}
public POCommGhosts(int TICK_THRESHOLD) {
super(true, new EnumMap(GHOST.class));
controllers.put(GHOST.BLINKY, new POCommGhost(GHOST.BLINKY, TICK_THRESHOLD));
controllers.put(GHOST.INKY, new POCommGhost(GHOST.INKY, TICK_THRESHOLD));
controllers.put(GHOST.PINKY, new POCommGhost(GHOST.PINKY, TICK_THRESHOLD));
controllers.put(GHOST.SUE, new POCommGhost(GHOST.SUE, TICK_THRESHOLD));
}
}
class POCommGhost extends IndividualGhostController{
private final static float CONSISTENCY = 0.9f; //attack Ms Pac-Man with this probability
private final static int PILL_PROXIMITY = 15; //if Ms Pac-Man is this close to a power pill, back away
Random rnd = new Random();
private int TICK_THRESHOLD;
private int lastPacmanIndex = -1;
private int tickSeen = -1;
public POCommGhost(GHOST ghost) {
this(ghost, 5);
}
public POCommGhost(GHOST ghost, int TICK_THRESHOLD) {
super(ghost);
this.TICK_THRESHOLD = TICK_THRESHOLD;
}
@Override
public MOVE getMove(Game game, long timeDue) {
// Housekeeping - throw out old info
int currentTick = game.getCurrentLevelTime();
if (currentTick <= 2 || currentTick - tickSeen >= TICK_THRESHOLD) {
lastPacmanIndex = -1;
tickSeen = -1;
}
// Can we see PacMan? If so tell people and update our info
int pacmanIndex = game.getPacmanCurrentNodeIndex();
int currentIndex = game.getGhostCurrentNodeIndex(ghost);
Messenger messenger = game.getMessenger();
if (pacmanIndex != -1) {
lastPacmanIndex = pacmanIndex;
tickSeen = game.getCurrentLevelTime();
if (messenger != null) {
messenger.addMessage(new BasicMessage(ghost, null, BasicMessage.MessageType.PACMAN_SEEN, pacmanIndex, game.getCurrentLevelTime()));
}
}
// Has anybody else seen PacMan if we haven't?
if (pacmanIndex == -1 && game.getMessenger() != null) {
for (Message message : messenger.getMessages(ghost)) {
if (message.getType() == BasicMessage.MessageType.PACMAN_SEEN) {
if (message.getTick() > tickSeen && message.getTick() < currentTick) { // Only if it is newer information
lastPacmanIndex = message.getData();
tickSeen = message.getTick();
}
}
}
}
if (pacmanIndex == -1) {
pacmanIndex = lastPacmanIndex;
}
Boolean requiresAction = game.doesGhostRequireAction(ghost);
if (requiresAction != null && requiresAction) //if ghost requires an action
{
if (pacmanIndex != -1) {
if (game.getGhostEdibleTime(ghost) > 0 || closeToPower(game)) //retreat from Ms Pac-Man if edible or if Ms Pac-Man is close to power pill
{
try {
return game.getApproximateNextMoveAwayFromTarget(game.getGhostCurrentNodeIndex(ghost),
game.getPacmanCurrentNodeIndex(), game.getGhostLastMoveMade(ghost), DM.PATH);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
System.out.println(pacmanIndex + " : " + currentIndex);
}
} else {
if (rnd.nextFloat() < CONSISTENCY) { //attack Ms Pac-Man otherwise (with certain probability)
try {
MOVE move = game.getApproximateNextMoveTowardsTarget(game.getGhostCurrentNodeIndex(ghost),
pacmanIndex, game.getGhostLastMoveMade(ghost), DM.PATH);
return move;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
System.out.println(pacmanIndex + " : " + currentIndex);
}
}
}
} else {
MOVE[] possibleMoves = game.getPossibleMoves(game.getGhostCurrentNodeIndex(ghost), game.getGhostLastMoveMade(ghost));
return possibleMoves[rnd.nextInt(possibleMoves.length)];
}
}
return null;
}
//This helper function checks if Ms Pac-Man is close to an available power pill
private boolean closeToPower(Game game) {
int[] powerPills = game.getPowerPillIndices();
for (int i = 0; i < powerPills.length; i++) {
Boolean powerPillStillAvailable = game.isPowerPillStillAvailable(i);
int pacmanNodeIndex = game.getPacmanCurrentNodeIndex();
if (pacmanNodeIndex == -1) {
pacmanNodeIndex = lastPacmanIndex;
}
if (powerPillStillAvailable == null || pacmanNodeIndex == -1) {
return false;
}
if (powerPillStillAvailable && game.getShortestPathDistance(powerPills[i], pacmanNodeIndex) < PILL_PROXIMITY) {
return true;
}
}
return false;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy