feat: Select random word from list

This commit is contained in:
sigmasternchen 2024-09-20 21:36:32 +02:00
parent 4c3c44f95e
commit 25a76e90fe
2 changed files with 36 additions and 1 deletions

View file

@ -5,6 +5,10 @@ import {id, objectMap, zip} from "../utils";
import {Keyboard} from "./Keyboard";
import {CellState, sortCellStates} from "../model/CellState";
import {Toast} from "./Toast";
import dictionary from "../data/dictionary.json"
import {newSFC32} from "../random/sfc32";
const validWordsRegex = /[A-Z]+/;
export const App = () => {
const [pastGuesses, setPastGuesses] = useState([]);
@ -13,9 +17,16 @@ export const App = () => {
const [message, setMessage] = useState("");
const length = 5;
const correct = "guess";
const availableWords = dictionary
.filter(word => validWordsRegex.test(word))
.filter(word => word.length === length);
const today = Date.now() / 1000 / 60 / 60 / 24;
const random = newSFC32(today);
const correct = availableWords[Math.floor(random() * availableWords.length)];
console.log(correct)
const fieldDataForPastGuesses = pastGuesses
.map(guess => guess.toUpperCase())

24
src/random/sfc32.js Normal file
View file

@ -0,0 +1,24 @@
export const newSFC32 = seed => {
// see https://github.com/bryc/code/blob/master/jshash/PRNGs.md#sfc32
let a = 0 | 0;
let b = seed >> 0;
let c = seed >> 16; // crutch because JS doesn't have 64 bit ints
let d = 1 | 0;
const next = () => {
let t = (a + b | 0) + d | 0;
d = d + 1 | 0;
a = b ^ b >>> 9;
b = c + (c << 3) | 0;
c = c << 21 | c >>> 11;
c = c + t | 0;
return (t >>> 0) / 4294967296;
}
for (let i = 0; i < 12; i++) {
next();
}
return next;
}