예제 #1
0
  /**
   * Method which simulates the game from a given node/state
   *
   * @param n
   * @return
   */
  private int defaultPolicy(Node n) {
    Game currentState = n.getState().copy();
    int d = 0;

    while (!isStateTerminal(currentState) && d != depth) {
      MOVE localCurrentDirection = null;
      if (d == 0
          || currentState.isJunction(
              currentState.getPacmanCurrentNodeIndex())) { // is in a junction
        MOVE[] moves = currentState.getPossibleMoves(currentState.getPacmanCurrentNodeIndex());
        int i = rng.nextInt(moves.length);
        localCurrentDirection = moves[i];
        while (moves[i] == localCurrentDirection.opposite()) {
          i = rng.nextInt(moves.length);
        }
        localCurrentDirection = moves[i];
      }

      currentState.advanceGame(
          localCurrentDirection,
          controller.Controller.getGhostController().getMove(currentState, 0));
      d++;
    }

    if (isStateTerminal(currentState)) {
      return -1;
    }
    return currentState.getScore();
  }
예제 #2
0
  @Override
  public MOVE getMove(Game g, long t) {
    if (g.gameOver()) {
      currentDirection = null;
    } else if (!moved) {
      moved = true;
      return currentDirection;
    } else if (!g.isJunction(g.getPacmanCurrentNodeIndex()) && currentDirection != null) {
      ArrayList<MOVE> moves =
          new ArrayList<MOVE>(Arrays.asList(g.getPossibleMoves(g.getPacmanCurrentNodeIndex())));
      // MOVE[]g.getPossibleMoves(pacmanNode)
      if (moves.contains(currentDirection)) {
        return currentDirection;
      } else {
        moves.remove(currentDirection.opposite());
        currentDirection = moves.get(0);
        return moves.get(0);
      }
    }

    if (0 < t) {
      return run(g, t);
    } else {
      // TESTING
      MOVE m = run(g);
      return m;
      //

      // return run(g);
    }
  }
예제 #3
0
  @Override
  public MOVE getMove(Game game, long timeDue) {
    int current = game.getPacmanCurrentNodeIndex();

    // Strategy 1: if any non-edible ghost is too close (less than MIN_DISTANCE), run away
    for (GHOST ghost : GHOST.values())
      if (game.getGhostEdibleTime(ghost) == 0 && game.getGhostLairTime(ghost) == 0)
        if (game.getShortestPathDistance(current, game.getGhostCurrentNodeIndex(ghost))
            < MIN_DISTANCE)
          return game.getNextMoveAwayFromTarget(
              game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(ghost), DM.PATH);

    // Strategy 2: find the nearest edible ghost and go after them
    int minDistance = Integer.MAX_VALUE;
    GHOST minGhost = null;

    for (GHOST ghost : GHOST.values())
      if (game.getGhostEdibleTime(ghost) > 0) {
        int distance = game.getShortestPathDistance(current, game.getGhostCurrentNodeIndex(ghost));

        if (distance < minDistance) {
          minDistance = distance;
          minGhost = ghost;
        }
      }

    if (minGhost != null) // we found an edible ghost
    return game.getNextMoveTowardsTarget(
          game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(minGhost), DM.PATH);

    // Strategy 3: go after the pills and power pills
    int[] pills = game.getPillIndices();
    int[] powerPills = game.getPowerPillIndices();

    ArrayList<Integer> targets = new ArrayList<Integer>();

    for (int i = 0; i < pills.length; i++) // check which pills are available	
    if (game.isPillStillAvailable(i)) targets.add(pills[i]);

    for (int i = 0; i < powerPills.length; i++) // check with power pills are available
    if (game.isPowerPillStillAvailable(i)) targets.add(powerPills[i]);

    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);
  }
