public List<Position> findAllRuleMatches(Rule r) { // System.out.printf("%s", r.dump()); WildcardMatcher matcher = new WildcardMatcher(r.getWildcard()); PlayfieldMatcher<CharacterElement> pm = new PlayfieldMatcher<CharacterElement>(); List<Position> positions = new ArrayList<Position>(); // We only want to match the LEFT side of the rule, so we do this: Rule matchSubject = r.createMatchSubject(); for (Position p : pm.getAllMatches(this, matchSubject, matcher)) { // System.out.println("CONSIDERING (" + p.getX() + "," + p.getY() + ")"); // Discard all the ones that are at or above the rule if (p.getY().compareTo(r.getOffsetY().add(r.getHeight())) >= 0) { // System.out.println("(" + p.getX() + "," + p.getY() + ")..." + r.getOffsetY()); positions.add(p); } } // System.out.println("---"); return positions; }
/* * We assume a match was made at the given position. */ public void applyRule(Rule r, Position p) { IntegerElement i, j; CharacterElement wildcard = r.getWildcard(); // System.out.printf("applying at %s: %s", p.toString(), r.dump()); /* Note that r.getHeight() == width/2 ... */ for (i = IntegerElement.ZERO; i.compareTo(r.getHeight()) < 0; i = i.succ()) { for (j = IntegerElement.ZERO; j.compareTo(r.getHeight()) < 0; j = j.succ()) { CharacterElement replacement = r.get(i.add(r.getHeight()), j); IntegerElement destX = p.getX().add(i); IntegerElement destY = p.getY().add(j); // System.out.printf("(%s,%s): [%s] '%s' -> (%s,%s)\n", i, j, wildcard, // replacement.getChar(), destX, destY); if (wildcard == null || wildcard.getChar() != replacement.getChar()) { set(destX, destY, replacement); } } } }