|
|
|
@ -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 ( |
|
|
|
|
<button type="submit" name={key} value={state.serialize()} disabled> |
|
|
|
|
<button type="submit" name={key} value={gameState.serialize()} disabled> |
|
|
|
|
{" "} |
|
|
|
|
</button> |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 ( |
|
|
|
|
<button |
|
|
|
|
type="submit" |
|
|
|
|
name={key} |
|
|
|
|
value={nextGameState.serialize()} |
|
|
|
|
disabled={squareState !== SquareState.Unoccupied} |
|
|
|
|
> |
|
|
|
|
<button type="submit" name={key} value={nextGameState.serialize()} disabled={nextGameState === gameState}> |
|
|
|
|
{formatSquareState(squareState)} |
|
|
|
|
</button> |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
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 ( |
|
|
|
|
<board-game> |
|
|
|
|
<form method="post"> |
|
|
|
|
<p>Current player: {state.currentPlayerName}</p> |
|
|
|
|
<p>Current player: {gameState.currentPlayerName}</p> |
|
|
|
|
|
|
|
|
|
<table> |
|
|
|
|
<tbody> |
|
|
|
|
{sequence(state.rows).map((row) => ( |
|
|
|
|
{sequence(gameState.rows).map((row) => ( |
|
|
|
|
<tr> |
|
|
|
|
{sequence(state.columns).map((column) => ( |
|
|
|
|
<td>{getCellHtml(key, state, row, column)}</td> |
|
|
|
|
{sequence(gameState.columns).map((column) => ( |
|
|
|
|
<td>{getCellHtml({ key, gameState, currentOutcome, row, column })}</td> |
|
|
|
|
))} |
|
|
|
|
</tr> |
|
|
|
|
))} |
|
|
|
|
</tbody> |
|
|
|
|
</table> |
|
|
|
|
|
|
|
|
|
<button type="submit" name={key} value={state.withEmptyBoard().serialize()}> |
|
|
|
|
<button type="submit" name={key} value={gameState.withEmptyBoard().serialize()}> |
|
|
|
|
Start game |
|
|
|
|
</button> |
|
|
|
|
</form> |
|
|
|
|