radomirregular region

This commit is contained in:
2026-05-15 21:53:08 +08:00
parent 0e87d5546d
commit e54b9a0b7a
3 changed files with 330 additions and 37 deletions

View File

@@ -4,14 +4,25 @@ import {
GameBoard,
Difficulty,
GameMode,
STANDARD_REGION_MAP,
generatePuzzle,
RegionMap,
generatePuzzleForMode,
getRegionId,
getRegionNeighbors,
getRegionMapByMode,
} from './sudokuEngine';
type HighlightType = 'none' | 'row' | 'col' | 'box' | 'sameNumber';
type SudokuRound = {
board: GameBoard;
regionMap: RegionMap;
};
const createSudokuRound = (difficulty: Difficulty, mode: GameMode): SudokuRound => {
const { puzzle, regionMap } = generatePuzzleForMode(difficulty, mode);
return {
board: puzzle,
regionMap,
};
};
const SudokuGame: React.FC = () => {
// 游戏难度
@@ -20,10 +31,15 @@ const SudokuGame: React.FC = () => {
// 游戏模式:标准或不规则
const [gameMode, setGameMode] = useState<GameMode>('standard');
// 使用 useMemo 确保 regionMap 随 gameMode 变化
const regionMap = useMemo(() => getRegionMapByMode(gameMode), [gameMode]);
const [board, setBoard] = useState<GameBoard>(() => generatePuzzle('hard', STANDARD_REGION_MAP));
const [round, setRound] = useState<SudokuRound>(() => createSudokuRound('hard', 'standard'));
const { board, regionMap } = round;
const setBoard = (nextBoard: GameBoard) => {
setRound((currentRound) => ({
...currentRound,
board: nextBoard,
}));
};
const [fixedCells, setFixedCells] = useState<boolean[][]>(() =>
board.map(row => row.map(cell => cell !== 0))
@@ -181,10 +197,9 @@ const SudokuGame: React.FC = () => {
};
const createNewGame = (level: Difficulty, mode: GameMode = gameMode) => {
const nextRegionMap = getRegionMapByMode(mode);
const newPuzzle = generatePuzzle(level, nextRegionMap);
setBoard(newPuzzle);
setFixedCells(newPuzzle.map(row => row.map(cell => cell !== 0)));
const nextRound = createSudokuRound(level, mode);
setRound(nextRound);
setFixedCells(nextRound.board.map(row => row.map(cell => cell !== 0)));
setSelectedCell(null);
setIsGameComplete(false);
setShowCompleteDialog(false);
@@ -306,7 +321,7 @@ const SudokuGame: React.FC = () => {
}
}
return highlighted;
}, [board, selectedCell, config]);
}, [board, selectedCell, config, regionMap]);
// 计算剩余未填写的数字数量
const remainingCount = useMemo(() => {