Ejemplo n.º 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();
  }
Ejemplo n.º 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);
    }
  }
Ejemplo n.º 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;
  }