ClosestGhostModeSelector extends MsPacManModeSelector { public int mode() { ArrayList<Integer> ghosts = new ArrayList<Integer>(CommonConstants.numActiveGhosts); for (int i = 0; i < CommonConstants.numActiveGhosts; i++) { ghosts.add(i); } Collections.sort(ghosts, new GhostComparator(gs, true, true)); int count = 0; for (Integer g : ghosts) { count++; if (gs.isGhostEdible(g)) { return CLOSEST_EDIBLE; } else if (gs.getNumNeighbours(gs.getGhostCurrentNodeIndex(g)) > 0 || count == 4) { return CLOSEST_THREAT; } } return CLOSEST_THREAT; }  int mode(); int numModes(); @Override /** * gets the associated fitness scores with this mode selector * * @return an int array holding the score for if the closest ghost is a * threat in the first index and the score for if the closest ghost * is edible in the second index */ int[] associatedFitnessScores(); static final int CLOSEST_THREAT; static final int CLOSEST_EDIBLE; }
@Test public void testMode() { GameFacade g = new GameFacade(new Game(0)); EnumMap<GHOST,MOVE> gm = new EnumMap<GHOST,MOVE>(GHOST.class); gm.put(GHOST.SUE, MOVE.NEUTRAL); g.newG.advanceGame(MOVE.LEFT, gm); select.giveGame(g); assertEquals(ClosestGhostModeSelector.CLOSEST_THREAT, select.mode()); for(int i = 0; i < 100; i++) g.newG.advanceGame(MOVE.LEFT, gm); select.giveGame(g); assertEquals(ClosestGhostModeSelector.CLOSEST_THREAT, select.mode()); while(!g.anyIsEdible()) g.newG.advanceGame(MOVE.DOWN, gm); select.giveGame(g); assertEquals(ClosestGhostModeSelector.CLOSEST_EDIBLE, select.mode()); for(int i = 0; i < 15; i++) g.newG.advanceGame(MOVE.UP, gm); for(int i = 0; i < 5; i++) g.newG.advanceGame(MOVE.RIGHT, gm); for(int i = 0; i < 70; i++) g.newG.advanceGame(MOVE.UP, gm); select.giveGame(g); assertEquals(ClosestGhostModeSelector.CLOSEST_EDIBLE, select.mode()); for(int i = 0; i < 15; i++) g.newG.advanceGame(MOVE.DOWN, gm); for(int i = 0; i < 20; i++) g.newG.advanceGame(MOVE.RIGHT, gm); for(int i = 0; i < 15; i++) g.newG.advanceGame(MOVE.LEFT, gm); select.giveGame(g); assertEquals(ClosestGhostModeSelector.CLOSEST_THREAT, select.mode()); }