-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.html
83 lines (69 loc) · 2.31 KB
/
chess.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chessboard</title>
<style>
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
flex-direction: column;
}
#chessboard {
border-collapse: collapse;
border: 2px solid #333;
margin-top: 20px;
}
#chessboard td {
width: 50px;
height: 50px;
border: 1px solid #ccc;
}
#chessboard .black {
background-color: #333;
color: #fff;
}
.input-container {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="input-container">
<label for="dimension">Enter Chessboard Dimension: </label>
<input type="number" id="dimension" value="8" min="1" max="20" />
</div>
<button onclick="resizeChessboard()">Resize</button>
<table id="chessboard"></table>
<script>
function createChessboard(dimension) {
const chessboard = document.getElementById('chessboard');
chessboard.innerHTML = '';
for (let i = 0; i < dimension; i++) {
const row = chessboard.insertRow(i);
for (let j = 0; j < dimension; j++) {
const cell = row.insertCell(j);
const isBlack = (i + j) % 2 !== 0;
cell.className = isBlack ? 'black' : '';
cell.textContent = isBlack ? 'B' : 'W';
}
}
}
function resizeChessboard() {
const dimensionInput = document.getElementById('dimension');
const dimension = parseInt(dimensionInput.value, 10);
if (!isNaN(dimension) && dimension >= 1 && dimension <= 20) {
createChessboard(dimension);
} else {
alert('Please enter a valid dimension between 1 and 20.');
}
}
// Initial creation of chessboard with default dimension
createChessboard(8);
</script>
</body>
</html>