-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.html
59 lines (44 loc) · 1.33 KB
/
index.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
<!doctype html>
<html>
<head>
<title>twitchchess</title>
<link rel="stylesheet" type="text/css" href="static/chessboard.min.css">
<script src="static/jquery.min.js"></script>
<script src="static/chessboard.min.js"></script>
</head>
<body style="font-size: 20px;">
<a href="/selfplay">Play vs itself</a>
<button onclick="newGame()">new game</button><br/>
<div id="board" style="width: 600px"></div>
<p></p>
<script type="text/javascript">
var board = ChessBoard('board', {
position: 'start',
draggable: true,
onDrop: onDrop
});
var files = {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7};
function get_square(sq) {
return 8*(parseInt(sq.charAt(1)) - 1) + files[sq.charAt(0)];
}
function onDrop(source, target, piece) {
if(source == target) return
var promotion = piece.toLowerCase().charAt(1) == 'p' && parseInt(target.charAt(1)) == 8;
$.get('/move_coordinates', {'from': get_square(source), 'to': get_square(target), 'promotion': promotion}, function(r) {
if (r.includes("game over")) {
document.querySelector('p').innerText = 'game over';
} else {
document.querySelector('p').innerText = '';
board.position(r);
}
});
}
function newGame() {
$.get('/newgame', function(r) {
document.querySelector('p').innerText = '';
board.position(r);
});
}
</script>
</body>
</html>