예제 #4
0
  /* (non-Javadoc)
   * @see pacman.controllers.Controller#getMove(pacman.game.Game, long)
   */
  public EnumMap<GHOST, MOVE> getMove(Game game, long timeDue) {
    int pacmanIndex = game.getPacmanCurrentNodeIndex();

    for (GHOST ghost : GHOST.values()) {
      if (game.doesGhostRequireAction(ghost)) {
        int currentIndex = game.getGhostCurrentNodeIndex(ghost);

        // if ghosts are all in close proximity and not near Ms Pac-Man, disperse
        if (isCrowded(game) && !closeToMsPacMan(game, currentIndex))
          myMoves.put(ghost, getRetreatActions(game, ghost)); // go towards the power pill locations
        // if edible or Ms Pac-Man is close to power pill, move away from Ms Pac-Man
        else if (game.getGhostEdibleTime(ghost) > 0 || closeToPower(game))
          myMoves.put(
              ghost,
              game.getApproximateNextMoveAwayFromTarget(
                  currentIndex,
                  pacmanIndex,
                  game.getGhostLastMoveMade(ghost),
                  DM.PATH)); // move away from ms pacman
        // else go towards Ms Pac-Man
        else
          myMoves.put(
              ghost,
              game.getApproximateNextMoveTowardsTarget(
                  currentIndex,
                  pacmanIndex,
                  game.getGhostLastMoveMade(ghost),
                  DM.PATH)); // go towards ms pacman
      }
    }

    return myMoves;
  }
예제 #5
0
  /**
   * Changes the state. Checks if there is active power pills nearby, if true go eat. Checks if
   * there still is a ghost close, if true stay in state Defaults to eat pill state
   *
   * @return returns change state. Returns null if it remains in this state
   */
  public State changeState(Game game, long timeDue) {
    int pacmanPos = game.getPacmanCurrentNodeIndex();
    // Check for power pill
    int closestPowerPill = 0;
    int closestDistPowerPill = -1;
    if (game.getNumberOfActivePowerPills() > 0) {
      for (int i : game.getActivePowerPillsIndices()) {
        int temp = game.getShortestPathDistance(pacmanPos, i);
        if (closestDistPowerPill > temp || closestDistPowerPill == -1) {
          closestPowerPill = i;
          closestDistPowerPill = temp;
        }
      }
    }
    if (game.getNumberOfActivePowerPills() > 0
        && game.getShortestPathDistance(pacmanPos, closestPowerPill) < mach.DistToPowerPill) {
      // System.out.println("(RunFromGhost)Try power pill");
      return (State) mach.dataStruc.get("moveNearestPowerPill");
    }
    // Check for ghost distance

    for (GHOST ghost : GHOST.values()) {
      if (game.getGhostLairTime(ghost) > 0 || game.isGhostEdible(ghost)) {
        continue;
      }
      if (game.getShortestPathDistance(pacmanPos, game.getGhostCurrentNodeIndex(ghost))
          < mach.DistFromNonEdible) {
        mach.dataStruc.put("ghost", ghost);
        return null;
      }
    }
    // System.out.println("(RunFromGhost) Try nearest pill");
    return (State) mach.dataStruc.get("moveNearestPill");
  }
예제 #6
0
 /** runs from current ghost */
 @Override
 public MOVE run(Game game, long timeDue) {
   return game.getNextMoveAwayFromTarget(
       game.getPacmanCurrentNodeIndex(),
       game.getGhostCurrentNodeIndex((GHOST) mach.dataStruc.get("ghost")),
       DM.PATH);
 }
예제 #7
0
 private double averageGhostDistance(Game state) {
   double sumDistance = 0;
   for (Constants.GHOST g : Constants.GHOST.values()) {
     double d =
         state.getManhattanDistance(
             state.getPacmanCurrentNodeIndex(), state.getGhostCurrentNodeIndex(g));
     sumDistance += d;
   }
   return (sumDistance / Constants.GHOST.values().length);
 }
