|
|
|
@ -1,15 +1,6 @@ |
|
|
|
|
import { sequence } from "../../shared/array-utils.ts"; |
|
|
|
|
import { BoardgameStateType, CurrentOutcome, GameRules, Player } from "../../shared/datatypes.ts"; |
|
|
|
|
import { ClassNames, getCellDisplayData, getDisplayStates } from "../../shared/display.ts"; |
|
|
|
|
|
|
|
|
|
const getClassAndDisplayAttributes = ( |
|
|
|
|
className: ClassNames, |
|
|
|
|
gameState: BoardgameStateType, |
|
|
|
|
currentOutcome: CurrentOutcome, |
|
|
|
|
) => ({ |
|
|
|
|
class: className, |
|
|
|
|
style: getDisplayStates(gameState, currentOutcome)[className] ? {} : { display: "none" }, |
|
|
|
|
}); |
|
|
|
|
import { getCellDisplayData, getDisplayStates } from "../../shared/display.ts"; |
|
|
|
|
|
|
|
|
|
const getSubmitAttributes = (key: string, targetState: BoardgameStateType) => ({ |
|
|
|
|
type: "submit" as const, |
|
|
|
@ -41,9 +32,13 @@ const getCellHtml = ({ |
|
|
|
|
|
|
|
|
|
export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rules: GameRules) => { |
|
|
|
|
const currentOutcome = gameState.board ? rules.getBoardOutcome(gameState.board) : CurrentOutcome.Undecided; |
|
|
|
|
const gameClasses = getDisplayStates(gameState, currentOutcome); |
|
|
|
|
const gameActiveClassNames = Object.entries(gameClasses) |
|
|
|
|
.filter(([, value]) => value) |
|
|
|
|
.map(([name]) => name); |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<board-game track={key}> |
|
|
|
|
<board-game track={key} class={gameActiveClassNames.join(" ")}> |
|
|
|
|
<form method="post" is="progressive-form"> |
|
|
|
|
<p> |
|
|
|
|
Current player: <span class="current-player-name">{gameState.currentPlayerName}</span> |
|
|
|
@ -62,33 +57,33 @@ export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rul |
|
|
|
|
</table> |
|
|
|
|
|
|
|
|
|
<p> |
|
|
|
|
<span {...getClassAndDisplayAttributes("outcome-winx", gameState, currentOutcome)}>Player X won</span> |
|
|
|
|
<span {...getClassAndDisplayAttributes("outcome-wino", gameState, currentOutcome)}>Player O won</span> |
|
|
|
|
<span {...getClassAndDisplayAttributes("outcome-draw", gameState, currentOutcome)}>Draw</span> |
|
|
|
|
<span class="when-outcome-winx">Player X won</span> |
|
|
|
|
<span class="when-outcome-wino">Player O won</span> |
|
|
|
|
<span class="when-outcome-draw">Draw</span> |
|
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
<p {...getClassAndDisplayAttributes("autoplayer-x-disabled", gameState, currentOutcome)}> |
|
|
|
|
<p class="when-autoplayer-x-disabled"> |
|
|
|
|
Currently X moves are made manually.{" "} |
|
|
|
|
<button class="autoplayer-x-enable" {...getSubmitAttributes(key, gameState.withAutoPlayer(Player.X))}> |
|
|
|
|
Make computer play for X. |
|
|
|
|
</button> |
|
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
<p {...getClassAndDisplayAttributes("autoplayer-x-enabled", gameState, currentOutcome)}> |
|
|
|
|
<p class="when-autoplayer-x-enabled"> |
|
|
|
|
Currently X moves are made by computer.{" "} |
|
|
|
|
<button class="autoplayer-x-disable" {...getSubmitAttributes(key, gameState.withoutAutoPlayer(Player.X))}> |
|
|
|
|
Make them manually. |
|
|
|
|
</button> |
|
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
<p {...getClassAndDisplayAttributes("autoplayer-o-disabled", gameState, currentOutcome)}> |
|
|
|
|
<p class="when-autoplayer-o-disabled"> |
|
|
|
|
Currently O moves are made manually.{" "} |
|
|
|
|
<button class="autoplayer-o-enable" {...getSubmitAttributes(key, gameState.withAutoPlayer(Player.O))}> |
|
|
|
|
Make computer play for O. |
|
|
|
|
</button> |
|
|
|
|
</p> |
|
|
|
|
|
|
|
|
|
<p {...getClassAndDisplayAttributes("autoplayer-o-enabled", gameState, currentOutcome)}> |
|
|
|
|
<p class="when-autoplayer-o-enabled"> |
|
|
|
|
Currently O moves are made by computer.{" "} |
|
|
|
|
<button class="autoplayer-o-disable" {...getSubmitAttributes(key, gameState.withoutAutoPlayer(Player.O))}> |
|
|
|
|
Make them manually. |
|
|
|
@ -97,8 +92,8 @@ export const getBoardgameHtml = (key: string, gameState: BoardgameStateType, rul |
|
|
|
|
|
|
|
|
|
<p> |
|
|
|
|
<button type="submit" class="game-start" name={key} value={gameState.withEmptyBoard().serialize()}> |
|
|
|
|
<span {...getClassAndDisplayAttributes("start-new-game", gameState, currentOutcome)}>Start game</span> |
|
|
|
|
<span {...getClassAndDisplayAttributes("start-clear-game", gameState, currentOutcome)}>Restart game</span> |
|
|
|
|
<span class="when-game-not-in-progress">Start game</span> |
|
|
|
|
<span class="when-game-in-progress">Restart game</span> |
|
|
|
|
</button> |
|
|
|
|
</p> |
|
|
|
|
</form> |
|
|
|
|