-
Notifications
You must be signed in to change notification settings - Fork 2
/
play.js
341 lines (261 loc) · 7.45 KB
/
play.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// https://github.com/op12no2
var PLAYBUILD = 'p1.10';
if (!window.Worker) {
document.write('<p><b>DUDE, YOUR BROWSER IS TOO OLD TO PLAY CHESS!<p>TRY <a href="http://www.google.co.uk/chrome/">GOOGLE CHROME</a></a><p>');
exit;
}
var args = lozGetURLArgs();
var board = null;
var chess = null;
var drag = true;
var engine = null;
var startFrom = 'startpos';
var startFromUI = 'start';
var level = 1;
lozData.page = 'play.htm';
lozData.idInfo = '#info';
lozData.idStats = '#stats';
//{{{ lozUpdateBestMove
function lozUpdateBestMove () {
var move = {};
move.from = lozData.bmFr;
move.to = lozData.bmTo;
if (lozData.bmPr) {
move.promotion = lozData.bmPr;
}
chess.move(move);
board.position(chess.fen());
$('#moves').html(chess.pgn({newline_char: '<br>'}));
if (!chess.game_over())
drag = true;
else
showEnd();
}
//}}}
//{{{ lozUpdatePV
//function lozUpdatePV () {
//if (lozData.units == 'cp')
//$(lozData.idInfo).prepend('depth ' + lozData.depth + ' (' + lozData.score + ') ' + lozData.pv + '<br>');
//if (lozData.score > 0 && lozData.units != 'cp')
//$(lozData.idInfo).prepend('depth ' + lozData.depth + ' (<b>mate in ' + lozData.score + '</b>) ' + lozData.pv + '<br>');
//else if (lozData.units != 'cp')
//$(lozData.idInfo).prepend('depth ' + lozData.depth + ' (<b>checkmate</b>) ' + lozData.pv + '<br>');
//}
//}}}
//{{{ onDrop
var onDrop = function(source, target, piece, newPos, oldPos, orientation) {
if (target == 'offboard' || target == source) {
//console.log('offboard');
return;
}
var move = chess.move({from: source, to: target, promotion: 'q'})
if (!move) {
//console.log('invalid');
return 'snapback';
}
if (move.flags == 'e' || move.flags == 'p' || move.flags == 'k' || move.flags == 'q')
board.position(chess.fen());
var pgn = chess.pgn({newline_char: '<br>'});
$('#moves').html(pgn);
drag = false;
if (!chess.game_over()) {
$(lozData.idInfo).html('');
engine.postMessage('position ' + startFrom + ' moves ' + strMoves());
postGo();
}
else
showEnd();
};
//}}}
//{{{ onDragStart
var onDragStart = function(source, piece, position, orientation) {
if ((!drag || orientation === 'white' && piece.search(/^w/) === -1) || (orientation === 'black' && piece.search(/^b/) === -1)) {
return false;
}
return true;
};
//}}}
//{{{ strMoves
function strMoves() {
var movesStr = '';
var moves = chess.history({verbose: true});
for (var i=0; i < moves.length; i++) {
if (i)
movesStr += ' ';
var move = moves[i];
movesStr += move.from + move.to;
if (move.promotion)
movesStr += move.promotion;
}
return movesStr;
}
//}}}
//{{{ showEnd
function showEnd () {
if (chess.in_checkmate())
$(lozData.idInfo).html('Checkmate');
else if (chess.insufficient_material())
$(lozData.idInfo).html('Draw due to insufficient material');
else if (chess.in_draw())
$(lozData.idInfo).html('Draw by 50 move rule');
else if (chess.in_stalemate())
$(lozData.idInfo).html('Draw by stalemate');
else if (chess.in_threefold_repetition())
$(lozData.idInfo).html('Draw by threefold repetition');
else
$(lozData.idInfo).html('Game over but not sure why!');
}
//}}}
//{{{ getLevel
function getLevel () {
level = parseInt(args.l);
if (level <= 0)
level = 1;
if (level > 10)
level = 10;
}
//}}}
//{{{ postGo
function postGo () {
var go = '';
if (level < 1)
level = 1;
if (args.m)
go = args.m;
else if (level <= 8) {
go = 'go depth ' + level;
}
else if (level == 9)
go = 'go movetime 2000';
else if (level >= 10)
go = 'go movetime 10000';
$('#strength').html('Strength (' + level + ')'); //jic
engine.postMessage('debug off')
engine.postMessage(go);
}
//}}}
$(function() {
//{{{ init DOM
if (args.l) {
getLevel();
}
//}}}
//{{{ handlers
$('#playw').click(function() {
window.location = lozMakeURL ({
l : level
});
return true;
});
$('#playb').click(function() {
window.location = lozMakeURL ({
l : level,
c : 'b'
});
return true;
});
$('#level1').click(function() {
level=1;
$('#strength').html('Strength (1)');
return true;
});
$('#level2').click(function() {
level=2;
$('#strength').html('Strength (2)');
return true;
});
$('#level3').click(function() {
level=3;
$('#strength').html('Strength (3)');
return true;
});
$('#level4').click(function() {
level=4;
$('#strength').html('Strength (4)');
return true;
});
$('#level5').click(function() {
level=5;
$('#strength').html('Strength (5)');
return true;
});
$('#level6').click(function() {
level=6;
$('#strength').html('Strength (6)');
return true;
});
$('#level7').click(function() {
level=7;
$('#strength').html('Strength (7)');
return true;
});
$('#level8').click(function() {
level=8;
$('#strength').html('Strength (8)');
return true;
});
$('#level9').click(function() {
level=9;
$('#strength').html('Strength (9)');
return true;
});
$('#level10').click(function() {
level=10;
$('#strength').html('Strength (10)');
return true;
});
$('#analyse').click(function() {
window.open("fen.htm?fen=" + chess.fen(),"_blank");
return false;
});
//}}}
engine = new Worker(lozData.source);
engine.onmessage = lozStandardRx;
if (args.fen) {
startFrom = 'fen ' + args.fen;
startFromUI = args.fen;
chess = new Chess(args.fen);
$("#playw").hide();
$("#playb").hide();
}
else
chess = new Chess();
board = new ChessBoard('board', {
showNotation : true,
draggable : true,
dropOffBoard : 'snapback',
onDrop : onDrop,
onDragStart : onDragStart,
position : startFromUI
});
//$(lozData.idInfo).prepend('Version ' + BUILD + ' ' + PLAYBUILD + '<br>');
engine.postMessage('uci')
engine.postMessage('ucinewgame')
engine.postMessage('debug off')
if (!args.fen && args.c == 'b' || args.fen && args.fen.search(' w') !== -1 && args.c == 'b') {
board.orientation('black');
engine.postMessage('position ' + startFrom);
postGo();
$(lozData.idInfo).prepend('You are black' + '<br>');
}
else if (!args.fen && args.c != 'b' || args.fen && args.fen.search(' w') !== -1 && args.c != 'b') {
board.orientation('white');
$(lozData.idInfo).prepend('Your move with white pieces' + '<br>');
}
else if (args.fen && args.fen.search(' b') !== -1 && args.c == 'b') {
board.orientation('black');
$(lozData.idInfo).prepend('Your move with black pieces' + '<br>');
}
else if (args.fen && args.fen.search(' b') !== -1 && args.c != 'b') {
board.orientation('white');
engine.postMessage('position ' + startFrom);
postGo();
$(lozData.idInfo).prepend('You are white' + '<br>');
}
else {
$(lozData.idInfo).prepend('INCONSISTENT ARGS' + '<br>');
}
//$(lozData.idInfo).prepend('Level ' + level + '<br>');
$('#strength').html('Strength (' + level + ')');
//console.log(args);
});