Repository navigation
Expand file tree
/
Copy pathuseGameState.ts
More file actions
109 lines (98 loc) · 3.06 KB
/
Copy pathuseGameState.ts
File metadata and controls
109 lines (98 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Game State Management Hook
*
* Tracks the guesses made and derives win/loss state from them. Filtering the
* dictionary down to the remaining words is NOT done here — that happens in the
* worker (the single source of truth), so the main thread never iterates the
* full dictionary on each guess.
*/
'use client';
import { useState, useCallback, useMemo } from 'react';
import type { GuessResult, ClueType } from '@/lib/types';
import {
isGameWon,
getRemainingAttempts,
validateGuess,
} from '@/lib/logic/game-state';
import { MAX_GUESSES } from '@/lib/constants';
/** Click cycle for a committed tile: present → correct → absent → present. */
const CLUE_CYCLE: ClueType[] = ['present', 'correct', 'absent'];
export interface UseGameStateResult {
guesses: GuessResult[];
addGuess: (guess: GuessResult) => void;
cycleClue: (guessIndex: number, tileIndex: number) => void;
reset: () => void;
isWon: boolean;
isLostByAttempts: boolean;
remainingAttempts: number;
validateWord: (
word: string,
isComplete: boolean
) => { valid: boolean; error?: string };
}
/**
* Hook for managing Wordle guesses and win/loss derivation.
*
* @param dictionary - Complete list of valid words (used only for validation)
*/
export function useGameState(dictionary: string[]): UseGameStateResult {
const [guesses, setGuesses] = useState<GuessResult[]>([]);
/**
* Add a new guess. No filtering happens here.
*/
const addGuess = useCallback((guess: GuessResult) => {
setGuesses((prev) => {
// Stop accepting guesses once the game is won or attempts are exhausted.
if (isGameWon(prev) || prev.length >= MAX_GUESSES) {
return prev;
}
return [...prev, guess];
});
}, []);
/**
* Cycle the clue color of a single tile in an already-committed guess. This
* lets the user correct a mis-entered feedback color; the new clues flow back
* into the solver, which recomputes suggestions.
*/
const cycleClue = useCallback((guessIndex: number, tileIndex: number) => {
setGuesses((prev) =>
prev.map((guess, gi) => {
if (gi !== guessIndex) return guess;
const clues = guess.clues.map((clue, ti) => {
if (ti !== tileIndex) return clue;
const next = CLUE_CYCLE[(CLUE_CYCLE.indexOf(clue.clue) + 1) % 3];
return { ...clue, clue: next };
});
return { ...guess, clues };
})
);
}, []);
const reset = useCallback(() => {
setGuesses([]);
}, []);
const validateWord = useCallback(
(word: string, isComplete: boolean) => {
return validateGuess(word, guesses, dictionary, isComplete);
},
[guesses, dictionary]
);
const isWon = useMemo(() => isGameWon(guesses), [guesses]);
const isLostByAttempts = useMemo(
() => !isWon && guesses.length >= MAX_GUESSES,
[isWon, guesses.length]
);
const remainingAttempts = useMemo(
() => getRemainingAttempts(guesses),
[guesses]
);
return {
guesses,
addGuess,
cycleClue,
reset,
isWon,
isLostByAttempts,
remainingAttempts,
validateWord,
};
}