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;
  }