-
Notifications
You must be signed in to change notification settings - Fork 2
/
mate.js
142 lines (118 loc) · 3.89 KB
/
mate.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
// https://github.com/op12no2
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 epds = [];
var time = 0;
var timeHandle = 0;
var goes = 10;
var right = 0;
function initRandom() {
var d = new Date();
var t = d.getTime();
var m = t % 1000;
var r = 0;
for (var i=0; i<m; i++)
r = r + Math.random();
}
function another () {
var epd = epds[Math.random() * epds.length | 0];
var epda = epd.split(' ');
epd = '';
for (var i = 0; i < 4; i++)
epd = epd + epda[i] + ' ';
epd = epd + '0 1';
chess.load(epd);
board.position(epd);
if (epda[1] == 'w')
board.orientation('white');
else
board.orientation('black');
//$('#info').html('<a target = "_blank" title="click to analyse in a new tab" href="fen.htm?act=ana&fen=' + epd + '">' + epd + '</a><br>');
$('#info').html('<br>');
}
var onDrop = function(source, target, piece, newPos, oldPos, orientation) {
if (target == 'offboard' || target == source)
return;
var move = chess.move({from: source, to: target, promotion: 'n'})
if (!move) {
$('#info').prepend('Illegal move, 10 second penalty.<br>');
time -= 10000;
return 'snapback';
}
if (!chess.in_checkmate()) {
chess.undo();
move = chess.move({from: source, to: target, promotion: 'b'})
}
if (!chess.in_checkmate()) {
chess.undo();
move = chess.move({from: source, to: target, promotion: 'r'})
}
if (!chess.in_checkmate()) {
chess.undo();
move = chess.move({from: source, to: target, promotion: 'q'})
}
if (chess.in_checkmate()) {
board.position(chess.fen());
right++;
if (right == goes) {
clearInterval(timeHandle);
$('#stats').html('' + Math.round((Date.now()-time)/1000));
$('#info').html('<b>Finished. Your score was ' + $('#stats').text() + '</b>');
$('#start').html('Try again');
}
else {
$('#board').find('.square-' + target).addClass('mate');
$('#info').html('<span style="color: green; font-weight: bold;">RIGHT</span>');
setTimeout(another,1000);
}
return 'trash';
}
else {
$('#info').prepend('wrong move, 5 second penalty.<br>');
time -= 5000;
chess.undo();
return 'snapback';
}
};
function timer() {
$('#stats').html('' + Math.round((Date.now()-time)/1000));
$('#stats').append(' ' + right + '/' + goes);
$('#stats').append(' <span style="background: yellow; padding: 3px;">' + board.orientation().toUpperCase() + ' to move</span>');
}
$(function() {
initRandom();
$('#stats').html('Loading EPD data from server...');
$.get('matein1.txt', function(data) {
data = data.trim();
data = data.replace(/\r/g,'');
epds = data.split('\n');
$('#stats').html('');
});
$('#start').click(function() {
$('#info').html('');
time = Date.now();
right = 0;
if (timeHandle)
clearInterval(timeHandle);
timeHandle = setInterval(timer, 90);
another();
return false;
});
chess = new Chess();
board = new ChessBoard('board', {
showNotation : true,
draggable : true,
dropOffBoard : 'snapback',
onDrop : onDrop
});
$('#info').append('After you click Start you must solve<br>');
$('#info').append(goes + ' mate-in-1 puzzles. Your total time is<br>');
$('#info').append('your final score, so lower scores are better.<br>');
$('#info').append('There are time penalties for illegal and wrong moves.<br>');
$('#info').append('Drag the piece to the mating square using the mouse.<br>');
});