From add682d6aaa50b2c1561731cdb1696bf7665cba3 Mon Sep 17 00:00:00 2001 From: nil Date: Sat, 14 Dec 2024 21:38:07 +0100 Subject: liberty fix and tidying code --- src/engine/placeLinks.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/engine/placeLinks.js (limited to 'src/engine/placeLinks.js') diff --git a/src/engine/placeLinks.js b/src/engine/placeLinks.js new file mode 100644 index 0000000..07d67d3 --- /dev/null +++ b/src/engine/placeLinks.js @@ -0,0 +1,38 @@ +import { board } from "../board.js" +import { Vector2 } from "../engine/vector2.js" +import { Link } from "../link.js" +import { isMoveLegal } from "../engine/moves.js" +import { playerTurn } from "../engine/turns.js" + +export function placeLinks() { + resetLink() + board.links = [...Array(board.size)].map(_ => Array(board.size)) + for (let row = 0; row < board.size; row++) { + for (let col = 0; col < board.size; col++) { + tryLink(new Vector2(row, col)) + } + } +} + +function resetLink() { + Array.from(board.links).forEach(x => { + Array.from(x) + .filter((x) => x !== undefined) + .map(y => { + y.removeLink() + }) + }) +} + +function tryLink(position) { + if (!isMoveLegal({ + position: position, + team: playerTurn + })) { + return + } + + new Link({ + position: position, + }) +} -- cgit v1.2.3