import React, { useState } from "react"; import type { GameState } from "../gameState"; import "./Pit.css"; type Flatten = T extends any[] ? T[number] : T; type Player = Flatten; type Pit = Flatten; type PitProps = { player: Player; index: number; pit: Pit; onClick?: (index: number, player: Player, pit: Pit) => (event: React.MouseEvent) => Promise displayStones: (pit: Pit, spread: "small" | "big") => JSX.Element[] } type SmallBowl = Pit & {kind: "small"}; type Kalaha = Pit & {kind: "big"}; export function Pit({player, index, pit, onClick, displayStones}: PitProps) { const smallBowl = pit as SmallBowl; smallBowl.kind = "small"; if (onClick) { return (
{pit.nrOfStones > 10 ? "" + pit.nrOfStones : ""}
{displayStones(pit, smallBowl.kind)}
); } else { return (
{pit.nrOfStones > 10 ? pit.nrOfStones : ""} {displayStones(pit, smallBowl.kind)}
); } } export function Kalaha({player, pit, displayStones}: PitProps) { const kalaha = pit as Kalaha; kalaha.kind = "big"; return (
{pit.nrOfStones > 10 ? pit.nrOfStones : ""}
{displayStones(pit, kalaha.kind)}
); }