import React, { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { AlertDialog, AlertDialogAction, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog'; const NonogramApp = () => { const [size, setSize] = useState(5); const [grid, setGrid] = useState([]); const [solution, setSolution] = useState([]); const [rowClues, setRowClues] = useState([]); const [colClues, setColClues] = useState([]); const [correctRows, setCorrectRows] = useState([]); const [correctColumns, setCorrectColumns] = useState([]); const [isComplete, setIsComplete] = useState(false); useEffect(() => { startNewGame(); }, [size]); const generateSolution = () => { return Array(size).fill().map(() => Array(size).fill().map(() => Math.random() > 0.5 ? 1 : 0) ); }; const generateClues = (solution) => { const rowClues = solution.map(row => { const clues = []; let count = 0; for (let i = 0; i <= row.length; i++) { if (i < row.length && row[i] === 1) { count++; } else if (count > 0) { clues.push(count); count = 0; } } return clues.length ? clues : [0]; }); const colClues = Array(size).fill().map((_, colIndex) => { const clues = []; let count = 0; for (let i = 0; i <= size; i++) { if (i < size && solution[i][colIndex] === 1) { count++; } else if (count > 0) { clues.push(count); count = 0; } } return clues.length ? clues : [0]; }); return { rowClues, colClues }; }; const startNewGame = () => { const newSolution = generateSolution(); const { rowClues, colClues } = generateClues(newSolution); setSolution(newSolution); setRowClues(rowClues); setColClues(colClues); setGrid(Array(size).fill().map(() => Array(size).fill(0))); setCorrectRows([]); setCorrectColumns([]); setIsComplete(false); }; const handleCellClick = (row, col, isRightClick) => { const newGrid = [...grid]; if (isRightClick) { newGrid[row][col] = newGrid[row][col] === 2 ? 0 : 2; // 2 represents a cross } else { newGrid[row][col] = newGrid[row][col] === 1 ? 0 : 1; } setGrid(newGrid); checkRowCompletion(row, newGrid); checkColumnCompletion(col, newGrid); checkPuzzleCompletion(newGrid); }; const checkRowCompletion = (row, currentGrid) => { const isCorrect = currentGrid[row].every((value, index) => (value === 1) === (solution[row][index] === 1) ); setCorrectRows(prev => isCorrect ? [...prev, row] : prev.filter(r => r !== row) ); }; const checkColumnCompletion = (col, currentGrid) => { const isCorrect = currentGrid.every((row, index) => (row[col] === 1) === (solution[index][col] === 1) ); setCorrectColumns(prev => isCorrect ? [...prev, col] : prev.filter(c => c !== col) ); }; const checkPuzzleCompletion = (currentGrid) => { const isComplete = currentGrid.every((row, rowIndex) => row.every((cell, colIndex) => (cell === 1) === (solution[rowIndex][colIndex] === 1) ) ); setIsComplete(isComplete); }; return (

Nonogram Puzzel

{colClues.map((clue, i) => (
{clue.map((num, j) => (
{num}
))}
))}
{rowClues.map((clue, i) => (
{clue.join(' ')}
))}
{grid.map((row, rowIndex) => (
{row.map((cell, colIndex) => (
handleCellClick(rowIndex, colIndex, false)} onContextMenu={(e) => { e.preventDefault(); handleCellClick(rowIndex, colIndex, true); }} > {cell === 2 && X}
))}
))}
Gefeliciteerd! Je hebt de puzzel opgelost! Nieuw spel
); }; export default NonogramApp;