Пример #1
0
  // pathing
  private List<Room> path(Room start, Room destination, Set<Room> travelable) {
    Queue<Room> frontier = new LinkedList<Room>();
    frontier.add(start);

    Map<Room, Room> visited = new HashMap<Room, Room>();
    visited.put(start, null);

    while (!frontier.isEmpty()) {
      Room current = frontier.poll();
      System.out.println("\n---\n" + frontier + "\n\n" + travelable + "\n---\n");

      if ((destination == null && current.visited == false) || current == destination) {
        List<Room> path = new ArrayList<Room>();

        Room next = current;
        while (visited.get(next) != null) {
          path.add(0, next);
          next = visited.get(next);
        }

        System.out.println("Returning: " + path);
        return path;
      }

      for (Room next : current.adjacent) {
        if (!visited.containsKey(next) && travelable.contains(next)) {
          frontier.add(next);
          visited.put(next, current);
        }
      }
    }

    System.out.println("Returning: null");
    return null;
  }
Пример #2
0
  private void updateInternal(WumpusWorld.Hunter hunter) {
    // initialization (unknown -> start building internal representation)
    if (!worldToPrivate.containsKey(hunter.location())) {
      worldToPrivate.put(hunter.location(), new Room());
    }

    // update local information
    Room here = worldToPrivate.get(hunter.location());
    here.visited = true;

    wumpusKB.assign(here, false);
    batKB.assign(here, false);
    pitKB.assign(here, false);

    // update adjacent rooms
    for (Edge edge : hunter.location().edges) {
      if (!worldToPrivate.containsKey(edge.end)) {
        worldToPrivate.put(edge.end, new Room());
      }
      here.adjacent.add(worldToPrivate.get(edge.end));
      worldToPrivate.get(edge.end).adjacent.add(here);
    }

    // update knowledge base regarding adjacent rooms
    boolean rebuild = false;
    for (Room room : here.adjacent) {
      if (!hunter.scent() && wumpusKB.conflicts(room, false)) {
        rebuild = true;
      }
    }
    if (rebuild) {
      /*
      	BoolTable newWumpusKB = new BoolTable();
      	for(Room room : privateToWorld.keySet()) {
      		if(wumpusKB.known(room) && wumpusKB.evaluate(room) == false) {
      			newWumpusKB.assign(room, false);
      		}
      		if(!wumpusKB.known(room)) {
      			System.out.format("Suspicious Room: %s!%n", room);
      			for(Room adj : room.adjacent) {
      				System.out.format("   Resetting: %s!%n", adj);
      				room.visited = false;
      			}
      		}
      	}
      	wumpusKB = newWumpusKB;
      */
    }

    for (Room room : here.adjacent) {
      if (!hunter.scent()) {
        wumpusKB.assign(room, false);
      }
      if (!hunter.flapping()) {
        batKB.assign(room, false);
      }
      if (!hunter.wind()) {
        pitKB.assign(room, false);
      }
    }

    Map<Room, Boolean> possible = new HashMap<Room, Boolean>();
    for (Room room : here.adjacent) {
      possible.put(room, true);
    }
    if (hunter.scent()) {
      wumpusKB.assign(possible);
    }
    if (hunter.flapping()) {
      batKB.assign(possible);
    }
    if (hunter.wind()) {
      pitKB.assign(possible);
    }

    System.out.format("Wumpus KB : %s%n", wumpusKB);
    System.out.format("Bat KB    : %s%n", batKB);
    System.out.format("Pit KB    : %s%n", pitKB);
  }