Esempio n. 1
0
  private Junction[] junctionDistances(Game game) {
    Maze m = game.getCurrentMaze();
    int[] indices = m.junctionIndices;

    Junction[] junctions = new Junction[indices.length];

    for (int q = 0; q < indices.length; q++) // from
    {
      MOVE[] possibleMoves =
          m.graph[indices[q]].allPossibleMoves.get(MOVE.NEUTRAL); // all possible moves

      junctions[q] = new Junction(q, indices[q], indices.length);

      for (int z = 0; z < indices.length; z++) // to (we need to include distance to itself)
      {
        for (int i = 0; i < possibleMoves.length; i++) {
          int neighbour = game.getNeighbour(indices[q], possibleMoves[i]);
          int[] p = m.astar.computePathsAStar(neighbour, indices[z], possibleMoves[i], game);
          m.astar.resetGraph();

          junctions[q].addPath(z, possibleMoves[i], p);
        }
      }
    }

    return junctions;
  }
Esempio n. 2
0
  public PathsCache(int mazeIndex) {
    junctionIndexConverter = new HashMap<Integer, Integer>();

    this.game = new Game(0, mazeIndex);
    Maze m = game.getCurrentMaze();

    int[] jctIndices = m.junctionIndices;

    for (int i = 0; i < jctIndices.length; i++) junctionIndexConverter.put(jctIndices[i], i);

    nodes = assignJunctionsToNodes(game);
    junctions = junctionDistances(game);

    for (int i = 0; i < junctions.length; i++) junctions[i].computeShortestPaths();
  }
Esempio 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;
  }