Add AI turn highlighting

This commit is contained in:
Joel Therrien 2023-09-14 19:38:34 -07:00
parent a014805cbf
commit c1d7a2f774
4 changed files with 41 additions and 8 deletions

View file

@ -9,6 +9,7 @@ import {
Tray, TurnAction, TurnAdvanceResult Tray, TurnAction, TurnAdvanceResult
} from "word_grid"; } from "word_grid";
import { import {
HighlightableLetterData,
LocationType, LocationType,
matchCoordinate, matchCoordinate,
mergeTrays, mergeTrays,
@ -37,8 +38,6 @@ export function Game(props: {
return props.wasm.get_board_cell_types(); return props.wasm.get_board_cell_types();
}, []); }, []);
const [confirmedScorePoints, setConfirmedScorePoints] = useState<number>(-1); const [confirmedScorePoints, setConfirmedScorePoints] = useState<number>(-1);
function movePlayableLetters(playerLetters: PlayableLetterData[], update: TileDispatchAction) { function movePlayableLetters(playerLetters: PlayableLetterData[], update: TileDispatchAction) {
@ -121,9 +120,33 @@ export function Game(props: {
const playerAndScores: PlayerAndScore[] = useMemo(() => { const playerAndScores: PlayerAndScore[] = useMemo(() => {
return props.wasm.get_scores(); return props.wasm.get_scores();
}, [turnCount]); }, [turnCount]);
const boardLetters: LetterData[] = useMemo(() => {
return props.wasm.get_board_letters(); const [boardLetters, setBoardLetters] = useState<HighlightableLetterData[]>(() => {
const newLetterData = [] as HighlightableLetterData[];
for(let i=0; i<15*15; i++) {
newLetterData.push(undefined);
}
return newLetterData;
});
useEffect(() => {
const newLetterData = props.wasm.get_board_letters() as LetterData[];
// we need to go through and set 'highlight' field to either true or false
// it will always be false if the player that just went was the AI
// TODO - build in support for multiple other players
for(let i=0; i<newLetterData.length; i++) {
const newLetter = newLetterData[i];
if(boardLetters[i] === undefined && newLetter !== undefined && playerTurnName == props.settings.playerName) {
newLetter.highlight = true;
} else if(newLetter !== undefined) {
newLetter.highlight = false;
}
}
setBoardLetters(newLetterData);
}, [turnCount]); }, [turnCount]);
const playerTurnName = useMemo(() => { const playerTurnName = useMemo(() => {
return props.wasm.get_current_player(); return props.wasm.get_current_player();
}, [turnCount]); }, [turnCount]);

View file

@ -6,7 +6,7 @@ import {
CellType, CellType,
LocationType, LocationType,
PlayableLetterData, PlayableLetterData,
CoordinateData, TileDispatchActionType, TileDispatch, CoordinateData, TileDispatchActionType, TileDispatch, HighlightableLetterData,
} from "./utils"; } from "./utils";
@ -41,7 +41,7 @@ export function TileSlot(props: { tile?: React.JSX.Element | undefined, location
} }
export function Letter(props: { data: LetterData, letterUpdater?: (value: string) => void }): React.JSX.Element { export function Letter(props: { data: HighlightableLetterData, letterUpdater?: (value: string) => void }): React.JSX.Element {
function modifyThisLetter(value:string){ function modifyThisLetter(value:string){
props.letterUpdater(value); props.letterUpdater(value);
@ -78,7 +78,11 @@ export function Letter(props: { data: LetterData, letterUpdater?: (value: string
if (props.data.is_blank) { // not ephemeral if (props.data.is_blank) { // not ephemeral
className += " prev-blank"; className += " prev-blank";
} }
return <div className="letter"> let letterClassName = "letter";
if (props.data.highlight) {
letterClassName += " highlight";
}
return <div className={letterClassName}>
<div className={className}>{props.data.text}</div> <div className={className}>{props.data.text}</div>
<div className="letter-points">{props.data.points}</div> <div className="letter-points">{props.data.points}</div>
</div> </div>
@ -130,7 +134,7 @@ export function TileTray(props: { letters: Array<PlayableLetterData>, trayLength
export function Grid(props: { export function Grid(props: {
cellTypes: CellType[], cellTypes: CellType[],
playerLetters: Array<PlayableLetterData>, playerLetters: Array<PlayableLetterData>,
boardLetters: LetterData[], boardLetters: HighlightableLetterData[],
dispatch: TileDispatch}) { dispatch: TileDispatch}) {
const elements = props.cellTypes.map((ct, i) => { const elements = props.cellTypes.map((ct, i) => {

View file

@ -122,6 +122,11 @@
font-style: italic; font-style: italic;
} }
}
.highlight {
color: red;
} }
.board-log { .board-log {

View file

@ -27,6 +27,7 @@ export interface CoordinateData {
} }
export type PlayableLetterData = LetterData & CoordinateData; export type PlayableLetterData = LetterData & CoordinateData;
export type HighlightableLetterData = LetterData & {highlight: boolean};
export enum TileDispatchActionType { export enum TileDispatchActionType {
MOVE, MOVE,