From 25a76e90fe534351184caf519e36745f50df9081 Mon Sep 17 00:00:00 2001 From: sigmasternchen Date: Fri, 20 Sep 2024 21:36:32 +0200 Subject: [PATCH] feat: Select random word from list --- src/components/App.jsx | 13 ++++++++++++- src/random/sfc32.js | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/random/sfc32.js diff --git a/src/components/App.jsx b/src/components/App.jsx index a66fade..0f6d353 100644 --- a/src/components/App.jsx +++ b/src/components/App.jsx @@ -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()) diff --git a/src/random/sfc32.js b/src/random/sfc32.js new file mode 100644 index 0000000..8392caf --- /dev/null +++ b/src/random/sfc32.js @@ -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; +} \ No newline at end of file