-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchess.html
More file actions
101 lines (84 loc) · 2.58 KB
/
chess.html
File metadata and controls
101 lines (84 loc) · 2.58 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
<!DOCTYPE html>
<html>
<head>
<style>
html, body, .grid {
height: 100%;
width: 100%;
margin: 0;
display: flex;
}
.grid {
background-color: lightgray;
min-width: 350px;
min-height: 350px;
max-width: 50%;
max-height: 50%;
margin: auto;
}
.piece {
display: block;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="grid"></div>
<script>
var grid = document.querySelector(".grid");
for (var y = 0; y < 8; y++) {
for (var x = 0; x < 8; x++) {
var cell = document.createElement("div");
cell.classList.add("cell");
cell.classList.add("x" + x + "y" + y);
if ((x + y) % 2 > 0) {
cell.classList.add("white");
}
var piece = getPieceForLocation(x, y);
if (piece) {
var pieceElement = document.createElement("div");
pieceElement.classList.add('piece');
pieceElement.classList.add(piece.type);
pieceElement.classList.add(piece.color);
cell.appendChild(pieceElement);
}
grid.appendChild(cell);
}
}
function getPieceForLocation(x, y) {
var piece = {};
if (y == 0 || y == 7) {
piece.color = (y == 0) ? 'white' : 'black';
switch (x) {
case 0:
case 7:
piece.type = 'rook';
break;
case 1:
case 6:
piece.type = 'knight';
break;
case 2:
case 5:
piece.type = 'bishop';
break;
case 3:
piece.type = (piece.color == 'white') ? 'queen' : 'king';
break;
case 4:
piece.type = (piece.color == 'white') ? 'king' : 'queen';
break;
}
}
else if (y == 1 || y == 6) {
piece.color = (y == 1) ? 'white' : 'black';
piece.type = 'pawn';
} else {
piece = null;
}
return piece;
}
</script>
</body>
</html>