Example #1
0
  public Action choose(LightsOut.Player actor, Set<Action> options) {
    if (presses == null) {
      presses = new HashSet<Location>();

      // linear algebra solution
      boolean[][] state = actor.state();

      boolean[] s = stateVector(state);
      boolean[][] M = stateMatrix(state[0].length, state.length);
      gauss(M, s);

      // translate to locations
      Location[][] locations = actor.locations();
      for (int x = 0; x < s.length; x++) {
        if (s[x]) {
          presses.add(locations[x / state.length][x % state[0].length]);
        }
      }
    }

    Location next = Groups.first(presses);
    presses.remove(next);

    for (Action action : Groups.ofType(LightsOut.Move.class, options)) {
      if (((LightsOut.Move) action).target.location().equals(next)) {
        return action;
      }
    }

    return null;
  }
Example #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);
  }
Example #3
0
 private Set<Room> safeRooms() {
   Set<Room> safeRooms = new HashSet<Room>();
   for (Room room : privateToWorld.keySet()) {
     if (safe(room)) {
       safeRooms.add(room);
     }
   }
   return safeRooms;
 }
Example #4
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;
  }