search(pattern, strict)

Searches this quadrille for every placement of pattern and returns the matches as an array of {row, col} upper-left corners — empty on failure, uniformly, with no special cases to test for: the same return convention path follows. Check hits.length for success; index hits[i].row / hits[i].col to use a match.

What counts as a match depends on strict:

  • Non-strict (default): a placement matches when this quadrille is filled wherever the pattern is filled — values are ignored, only the shape matters.
  • Strict: the values must additionally be the same instances (===) — reference identity, not lookalikes.
let shared = color('blue');          // one instance, stored once, used twice
let quadrille = createQuadrille([125, shared, 'hi']);

let pattern = createQuadrille([shared, 'hi']);
quadrille.search(pattern, true);     // [{row: 0, col: 1}] — same 'blue' instance

pattern = createQuadrille([color('blue'), 'hi']);
quadrille.search(pattern, true);     // [] — a lookalike 'blue' is a different instance
quadrille.search(pattern);           // [{row: 0, col: 0}, {row: 0, col: 1}] — non-strict: shape only

Primitives compare by value ('hi' === 'hi'), so strict mode bites on objects — colors, images, class instances: two calls to color('blue') produce two distinct instances that render identically yet never strict-match. Store a shared instance once (the shared variable above) when strict matching is the goal.

Example

(click cells to edit pattern and quadrille; left / right arrow keys to move to next found hit)

edit, then run

Note

  • The reset function initializes the pattern and board quadrilles with random sizes and colors. The pattern is smaller and random, while the board represents the larger search area, containing various colors. This setup is useful for generating new data sets to test the search functionality.
  • The update function recalculates the matches (stored in the hits array) between the pattern and the board quadrilles. It also creates a hint quadrille, which visually highlights the current pattern match during navigation. Without update, the application would not reflect changes after editing or randomizing quadrilles.

Note

In game code the hits array is the whole story: hits.length answers did the pattern occur (three-in-a-row, a tetromino, a formation), and each {row, col} corner places a response — highlight, capture, score — via fill or a second quadrille drawn at that offset, as the example's hint does. path speaks the same dialect for movement: array in hand, empty means no.

Syntax

search(pattern, [strict = false])

Parameters

Param Description
pattern Quadrille: pattern to be searched
strict Boolean: If false (default), a match only requires filled cells wherever the pattern is filled (shape only); if true, values must also be identical instances (===)
What this page's examples use
p5.quadrille
drawQuadrille createQuadrille cellLength rand clone size not search
p5.js
createCanvas color value position random createCheckbox createSelect checked option selected height blue int red style green

Names the analysis can prove are called, and the line each one is documented on. Local variables and members of an unknown receiver are left out.