match-n-scanner
v1.0.0
Published
Scan the matrix and return parts where equal values are connected
Downloads
7
Readme
match-n-scanner
Scan the matrix and return parts where equal values are connected.
It is intended to be used for Match 3 games, Match 4 games, .. and others.
Installation
npm install match-n-scanner
Basic usage
const MatchNScanner = require('match-n-scanner');
const matrix = [
['A', 'B', 'C'],
['D', 'D', 'C'],
['C', 'A', 'C'],
];
const scanner = new MatchNScanner(matrix);
const matchesAbove3Length = scanner.scan({minMatchLength: 3});
console.log(matchesAbove3Length);
// ->
// [ [ { element: 'C', rowIndex: 0, columnIndex: 2 },
// { element: 'C', rowIndex: 1, columnIndex: 2 },
// { element: 'C', rowIndex: 2, columnIndex: 2 } ] ]
const matchesAbove2Length = scanner.scan({minMatchLength: 2});
console.log(matchesAbove2Length);
// ->
// [ [ { element: 'C', rowIndex: 0, columnIndex: 2 },
// { element: 'C', rowIndex: 1, columnIndex: 2 },
// { element: 'C', rowIndex: 2, columnIndex: 2 } ],
// [ { element: 'D', rowIndex: 1, columnIndex: 0 },
// { element: 'D', rowIndex: 1, columnIndex: 1 } ] ]
APIs
Sorry, see tests or source code for a detailed explanation.
MatchNScanner class
constructor(matrix, options = {})
options: {
// Expression for checking whether elements are equal
equalityChecker?: (elementA, elementB) => boolean,
} = {};
fromText(matrixAsText, options = {})
MatchNScanner instance
scan(options = {})
options: {
// Minimum length to return
minMatchLength?: number,
// If it returns false, this element will be ignored
sieve?: (any) => boolean,
} = {}