-
Notifications
You must be signed in to change notification settings - Fork 2
/
tune.js
235 lines (183 loc) · 5.32 KB
/
tune.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
var verbose = 0; // display each move
var moveTime = 50; // ms (+ random element)
///////////////////////////////
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 e1 = null;
var e2 = null;
var numOpenings = OPENINGS.length;
var numGames = 0;
var numMoves = 0;
var e1Wins = 0;
var e2Wins = 0;
var numDraws = 0;
var first = 1;
var id = 0;
var timeThen = Date.now();
var stopping = 0;
function randMoveTime () {
var r = moveTime + ((moveTime/5) * Math.random()) - (moveTime/10) | 0;
return r;
}
function randomise () {
var t = Date.now();
var m = t % 10000;
var r = 0;
for (var i=0; i<m; i++)
r = r + Math.random();
return r;
}
lozData.page = 'tune.htm';
lozData.idInfo = '#info';
lozData.idStats = '#stats';
function e1Message (e) {
eMessage(e,e1,e2,1);
}
function e2Message (e) {
eMessage(e,e2,e1,2);
}
function eMessage (e,me,tx,n) {
lozData.message = e.data;
lozData.message = lozData.message.trim();
lozData.message = lozData.message.replace(/\s+/g,' ');
lozData.tokens = lozData.message.split(' ');
if (lozData.tokens[0] == 'bestmove') {
var move = {};
lozData.bm = lozGetStr('bestmove','');
lozData.bmFr = lozData.bm[0] + lozData.bm[1];
lozData.bmTo = lozData.bm[2] + lozData.bm[3];
if (lozData.bm.length > 4) {
lozData.bmPr = lozData.bm[4];
move.promotion = lozData.bmPr;
}
else
lozData.bmPr = '';
move.from = lozData.bmFr;
move.to = lozData.bmTo;
if (!chess.move(move))
console.log('e',n,'invalid move',move,lozData.bm);
if (verbose)
board.position(chess.fen());
if (!chess.game_over()) {
tx.postMessage('position startpos moves ' + strMoves());
tx.postMessage('go movetime ' + randMoveTime());
}
else {
board.position(chess.fen());
showEnd(n);
}
}
else if (lozData.tokens[0] == 'info') {
var depth = lozGetInt('depth',0);
var seldepth = lozGetInt('seldepth',0);
if (depth && seldepth)
$(lozData.idStats).html('' + depth + '/' + seldepth + ' ' + numMoves);
}
else if (id < 2 && lozData.tokens[1] == 'name') {
$(lozData.idInfo).append(lozGetStr('Lozza','?')+'<br>');
id++;
}
}
function strMoves() {
var movesStr = '';
var moves = chess.history({verbose: true});
numMoves = moves.length / 2 | 0;
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;
}
function showEnd (n) {
if (n) {
e1.terminate();
e2.terminate();
if (chess.in_checkmate()) {
if (n == 1) {
e1Wins += 1;
}
else if (n == 2) {
e2Wins += 1;
}
else
console.log('showEnd bad n value',n);
}
else {
numDraws += 1;
}
numGames++;
if (numGames != numDraws + e1Wins + e2Wins)
console.log('scoring error',numGames,numDraws,e1Wins,e2Wins);
var e1Points = e1Wins + 0.5 * numDraws;
var e2Points = e2Wins + 0.5 * numDraws;
var e1Percent = 0.5 + e1Points / (e1Points+e2Points) * 100 | 0;
$(lozData.idInfo).html('' + numGames + ': ' + e1Wins + '-' + e2Wins + '-' + numDraws + ' ' + e1Percent + '%<br>');
var timeNow = Date.now();
if (numGames % 100 == 0)
console.log('games per hour',((numGames/(timeNow-timeThen))*1000*60*60)|0);
}
if (stopping)
return;
var choose = Math.random() * numOpenings | 0;
var opening = OPENINGS[choose];
var openMoves = opening.split(' ');
chess.reset();
for (var i=0; i < openMoves.length; i++) {
var move = openMoves[i];
if (!chess.move({from: move[0]+move[1], to: move[2]+move[3]}))
console.log('invalid opening move',i,move);
}
if (verbose)
board.position(chess.fen());
e1 = new Worker('lozza.js');
e1.onmessage = e1Message;
e2 = new Worker('lozza.js');
e2.onmessage = e2Message;
e1.postMessage('uci')
e1.postMessage('ucinewgame')
e1.postMessage('debug off')
e2.postMessage('uci')
e2.postMessage('ucinewgame')
e2.postMessage('debug off')
if (first == 1) {
e1.postMessage('position startpos moves ' + strMoves());
e1.postMessage('go movetime ' + randMoveTime());
first = 2;
}
else if (first == 2){
e2.postMessage('position startpos moves ' + strMoves());
e2.postMessage('go movetime ' + randMoveTime());
first = 1;
}
else
console.log('showEnd bad first value',first);
}
$(function() {
$('#stop').click(function() {
stopping = 1;
console.log('stopping after this game...');
return true;
});
$('#restart').click(function() {
stopping = 0;
showEnd(0);
return true;
});
randomise();
chess = new Chess();
board = new ChessBoard('board', {
showNotation : true,
draggable : false,
position : 'start'
});
showEnd(0);
});