diff options
author | nil <niljo@airmail.cc> | 2024-12-12 18:20:22 +0100 |
---|---|---|
committer | nil <niljo@airmail.cc> | 2024-12-12 18:20:22 +0100 |
commit | 16e02376e2273eeb25a6461f84641e96a485f652 (patch) | |
tree | 64a1deee7d8fdfda0a75475642364fc1ba4d8500 | |
parent | 8a49fc718cbfebe593d18dcf37fef2a19f3aa570 (diff) |
add legal movements and organize
-rw-r--r-- | src/board.js | 18 | ||||
-rw-r--r-- | src/engine/moves.js | 11 | ||||
-rw-r--r-- | src/engine/turns.js | 24 | ||||
-rw-r--r-- | src/engine/visual/createBoard.js | 15 | ||||
-rw-r--r-- | src/engine/visual/placeOnBoard.js | 34 | ||||
-rw-r--r-- | src/link.js | 7 | ||||
-rw-r--r-- | src/stone.js | 1 |
7 files changed, 70 insertions, 40 deletions
diff --git a/src/board.js b/src/board.js index 82b6b28..3777080 100644 --- a/src/board.js +++ b/src/board.js @@ -1,3 +1,5 @@ +import { createBoard } from "./engine/visual/createBoard.js" + export let board = { element: document.getElementById("board"), size: 9, @@ -9,19 +11,3 @@ export function prepareBoard() { board.stones = [...Array(board.size)].map(_ => Array(board.size)) createBoard(board.size) } - -// ########### board engine ############# - -function createBoard(size) { - let table = document.createElement("table") - for (let r = 1; r < size; r++) { - let tr = document.createElement("tr") - - for (let c = 1; c < size; c++) { - let td = document.createElement("td") - tr.appendChild(td) - } - table.appendChild(tr) - } - board.element.appendChild(table) -} diff --git a/src/engine/moves.js b/src/engine/moves.js new file mode 100644 index 0000000..f820f97 --- /dev/null +++ b/src/engine/moves.js @@ -0,0 +1,11 @@ +import { board } from "../board.js" + +export function isMoveLegal({ + position, + turn = "none" +}) { + if (board.stones[position.y][position.x] != undefined) { + return false + } + return true +} diff --git a/src/engine/turns.js b/src/engine/turns.js index 7026dcc..6a5e6fb 100644 --- a/src/engine/turns.js +++ b/src/engine/turns.js @@ -2,14 +2,19 @@ import { board } from "../board.js" import { Vector2 } from "../engine/visual/vector2.js" import { Link } from "../link.js" import { PLAYER } from "../constants.js" +import { isMoveLegal } from "../engine/moves.js" -export let turn = "" +export let playerTurn = "" export function nextTurn() { - turn = turn == PLAYER.BLACK ? PLAYER.WHITE : PLAYER.BLACK + changeTurn() placeLinks() } +function changeTurn() { + playerTurn = playerTurn == PLAYER.BLACK ? PLAYER.WHITE : PLAYER.BLACK +} + function placeLinks() { resetLink() board.links = [...Array(board.size)].map(_ => Array(board.size)) @@ -22,13 +27,22 @@ function placeLinks() { function resetLink() { Array.from(board.links).forEach(x => { - Array.from(x).filter((x) => x !== undefined).map(y => { - y.removeLink() - }) + 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, }) diff --git a/src/engine/visual/createBoard.js b/src/engine/visual/createBoard.js new file mode 100644 index 0000000..06fcf5e --- /dev/null +++ b/src/engine/visual/createBoard.js @@ -0,0 +1,15 @@ +import { board } from "../../board.js" + +export function createBoard(size) { + let table = document.createElement("table") + for (let r = 1; r < size; r++) { + let tr = document.createElement("tr") + + for (let c = 1; c < size; c++) { + let td = document.createElement("td") + tr.appendChild(td) + } + table.appendChild(tr) + } + board.element.appendChild(table) +} diff --git a/src/engine/visual/placeOnBoard.js b/src/engine/visual/placeOnBoard.js index cee2969..4eea0d6 100644 --- a/src/engine/visual/placeOnBoard.js +++ b/src/engine/visual/placeOnBoard.js @@ -6,17 +6,23 @@ const HEAD_TOP_VH = 20 const MEDIA_LISTENER = window.matchMedia("(max-width: 600px)") MEDIA_LISTENER.addEventListener("change", function() { - Array.from(board.stones).forEach(x => { - Array.from(x).filter((x) => x !== undefined).map(y => { - y.update(MEDIA_LISTENER) - }) - }) - - Array.from(board.links).forEach(x => { - Array.from(x).filter((x) => x !== undefined).map(y => { - y.update(MEDIA_LISTENER) - }) - }) + Array.from(board.stones) + .forEach(x => { + Array.from(x) + .filter((x) => x !== undefined) + .map(y => { + y.update(MEDIA_LISTENER) + }) + }) + + Array.from(board.links) + .forEach(x => { + Array.from(x) + .filter((x) => x !== undefined) + .map(y => { + y.update(MEDIA_LISTENER) + }) + }) }); const PLACE_VALUES = { @@ -34,12 +40,12 @@ const PLACE_VALUES = { export function placeOnBoard({ position = new Vector2(0, 0), board_size = 9, - use_real_size = false, + use_link_size = false, }) { let response = {} if (MEDIA_LISTENER.matches) { let totalSize = ( - use_real_size ? + use_link_size ? PLACE_VALUES.link.size_mob : PLACE_VALUES.stone.size_mob ) let size = totalSize / board_size @@ -48,7 +54,7 @@ export function placeOnBoard({ response["size"] = `${size}vw` } else { let totalSize = ( - use_real_size ? + use_link_size ? PLACE_VALUES.link.size : PLACE_VALUES.stone.size ) let size = totalSize / board_size diff --git a/src/link.js b/src/link.js index 7d10413..e0c8266 100644 --- a/src/link.js +++ b/src/link.js @@ -3,15 +3,14 @@ import { Vector2 } from "./engine/visual/vector2.js" import { board } from "./board.js" import { ASSETS_BLACK, ASSETS_WHITE, PLAYER } from "./constants.js" import { Stone } from "./stone.js" -import { nextTurn, turn } from "./engine/turns.js" - +import { nextTurn, playerTurn } from "./engine/turns.js" export class Link { constructor({ position = new Vector2(0, 0), }) { - this.team = turn + this.team = playerTurn this.position = position this.createSpan() @@ -43,7 +42,7 @@ export class Link { let response = placeOnBoard({ position: this.position, board_size: board.size, - use_real_size: true, + use_link_size: true, }) this.span.style.left = response.left diff --git a/src/stone.js b/src/stone.js index 5203fed..0fc8607 100644 --- a/src/stone.js +++ b/src/stone.js @@ -4,7 +4,6 @@ import { board } from "./board.js" import { ASSETS_BLACK, ASSETS_WHITE, PLAYER } from "./constants.js" - export class Stone { constructor({ team = PLAYER.BLACK, |