-
Notifications
You must be signed in to change notification settings - Fork 0
/
codepen.js
153 lines (135 loc) · 3.24 KB
/
codepen.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
var board,
game = new Chess(),
statusEl = $('#status'),
fenEl = $('#fen'),
pgnEl = $('#pgn');
kastleEl = $('#kastle');
// do not pick up pieces if the game is over
// only pick up pieces for the side to move
var onDragStart = function(source, piece, position, orientation) {
if (game.game_over() === true ||
(game.turn() === 'w' && piece.search(/^b/) !== -1) ||
(game.turn() === 'b' && piece.search(/^w/) !== -1)) {
return false;
}
};
var board = new ChessBoard('board', cfg);
var onDrop = function(source, target) {
// see if the move is legal
var move = game.move({
from: source,
to: target,
promotion: 'q'
});
// illegal move
if (move === null) return 'snapback';
updateStatus();
};
// update the board position after the piece snap
// for castling, en passant, pawn promotion
var onSnapEnd = function() {
board.position(game.fen());
};
var updateStatus = function() {
var status = '';
var endFen = '';
var moveColor = 'White';
if (game.turn() === 'b') {
moveColor = 'Black';
}
// checkmate?
if (game.in_checkmate() === true) {
status = 'Game over, ' + moveColor + ' is in checkmate.'
if(moveColor === "White"){
endFen += " K ";
}
else{
endFen += " k ";
}
}
// draw?
else if (game.in_draw() === true) {
status = 'Game over, drawn position';
}
// game still on
else {
status = moveColor + ' to move';
// check?
if (game.in_check() === true) {
status += ', ' + moveColor + ' is in check';
if(moveColor === "White"){
endFen += " Q ";
}
else{
endFen += " q ";
}
}
else{
endFen += " - ";
}
}
pgnparts = game.pgn().split(/. /);
if(pgnparts[pgnparts.length - 1] === 'O-O'){
endFen += "1";
}
else{
endFen += "0";
}
statusEl.html(status);
fenEl.html(game.fen() + endFen);
pgnEl.html(game.pgn());
if(typeof(boardToBinary)==='undefined'){
kastleEl.html('0');
} else {
kastle = boardToBinary(game.fen().split(' ')[0])
kastleEl.html((kastle=='')?'0':kastle);
}
}
var PGN2FEN = function(PGN){
var convert = new Chess();
var endFen = '';
convert.load_pgn(PGN);
if(convert.in_checkmate() === true){
if(convert.turn() === 'w'){
endFen += " K ";
}
else{
endFen += " k ";
}
}
else if(convert.in_check() === true){
if(convert.turn() === 'w'){
endFen += ' Q ';
}
else{
endFen += ' q ';
}
}
else{
endFen += ' - ';
}
pgnparts = game.pgn().split(/. /);
if(pgnparts[pgnparts.length - 1] === 'O-O'){
endFen += "1";
}
else{
endFen += "0";
}
return convert.fen() + endFen;
};
var cfg = {
snapbackSpeed: 550,
appearSpeed: 1500,
draggable: true,
position: 'start',
onDragStart: onDragStart,
onDrop: onDrop,
pieceTheme: 'http://www.willangles.com/projects/chessboard/img/chesspieces/wikipedia/{piece}.png',
onSnapEnd: onSnapEnd
};
board = new ChessBoard('board', cfg);
$(window).resize(board.resize);
updateStatus();
$('#startBtn').on('click', board.start);
$('#clearBtn').on('click', board.clear);
$('#colorBtn').on('click', board.flip);