summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-11-11 15:48:28 -0600
committersanine <sanine.not@pm.me>2023-11-11 15:48:28 -0600
commit4715baef25b39d2614b1d0cc67d8bcff5676b6ce (patch)
tree52cdaf566888f163556cd77afe98db1b8e8f716b /src
parentf995438c0e72e1d08203609d74854d07dbecd661 (diff)
add setup_board
Diffstat (limited to 'src')
-rw-r--r--src/simulation/game.js46
-rw-r--r--src/simulation/game.test.js33
2 files changed, 79 insertions, 0 deletions
diff --git a/src/simulation/game.js b/src/simulation/game.js
new file mode 100644
index 0000000..991d125
--- /dev/null
+++ b/src/simulation/game.js
@@ -0,0 +1,46 @@
+'use strict';
+
+
+function is_wall(size, x, y) {
+ return (
+ x === 0 || x === size-1 ||
+ y === 0 || y === size-1
+ );
+}
+function is_corner(size, x, y) {
+ const subsize = Math.floor(size/3);
+ return (
+ (x < subsize || x >= 2*subsize) &&
+ (y < subsize || y >= 2*subsize)
+ );
+}
+function get_team(size, x, y) {
+ const subsize = Math.floor(size/3);
+ if (y < subsize) {
+ return 0;
+ } else if (x >= 2*subsize) {
+ return 1;
+ } else if (y >= 2*subsize) {
+ return 2;
+ } else if (x < subsize) {
+ return 3;
+ } else {
+ return undefined;
+ }
+}
+
+export function setup_board(size) {
+ const lattice = [...Array(size)]
+ .map(() => [...Array(size)])
+ .map((row, y) => row.map((_, x) => {
+ if (is_wall(size, x, y)) {
+ return { type: 'immutable', flags: {} };
+ } else if (is_corner(size, x, y)) {
+ return { type: 'immutable', flags: {} };
+ } else {
+ const team = get_team(size, x, y);
+ return { type: 'empty', flags: { team } };
+ }
+ }));
+ return lattice;
+}
diff --git a/src/simulation/game.test.js b/src/simulation/game.test.js
new file mode 100644
index 0000000..ac65618
--- /dev/null
+++ b/src/simulation/game.test.js
@@ -0,0 +1,33 @@
+'use strict';
+
+import { setup_board } from './game.js';
+
+
+test("set up boards correctly", () => {
+ const _ = { type: 'empty', flags: {} };
+ const a = { type: 'empty', flags: { team: 0 } };
+ const b = { type: 'empty', flags: { team: 1 } };
+ const c = { type: 'empty', flags: { team: 2 } };
+ const d = { type: 'empty', flags: { team: 3 } };
+ const W = { type: 'immutable', flags: {} };
+
+ expect(setup_board(6)).toEqual([
+ [ W, W, W, W, W, W, ],
+ [ W, W, a, a, W, W, ],
+ [ W, d, _, _, b, W, ],
+ [ W, d, _, _, b, W, ],
+ [ W, W, c, c, W, W, ],
+ [ W, W, W, W, W, W, ],
+ ]);
+ expect(setup_board(9)).toEqual([
+ [ W, W, W, W, W, W, W, W, W, ],
+ [ W, W, W, a, a, a, W, W, W, ],
+ [ W, W, W, a, a, a, W, W, W, ],
+ [ W, d, d, _, _, _, b, b, W, ],
+ [ W, d, d, _, _, _, b, b, W, ],
+ [ W, d, d, _, _, _, b, b, W, ],
+ [ W, W, W, c, c, c, W, W, W, ],
+ [ W, W, W, c, c, c, W, W, W, ],
+ [ W, W, W, W, W, W, W, W, W, ],
+ ]);
+});