Exemplo n.º 1
0
 public Rule(
     Playfield<CharacterElement> p,
     IntegerElement x,
     IntegerElement y,
     IntegerElement width,
     CharacterElement wildcard) {
   super(p, x, y, width, width.divide(TWO));
   this.width = width;
   this.height = width.divide(TWO);
   this.wildcard = wildcard;
 }
Exemplo n.º 2
0
  /*
   * 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);
        }
      }
    }
  }
Exemplo n.º 3
0
  // This should be a method on a Dumper object which takes a Codec.
  public String dump() {
    IntegerElement min_x = getMinX();
    IntegerElement min_y = getMinY();
    IntegerElement max_x = getMaxX();
    IntegerElement max_y = getMaxY();
    IntegerElement x = min_x;
    IntegerElement y = min_y;
    StringBuffer buf = new StringBuffer();

    // System.out.println("(" + getOffsetX() + "," + getOffsetY() + ")_(" + width + "x" + height +
    // ")");
    while (y.compareTo(max_y) <= 0) {
      x = min_x;
      while (x.compareTo(max_x) <= 0) {
        CharacterElement e = get(x, y);
        buf.append(dumpElement(e));
        x = x.succ();
      }
      y = y.succ();
      buf.append("\n");
    }

    return buf.toString();
  }
Exemplo n.º 4
0
  public List<Rule> findAllRules() {
    IntegerElement x, y;
    ArrayList<Rule> rules = new ArrayList<Rule>();

    for (x = (IntegerElement) getMinX(); x.compareTo(getMaxX()) <= 0; x = x.succ()) {
      for (y = (IntegerElement) getMinY(); y.compareTo(getMaxY()) <= 0; y = y.succ()) {
        boolean escaped = (!y.isZero() && get(x, y.pred()).getChar() != ' ');
        if (escaped) continue;
        if (get(x, y).getChar() == '(') {
          // System.out.println("Found a rule start at " + x + ", " + y);
          IntegerElement x2 = x;
          CharacterElement g = get(x2, y);
          while (g.getChar() != ')' && x2.compareTo(getMaxX()) <= 0) {
            x2 = x2.succ();
            g = get(x2, y);
          }
          if (g.getChar() != ')') continue;
          // System.out.println("Found a rule from " + x + "," + y + " to " + x2);
          CharacterElement w = get(x2.pred(), y);
          if (w.getChar() == ' ') w = null;
          Rule r = new Rule(this, x.succ(), y.succ(), (x2.subtract(x)).pred(), w);
          rules.add(r);
        }
      }
    }

    return rules;
  }
Exemplo n.º 5
0
  public Set<Position> getAllMatches(
      Playfield<E> haystack, Playfield<E> needle, Matcher<E, E> matcher) {
    Set<Position> results = new HashSet<Position>();

    IntegerElement haystackWidth = haystack.getMaxX().subtract(haystack.getMinX()).succ();
    IntegerElement haystackHeight = haystack.getMaxY().subtract(haystack.getMinY()).succ();
    IntegerElement needleWidth = needle.getMaxX().subtract(needle.getMinX()).succ();
    IntegerElement needleHeight = needle.getMaxY().subtract(needle.getMinY()).succ();

    if (needleWidth.compareTo(haystackWidth) > 0 || needleHeight.compareTo(haystackHeight) > 0) {
      // thing being sought is larger than the thing seeking in, so, no
      return results;
    }

    IntegerElement xSpan = haystackWidth.subtract(needleWidth).succ();
    IntegerElement ySpan = haystackHeight.subtract(needleHeight).succ();

    IntegerElement x, y;
    for (x = IntegerElement.ZERO; x.compareTo(xSpan) < 0; x = x.succ()) {
      for (y = IntegerElement.ZERO; y.compareTo(ySpan) < 0; y = y.succ()) {
        if (isMatchAt(haystack, needle, matcher, x, y)) {
          results.add(new Position(x, y));
        } else {
        }
      }
    }
    return results;
  }
Exemplo n.º 6
0
  public boolean isMatchAt(
      Playfield<E> haystack,
      Playfield<E> needle,
      Matcher<E, E> m,
      IntegerElement x,
      IntegerElement y) {
    IntegerElement width = needle.getMaxX().subtract(needle.getMinX()).succ();
    IntegerElement height = needle.getMaxY().subtract(needle.getMinY()).succ();

    if (x.pred().add(width).compareTo(haystack.getMaxX()) > 0
        || y.pred().add(height).compareTo(haystack.getMaxY()) > 0) {
      // exceeds the right or bottom edge, so, no
      return false;
    }
    IntegerElement cx, cy, dx, dy;
    for (cx = x, dx = needle.getMinX();
        dx.compareTo(needle.getMaxX()) <= 0;
        cx = cx.succ(), dx = dx.succ()) {
      for (cy = y, dy = needle.getMinY();
          dy.compareTo(needle.getMinY()) <= 0;
          cy = cy.succ(), dy = dy.succ()) {
        E soughtElem = needle.get(dx, dy);
        E foundElem = haystack.get(cx, cy);
        // System.out.printf("sought (%s,%s): '%s', found (%s,%s): '%s'\n",
        //     dx, dy, soughtElem.getName(),
        //     cx, cy, foundElem.getName());
        if (m.match(soughtElem, foundElem)) {
          // add something to the result-list
        } else {
          return false;
        }
      }
    }
    return true;
  }