mirror of
https://github.com/arthur-pbty/imprimersudoku.git
synced 2026-06-26 22:23:13 +02:00
first commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
"use client";
|
||||
|
||||
import { Grid } from "../lib/sudoku";
|
||||
|
||||
interface SudokuGridProps {
|
||||
grid: Grid;
|
||||
index: number;
|
||||
isSolution?: boolean;
|
||||
}
|
||||
|
||||
export default function SudokuGrid({ grid, index, isSolution = false }: SudokuGridProps) {
|
||||
return (
|
||||
<figure className="sudoku-container" aria-label={`Grille de sudoku numéro ${index + 1}${isSolution ? " (solution)" : ""}`}>
|
||||
<figcaption className="sudoku-number">N°{index + 1}</figcaption>
|
||||
<table className="sudoku-table" role="grid" aria-label={`Sudoku ${index + 1}`}>
|
||||
<tbody>
|
||||
{grid.map((row, r) => (
|
||||
<tr key={r}>
|
||||
{row.map((cell, c) => {
|
||||
const borderClasses = [
|
||||
"sudoku-cell",
|
||||
c % 3 === 0 ? "border-l-thick" : "",
|
||||
c === 8 ? "border-r-thick" : "",
|
||||
r % 3 === 0 ? "border-t-thick" : "",
|
||||
r === 8 ? "border-b-thick" : "",
|
||||
].filter(Boolean).join(" ");
|
||||
|
||||
return (
|
||||
<td key={c} className={borderClasses}>
|
||||
{cell !== 0 ? (
|
||||
<span className={isSolution && cell ? "solution-digit" : "given-digit"}>
|
||||
{cell}
|
||||
</span>
|
||||
) : null}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</figure>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
+419
@@ -0,0 +1,419 @@
|
||||
@import "tailwindcss";
|
||||
|
||||
:root {
|
||||
--background: #ffffff;
|
||||
--foreground: #171717;
|
||||
}
|
||||
|
||||
@theme inline {
|
||||
--color-background: var(--background);
|
||||
--color-foreground: var(--foreground);
|
||||
--font-sans: var(--font-geist-sans);
|
||||
--font-mono: var(--font-geist-mono);
|
||||
}
|
||||
|
||||
body {
|
||||
background: var(--background);
|
||||
color: var(--foreground);
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
/* ========== APP LAYOUT ========== */
|
||||
|
||||
.app-container {
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
/* ========== CONTROLS (screen only) ========== */
|
||||
|
||||
.controls {
|
||||
text-align: center;
|
||||
padding: 1.5rem 0.75rem 1.2rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.app-title {
|
||||
font-size: 1.4rem;
|
||||
font-weight: 700;
|
||||
margin: 0 0 0.4rem;
|
||||
}
|
||||
|
||||
.app-subtitle {
|
||||
font-size: 0.85rem;
|
||||
opacity: 0.9;
|
||||
margin: 0 0 1rem;
|
||||
max-width: 500px;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
padding: 0 0.5rem;
|
||||
}
|
||||
|
||||
.button-group {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.4rem;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
padding: 0 0.5rem;
|
||||
}
|
||||
|
||||
.btn-difficulty {
|
||||
padding: 0.5rem 1rem;
|
||||
border: 2px solid rgba(255, 255, 255, 0.6);
|
||||
border-radius: 8px;
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
color: white;
|
||||
font-size: 0.85rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.btn-difficulty:hover:not(:disabled) {
|
||||
background: rgba(255, 255, 255, 0.3);
|
||||
border-color: white;
|
||||
}
|
||||
|
||||
.btn-difficulty.btn-active {
|
||||
background: white;
|
||||
color: #764ba2;
|
||||
border-color: white;
|
||||
}
|
||||
|
||||
.btn-difficulty:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-print {
|
||||
padding: 0.5rem 1.2rem;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
background: #fbbf24;
|
||||
color: #1a1a1a;
|
||||
font-size: 0.85rem;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
width: 100%;
|
||||
margin-top: 0.3rem;
|
||||
}
|
||||
|
||||
.btn-print:hover:not(:disabled) {
|
||||
background: #f59e0b;
|
||||
transform: scale(1.02);
|
||||
}
|
||||
|
||||
.btn-print:disabled {
|
||||
opacity: 0.4;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.generating-text {
|
||||
margin-top: 0.8rem;
|
||||
font-size: 0.9rem;
|
||||
animation: pulse 1.2s infinite;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.4; }
|
||||
}
|
||||
|
||||
/* ========== RESPONSIVE: TABLET (>=600px) ========== */
|
||||
|
||||
@media screen and (min-width: 600px) {
|
||||
.controls {
|
||||
padding: 2rem 1.5rem 1.5rem;
|
||||
}
|
||||
|
||||
.app-title {
|
||||
font-size: 1.8rem;
|
||||
}
|
||||
|
||||
.app-subtitle {
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.btn-difficulty {
|
||||
padding: 0.6rem 1.4rem;
|
||||
font-size: 0.95rem;
|
||||
flex: 0 1 auto;
|
||||
}
|
||||
|
||||
.btn-print {
|
||||
width: auto;
|
||||
padding: 0.6rem 1.6rem;
|
||||
font-size: 0.95rem;
|
||||
margin-top: 0;
|
||||
margin-left: 0.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
/* ========== RESPONSIVE: DESKTOP (>=900px) ========== */
|
||||
|
||||
@media screen and (min-width: 900px) {
|
||||
.controls {
|
||||
padding: 2.5rem 2rem 1.5rem;
|
||||
}
|
||||
|
||||
.app-title {
|
||||
font-size: 2rem;
|
||||
}
|
||||
}
|
||||
|
||||
/* ========== SEO CONTENT (hidden visually, accessible to crawlers) ========== */
|
||||
|
||||
.seo-content {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.seo-content h2 {
|
||||
font-size: 1rem;
|
||||
font-weight: 700;
|
||||
color: #374151;
|
||||
margin: 0 0 0.5rem;
|
||||
}
|
||||
|
||||
.seo-content p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* ========== PRINT AREA (visible on screen + print) ========== */
|
||||
|
||||
.print-area {
|
||||
padding: 0.5rem;
|
||||
}
|
||||
|
||||
.print-page {
|
||||
max-width: 210mm;
|
||||
margin: 0 auto 1.5rem;
|
||||
padding: 0.75rem;
|
||||
background: white;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 8px;
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
text-align: center;
|
||||
font-size: 1.1rem;
|
||||
font-weight: 700;
|
||||
margin: 0.2rem 0 0.6rem;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
|
||||
.sudoku-grid-container {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr;
|
||||
gap: 0.8rem;
|
||||
justify-items: center;
|
||||
}
|
||||
|
||||
@media screen and (min-width: 480px) {
|
||||
.sudoku-grid-container {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
gap: 0.6rem 0.8rem;
|
||||
}
|
||||
}
|
||||
|
||||
@media screen and (min-width: 600px) {
|
||||
.print-area {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.print-page {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
font-size: 1.3rem;
|
||||
margin: 0.3rem 0 0.8rem;
|
||||
}
|
||||
|
||||
.sudoku-grid-container {
|
||||
gap: 0.8rem 1.2rem;
|
||||
}
|
||||
}
|
||||
|
||||
@media screen and (min-width: 900px) {
|
||||
.print-page {
|
||||
margin: 0 auto 2rem;
|
||||
}
|
||||
}
|
||||
|
||||
/* ========== SUDOKU GRID ========== */
|
||||
|
||||
.sudoku-container {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.sudoku-number {
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
color: #6b7280;
|
||||
margin: 0 0 0.15rem;
|
||||
}
|
||||
|
||||
.sudoku-table {
|
||||
border-collapse: collapse;
|
||||
width: 100%;
|
||||
max-width: 270px;
|
||||
}
|
||||
|
||||
.sudoku-cell {
|
||||
width: clamp(22px, 4.5vw, 32px);
|
||||
height: clamp(22px, 4.5vw, 32px);
|
||||
text-align: center;
|
||||
vertical-align: middle;
|
||||
font-size: clamp(0.65rem, 1.8vw, 0.95rem);
|
||||
font-weight: 500;
|
||||
border: 1px solid #bbb;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
@media screen and (min-width: 480px) {
|
||||
.sudoku-cell {
|
||||
width: clamp(24px, 3.2vw, 30px);
|
||||
height: clamp(24px, 3.2vw, 30px);
|
||||
font-size: clamp(0.7rem, 1.5vw, 0.9rem);
|
||||
}
|
||||
|
||||
.sudoku-table {
|
||||
max-width: 280px;
|
||||
}
|
||||
}
|
||||
|
||||
@media screen and (min-width: 900px) {
|
||||
.sudoku-cell {
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.sudoku-table {
|
||||
max-width: 300px;
|
||||
}
|
||||
}
|
||||
|
||||
.border-l-thick { border-left: 2.5px solid #222 !important; }
|
||||
.border-r-thick { border-right: 2.5px solid #222 !important; }
|
||||
.border-t-thick { border-top: 2.5px solid #222 !important; }
|
||||
.border-b-thick { border-bottom: 2.5px solid #222 !important; }
|
||||
|
||||
.given-digit {
|
||||
color: #1a1a1a;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.solution-digit {
|
||||
color: #1a1a1a;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
/* ========== PRINT STYLES ========== */
|
||||
|
||||
.no-print {
|
||||
display: block;
|
||||
}
|
||||
|
||||
@media print {
|
||||
@page {
|
||||
size: A4 portrait;
|
||||
margin: 8mm 10mm 8mm 10mm;
|
||||
}
|
||||
|
||||
body {
|
||||
background: white !important;
|
||||
color: black !important;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
-webkit-print-color-adjust: exact;
|
||||
print-color-adjust: exact;
|
||||
}
|
||||
|
||||
.no-print {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
.print-area {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.print-page {
|
||||
border: none !important;
|
||||
border-radius: 0 !important;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
max-width: none;
|
||||
page-break-after: always;
|
||||
break-after: page;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.print-page:last-child {
|
||||
page-break-after: avoid;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
font-size: 12pt;
|
||||
margin: 0 0 2mm;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.sudoku-grid-container {
|
||||
flex: 1;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
gap: 2mm 6mm;
|
||||
justify-items: center;
|
||||
align-content: start;
|
||||
}
|
||||
|
||||
.sudoku-container {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.sudoku-number {
|
||||
font-size: 7pt;
|
||||
color: #333;
|
||||
margin: 0 0 0.5mm;
|
||||
}
|
||||
|
||||
.sudoku-cell {
|
||||
width: 8mm;
|
||||
height: 8mm;
|
||||
font-size: 10pt;
|
||||
border: 1pt solid #666;
|
||||
}
|
||||
|
||||
.border-l-thick { border-left: 2.5pt solid #000 !important; }
|
||||
.border-r-thick { border-right: 2.5pt solid #000 !important; }
|
||||
.border-t-thick { border-top: 2.5pt solid #000 !important; }
|
||||
.border-b-thick { border-bottom: 2.5pt solid #000 !important; }
|
||||
|
||||
.given-digit {
|
||||
color: black;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.solution-digit {
|
||||
color: black;
|
||||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
import type { Metadata, Viewport } from "next";
|
||||
import { Geist, Geist_Mono } from "next/font/google";
|
||||
import "./globals.css";
|
||||
|
||||
const geistSans = Geist({
|
||||
variable: "--font-geist-sans",
|
||||
subsets: ["latin"],
|
||||
display: "swap",
|
||||
});
|
||||
|
||||
const geistMono = Geist_Mono({
|
||||
variable: "--font-geist-mono",
|
||||
subsets: ["latin"],
|
||||
display: "swap",
|
||||
});
|
||||
|
||||
export const viewport: Viewport = {
|
||||
width: "device-width",
|
||||
initialScale: 1,
|
||||
themeColor: "#667eea",
|
||||
};
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Générateur de Sudoku gratuit — Imprimer des grilles avec solutions",
|
||||
description:
|
||||
"Générez et imprimez gratuitement des grilles de Sudoku avec leurs solutions sur une feuille A4. 4 niveaux de difficulté : facile, moyen, difficile et expert. Aucune inscription requise.",
|
||||
keywords: [
|
||||
"sudoku",
|
||||
"grille sudoku",
|
||||
"imprimer sudoku",
|
||||
"sudoku gratuit",
|
||||
"sudoku à imprimer",
|
||||
"sudoku avec solution",
|
||||
"générateur sudoku",
|
||||
"sudoku facile",
|
||||
"sudoku moyen",
|
||||
"sudoku difficile",
|
||||
"sudoku expert",
|
||||
"sudoku PDF",
|
||||
"jeu de logique",
|
||||
],
|
||||
authors: [{ name: "Sudoku Générateur" }],
|
||||
creator: "Sudoku Générateur",
|
||||
publisher: "Sudoku Générateur",
|
||||
robots: {
|
||||
index: true,
|
||||
follow: true,
|
||||
googleBot: {
|
||||
index: true,
|
||||
follow: true,
|
||||
"max-snippet": -1,
|
||||
"max-image-preview": "large",
|
||||
},
|
||||
},
|
||||
openGraph: {
|
||||
type: "website",
|
||||
locale: "fr_FR",
|
||||
title: "Générateur de Sudoku gratuit — Imprimer des grilles avec solutions",
|
||||
description:
|
||||
"Générez et imprimez gratuitement des grilles de Sudoku avec solutions. 4 niveaux : facile, moyen, difficile, expert.",
|
||||
siteName: "Générateur de Sudoku",
|
||||
},
|
||||
twitter: {
|
||||
card: "summary_large_image",
|
||||
title: "Générateur de Sudoku gratuit — Imprimer des grilles",
|
||||
description:
|
||||
"Générez et imprimez gratuitement des grilles de Sudoku avec solutions. 4 niveaux de difficulté.",
|
||||
},
|
||||
alternates: {
|
||||
canonical: "/",
|
||||
},
|
||||
};
|
||||
|
||||
export default function RootLayout({
|
||||
children,
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
const jsonLd = {
|
||||
"@context": "https://schema.org",
|
||||
"@type": "WebApplication",
|
||||
name: "Générateur de Sudoku",
|
||||
description:
|
||||
"Générez et imprimez gratuitement des grilles de Sudoku avec solutions sur feuille A4.",
|
||||
applicationCategory: "GameApplication",
|
||||
operatingSystem: "Tous",
|
||||
offers: {
|
||||
"@type": "Offer",
|
||||
price: "0",
|
||||
priceCurrency: "EUR",
|
||||
},
|
||||
inLanguage: "fr",
|
||||
browserRequirements: "Requires JavaScript. Requires HTML5.",
|
||||
featureList: [
|
||||
"Génération de sudoku valides et solvables",
|
||||
"4 niveaux de difficulté",
|
||||
"Impression optimisée A4",
|
||||
"Solutions incluses",
|
||||
],
|
||||
};
|
||||
|
||||
return (
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<script
|
||||
type="application/ld+json"
|
||||
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
|
||||
/>
|
||||
</head>
|
||||
<body
|
||||
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||
>
|
||||
{children}
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
// Sudoku generator - generates valid, solvable puzzles
|
||||
|
||||
export type Grid = number[][]; // 0 = empty cell, 1-9 = filled
|
||||
|
||||
export type Difficulty = "facile" | "moyen" | "difficile" | "expert";
|
||||
|
||||
const DIFFICULTY_LABELS: Record<Difficulty, string> = {
|
||||
facile: "Facile",
|
||||
moyen: "Moyen",
|
||||
difficile: "Difficile",
|
||||
expert: "Expert",
|
||||
};
|
||||
|
||||
export function getDifficultyLabel(d: Difficulty): string {
|
||||
return DIFFICULTY_LABELS[d];
|
||||
}
|
||||
|
||||
// Number of cells to remove per difficulty
|
||||
const CELLS_TO_REMOVE: Record<Difficulty, number> = {
|
||||
facile: 36,
|
||||
moyen: 46,
|
||||
difficile: 52,
|
||||
expert: 58,
|
||||
};
|
||||
|
||||
function shuffleArray<T>(arr: T[]): T[] {
|
||||
const a = [...arr];
|
||||
for (let i = a.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[a[i], a[j]] = [a[j], a[i]];
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
function createEmptyGrid(): Grid {
|
||||
return Array.from({ length: 9 }, () => Array(9).fill(0));
|
||||
}
|
||||
|
||||
function cloneGrid(grid: Grid): Grid {
|
||||
return grid.map((row) => [...row]);
|
||||
}
|
||||
|
||||
function isValid(grid: Grid, row: number, col: number, num: number): boolean {
|
||||
// Check row
|
||||
for (let c = 0; c < 9; c++) {
|
||||
if (grid[row][c] === num) return false;
|
||||
}
|
||||
// Check column
|
||||
for (let r = 0; r < 9; r++) {
|
||||
if (grid[r][col] === num) return false;
|
||||
}
|
||||
// Check 3x3 box
|
||||
const boxRow = Math.floor(row / 3) * 3;
|
||||
const boxCol = Math.floor(col / 3) * 3;
|
||||
for (let r = boxRow; r < boxRow + 3; r++) {
|
||||
for (let c = boxCol; c < boxCol + 3; c++) {
|
||||
if (grid[r][c] === num) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fill the grid completely using backtracking with randomization
|
||||
function fillGrid(grid: Grid): boolean {
|
||||
for (let row = 0; row < 9; row++) {
|
||||
for (let col = 0; col < 9; col++) {
|
||||
if (grid[row][col] === 0) {
|
||||
const nums = shuffleArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
for (const num of nums) {
|
||||
if (isValid(grid, row, col, num)) {
|
||||
grid[row][col] = num;
|
||||
if (fillGrid(grid)) return true;
|
||||
grid[row][col] = 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Count solutions (stop at 2 to check uniqueness)
|
||||
function countSolutions(grid: Grid, limit: number = 2): number {
|
||||
let count = 0;
|
||||
|
||||
function solve(g: Grid): boolean {
|
||||
for (let row = 0; row < 9; row++) {
|
||||
for (let col = 0; col < 9; col++) {
|
||||
if (g[row][col] === 0) {
|
||||
for (let num = 1; num <= 9; num++) {
|
||||
if (isValid(g, row, col, num)) {
|
||||
g[row][col] = num;
|
||||
if (solve(g)) {
|
||||
if (count >= limit) return true;
|
||||
}
|
||||
g[row][col] = 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
count++;
|
||||
return count >= limit;
|
||||
}
|
||||
|
||||
solve(cloneGrid(grid));
|
||||
return count;
|
||||
}
|
||||
|
||||
// Generate a puzzle by removing cells from a complete grid
|
||||
function generatePuzzle(
|
||||
solution: Grid,
|
||||
difficulty: Difficulty
|
||||
): Grid {
|
||||
const puzzle = cloneGrid(solution);
|
||||
const toRemove = CELLS_TO_REMOVE[difficulty];
|
||||
|
||||
// Create list of all cell positions and shuffle
|
||||
const positions: [number, number][] = [];
|
||||
for (let r = 0; r < 9; r++) {
|
||||
for (let c = 0; c < 9; c++) {
|
||||
positions.push([r, c]);
|
||||
}
|
||||
}
|
||||
const shuffled = shuffleArray(positions);
|
||||
|
||||
let removed = 0;
|
||||
for (const [r, c] of shuffled) {
|
||||
if (removed >= toRemove) break;
|
||||
|
||||
const backup = puzzle[r][c];
|
||||
puzzle[r][c] = 0;
|
||||
|
||||
// For easier difficulties, always verify uniqueness
|
||||
// For expert, we relax after enough removals to speed up generation
|
||||
if (difficulty === "expert" && removed > 45) {
|
||||
removed++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (countSolutions(puzzle) !== 1) {
|
||||
puzzle[r][c] = backup; // restore - would create multiple solutions
|
||||
} else {
|
||||
removed++;
|
||||
}
|
||||
}
|
||||
|
||||
return puzzle;
|
||||
}
|
||||
|
||||
export interface SudokuPuzzle {
|
||||
puzzle: Grid;
|
||||
solution: Grid;
|
||||
}
|
||||
|
||||
// Generate a single sudoku puzzle
|
||||
export function generateSudoku(difficulty: Difficulty): SudokuPuzzle {
|
||||
const solution = createEmptyGrid();
|
||||
fillGrid(solution);
|
||||
const puzzle = generatePuzzle(solution, difficulty);
|
||||
return { puzzle, solution };
|
||||
}
|
||||
|
||||
// Generate multiple sudoku puzzles
|
||||
export function generateSudokus(
|
||||
difficulty: Difficulty,
|
||||
count: number = 6
|
||||
): SudokuPuzzle[] {
|
||||
const puzzles: SudokuPuzzle[] = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
puzzles.push(generateSudoku(difficulty));
|
||||
}
|
||||
return puzzles;
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useCallback, useEffect } from "react";
|
||||
import { Difficulty, getDifficultyLabel, generateSudokus, SudokuPuzzle } from "./lib/sudoku";
|
||||
import SudokuGrid from "./components/SudokuGrid";
|
||||
|
||||
const DIFFICULTIES: Difficulty[] = ["facile", "moyen", "difficile", "expert"];
|
||||
const SUDOKU_COUNT = 6;
|
||||
|
||||
export default function Home() {
|
||||
const [difficulty, setDifficulty] = useState<Difficulty>("moyen");
|
||||
const [puzzles, setPuzzles] = useState<SudokuPuzzle[] | null>(null);
|
||||
const [generating, setGenerating] = useState(false);
|
||||
|
||||
// Générer automatiquement des sudoku "Moyen" au chargement
|
||||
useEffect(() => {
|
||||
setGenerating(true);
|
||||
setTimeout(() => {
|
||||
const generated = generateSudokus("moyen", SUDOKU_COUNT);
|
||||
setPuzzles(generated);
|
||||
setGenerating(false);
|
||||
}, 50);
|
||||
}, []);
|
||||
|
||||
const handleGenerate = useCallback((diff: Difficulty) => {
|
||||
setDifficulty(diff);
|
||||
setGenerating(true);
|
||||
// Use setTimeout to let UI update before heavy computation
|
||||
setTimeout(() => {
|
||||
const generated = generateSudokus(diff, SUDOKU_COUNT);
|
||||
setPuzzles(generated);
|
||||
setGenerating(false);
|
||||
}, 50);
|
||||
}, []);
|
||||
|
||||
const handlePrint = useCallback(() => {
|
||||
if (!puzzles) {
|
||||
setGenerating(true);
|
||||
setTimeout(() => {
|
||||
const generated = generateSudokus(difficulty, SUDOKU_COUNT);
|
||||
setPuzzles(generated);
|
||||
setGenerating(false);
|
||||
setTimeout(() => window.print(), 100);
|
||||
}, 50);
|
||||
} else {
|
||||
window.print();
|
||||
}
|
||||
}, [puzzles, difficulty]);
|
||||
|
||||
const label = getDifficultyLabel(difficulty);
|
||||
|
||||
return (
|
||||
<div className="app-container">
|
||||
{/* Controls - hidden when printing */}
|
||||
<header className="controls no-print" role="banner">
|
||||
<h1 className="app-title">🧩 Générateur de Sudoku</h1>
|
||||
<p className="app-subtitle">
|
||||
Choisissez une difficulté pour générer {SUDOKU_COUNT} sudoku sur une feuille A4 avec leurs solutions.
|
||||
</p>
|
||||
|
||||
<nav className="button-group" role="navigation" aria-label="Choix de difficulté">
|
||||
{DIFFICULTIES.map((diff) => (
|
||||
<button
|
||||
key={diff}
|
||||
onClick={() => handleGenerate(diff)}
|
||||
disabled={generating}
|
||||
aria-pressed={difficulty === diff && !!puzzles}
|
||||
className={`btn-difficulty ${difficulty === diff && puzzles ? "btn-active" : ""}`}
|
||||
>
|
||||
{getDifficultyLabel(diff)}
|
||||
</button>
|
||||
))}
|
||||
|
||||
<button
|
||||
onClick={handlePrint}
|
||||
disabled={generating || !puzzles}
|
||||
className="btn-print"
|
||||
aria-label="Imprimer les grilles de sudoku"
|
||||
>
|
||||
🖨️ Imprimer
|
||||
</button>
|
||||
</nav>
|
||||
|
||||
{generating && (
|
||||
<p className="generating-text" role="status" aria-live="polite">Génération en cours…</p>
|
||||
)}
|
||||
</header>
|
||||
|
||||
{/* SEO content - visible to crawlers, visually subtle */}
|
||||
<section className="seo-content no-print" aria-label="À propos">
|
||||
<h2>Générateur de Sudoku gratuit à imprimer</h2>
|
||||
<p>
|
||||
Créez et imprimez des grilles de Sudoku gratuitement. Notre générateur produit des puzzles
|
||||
valides et solvables avec 4 niveaux de difficulté : facile, moyen, difficile et expert.
|
||||
Chaque feuille A4 contient 6 grilles 9×9 accompagnées de leurs solutions sur une page séparée.
|
||||
Idéal pour s'entraîner, jouer en famille ou en classe.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
{/* Printable content */}
|
||||
{puzzles && (
|
||||
<main className="print-area" role="main">
|
||||
{/* Page 1: Puzzles */}
|
||||
<section className="print-page" aria-label={`Grilles de sudoku niveau ${label}`}>
|
||||
<h2 className="page-title">Sudoku — Niveau {label}</h2>
|
||||
<div className="sudoku-grid-container">
|
||||
{puzzles.map((p, i) => (
|
||||
<SudokuGrid key={`puzzle-${i}`} grid={p.puzzle} index={i} />
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Page 2: Solutions */}
|
||||
<section className="print-page" aria-label={`Solutions niveau ${label}`}>
|
||||
<h2 className="page-title">Solutions — Niveau {label}</h2>
|
||||
<div className="sudoku-grid-container">
|
||||
{puzzles.map((p, i) => (
|
||||
<SudokuGrid key={`solution-${i}`} grid={p.solution} index={i} isSolution />
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import type { MetadataRoute } from "next";
|
||||
|
||||
export default function robots(): MetadataRoute.Robots {
|
||||
return {
|
||||
rules: [
|
||||
{
|
||||
userAgent: "*",
|
||||
allow: "/",
|
||||
},
|
||||
],
|
||||
sitemap: "https://imprimersudoku.arthurp.fr/sitemap.xml",
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { MetadataRoute } from "next";
|
||||
|
||||
export default function sitemap(): MetadataRoute.Sitemap {
|
||||
return [
|
||||
{
|
||||
url: "https://imprimersudoku.arthurp.fr",
|
||||
lastModified: new Date(),
|
||||
changeFrequency: "monthly",
|
||||
priority: 1,
|
||||
},
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user