예제 #8
0
  /**
   * Close to power.
   *
   * @param game the game
   * @return true, if successful
   */
  private boolean closeToPower(Game game) {
    int pacmanIndex = game.getPacmanCurrentNodeIndex();
    int[] powerPillIndices = game.getActivePowerPillsIndices();

    for (int i = 0; i < powerPillIndices.length; i++)
      if (game.getShortestPathDistance(powerPillIndices[i], pacmanIndex) < PILL_PROXIMITY)
        return true;

    return false;
  }
예제 #9
0
  /**
   * Gets the retreat actions.
   *
   * @param game the game
   * @param ghost the ghost
   * @return the retreat actions
   */
  private MOVE getRetreatActions(Game game, GHOST ghost) {
    int currentIndex = game.getGhostCurrentNodeIndex(ghost);
    int pacManIndex = game.getPacmanCurrentNodeIndex();

    if (game.getGhostEdibleTime(ghost) == 0
        && game.getShortestPathDistance(currentIndex, pacManIndex) < PACMAN_DISTANCE)
      return game.getApproximateNextMoveTowardsTarget(
          currentIndex, pacManIndex, game.getGhostLastMoveMade(ghost), DM.PATH);
    else
      return game.getApproximateNextMoveTowardsTarget(
          currentIndex,
          game.getPowerPillIndices()[cornerAllocation.get(ghost)],
          game.getGhostLastMoveMade(ghost),
          DM.PATH);
  }
  /* (non-Javadoc)
   * @see pacman.controllers.Controller#getMove(pacman.game.Game, long)
   */
  public EnumMap<GHOST, MOVE> getMove(Game game, long timeDue) {
    myMoves.clear();

    int targetNode = game.getPacmanCurrentNodeIndex();

    if (game.doesGhostRequireAction(GHOST.BLINKY))
      myMoves.put(
          GHOST.BLINKY,
          game.getApproximateNextMoveTowardsTarget(
              game.getGhostCurrentNodeIndex(GHOST.BLINKY),
              targetNode,
              game.getGhostLastMoveMade(GHOST.BLINKY),
              DM.PATH));

    if (game.doesGhostRequireAction(GHOST.INKY))
      myMoves.put(
          GHOST.INKY,
          game.getApproximateNextMoveTowardsTarget(
              game.getGhostCurrentNodeIndex(GHOST.INKY),
              targetNode,
              game.getGhostLastMoveMade(GHOST.INKY),
              DM.MANHATTAN));

    if (game.doesGhostRequireAction(GHOST.PINKY))
      myMoves.put(
          GHOST.PINKY,
          game.getApproximateNextMoveTowardsTarget(
              game.getGhostCurrentNodeIndex(GHOST.PINKY),
              targetNode,
              game.getGhostLastMoveMade(GHOST.PINKY),
              DM.EUCLID));

    if (game.doesGhostRequireAction(GHOST.SUE))
      myMoves.put(GHOST.SUE, moves[rnd.nextInt(moves.length)]);

    return myMoves;
  }
