Add ?seed= support
This commit is contained in:
12
src/Game.tsx
12
src/Game.tsx
@@ -4,7 +4,7 @@ import dictionary from "./dictionary.json";
|
|||||||
import { Clue, clue } from "./clue";
|
import { Clue, clue } from "./clue";
|
||||||
import { Keyboard } from "./Keyboard";
|
import { Keyboard } from "./Keyboard";
|
||||||
import common from "./common.json";
|
import common from "./common.json";
|
||||||
import { dictionarySet, pick } from "./util";
|
import { dictionarySet, pick, resetRng } from "./util";
|
||||||
import { names } from "./names";
|
import { names } from "./names";
|
||||||
|
|
||||||
enum GameState {
|
enum GameState {
|
||||||
@@ -33,7 +33,7 @@ function Game(props: GameProps) {
|
|||||||
const [currentGuess, setCurrentGuess] = useState<string>("");
|
const [currentGuess, setCurrentGuess] = useState<string>("");
|
||||||
const [wordLength, setWordLength] = useState(5);
|
const [wordLength, setWordLength] = useState(5);
|
||||||
const [hint, setHint] = useState<string>(`Make your first guess!`);
|
const [hint, setHint] = useState<string>(`Make your first guess!`);
|
||||||
const [target, setTarget] = useState(randomTarget(wordLength));
|
const [target, setTarget] = useState(() => randomTarget(wordLength));
|
||||||
|
|
||||||
const reset = () => {
|
const reset = () => {
|
||||||
setTarget(randomTarget(wordLength));
|
setTarget(randomTarget(wordLength));
|
||||||
@@ -129,13 +129,19 @@ function Game(props: GameProps) {
|
|||||||
min="4"
|
min="4"
|
||||||
max="11"
|
max="11"
|
||||||
id="wordLength"
|
id="wordLength"
|
||||||
disabled={guesses.length > 0 || currentGuess !== ""}
|
disabled={
|
||||||
|
gameState === GameState.Playing &&
|
||||||
|
(guesses.length > 0 || currentGuess !== "")
|
||||||
|
}
|
||||||
value={wordLength}
|
value={wordLength}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const length = Number(e.target.value);
|
const length = Number(e.target.value);
|
||||||
|
resetRng();
|
||||||
|
setGuesses([]);
|
||||||
setTarget(randomTarget(length));
|
setTarget(randomTarget(length));
|
||||||
setWordLength(length);
|
setWordLength(length);
|
||||||
setHint(`${length} letters`);
|
setHint(`${length} letters`);
|
||||||
|
(document.activeElement as HTMLElement)?.blur();
|
||||||
}}
|
}}
|
||||||
></input>
|
></input>
|
||||||
<button
|
<button
|
||||||
|
|||||||
@@ -13277,7 +13277,6 @@
|
|||||||
"deleted",
|
"deleted",
|
||||||
"ignition",
|
"ignition",
|
||||||
"rehearsal",
|
"rehearsal",
|
||||||
"ness",
|
|
||||||
"pediatric",
|
"pediatric",
|
||||||
"helmet",
|
"helmet",
|
||||||
"anthology",
|
"anthology",
|
||||||
|
|||||||
21
src/util.ts
21
src/util.ts
@@ -2,6 +2,23 @@ import dictionary from "./dictionary.json";
|
|||||||
|
|
||||||
export const dictionarySet: Set<string> = new Set(dictionary);
|
export const dictionarySet: Set<string> = new Set(dictionary);
|
||||||
|
|
||||||
export function pick<T>(array: Array<T>): T {
|
function mulberry32(a: number) {
|
||||||
return array[Math.floor(array.length * Math.random())];
|
return function () {
|
||||||
|
var t = (a += 0x6d2b79f5);
|
||||||
|
t = Math.imul(t ^ (t >>> 15), t | 1);
|
||||||
|
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
||||||
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const seed = Number(new URLSearchParams(window.location.search).get("seed"));
|
||||||
|
const makeRandom = () => (seed ? mulberry32(seed) : () => Math.random());
|
||||||
|
let random = makeRandom();
|
||||||
|
|
||||||
|
export function resetRng(): void {
|
||||||
|
random = makeRandom();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function pick<T>(array: Array<T>): T {
|
||||||
|
return array[Math.floor(array.length * random())];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user