-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.html
More file actions
142 lines (128 loc) · 3.59 KB
/
sudoku.html
File metadata and controls
142 lines (128 loc) · 3.59 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Sudoku-Spiel</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f9f9f9;
text-align: center;
}
h1 {
color: #333;
}
table {
border-collapse: collapse;
margin: 20px auto;
}
td {
border: 1px solid #888;
width: 40px;
height: 40px;
}
input {
width: 38px;
height: 38px;
text-align: center;
font-size: 18px;
border: none;
}
.fixed {
background-color: #ddd;
font-weight: bold;
}
button {
padding: 10px 20px;
margin-top: 20px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Sudoku-Spiel</h1>
<p>Gib die fehlenden Zahlen ein (1–9) und überprüfe deine Lösung!</p>
<table id="sudoku-grid"></table>
<button onclick="checkSolution()">Lösungen prüfen</button>
<p id="result"></p>
<script>
// Beispiel-Puzzle (0 = leer)
const puzzle = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9]
];
function createGrid() {
const grid = document.getElementById("sudoku-grid");
for (let row = 0; row < 9; row++) {
const tr = document.createElement("tr");
for (let col = 0; col < 9; col++) {
const td = document.createElement("td");
const input = document.createElement("input");
input.type = "text";
input.maxLength = "1";
input.dataset.row = row;
input.dataset.col = col;
if (puzzle[row][col] !== 0) {
input.value = puzzle[row][col];
input.disabled = true;
input.classList.add("fixed");
}
td.appendChild(input);
tr.appendChild(td);
}
grid.appendChild(tr);
}
}
function checkSolution() {
const result = document.getElementById("result");
const grid = [...Array(9)].map(() => Array(9).fill(0));
// Raccoglie i valori dalla griglia
document.querySelectorAll("input").forEach(input => {
const row = parseInt(input.dataset.row);
const col = parseInt(input.dataset.col);
const value = parseInt(input.value);
grid[row][col] = isNaN(value) ? 0 : value;
});
// Funzioni di validazione
function isValidGroup(group) {
const nums = group.filter(n => n > 0);
return new Set(nums).size === nums.length;
}
// Controllo righe, colonne, e box
for (let i = 0; i < 9; i++) {
const row = grid[i];
const col = grid.map(r => r[i]);
if (!isValidGroup(row) || !isValidGroup(col)) {
result.textContent = "❌ Fehler gefunden! Überprüfe deine Eingaben.";
result.style.color = "red";
return;
}
}
// Controllo 3x3 box
for (let r = 0; r < 9; r += 3) {
for (let c = 0; c < 9; c += 3) {
const box = [];
for (let i = 0; i < 3; i++)
for (let j = 0; j < 3; j++)
box.push(grid[r + i][c + j]);
if (!isValidGroup(box)) {
result.textContent = "❌ Fehler im Block entdeckt!";
result.style.color = "red";
return;
}
}
}
result.textContent = "✅ Gut gemacht! Das Sudoku ist korrekt.";
result.style.color = "green";
}
createGrid();
</script>
</body>
</html>