import { useRef, useState } from 'react'; import { SudokuGenerator } from './SudokuGenerator'; import { SudokuRenderer } from './SudokuRenderer'; import type { Difficulty } from './SudokuGenerator'; import type { HintResult } from './types'; export function useBoard() { const containerRef = useRef(null); const generatorRef = useRef(null); const rendererRef = useRef(null); const boardRef = useRef(''); const solutionRef = useRef(''); const initialBoardRef = useRef(''); const [boardRendered, setBoardRendered] = useState(0); function generateBoard(difficulty: Difficulty = 'easy') { if (!generatorRef.current) { generatorRef.current = new SudokuGenerator(); } generatorRef.current.generate(difficulty); const puzzle = generatorRef.current.getPuzzle(); const solution = generatorRef.current.getSolution(); boardRef.current = puzzle; solutionRef.current = solution; initialBoardRef.current = puzzle; } function renderBoard() { const container = containerRef.current; if (!container) return; if (!rendererRef.current) { rendererRef.current = new SudokuRenderer(container); } const board = boardRef.current; const initialBoard = initialBoardRef.current; if (board) { rendererRef.current.render(board, initialBoard); } else { rendererRef.current.renderEmpty(); } setBoardRendered(prev => prev + 1); } function setCell(row: number, col: number, value: string): void { const board = boardRef.current.split(''); const index = row * 9 + col; board[index] = value; boardRef.current = board.join(''); renderBoard(); } function getHint(): HintResult { const board = boardRef.current; const solution = solutionRef.current; const initialBoard = initialBoardRef.current; if (!board || !solution) return null; const incorrectCells: Array<{ row: number; col: number }> = []; const emptyCells: Array<{ row: number; col: number }> = []; for (let i = 0; i < 81; i++) { const row = Math.floor(i / 9); const col = i % 9; const currentValue = board[i]; const correctValue = solution[i]; const initialValue = initialBoard[i]; if (currentValue !== '.' && initialValue === '.' && currentValue !== correctValue) { incorrectCells.push({ row, col }); } if (currentValue === '.') { emptyCells.push({ row, col }); } } if (incorrectCells.length > 0) { const randomCell = incorrectCells[Math.floor(Math.random() * incorrectCells.length)]; return { ...randomCell, type: 'error' }; } if (emptyCells.length > 0) { const randomCell = emptyCells[Math.floor(Math.random() * emptyCells.length)]; const index = randomCell.row * 9 + randomCell.col; const correctValue = solution[index]; return { ...randomCell, type: 'correct', value: correctValue }; } return null; } return { containerRef, boardRef, boardRendered, generateBoard, renderBoard, setCell, getHint, }; }