Exemplo n.º 1
0
 int[] nextPillBFS(Node start) {
   ArrayList<Node> alreadyVisited = new ArrayList<Node>();
   Queue<Node> nextToVisit = new LinkedList<Node>();
   Queue<Tuple> startDirection = new LinkedList<Tuple>();
   ArrayList<Edge> firstDirections = start.getEdges();
   for (int i = 0; i < firstDirections.size(); i++) {
     Node nextFieldToVisit = firstDirections.get(i).end;
     nextToVisit.add(nextFieldToVisit);
     startDirection.add(new Tuple(nextFieldToVisit.x - start.x, nextFieldToVisit.y - start.y));
   }
   Node lastTouched;
   Tuple beginningDirection;
   while (true) {
     if (nextToVisit.size() == 0) return new int[] {0, 0};
     lastTouched = nextToVisit.remove();
     beginningDirection = startDirection.remove();
     if (alreadyVisited.contains(lastTouched)) continue;
     if (lastTouched.hasPill()) {
       break;
     }
     alreadyVisited.add(lastTouched);
     ArrayList<Edge> neighbourEdges = lastTouched.getEdges();
     for (int i = 0; i < neighbourEdges.size(); i++) {
       nextToVisit.add(neighbourEdges.get(i).end);
       startDirection.add(beginningDirection);
     }
   }
   return new int[] {beginningDirection.x, beginningDirection.y};
 }
Exemplo n.º 2
0
  // returns an undiscovered child node if one exists, null if it doesn't
  public static Node getUnvisitedChild(Node parent) {
    // copies the parent node's edge list into an array
    Integer[] edges = parent.getEdges().toArray(new Integer[parent.getEdges().size()]);

    // for each edge in edges
    for (Integer edge : edges) {
      // if the edge is undiscovered, return the node that edge refers to
      if (state[edge] == UNDISCOVERED) return scanner.bigGraphOfJustice.getNode(edge);
    }

    // the node has no edges that are undiscovered
    return null;
  }
Exemplo n.º 3
0
 int[] avoidWalls(int[] directions) {
   Node current = level.getNodeAt(pacX, pacY);
   Node toGoToNode = level.getNodeAt(pacX + directions[0], pacY + directions[1]);
   ArrayList<Edge> neighbourEdges = current.getEdges();
   for (int i = 0; i < neighbourEdges.size(); i++)
     if (neighbourEdges.get(i).start == current && neighbourEdges.get(i).end == toGoToNode)
       return directions;
   System.out.println("collision detected");
   System.out.println(directions[0] + " " + directions[1]);
   int randomDirection = (int) Math.signum(r.nextFloat() - .5);
   System.out.println(randomDirection);
   return new int[] {directions[1] * randomDirection, directions[0] * randomDirection};
 }
Exemplo n.º 4
0
  /*
   * Finds the shortest route from a specified start node to a specified end node
   */
  private static void findShortestRoute(String inputString) {
    HashMap<Character, Node> nodeMapTemp = new HashMap<Character, Node>(nodeMap);
    HashMap<Character, Node> nodeMapDijkstra = new HashMap<Character, Node>();
    PriorityQueue<Node> pq = new PriorityQueue<Node>();
    Node current;
    Node currentTo;
    int shortestRoute = INFINITY;
    Set nodeSet;

    first = inputString.charAt(0);
    second = inputString.charAt(2);

    for (char key : nodeMap.keySet()) {
      nodeMap.get(key).setDistance(INFINITY);
    }

    if (first == second) {
      edgeList = nodeMapTemp.get(first).getEdges();
      for (i = 0; i < edgeList.size(); i++) {
        current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
        current.setDistance(edgeList.get(i).getEdgeLength());
        pq.add(current);
      }
      nodeMapTemp.remove(first);
      while (pq.size() != 0) {
        current = pq.peek();
        edgeList = current.getEdges();
        if (current.getName() == second) {
          break;
        } else {
          nodeMapDijkstra.put(pq.peek().getName(), pq.peek());
          nodeMapTemp.remove(pq.poll().getName());
        }
        for (i = 0; i < edgeList.size(); i++) {
          if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) {
            currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
            if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) {
              currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance());
            }
            pq.add(currentTo);
          } else {
          }
        }
      }
      for (char key : nodeMapDijkstra.keySet()) {
        edgeList = nodeMapDijkstra.get(key).getEdges();
        for (i = 0; i < edgeList.size(); i++) {
          if (edgeList.get(i).getEdgeDestination() == second) {
            if (nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength()
                < shortestRoute) {
              shortestRoute =
                  nodeMapDijkstra.get(key).getDistance() + edgeList.get(i).getEdgeLength();
            }
          }
        }
      }
    } else {
      edgeList = nodeMapTemp.get(first).getEdges();
      for (i = 0; i < edgeList.size(); i++) {
        current = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
        current.setDistance(edgeList.get(i).getEdgeLength());
        pq.add(current);
      }
      nodeMapTemp.remove(first);
      while (nodeMapTemp.get(second) != null) {
        current = pq.peek();
        edgeList = current.getEdges();
        if (current.getName() == second) {
          break;
        } else {
          nodeMapTemp.remove(pq.poll().getName());
        }
        for (i = 0; i < edgeList.size(); i++) {
          if (nodeMapTemp.get(edgeList.get(i).getEdgeDestination()) != null) {
            currentTo = nodeMapTemp.get(edgeList.get(i).getEdgeDestination());
            if (currentTo.getDistance() > edgeList.get(i).getEdgeLength() + current.getDistance()) {
              currentTo.setDistance(edgeList.get(i).getEdgeLength() + current.getDistance());
            }
            pq.add(currentTo);
          }
        }
      }
      shortestRoute = nodeMapTemp.get(second).getDistance();
    }
    try {
      bw.write(Integer.toString(shortestRoute));
      bw.newLine();
    } catch (IOException e) {
      System.out.println(e);
    }
  }