Skip to content

Commit

Permalink
Little improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
reinold123 committed Apr 3, 2018
1 parent 578d234 commit 0be342f
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 90 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

225 changes: 163 additions & 62 deletions .idea/workspace.xml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.util.EnumMap;

import static pacman.game.internal.POType.RADIUS;


/**
* Created by pwillic on 06/05/2016.
Expand All @@ -22,6 +24,7 @@ public static void main(String[] args) {

Executor executor = new Executor.Builder()
.setVisual(true)
.setPOType(RADIUS)
.setTickLimit(4000)
.build();

Expand Down
97 changes: 69 additions & 28 deletions src/main/java/ReinaudLovesPacman/MyPacMan.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package ReinaudLovesPacman;

import pacman.controllers.MASController;
import ReinaudLovesPacman.resource.NextStep;
import pacman.controllers.PacmanController;
import pacman.controllers.examples.po.POCommGhosts;
import pacman.game.Constants;
import pacman.game.Game;
import pacman.game.info.GameInfo;
import pacman.game.internal.Ghost;
import pacman.game.internal.Maze;
import sun.security.krb5.internal.crypto.CksumType;

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.summarizingDouble;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

Expand All @@ -23,41 +22,83 @@ public class MyPacMan extends PacmanController {

@Override
public Constants.MOVE getMove(Game game, long timeDue) {
Game coGame;
GameInfo info = game.getPopulatedGameInfo();
info.fixGhosts((ghost) -> new Ghost(
ghost,
game.getCurrentMaze().lairNodeIndex,
-1,
-1,
Constants.MOVE.NEUTRAL
));
coGame = game.getGameFromInfo(info);

List<Integer> eatThem = Arrays.stream(Constants.GHOST.values())
.filter(ghost -> game.getGhostLairTime(ghost) >= 0)
.filter(ghost -> game.isGhostEdible(ghost))
.filter(ghost -> Optional.ofNullable(game.isGhostEdible(ghost)).orElse(false))
.map(ghost -> game.getGhostCurrentNodeIndex(ghost))
.collect(toList());

return getBestOptionToIndexies(game, eatThem.size() > 0 ? eatThem.stream().mapToInt(i->i).toArray() : game.getActivePillsIndices());
return getBestOptionToIndexies(game, ghostsOrPill(game, eatThem));
}

private int[] ghostsOrPill(Game game, List<Integer> eatThem) {
if(eatThem.size() >= 4) {
System.out.println("Ghosts --> ");
return eatThem.stream().mapToInt(i->i).toArray();
} else {
System.out.println("Pill --> ");
return game.getActivePillsIndices();
}
}

private Constants.MOVE getBestOptionToIndexies(Game game, int[] indexes) {
int pacManIndex = game.getPacmanCurrentNodeIndex();
Map<Integer, Integer> closestPill = new HashMap<>();
List<NextStep> closestIndex = new ArrayList<>();

for(Integer index : indexes) {
int distance = game.getShortestPathDistance(pacManIndex, index);
Constants.MOVE move = game.getNextMoveTowardsTarget(pacManIndex, index, Constants.DM.PATH);
int neighbourIndex = game.getNeighbour(pacManIndex, move);
double score = distance + ghostScore(game, neighbourIndex);
if(score > 100) {
move = game.getNextMoveAwayFromTarget(pacManIndex, neighbourIndex, Constants.DM.PATH);
}
closestIndex.add(new NextStep(index, move, neighbourIndex, score));
}

return closestIndex.stream()
.sorted(Comparator.comparing(NextStep::getScore))
.findFirst()
.map(NextStep::getFirstMove)
.orElseGet(() -> getClosetsJunction(game, pacManIndex));
}

for(Integer pillIndex : indexes) {
int distance = game.getShortestPathDistance(pacManIndex, pillIndex, game.getPacmanLastMoveMade());
closestPill.put(pillIndex, distance);
private Constants.MOVE getClosetsJunction(Game game, int pacManIndex) {

List<NextStep> closestIndex = new ArrayList<>();

for(Integer index : game.getJunctionIndices()) {
if(pacManIndex != index) {
double distance = game.getShortestPathDistance(pacManIndex, index, game.getPacmanLastMoveMade());
Constants.MOVE move = game.getNextMoveTowardsTarget(pacManIndex, index, Constants.DM.PATH);
int neighbourIndex = game.getNeighbour(pacManIndex, move);
closestIndex.add(new NextStep(index, move, neighbourIndex, distance + ghostScore(game, neighbourIndex)));
}
}

Integer pillIndex = closestPill.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.map(integerIntegerEntry -> integerIntegerEntry.getKey())
.findFirst().orElse(0);
return closestIndex.stream()
.sorted(Comparator.comparing(NextStep::getScore))
.map(NextStep::getFirstMove)
.findFirst().orElse(Constants.MOVE.NEUTRAL);

}

private Double ghostScore(Game game, int index) {
Set<Double> ghostScores = Arrays.stream(Constants.GHOST.values())
.filter(ghost -> !Optional.ofNullable(game.isGhostEdible(ghost)).orElse(false))
.map(ghost -> game.getGhostCurrentNodeIndex(ghost))
.filter(ghostIndex -> ghostIndex > 0)
.map(ghostIndex -> game.getDistance(index, ghostIndex, Constants.DM.MANHATTAN))
.collect(Collectors.toSet());

double score = 0;
for (double ghostScore: ghostScores) {
if(ghostScore < 30) {
score += 100;
}
score -= ghostScore;
}

return game.getNextMoveTowardsTarget(pacManIndex, pillIndex, game.getPacmanLastMoveMade(), Constants.DM.PATH);
return score;
}

}
33 changes: 33 additions & 0 deletions src/main/java/ReinaudLovesPacman/resource/NextStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ReinaudLovesPacman.resource;

import pacman.game.Constants;

public class NextStep {
Integer goalIndex;
Constants.MOVE firstMove;
Integer neighbourIndex;
Double score;

public NextStep(Integer goalIndex, Constants.MOVE firstMove, Integer neighbourIndex, Double score) {
this.goalIndex = goalIndex;
this.firstMove = firstMove;
this.neighbourIndex = neighbourIndex;
this.score = score;
}

public Integer getGoalIndex() {
return goalIndex;
}

public Constants.MOVE getFirstMove() {
return firstMove;
}

public Integer getNeighbourIndex() {
return neighbourIndex;
}

public Double getScore() {
return score;
}
}
Binary file modified target/classes/Main.class
Binary file not shown.
Binary file modified target/classes/ReinaudLovesPacman/MyPacMan.class
Binary file not shown.
Binary file not shown.

0 comments on commit 0be342f

Please sign in to comment.