From 83e63f0538fd800c78744c55acd4124fe1fdf4ec Mon Sep 17 00:00:00 2001 From: Inga Date: Tue, 19 Nov 2024 00:48:53 +0000 Subject: [PATCH] handle decided boards --- src/backend/components/boardgame.tsx | 54 ++++++++++++++++++--------- src/backend/main/boardgame-handler.ts | 3 +- src/shared/rules.spec.ts | 5 +++ 3 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 src/shared/rules.spec.ts diff --git a/src/backend/components/boardgame.tsx b/src/backend/components/boardgame.tsx index 5e29329..fbbd39d 100644 --- a/src/backend/components/boardgame.tsx +++ b/src/backend/components/boardgame.tsx @@ -1,48 +1,66 @@ import { sequence } from "../../shared/array-utils.ts"; -import { BoardgameStateType, SquareState, formatSquareState } from "../../shared/datatypes.ts"; +import { + BoardgameStateType, + CurrentOutcome, + GameRules, + SquareState, + formatSquareState, +} from "../../shared/datatypes.ts"; -const getCellHtml = (key: string, state: BoardgameStateType, row: number, column: number) => { - if (!state.board) { +const getCellHtml = ({ + key, + gameState, + currentOutcome, + row, + column, +}: { + key: string; + gameState: BoardgameStateType; + currentOutcome: CurrentOutcome; + row: number; + column: number; +}) => { + if (!gameState.board) { return ( - ); } - const squareState = state.board.get(row, column); - const nextGameState = squareState === SquareState.Unoccupied ? state.withMove(row, column) : state; + const squareState = gameState.board.get(row, column); + const nextGameState = + squareState === SquareState.Unoccupied && currentOutcome === CurrentOutcome.Undecided + ? gameState.withMove(row, column) + : gameState; return ( - ); }; -export const getBoardgameHtml = (key: string, state: BoardgameStateType) => { +export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rules: GameRules) => { + const currentOutcome = gameState.board ? rules.getBoardOutcome(gameState.board) : CurrentOutcome.Undecided; + return (
-

Current player: {state.currentPlayerName}

+

Current player: {gameState.currentPlayerName}

- {sequence(state.rows).map((row) => ( + {sequence(gameState.rows).map((row) => ( - {sequence(state.columns).map((column) => ( - + {sequence(gameState.columns).map((column) => ( + ))} ))}
{getCellHtml(key, state, row, column)}{getCellHtml({ key, gameState, currentOutcome, row, column })}
-
diff --git a/src/backend/main/boardgame-handler.ts b/src/backend/main/boardgame-handler.ts index 7438017..f04931c 100644 --- a/src/backend/main/boardgame-handler.ts +++ b/src/backend/main/boardgame-handler.ts @@ -2,6 +2,7 @@ import type { Request, Response } from "express"; import { rewriteQueryParamsWith, safeGetQueryValue } from "../utils.ts"; import { BoardgameState } from "../../shared/boardgame-state.ts"; import { getBoardgameHtml } from "../components/boardgame.tsx"; +import { gamesRules } from "../../shared/rules.spec.ts"; // Returns nothing if query parameter is uninitialized and a redirect was made, // or component if query parameter is initialized. @@ -15,5 +16,5 @@ export const handleBoardgame = (req: Request, res: Response, key: string) => { return; } - return getBoardgameHtml(key, state); + return getBoardgameHtml(key, state, gamesRules.tictactoe); }; diff --git a/src/shared/rules.spec.ts b/src/shared/rules.spec.ts new file mode 100644 index 0000000..ce7df04 --- /dev/null +++ b/src/shared/rules.spec.ts @@ -0,0 +1,5 @@ +import { rules as tictactoeRules } from "./tictactoe-rules.ts"; + +export const gamesRules = { + tictactoe: tictactoeRules, +};