forked from op12no2/lozza-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfen.js
192 lines (155 loc) · 3.99 KB
/
fen.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
// https://github.com/op12no2
var FENBUILD = 'a1.10';
var args = lozGetURLArgs();
var uiBoard = null;
var engine = null;
lozData.page = 'fen.htm';
lozData.idInfo = '#info';
lozData.idStats = '#stats';
//{{{ updateBoardFromFen
function updateBoardFromFen () {
var fen = $('#fen').val().trim();
uiBoard.position(fen);
};
//}}}
//{{{ updateFenFromBoard
function updateFenFromBoard () {
var turn = lozDecodeFEN($('#fen').val()).turn;
var fen = uiBoard.fen();
var pos = uiBoard.position();
var rights = '';
if (pos.e1 == 'wK' && pos.h1 == 'wR')
rights += 'K';
if (pos.e1 == 'wK' && pos.a1 == 'wR')
rights += 'Q';
if (pos.e8 == 'bK' && pos.h8 == 'bR')
rights += 'k';
if (pos.e8 == 'bK' && pos.h8 == 'bR')
rights += 'q';
if (!rights)
rights = '-';
$('#fen').val(fen + ' ' + turn + ' ' + rights + ' - 0 1');
};
//}}}
//{{{ onDrop
var onDrop = function(source, target, piece, newPos, oldPos, orientation) {
uiBoard.position(newPos);
updateFenFromBoard();
};
//}}}
//{{{ eval
function eval() {
if (engine)
engine.terminate();
engine = new Worker(lozData.source);
engine.onmessage = lozStandardRx;
$(lozData.idInfo).html('');
var fen = $('#fen').val().trim();
engine.postMessage('ucinewgame');
if (fen)
engine.postMessage('position fen ' + fen);
else
engine.postMessage('position startpos');
engine.postMessage('eval');
}
//}}}
//{{{ anal
function anal() {
if (engine)
engine.terminate();
engine = new Worker(lozData.source);
engine.onmessage = lozStandardRx;
$(lozData.idInfo).html('');
var fen = $('#fen').val().trim();
engine.postMessage('ucinewgame');
if (fen)
engine.postMessage('position fen ' + fen);
else
engine.postMessage('position startpos');
engine.postMessage('go depth 99');
}
//}}}
$(function() {
//{{{ init DOM
$('button').tooltip({delay: {"show": 1000, "hide": 100}});
//}}}
//{{{ handlers
$('#aswhite').click(function() {
window.open('play.htm?c=w&fen=' + $('#fen').val().trim(),'_blank');
return true;
});
$('#asblack').click(function() {
window.open('play.htm?c=b&fen=' + $('#fen').val().trim(),'_blank');
return true;
});
$('#startpos').click(function() {
$(lozData.idInfo).html('');
uiBoard.start();
updateFenFromBoard();
return false;
});
$('#clearpos').click(function() {
$(lozData.idInfo).html('');
uiBoard.position('4k3/8/8/8/8/8/8/4K3');
updateFenFromBoard();
return false;
});
$('#flippos').click(function() {
uiBoard.orientation('flip');
return false;
});
$('#flipturn').click(function() {
var feno = lozDecodeFEN($('#fen').val());
if (feno.turn == 'w')
feno.turn = 'b';
else
feno.turn = 'w';
$('#fen').val(lozEncodeFEN(feno));
return false;
});
$('#fen').blur(function() {
updateBoardFromFen();
return false;
});
$('#start').click(function() {
anal();
return false;
});
$('#stop').click(function() {
engine.terminate();
engine = null;
return false;
});
$('#eval').click(function() {
eval();
return false;
});
//}}}
uiBoard = new ChessBoard('board', {
showNotation : true,
draggable : true,
dropOffBoard : 'trash',
sparePieces : true,
onDrop : onDrop,
position : 'start'
});
if (args.fen) {
$('#fen').val(args.fen)
updateBoardFromFen();
}
else
updateFenFromBoard();
if (args.act == 'eva')
eval();
else if (args.act == 'ana')
anal();
else {
if (engine)
engine.terminate();
engine = new Worker(lozData.source);
engine.onmessage = lozStandardRx;
$(lozData.idInfo).html('');
$(lozData.idInfo).prepend('Version ' + BUILD + ' ' + FENBUILD + '<br>');
engine.postMessage('uci');
}
});