@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);
    }
  }
  /**
   * 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();
  }
Example #3
0
  private DNode[] assignJunctionsToNodes(Game game) {
    Maze m = game.getCurrentMaze();
    int numNodes = m.graph.length;

    DNode[] allNodes = new DNode[numNodes];

    for (int i = 0; i < numNodes; i++) {
      boolean isJunction = game.isJunction(i);
      allNodes[i] = new DNode(i, isJunction);

      if (!isJunction) {
        MOVE[] possibleMoves = m.graph[i].allPossibleMoves.get(MOVE.NEUTRAL);

        for (int j = 0; j < possibleMoves.length; j++) {
          ArrayList<Integer> path = new ArrayList<Integer>();

          MOVE lastMove = possibleMoves[j];
          int currentNode = game.getNeighbour(i, lastMove);
          path.add(currentNode);

          while (!game.isJunction(currentNode)) {
            MOVE[] newPossibleMoves = game.getPossibleMoves(currentNode);

            for (int q = 0; q < newPossibleMoves.length; q++)
              if (newPossibleMoves[q].opposite() != lastMove) {
                lastMove = newPossibleMoves[q];
                break;
              }

            currentNode = game.getNeighbour(currentNode, lastMove);
            path.add(currentNode);
          }

          int[] array = new int[path.size()];

          for (int w = 0; w < path.size(); w++) array[w] = path.get(w);

          allNodes[i].addPath(array[array.length - 1], possibleMoves[j], i, array, lastMove);
        }
      }
    }

    return allNodes;
  }
  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;
  }