├── index.html
├── script.js
└── style.css
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Sudoku Game
6 |
7 |
8 |
9 | Sudoku Game
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/script.js:
--------------------------------------------------------------------------------
1 | const puzzle = [
2 | [5, 3, 0, 0, 7, 0, 0, 0, 0],
3 | [6, 0, 0, 1, 9, 5, 0, 0, 0],
4 | [0, 9, 8, 0, 0, 0, 0, 6, 0],
5 | [8, 0, 0, 0, 6, 0, 0, 0, 3],
6 | [4, 0, 0, 8, 0, 3, 0, 0, 1],
7 | [7, 0, 0, 0, 2, 0, 0, 0, 6],
8 | [0, 6, 0, 0, 0, 0, 2, 8, 0],
9 | [0, 0, 0, 4, 1, 9, 0, 0, 5],
10 | [0, 0, 0, 0, 8, 0, 0, 7, 9]
11 | ];
12 |
13 | const board = document.getElementById('sudoku-board');
14 |
15 | function createBoard() {
16 | for (let row = 0; row < 9; row++) {
17 | for (let col = 0; col < 9; col++) {
18 | const input = document.createElement('input');
19 | input.type = 'text';
20 | input.maxLength = 1;
21 | input.dataset.row = row;
22 | input.dataset.col = col;
23 |
24 | if (puzzle[row][col] !== 0) {
25 | input.value = puzzle[row][col];
26 | input.disabled = true;
27 | }
28 |
29 | board.appendChild(input);
30 | }
31 | }
32 | }
33 |
34 | function checkSolution() {
35 | const inputs = document.querySelectorAll('#sudoku-board input');
36 | const grid = Array.from({ length: 9 }, () => Array(9).fill(0));
37 |
38 | inputs.forEach(input => {
39 | const row = input.dataset.row;
40 | const col = input.dataset.col;
41 | const val = parseInt(input.value);
42 | if (!isNaN(val)) grid[row][col] = val;
43 | });
44 |
45 | for (let i = 0; i < 9; i++) {
46 | const rowSet = new Set();
47 | const colSet = new Set();
48 | const boxSet = new Set();
49 |
50 | for (let j = 0; j < 9; j++) {
51 | // Row and column check
52 | const rowVal = grid[i][j];
53 | const colVal = grid[j][i];
54 | const boxVal = grid[3 * Math.floor(i / 3) + Math.floor(j / 3)][3 * (i % 3) + (j % 3)];
55 |
56 | if (rowVal === 0 || rowSet.has(rowVal)) return alert("Incorrect solution");
57 | if (colVal === 0 || colSet.has(colVal)) return alert("Incorrect solution");
58 | if (boxVal === 0 || boxSet.has(boxVal)) return alert("Incorrect solution");
59 |
60 | rowSet.add(rowVal);
61 | colSet.add(colVal);
62 | boxSet.add(boxVal);
63 | }
64 | }
65 |
66 | alert("Congratulations! You solved it!");
67 | }
68 |
69 | createBoard();
--------------------------------------------------------------------------------
/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | font-family: Arial, sans-serif;
3 | text-align: center;
4 | margin-top: 20px;
5 | }
6 |
7 | #sudoku-board {
8 | display: grid;
9 | grid-template-columns: repeat(9, 40px);
10 | grid-gap: 2px;
11 | margin: 20px auto;
12 | width: fit-content;
13 | }
14 |
15 | input {
16 | width: 38px;
17 | height: 38px;
18 | font-size: 18px;
19 | text-align: center;
20 | }
21 |
22 | input:disabled {
23 | background-color: #ddd;
24 | }
--------------------------------------------------------------------------------