Skip to content

Commit

Permalink
feat: available movements considering a piece of the same owner
Browse files Browse the repository at this point in the history
  • Loading branch information
manuartero committed Jan 9, 2023
1 parent 388c4b3 commit e684f65
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 44 deletions.
11 changes: 5 additions & 6 deletions src/components/board/state/board-state-reducer.ts
Original file line number Diff line number Diff line change
@@ -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";
});
}
Expand Down
17 changes: 14 additions & 3 deletions src/models/board.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { rangeTiles as tilesInRange } from "./tiles";

function boardModel(b: BoardState) {
return {
hasAnySelectedTile() {
Expand All @@ -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
);
},
};
}
Expand Down
35 changes: 0 additions & 35 deletions src/models/movements.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/models/tiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] : [];
});
}

0 comments on commit e684f65

Please sign in to comment.