import { useEffect, useRef, useState } from "react"; import { Row, RowState } from "./Row"; import dictionary from "./dictionary.json"; import { Clue, clue, describeClue, violation } from "./clue"; import { Keyboard } from "./Keyboard"; import targetList from "./targets.json"; import { dictionarySet, Difficulty, pick, resetRng, seed, speak, urlParam, } from "./util"; import { decode, encode } from "./base64"; enum GameState { Playing, Won, Lost, } interface GameProps { maxGuesses: number; hidden: boolean; difficulty: Difficulty; } const targets = targetList.slice(0, targetList.indexOf("murky") + 1); // Words no rarer than this one const minWordLength = 4; const maxWordLength = 11; function randomTarget(wordLength: number): string { const eligible = targets.filter((word) => word.length === wordLength); let candidate: string; do { candidate = pick(eligible); } while (/\*/.test(candidate)); return candidate; } function getChallengeUrl(target: string): string { return ( window.location.origin + window.location.pathname + "?challenge=" + encode(target) ); } let initChallenge = ""; let challengeError = false; try { initChallenge = decode(urlParam("challenge") ?? "").toLowerCase(); } catch (e) { console.warn(e); challengeError = true; } if (initChallenge && !dictionarySet.has(initChallenge)) { initChallenge = ""; challengeError = true; } function Game(props: GameProps) { const [gameState, setGameState] = useState(GameState.Playing); const [guesses, setGuesses] = useState([]); const [currentGuess, setCurrentGuess] = useState(""); const [hint, setHint] = useState( challengeError ? `Invalid challenge string, playing random game.` : `Make your first guess!` ); const [challenge, setChallenge] = useState(initChallenge); const [wordLength, setWordLength] = useState( challenge ? challenge.length : 5 ); const [target, setTarget] = useState(() => { resetRng(); return challenge || randomTarget(wordLength); }); const [gameNumber, setGameNumber] = useState(1); const tableRef = useRef(null); const startNextGame = () => { if (challenge) { // Clear the URL parameters: window.history.replaceState({}, document.title, window.location.pathname); } setChallenge(""); const newWordLength = wordLength < minWordLength || wordLength > maxWordLength ? 5 : wordLength; setWordLength(newWordLength); setTarget(randomTarget(newWordLength)); setGuesses([]); setCurrentGuess(""); setHint(""); setGameState(GameState.Playing); setGameNumber((x) => x + 1); }; async function share(url: string, copiedHint: string, text?: string) { const body = url + (text ? "\n\n" + text : ""); if ( /android|iphone|ipad|ipod|webos/i.test(navigator.userAgent) && !/firefox/i.test(navigator.userAgent) ) { try { await navigator.share({ text: body }); return; } catch (e) { console.warn("navigator.share failed:", e); } } try { await navigator.clipboard.writeText(body); setHint(copiedHint); return; } catch (e) { console.warn("navigator.clipboard.writeText failed:", e); } setHint(url); } const onKey = (key: string) => { if (gameState !== GameState.Playing) { if (key === "Enter") { startNextGame(); } return; } if (guesses.length === props.maxGuesses) return; if (/^[a-z]$/i.test(key)) { setCurrentGuess((guess) => (guess + key.toLowerCase()).slice(0, wordLength) ); tableRef.current?.focus(); setHint(""); } else if (key === "Backspace") { setCurrentGuess((guess) => guess.slice(0, -1)); setHint(""); } else if (key === "Enter") { if (currentGuess.length !== wordLength) { setHint("Too short"); return; } if (!dictionary.includes(currentGuess)) { setHint("Not a valid word"); return; } for (const g of guesses) { const c = clue(g, target); const feedback = violation(props.difficulty, c, currentGuess); if (feedback) { setHint(feedback); return; } } setGuesses((guesses) => guesses.concat([currentGuess])); setCurrentGuess((guess) => ""); const gameOver = (verbed: string) => `You ${verbed}! The answer was ${target.toUpperCase()}. (Enter to ${ challenge ? "play a random game" : "play again" })`; if (currentGuess === target) { setHint(gameOver("won")); setGameState(GameState.Won); } else if (guesses.length + 1 === props.maxGuesses) { setHint(gameOver("lost")); setGameState(GameState.Lost); } else { setHint(""); speak(describeClue(clue(currentGuess, target))); } } }; useEffect(() => { const onKeyDown = (e: KeyboardEvent) => { if (!e.ctrlKey && !e.metaKey) { onKey(e.key); } if (e.key === "Backspace") { e.preventDefault(); } }; document.addEventListener("keydown", onKeyDown); return () => { document.removeEventListener("keydown", onKeyDown); }; }, [currentGuess, gameState]); let letterInfo = new Map(); const tableRows = Array(props.maxGuesses) .fill(undefined) .map((_, i) => { const guess = [...guesses, currentGuess][i] ?? ""; const cluedLetters = clue(guess, target); const lockedIn = i < guesses.length; if (lockedIn) { for (const { clue, letter } of cluedLetters) { if (clue === undefined) break; const old = letterInfo.get(letter); if (old === undefined || clue > old) { letterInfo.set(letter, clue); } } } return ( ); }); return (
0 || currentGuess !== "" || challenge !== "") } value={wordLength} onChange={(e) => { const length = Number(e.target.value); resetRng(); setGameNumber(1); setGameState(GameState.Playing); setGuesses([]); setCurrentGuess(""); setTarget(randomTarget(length)); setWordLength(length); setHint(`${length} letters`); }} >
{tableRows}

{hint || `\u00a0`}

{gameState !== GameState.Playing && (

{" "}

)} {challenge ? (
playing a challenge game
) : seed ? (
seed {seed}, length {wordLength}, game {gameNumber}
) : undefined}
); } export default Game;