-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchessgame.html
More file actions
144 lines (123 loc) · 3.45 KB
/
chessgame.html
File metadata and controls
144 lines (123 loc) · 3.45 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="fr">
<head>
<meta charset="utf-8">
<title>ChessGame</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style type="text/css">
html, body {
width: 100%;
height: 100%;
}
body {
margin: 0;
padding: 0;
}
#board {
overflow: hidden;
height: 100vh;
}
.coords {
font-size: 20px;
font-family: sans-serif;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-8">
<!-- The SVG Board container -->
<div id="board"></div>
</div>
<div class="col-md-4"></div>
</div>
</div>
<script type="text/javascript" src="bower_components/svg.js/dist/svg.js"></script>
<script type="text/javascript" src="bower_components/svg.draggable.js/dist/svg.draggable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"></script>
<script type="text/javascript" src="svg.chessboard.js"></script>
<script type="text/javascript">
var board;
window.onload = function() {
board = new SvgChessBoard('board', {
margin : 60,
squareStyle : 'wood',
pieceStyle : 'merida-outline-shadow'
});
var typesCorrespondance = {
'p' : 'Pawn',
'n' : 'Knight',
'b' : 'Bishop',
'r' : 'Rook',
'q' : 'Queen',
'k' : 'King'
};
var position = new Chess();
// Put pieces on board
for (var i=0, piece= null; i < board.coordinates.length ; i++) {
piece = position.get(board.coordinates[i]);
if (piece && typesCorrespondance[piece.type]) {
board.addPiece(piece.color + typesCorrespondance[piece.type], board.coordinates[i]);
}
}
board.on('beforedrop', function(e) {
// Check if we can drop a piece
var legalMoves = position.moves({ verbose: true });
var move = e.detail.move;
e.detail.candrop = false;
for (var i=0; i < legalMoves.length; i++ ) {
if (move.from == legalMoves[i].from && move.to == legalMoves[i].to) {
e.detail.candrop = true;
break;
}
}
});
board.on('afterdrop', function(e) {
var move = e.detail.move;
if (move.to.charAt(1) == 8 && move.piece == 'wPawn' ) {
move.promotion = 'q';
} else if (move.to.charAt(1) == 1 && move.piece == 'bPawn' ) {
move.promotion = 'q';
}
move = position.move(move);
if (move == null) {
return;
}
switch (move.flags) {
case 'k' : // Kingside castling
if (move.color == 'w') {
board.movePiece('h1', 'f1');
} else {
board.movePiece('h8', 'f8');
}
break;
case 'q' : // Queenside castling
if (move.color == 'w') {
board.movePiece('a1', 'd1');
} else {
board.movePiece('a8', 'd8');
}
break;
case 'e' : // En passant capture
var captured = move.san.substring(2);
var file = captured.substring(0,1);
var rank = Number(captured.substring(1));
if (move.color == 'w') {
board.removePiece(file+(rank-1));
} else {
board.removePiece(file+(rank+1));
}
break;
case 'p':
case 'np':
case 'pc': // Force promotion to Quuen
board.removePiece(square);
board.addPiece(move.color + 'Queen', move.to);
break;
}
});
};
</script>
</body>
</html>