forked from patorjk/JavaScript-Snake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.js
More file actions
105 lines (96 loc) · 3 KB
/
sudoku.js
File metadata and controls
105 lines (96 loc) · 3 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
<!DOCTYPE html>
<html>
<head>
<title>Sudoku Game</title>
<style>
table {
border-collapse: collapse;
}
td {
width: 30px;
height: 30px;
text-align: center;
border: 1px solid #000;
font-size: 20px;
}
input[type="number"] {
width: 30px;
height: 30px;
text-align: center;
font-size: 20px;
border: none;
}
.readonly {
background-color: #ccc;
}
</style>
</head>
<body>
<h1>Sudoku Game</h1>
<table>
<tbody id="sudoku-board">
</tbody>
</table>
<br>
<button id="new-game">New Game</button>
<script>
const boardSize = 9;
const boxSize = 3;
let board = [];
function createSudokuBoard() {
const boardContainer = document.getElementById("sudoku-board");
boardContainer.innerHTML = "";
for (let i = 0; i < boardSize; i++) {
const row = document.createElement("tr");
board.push([]);
for (let j = 0; j < boardSize; j++) {
const cell = document.createElement("td");
const input = document.createElement("input");
input.type = "number";
input.min = 1;
input.max = 9;
cell.appendChild(input);
row.appendChild(cell);
board[i].push(input);
}
boardContainer.appendChild(row);
}
}
function generateSudoku() {
// Code to generate a Sudoku puzzle goes here.
// You can use Sudoku generation algorithms to create a puzzle.
// For simplicity, you can pre-define a puzzle here.
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]
];
for (let i = 0; i < boardSize; i++) {
for (let j = 0; j < boardSize; j++) {
if (puzzle[i][j] !== 0) {
board[i][j].value = puzzle[i][j];
board[i][j].readOnly = true;
} else {
board[i][j].value = "";
board[i][j].readOnly = false;
}
}
}
}
document.getElementById("new-game").addEventListener("click", () => {
createSudokuBoard();
generateSudoku();
});
createSudokuBoard();
generateSudoku();
//hello world
console.log("Hello World")
</script>
</body>
</html>