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; }
@Test public void testContainsType() { Collection<A> collection = new HashSet<A>(); collection.add(new B()); collection.add(new B()); assertTrue(Groups.containsType(A.class, collection)); assertTrue(Groups.containsType(B.class, collection)); assertFalse(Groups.containsType(C.class, collection)); }
@Test public void testFirstOfType() { Collection<A> collection = new HashSet<A>(); collection.add(new B()); collection.add(new B()); assertTrue(Groups.firstOfType(C.class, collection) == null); assertTrue(collection.contains(Groups.firstOfType(A.class, collection))); assertTrue(collection.contains(Groups.firstOfType(B.class, collection))); }
@Test public void testFirstEmpty() { Collection<String> collection = new HashSet<String>(); assertTrue(Groups.first(collection) == null); collection.add("Hello World"); assertTrue(Groups.first(collection).equals("Hello World")); collection.add("Goodbye World"); assertTrue(collection.contains(Groups.first(collection))); }
@Test public void testOfType() { Collection<A> collection = new HashSet<A>(); collection.add(new A()); collection.add(new B()); collection.add(new B()); collection.add(new A()); collection.add(new B()); collection.add(new A()); collection.add(new A()); collection.add(new C()); assertTrue(Groups.ofType(A.class, collection).size() == 8); assertTrue(Groups.ofType(B.class, collection).size() == 3); assertTrue(Groups.ofType(C.class, collection).size() == 1); }
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); }