Exemplo n.º 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;
  }
Exemplo n.º 2
0
  public Action choose(WumpusWorld.Hunter hunter, Set<Action> options) {
    System.out.format("%n%n");
    updateInternal(hunter);
    Room here = worldToPrivate.get(hunter.location());

    // check to see if we need to make a new decision
    if (decision != null && !decision.done(options)) {
      return decision.step(options);
    }

    // kill any adjacent, known wumpuses
    for (Room wumpus : here.adjacent) {
      if (wumpusKB.known(wumpus) && wumpusKB.evaluate(wumpus)) {
        System.out.println("DIE!");
        decision = new KillDecision(hunter, wumpus);
        return decision.step(options);
      }
    }

    // find any known wumpuses to kill
    for (Room wumpus : privateToWorld.keySet()) {
      if (wumpusKB.known(wumpus) && wumpusKB.evaluate(wumpus)) {
        Set<Room> safeRooms = safeRooms();
        safeRooms.add(wumpus);

        List<Room> path = path(here, wumpus, safeRooms);
        path.remove(wumpus);
        System.out.println("I've got it!");
        decision = new PathDecision(hunter, path);
        return decision.step(options);
      }
    }

    // find safe rooms to explore
    {
      System.out.println(safeRooms());
      List<Room> path = path(here, safeRooms());
      if (path != null) {
        System.out.println("Walking!");
        decision = new PathDecision(hunter, path);
        return decision.step(options);
      }
    }

    // find bats to exploit
    for (Room bat : privateToWorld.keySet()) {
      if (batKB.known(bat) && batKB.evaluate(bat)) {
        Set<Room> safeRooms = safeRooms();
        safeRooms.add(bat);
        System.out.println("Find a Bat!");
        List<Room> path = path(here, bat, safeRooms);
        return decision.step(options);
      }
    }

    // otherwise, take your 3 chances... they're only arrows!
    decision = new KillDecision(hunter, Groups.any(here.adjacent));
    return decision.step(options);
  }
Exemplo n.º 3
0
    public Action step(Set<Action> options) {
      Room here = worldToPrivate.get(hunter.location());

      if (path == null || path.isEmpty()) {
        return null;
      }
      Room next = path.remove(0);
      return hunter.move(privateToWorld.get(next), options);
    }
Exemplo n.º 4
0
 public boolean done(Set<Action> options) {
   // TODO verify still valid
   return path == null || path.isEmpty();
 }