From a014805cbfed15c3e37b54d2f96c7819f9af35c1 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Thu, 14 Sep 2023 19:14:19 -0700 Subject: [PATCH] Automate AI plays --- src/wasm.rs | 4 +++ ui/src/Game.tsx | 81 +++++++++++++++++++++++++++--------------------- ui/src/index.tsx | 6 +++- ui/src/utils.ts | 1 + 4 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/wasm.rs b/src/wasm.rs index 186b456..234e238 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -151,4 +151,8 @@ impl GameWasm { } + pub fn get_current_player(&self) -> String { + self.0.current_player_name() + } + } \ No newline at end of file diff --git a/ui/src/Game.tsx b/ui/src/Game.tsx index 04c8d08..e1482a2 100644 --- a/ui/src/Game.tsx +++ b/ui/src/Game.tsx @@ -37,6 +37,8 @@ export function Game(props: { return props.wasm.get_board_cell_types(); }, []); + + const [confirmedScorePoints, setConfirmedScorePoints] = useState(-1); function movePlayableLetters(playerLetters: PlayableLetterData[], update: TileDispatchAction) { @@ -85,36 +87,6 @@ export function Game(props: { } - - const [playerLetters, trayDispatch] = useReducer(movePlayableLetters, []); - const [logInfo, logDispatch] = useReducer(addLogInfo, []); - - const [turnCount, setTurnCount] = useState(1); - const playerAndScores: PlayerAndScore[] = useMemo(() => { - return props.wasm.get_scores(); - }, [turnCount]); - const boardLetters: LetterData[] = useMemo(() => { - return props.wasm.get_board_letters(); - }, [turnCount]); - - useEffect(() => { - logDispatch(

Turn {turnCount}

); - setConfirmedScorePoints(-1); - trayDispatch({action: TileDispatchActionType.RETRIEVE}); - - }, [turnCount]); - - const logDivRef = useRef(null); - - const [isTileExchangeOpen, setIsTileExchangeOpen] = useState(false); - - useEffect(() => { - // Effect is to keep the log window scrolled down - if (logDivRef.current != null) { - logDivRef.current.scrollTo(0, logDivRef.current.scrollHeight); // scroll down - } - }, [logInfo]) - function exchangeFunction(selectedArray: Array) { const result: MyResult = props.wasm.exchange_tiles(selectedArray); @@ -135,6 +107,50 @@ export function Game(props: { logDispatch(
{word} was added to dictionary.
); } + function runAI() { + const result: TurnAdvanceResult = props.wasm.advance_turn(); + console.info({result}); + setTurnCount(turnCount + 1); + } + + + const [playerLetters, trayDispatch] = useReducer(movePlayableLetters, []); + const [logInfo, logDispatch] = useReducer(addLogInfo, []); + + const [turnCount, setTurnCount] = useState(1); + const playerAndScores: PlayerAndScore[] = useMemo(() => { + return props.wasm.get_scores(); + }, [turnCount]); + const boardLetters: LetterData[] = useMemo(() => { + return props.wasm.get_board_letters(); + }, [turnCount]); + const playerTurnName = useMemo(() => { + return props.wasm.get_current_player(); + }, [turnCount]); + + + useEffect(() => { + logDispatch(

Turn {turnCount}

); + logDispatch(
{playerTurnName}'s turn
) + setConfirmedScorePoints(-1); + trayDispatch({action: TileDispatchActionType.RETRIEVE}); + if(playerTurnName != props.settings.playerName) { + runAI(); + } + + }, [turnCount]); + + const logDivRef = useRef(null); + + const [isTileExchangeOpen, setIsTileExchangeOpen] = useState(false); + + useEffect(() => { + // Effect is to keep the log window scrolled down + if (logDivRef.current != null) { + logDivRef.current.scrollTo(0, logDivRef.current.scrollHeight); // scroll down + } + }, [logInfo]) + return <> Player skipped their turn); setTurnCount(turnCount + 1); }}>Skip Turn - ; } diff --git a/ui/src/index.tsx b/ui/src/index.tsx index f8c737e..68b13f3 100644 --- a/ui/src/index.tsx +++ b/ui/src/index.tsx @@ -48,7 +48,11 @@ async function run() { const root = createRoot(document.getElementById("root")); - root.render(); + root.render(); } diff --git a/ui/src/utils.ts b/ui/src/utils.ts index 5618673..b6803aa 100644 --- a/ui/src/utils.ts +++ b/ui/src/utils.ts @@ -13,6 +13,7 @@ export enum CellType { export interface Settings { trayLength: number; + playerName: string; } export enum LocationType {