예제 #11
0
  public DataTuple(Game game, MOVE move) {
    if (move == MOVE.NEUTRAL) {
      move = game.getPacmanLastMoveMade();
    }

    this.DirectionChosen = move;

    this.mazeIndex = game.getMazeIndex();
    this.currentLevel = game.getCurrentLevel();
    this.pacmanPosition = game.getPacmanCurrentNodeIndex();
    this.pacmanLivesLeft = game.getPacmanNumberOfLivesRemaining();
    this.currentScore = game.getScore();
    this.totalGameTime = game.getTotalTime();
    this.currentLevelTime = game.getCurrentLevelTime();
    this.numOfPillsLeft = game.getNumberOfActivePills();
    this.numOfPowerPillsLeft = game.getNumberOfActivePowerPills();

    if (game.getGhostLairTime(GHOST.BLINKY) == 0) {
      this.isBlinkyEdible = game.isGhostEdible(GHOST.BLINKY);
      this.blinkyDist =
          game.getShortestPathDistance(
              game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(GHOST.BLINKY));
    }

    if (game.getGhostLairTime(GHOST.INKY) == 0) {
      this.isInkyEdible = game.isGhostEdible(GHOST.INKY);
      this.inkyDist =
          game.getShortestPathDistance(
              game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(GHOST.INKY));
    }

    if (game.getGhostLairTime(GHOST.PINKY) == 0) {
      this.isPinkyEdible = game.isGhostEdible(GHOST.PINKY);
      this.pinkyDist =
          game.getShortestPathDistance(
              game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(GHOST.PINKY));
    }

    if (game.getGhostLairTime(GHOST.SUE) == 0) {
      this.isSueEdible = game.isGhostEdible(GHOST.SUE);
      this.sueDist =
          game.getShortestPathDistance(
              game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(GHOST.SUE));
    }

    this.blinkyDir =
        game.getNextMoveTowardsTarget(
            game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(GHOST.BLINKY), DM.PATH);
    this.inkyDir =
        game.getNextMoveTowardsTarget(
            game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(GHOST.INKY), DM.PATH);
    this.pinkyDir =
        game.getNextMoveTowardsTarget(
            game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(GHOST.PINKY), DM.PATH);
    this.sueDir =
        game.getNextMoveTowardsTarget(
            game.getPacmanCurrentNodeIndex(), game.getGhostCurrentNodeIndex(GHOST.SUE), DM.PATH);

    this.numberOfNodesInLevel = game.getNumberOfNodes();
    this.numberOfTotalPillsInLevel = game.getNumberOfPills();
    this.numberOfTotalPowerPillsInLevel = game.getNumberOfPowerPills();
  }
예제 #12
0
  public Constants.MOVE kNN(Game game, int k, long timeDue) {
    Constants.MOVE bestMove = myMove;

    int current = game.getPacmanCurrentNodeIndex();

    double averageDistance = averageGhostDistance(game);

    int positionBottom = kNNSearch(0, historicalData.size(), averageDistance);
    int positionTop = positionBottom + 1;
    k--;
    int totalCount = k / 2;
    while (totalCount > 0 && k > 0) {
      if (positionBottom > 0) {
        if (historicalData.get(positionBottom).moveAway) {
          totalCount--;
        }
        positionBottom--;
        k--;
        if (k <= 0) break;
      }

      if (positionTop < historicalData.size()) {
        if (historicalData.get(positionTop).moveAway) {
          totalCount--;
        }
        positionTop++;
        k--;
      }
    }

    Constants.MOVE[] next = game.getPossibleMoves(current);
    if (totalCount > 0) {
      double closestAway = Double.POSITIVE_INFINITY;
      for (int index : game.getActivePillsIndices()) {
        double distanceAway = game.getShortestPathDistance(game.getPacmanCurrentNodeIndex(), index);
        if (distanceAway < closestAway) closestAway = distanceAway;
      }

      for (Constants.MOVE eachMove : next) {
        Game newState = game.copy();
        newState.advanceGame(eachMove, new AggressiveGhosts().getMove());
        // if near to pill good;
        double nextClosestAway = Double.POSITIVE_INFINITY;
        for (int index : newState.getActivePillsIndices()) {
          double distanceAway =
              newState.getShortestPathDistance(newState.getPacmanCurrentNodeIndex(), index);
          if (distanceAway < closestAway) nextClosestAway = distanceAway;
        }
        if (nextClosestAway <= closestAway) bestMove = eachMove;
      }
    } else {
      for (Constants.MOVE eachMove : next) {
        Game newState = game.copy();
        newState.advanceGame(eachMove, new AggressiveGhosts().getMove());
        if (averageGhostDistance(newState) < averageDistance) {
          bestMove = eachMove;
        }
      }
    }

    lastMove = bestMove;
    return bestMove;
  }
예제 #13
0
  /**
   * Close to ms pac man.
   *
   * @param game the game
   * @param location the location
   * @return true, if successful
   */
  private boolean closeToMsPacMan(Game game, int location) {
    if (game.getShortestPathDistance(game.getPacmanCurrentNodeIndex(), location) < PACMAN_DISTANCE)
      return true;

    return false;
  }