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
patternis 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)
Note
- The
resetfunction initializes thepatternandboardquadrilles with random sizes and colors. Thepatternis smaller and random, while theboardrepresents the larger search area, containing various colors. This setup is useful for generating new data sets to test thesearchfunctionality.- The
updatefunction recalculates the matches (stored in thehitsarray) between thepatternand theboardquadrilles. It also creates ahintquadrille, which visually highlights the currentpatternmatch during navigation. Withoutupdate, the application would not reflect changes after editing or randomizing quadrilles.
Note
In game code the
hitsarray is the whole story:hits.lengthanswers 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'shintdoes. 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
drawQuadrillecreateQuadrillecellLengthrandclonesizenotsearch- p5.js
createCanvascolorvaluepositionrandomcreateCheckboxcreateSelectcheckedoptionselectedheightblueintredstylegreen
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.