From e684f651015789a76d1e3fdefc3e3dbb936eb556 Mon Sep 17 00:00:00 2001 From: Manu Artero Anguita Date: Tue, 20 Sep 2022 18:46:41 +0200 Subject: [PATCH] feat: available movements considering a piece of the same owner --- .../board/state/board-state-reducer.ts | 11 +++--- src/models/board.ts | 17 +++++++-- src/models/movements.ts | 35 ------------------- src/models/tiles.ts | 32 +++++++++++++++++ 4 files changed, 51 insertions(+), 44 deletions(-) delete mode 100644 src/models/movements.ts diff --git a/src/components/board/state/board-state-reducer.ts b/src/components/board/state/board-state-reducer.ts index fc3d787..aaf833f 100644 --- a/src/components/board/state/board-state-reducer.ts +++ b/src/components/board/state/board-state-reducer.ts @@ -1,26 +1,25 @@ import boardModel from "models/board"; -import { getAvailableMovements } from "models/movements"; function boardStateReducer( state: BoardState, action: BoardStateAction ): BoardState { console.debug(`boardStateReducer(${action.type})`); - // const board = boardModel(state); + const board = boardModel(state); switch (action.type) { case "select-tile": { if (!action.tile) { throw Error('Inconsistent state: "select-tile" action requires "tile"'); } - // if (board.hasAnySelectedTile()) { - // state[board.getSelectedTile()].status = "idle"; - // } + if (board.hasAnySelectedTile()) { + state[board.getSelectedTile()].status = "idle"; + } const tile = state[action.tile]; tile.status = "selected"; if (tile.piece && tile.piece?.type === "soldier") { - getAvailableMovements(action.tile).forEach((availableTile) => { + board.getAvailableMovements(action.tile).forEach((availableTile) => { state[availableTile].status = "available"; }); } diff --git a/src/models/board.ts b/src/models/board.ts index 5aea7d5..5a47fda 100644 --- a/src/models/board.ts +++ b/src/models/board.ts @@ -1,3 +1,5 @@ +import { rangeTiles as tilesInRange } from "./tiles"; + function boardModel(b: BoardState) { return { hasAnySelectedTile() { @@ -11,15 +13,24 @@ function boardModel(b: BoardState) { }, getSelectedTile() { - const selectedTile = Object.entries(b).find( + const selectedTileID = Object.entries(b).find( ([, tile]) => tile.status === "selected" )?.[0]; - if (!selectedTile) { + if (!selectedTileID) { throw new Error( 'Inconsistent state: "getSelectedTileID" was unable to find any selected tile' ); } - return selectedTile as TileID; + return selectedTileID as TileID; + }, + + getAvailableMovements(tile: TileID) { + return tilesInRange(tile).filter( + (candidateTile) => + /* valid options: */ + !b[candidateTile].piece || + b[candidateTile].piece?.owner !== b[tile].piece?.owner + ); }, }; } diff --git a/src/models/movements.ts b/src/models/movements.ts deleted file mode 100644 index d972404..0000000 --- a/src/models/movements.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { asTileID, coordinates } from "models/tiles"; - -export function getAvailableMovements(tile: TileID, range = 1) { - console.debug(`getAvailableMovements(${tile})`); - const { x, y } = coordinates(tile); - - // TODO use range param instead of fixed array - // TODO avoid fixed arrays? - const range1Movements = - y % 2 === 0 - ? [ - [0, -1], - [+1, -1], - [-1, 0], - [+1, 0], - [0, +1], - [+1, +1], - ] - : [ - [-1, -1], - [0, -1], - [-1, 0], - [+1, 0], - [-1, +1], - [0, +1], - ]; - - return range1Movements.flatMap((variance) => { - const tileIdStr = asTileID({ - x: x + variance[0], - y: y + variance[1], - }); - return tileIdStr ? [tileIdStr] : []; - }); -} diff --git a/src/models/tiles.ts b/src/models/tiles.ts index ac78a83..1f929cf 100644 --- a/src/models/tiles.ts +++ b/src/models/tiles.ts @@ -74,3 +74,35 @@ export function asTileID({ x, y }: { x: number; y: number }): TileID | null { export function row(n: -3 | -2 | -1 | 0 | 1 | 2 | 3): TileID[] { return tiles.filter((id) => coordinates(id).y === n); } + +export function rangeTiles(tile: TileID, range = 1): TileID[] { + const { x, y } = coordinates(tile); + + // TODO use range param instead of fixed array? + const range1Movements = + y % 2 === 0 + ? [ + [0, -1], + [+1, -1], + [-1, 0], + [+1, 0], + [0, +1], + [+1, +1], + ] + : [ + [-1, -1], + [0, -1], + [-1, 0], + [+1, 0], + [-1, +1], + [0, +1], + ]; + + return range1Movements.flatMap((variance) => { + const tileIdStr = asTileID({ + x: x + variance[0], + y: y + variance[1], + }); + return tileIdStr ? [tileIdStr] : []; + }); +}