From 4715baef25b39d2614b1d0cc67d8bcff5676b6ce Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 11 Nov 2023 15:48:28 -0600 Subject: add setup_board --- src/simulation/game.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/simulation/game.js (limited to 'src/simulation/game.js') 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; +} -- cgit v1.2.1