-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
86 lines (68 loc) · 2.37 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
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
<!doctype html>
<html>
<head>
<title>Chess</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/>
<button onclick="play(1)">play as white</button><br/>
<button onclick="play(0)">play as black</button><br/>
<div id="board" style="width: 600px; visibility: hidden;"></div>
<p></p>
<script type="text/javascript">
var iaColor;
var board;
function onDrop(source, target, piece, newPos, oldPos, orientation) {
if(source == target || isNaN(get_square(target)))
return 'snapback'
var promotion = (piece.toLowerCase().charAt(1) == 'p' && (parseInt(target.charAt(1)) == 8 || parseInt(target.charAt(1))==1));
$.get('/move_coordinates', {'from': (source), 'to': (target), 'promotion': promotion, 'color': iaColor, 'isFirst': 'False'}, function(r,text,request) {
console.log(request.getResponseHeader('game_over'));
if (request.getResponseHeader('game_over') == true) {
document.querySelector('p').innerText = 'game over';
board.position(r);
}
else {
document.querySelector('p').innerText = '';
board.position(r);
}
});
}
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 newGame() {
$.get('/newgame', function(r) {
document.querySelector('p').innerText = '';
board.position(r);
});
}
function play(color){
// TODO: hacer invisibles los botones de iniciar partida y visibles de nuevo cuando haya game over
var boardDiv = document.getElementById("board")
boardDiv.style.visibility = 'visible';
var iaColor = color;
var orienation = 'black';
if (color)
orienation = 'white'
board = ChessBoard('board', {
position: 'start',
draggable: true,
onDrop: onDrop,
orientation: orienation
});
document.getElementsByTagName('body')[0].style.backgroundColor = "grey";
if (!color)
$.get('/move_coordinates', {'from': '', 'to': '', 'promotion': '', 'color': iaColor, 'isFirst': 'True'}, function(r) {
document.querySelector('p').innerText = '';
board.position(r);
})
}
</script>
</body>
</html>