-
Notifications
You must be signed in to change notification settings - Fork 0
/
mon10-codeplay-example-grid-summary.js
executable file
·69 lines (54 loc) · 1.84 KB
/
mon10-codeplay-example-grid-summary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
mon10-codeplay-example-grid-summary.js / gridSummary()
================================================================================
about: uses mon10.getXY() and mon10.size() to build a simple summary of
the current grid state, including positions of tiles by grouped by value
requires: Mon10 object from https://madeofnumbers.com/10/mon10-latest.min.js
example usage : let mon10 = new Mon10(document.body)
usage: gridSummary() // returns grid summary result object
*/
function gridSummary() {
let result = {};
// ===== add array of numbers to result (value -10 through to 10) =====
result.numbers = [];
for (let i = 0; i < 19; i++) {
result.numbers.push({
value: i - 9,
positions: []
});
}
// ===== add array of blocks to result (move 0 through to 7) =====
result.blocks = [];
for (let i = 0; i < 8; i++) {
result.blocks.push({
move: i,
positions: []
});
}
// ===== add counters for numbers/blocks found to result =====
result.numbersFound = 0;
result.blocksFound = 0;
// ===== loop through grid checking for numbers and blocks =====
for (let iy = 0; iy < mon10.size(); iy++) {
for (let ix = 0; ix < mon10.size(); ix++) {
let resultGetXY = mon10.getXY(ix, iy);
if (!resultGetXY.block && !resultGetXY.empty) {
// ===== push position into number (of same value) and increment numbersFound =====
result.numbers[resultGetXY.number + 9].positions.push({
x: ix,
y: iy
});
result.numbersFound++;
}
if (resultGetXY.block) {
// ===== push position into block (of same move) and increment blocksFound =====
result.blocks[resultGetXY.move].positions.push({
x: ix,
y: iy
});
result.blocksFound++;
}
}
}
return result;
}