Automate AI plays

This commit is contained in:
Joel Therrien 2023-09-14 19:14:19 -07:00
parent ef4506b55e
commit a014805cbf
4 changed files with 56 additions and 36 deletions

View file

@ -151,4 +151,8 @@ impl GameWasm {
}
pub fn get_current_player(&self) -> String {
self.0.current_player_name()
}
}

View file

@ -37,6 +37,8 @@ export function Game(props: {
return props.wasm.get_board_cell_types();
}, []);
const [confirmedScorePoints, setConfirmedScorePoints] = useState<number>(-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<number>(1);
const playerAndScores: PlayerAndScore[] = useMemo(() => {
return props.wasm.get_scores();
}, [turnCount]);
const boardLetters: LetterData[] = useMemo(() => {
return props.wasm.get_board_letters();
}, [turnCount]);
useEffect(() => {
logDispatch(<h4>Turn {turnCount}</h4>);
setConfirmedScorePoints(-1);
trayDispatch({action: TileDispatchActionType.RETRIEVE});
}, [turnCount]);
const logDivRef = useRef(null);
const [isTileExchangeOpen, setIsTileExchangeOpen] = useState<boolean>(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<boolean>) {
const result: MyResult<TurnAction | string> = props.wasm.exchange_tiles(selectedArray);
@ -135,6 +107,50 @@ export function Game(props: {
logDispatch(<div><em>{word} was added to dictionary.</em></div>);
}
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<number>(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(<h4>Turn {turnCount}</h4>);
logDispatch(<div>{playerTurnName}'s turn</div>)
setConfirmedScorePoints(-1);
trayDispatch({action: TileDispatchActionType.RETRIEVE});
if(playerTurnName != props.settings.playerName) {
runAI();
}
}, [turnCount]);
const logDivRef = useRef(null);
const [isTileExchangeOpen, setIsTileExchangeOpen] = useState<boolean>(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 <>
<TileExchangeModal
@ -222,11 +238,6 @@ export function Game(props: {
logDispatch(<div><em>Player skipped their turn</em></div>);
setTurnCount(turnCount + 1);
}}>Skip Turn</button>
<button onClick={() => {
const result: TurnAdvanceResult = props.wasm.advance_turn();
console.info({result});
setTurnCount(turnCount + 1);
}}>Run AI</button>
</>;
}

View file

@ -48,7 +48,11 @@ async function run() {
const root = createRoot(document.getElementById("root"));
root.render(<Menu dictionary_text={dictionary_text} settings={{trayLength: 7}}/>);
root.render(<Menu dictionary_text={dictionary_text} settings={{
trayLength: 7,
playerName: 'Player',
}}/>);
}

View file

@ -13,6 +13,7 @@ export enum CellType {
export interface Settings {
trayLength: number;
playerName: string;
}
export enum LocationType {