Exemple #1
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;
  }
Exemple #2
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;
  }