3 * Copyright (c) 2014, Jeff Hlywa (jhlywa@gmail.com)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 *----------------------------------------------------------------------------*/
29 /* minified license below */
31 /*! Copyright (c) 2014, Jeff Hlywa (jhlywa@gmail.com)
32 * Released under the BSD license
33 * https://github.com/jhlywa/chess.js/blob/master/LICENSE
37 var Chess = function(fen) {
39 /* jshint indent: false */
53 var SYMBOLS = 'pnbrqkPNBRQK';
55 var DEFAULT_POSITION = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
57 var POSSIBLE_RESULTS = ['1-0', '0-1', '1/2-1/2', '*'];
61 w: [-16, -32, -17, -15]
65 n: [-18, -33, -31, -14, 18, 33, 31, 14],
66 b: [-17, -15, 17, 15],
68 q: [-17, -16, -15, 1, 17, 16, 15, -1],
69 k: [-17, -16, -15, 1, 17, 16, 15, -1]
73 20, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0,20, 0,
74 0,20, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0,20, 0, 0,
75 0, 0,20, 0, 0, 0, 0, 24, 0, 0, 0, 0,20, 0, 0, 0,
76 0, 0, 0,20, 0, 0, 0, 24, 0, 0, 0,20, 0, 0, 0, 0,
77 0, 0, 0, 0,20, 0, 0, 24, 0, 0,20, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0,20, 2, 24, 2,20, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 2,53, 56, 53, 2, 0, 0, 0, 0, 0, 0,
80 24,24,24,24,24,24,56, 0, 56,24,24,24,24,24,24, 0,
81 0, 0, 0, 0, 0, 2,53, 56, 53, 2, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0,20, 2, 24, 2,20, 0, 0, 0, 0, 0, 0,
83 0, 0, 0, 0,20, 0, 0, 24, 0, 0,20, 0, 0, 0, 0, 0,
84 0, 0, 0,20, 0, 0, 0, 24, 0, 0, 0,20, 0, 0, 0, 0,
85 0, 0,20, 0, 0, 0, 0, 24, 0, 0, 0, 0,20, 0, 0, 0,
86 0,20, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0,20, 0, 0,
87 20, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0,20
91 17, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 15, 0,
92 0, 17, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 15, 0, 0,
93 0, 0, 17, 0, 0, 0, 0, 16, 0, 0, 0, 0, 15, 0, 0, 0,
94 0, 0, 0, 17, 0, 0, 0, 16, 0, 0, 0, 15, 0, 0, 0, 0,
95 0, 0, 0, 0, 17, 0, 0, 16, 0, 0, 15, 0, 0, 0, 0, 0,
96 0, 0, 0, 0, 0, 17, 0, 16, 0, 15, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 0, 0, 0, 17, 16, 15, 0, 0, 0, 0, 0, 0, 0,
98 1, 1, 1, 1, 1, 1, 1, 0, -1, -1, -1,-1, -1, -1, -1, 0,
99 0, 0, 0, 0, 0, 0,-15,-16,-17, 0, 0, 0, 0, 0, 0, 0,
100 0, 0, 0, 0, 0,-15, 0,-16, 0,-17, 0, 0, 0, 0, 0, 0,
101 0, 0, 0, 0,-15, 0, 0,-16, 0, 0,-17, 0, 0, 0, 0, 0,
102 0, 0, 0,-15, 0, 0, 0,-16, 0, 0, 0,-17, 0, 0, 0, 0,
103 0, 0,-15, 0, 0, 0, 0,-16, 0, 0, 0, 0,-17, 0, 0, 0,
104 0,-15, 0, 0, 0, 0, 0,-16, 0, 0, 0, 0, 0,-17, 0, 0,
105 -15, 0, 0, 0, 0, 0, 0,-16, 0, 0, 0, 0, 0, 0,-17
108 var SHIFTS = { p: 0, n: 1, b: 2, r: 3, q: 4, k: 5 };
140 a8: 0, b8: 1, c8: 2, d8: 3, e8: 4, f8: 5, g8: 6, h8: 7,
141 a7: 16, b7: 17, c7: 18, d7: 19, e7: 20, f7: 21, g7: 22, h7: 23,
142 a6: 32, b6: 33, c6: 34, d6: 35, e6: 36, f6: 37, g6: 38, h6: 39,
143 a5: 48, b5: 49, c5: 50, d5: 51, e5: 52, f5: 53, g5: 54, h5: 55,
144 a4: 64, b4: 65, c4: 66, d4: 67, e4: 68, f4: 69, g4: 70, h4: 71,
145 a3: 80, b3: 81, c3: 82, d3: 83, e3: 84, f3: 85, g3: 86, h3: 87,
146 a2: 96, b2: 97, c2: 98, d2: 99, e2: 100, f2: 101, g2: 102, h2: 103,
147 a1: 112, b1: 113, c1: 114, d1: 115, e1: 116, f1: 117, g1: 118, h1: 119
151 w: [{square: SQUARES.a1, flag: BITS.QSIDE_CASTLE},
152 {square: SQUARES.h1, flag: BITS.KSIDE_CASTLE}],
153 b: [{square: SQUARES.a8, flag: BITS.QSIDE_CASTLE},
154 {square: SQUARES.h8, flag: BITS.KSIDE_CASTLE}]
157 var board = new Array(128);
158 var kings = {w: EMPTY, b: EMPTY};
160 var castling = {w: 0, b: 0};
161 var ep_square = EMPTY;
167 /* if the user passes in a fen string, load it, else default to
170 if (typeof fen === 'undefined') {
171 load(DEFAULT_POSITION);
177 board = new Array(128);
178 kings = {w: EMPTY, b: EMPTY};
180 castling = {w: 0, b: 0};
186 update_setup(generate_fen());
190 load(DEFAULT_POSITION);
194 var tokens = fen.split(/\s+/);
195 var position = tokens[0];
197 var valid = SYMBOLS + '12345678/';
199 if (!validate_fen(fen).valid) {
205 for (var i = 0; i < position.length; i++) {
206 var piece = position.charAt(i);
210 } else if (is_digit(piece)) {
211 square += parseInt(piece, 10);
213 var color = (piece < 'a') ? WHITE : BLACK;
214 put({type: piece.toLowerCase(), color: color}, algebraic(square));
221 if (tokens[2].indexOf('K') > -1) {
222 castling.w |= BITS.KSIDE_CASTLE;
224 if (tokens[2].indexOf('Q') > -1) {
225 castling.w |= BITS.QSIDE_CASTLE;
227 if (tokens[2].indexOf('k') > -1) {
228 castling.b |= BITS.KSIDE_CASTLE;
230 if (tokens[2].indexOf('q') > -1) {
231 castling.b |= BITS.QSIDE_CASTLE;
234 ep_square = (tokens[3] === '-') ? EMPTY : SQUARES[tokens[3]];
235 half_moves = parseInt(tokens[4], 10);
236 move_number = parseInt(tokens[5], 10);
238 update_setup(generate_fen());
243 function validate_fen(fen) {
246 1: 'FEN string must contain six space-delimited fields.',
247 2: '6th field (move number) must be a positive integer.',
248 3: '5th field (half move counter) must be a non-negative integer.',
249 4: '4th field (en-passant square) is invalid.',
250 5: '3rd field (castling availability) is invalid.',
251 6: '2nd field (side to move) is invalid.',
252 7: '1st field (piece positions) does not contain 8 \'/\'-delimited rows.',
253 8: '1st field (piece positions) is invalid [consecutive numbers].',
254 9: '1st field (piece positions) is invalid [invalid piece].',
255 10: '1st field (piece positions) is invalid [row too large].',
258 /* 1st criterion: 6 space-seperated fields? */
259 var tokens = fen.split(/\s+/);
260 if (tokens.length !== 6) {
261 return {valid: false, error_number: 1, error: errors[1]};
264 /* 2nd criterion: move number field is a integer value > 0? */
265 if (isNaN(tokens[5]) || (parseInt(tokens[5], 10) <= 0)) {
266 return {valid: false, error_number: 2, error: errors[2]};
269 /* 3rd criterion: half move counter is an integer >= 0? */
270 if (isNaN(tokens[4]) || (parseInt(tokens[4], 10) < 0)) {
271 return {valid: false, error_number: 3, error: errors[3]};
274 /* 4th criterion: 4th field is a valid e.p.-string? */
275 if (!/^(-|[abcdefgh][36])$/.test(tokens[3])) {
276 return {valid: false, error_number: 4, error: errors[4]};
279 /* 5th criterion: 3th field is a valid castle-string? */
280 if( !/^(KQ?k?q?|Qk?q?|kq?|q|-)$/.test(tokens[2])) {
281 return {valid: false, error_number: 5, error: errors[5]};
284 /* 6th criterion: 2nd field is "w" (white) or "b" (black)? */
285 if (!/^(w|b)$/.test(tokens[1])) {
286 return {valid: false, error_number: 6, error: errors[6]};
289 /* 7th criterion: 1st field contains 8 rows? */
290 var rows = tokens[0].split('/');
291 if (rows.length !== 8) {
292 return {valid: false, error_number: 7, error: errors[7]};
295 /* 8th criterion: every row is valid? */
296 for (var i = 0; i < rows.length; i++) {
297 /* check for right sum of fields AND not two numbers in succession */
299 var previous_was_number = false;
301 for (var k = 0; k < rows[i].length; k++) {
302 if (!isNaN(rows[i][k])) {
303 if (previous_was_number) {
304 return {valid: false, error_number: 8, error: errors[8]};
306 sum_fields += parseInt(rows[i][k], 10);
307 previous_was_number = true;
309 if (!/^[prnbqkPRNBQK]$/.test(rows[i][k])) {
310 return {valid: false, error_number: 9, error: errors[9]};
313 previous_was_number = false;
316 if (sum_fields !== 8) {
317 return {valid: false, error_number: 10, error: errors[10]};
321 /* everything's okay! */
322 return {valid: true, error_number: 0, error: errors[0]};
325 function generate_fen() {
329 for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
330 if (board[i] == null) {
337 var color = board[i].color;
338 var piece = board[i].type;
340 fen += (color === WHITE) ?
341 piece.toUpperCase() : piece.toLowerCase();
344 if ((i + 1) & 0x88) {
349 if (i !== SQUARES.h1) {
359 if (castling[WHITE] & BITS.KSIDE_CASTLE) { cflags += 'K'; }
360 if (castling[WHITE] & BITS.QSIDE_CASTLE) { cflags += 'Q'; }
361 if (castling[BLACK] & BITS.KSIDE_CASTLE) { cflags += 'k'; }
362 if (castling[BLACK] & BITS.QSIDE_CASTLE) { cflags += 'q'; }
364 /* do we have an empty castling flag? */
365 cflags = cflags || '-';
366 var epflags = (ep_square === EMPTY) ? '-' : algebraic(ep_square);
368 return [fen, turn, cflags, epflags, half_moves, move_number].join(' ');
371 function set_header(args) {
372 for (var i = 0; i < args.length; i += 2) {
373 if (typeof args[i] === 'string' &&
374 typeof args[i + 1] === 'string') {
375 header[args[i]] = args[i + 1];
381 /* called when the initial board setup is changed with put() or remove().
382 * modifies the SetUp and FEN properties of the header object. if the FEN is
383 * equal to the default position, the SetUp and FEN are deleted
384 * the setup is only updated if history.length is zero, ie moves haven't been
387 function update_setup(fen) {
388 if (history.length > 0) return;
390 if (fen !== DEFAULT_POSITION) {
391 header['SetUp'] = '1';
394 delete header['SetUp'];
395 delete header['FEN'];
399 function get(square) {
400 var piece = board[SQUARES[square]];
401 return (piece) ? {type: piece.type, color: piece.color} : null;
404 function put(piece, square) {
405 /* check for valid piece object */
406 if (!('type' in piece && 'color' in piece)) {
410 /* check for piece */
411 if (SYMBOLS.indexOf(piece.type.toLowerCase()) === -1) {
415 /* check for valid square */
416 if (!(square in SQUARES)) {
420 var sq = SQUARES[square];
422 /* don't let the user place more than one king */
423 if (piece.type == KING &&
424 !(kings[piece.color] == EMPTY || kings[piece.color] == sq)) {
428 board[sq] = {type: piece.type, color: piece.color};
429 if (piece.type === KING) {
430 kings[piece.color] = sq;
433 update_setup(generate_fen());
438 function remove(square) {
439 var piece = get(square);
440 board[SQUARES[square]] = null;
441 if (piece && piece.type === KING) {
442 kings[piece.color] = EMPTY;
445 update_setup(generate_fen());
450 function build_move(board, from, to, flags, promotion) {
456 piece: board[from].type
460 move.flags |= BITS.PROMOTION;
461 move.promotion = promotion;
465 move.captured = board[to].type;
466 } else if (flags & BITS.EP_CAPTURE) {
467 move.captured = PAWN;
472 function generate_moves(options) {
473 function add_move(board, moves, from, to, flags) {
474 /* if pawn promotion */
475 if (board[from].type === PAWN &&
476 (rank(to) === RANK_8 || rank(to) === RANK_1)) {
477 var pieces = [QUEEN, ROOK, BISHOP, KNIGHT];
478 for (var i = 0, len = pieces.length; i < len; i++) {
479 moves.push(build_move(board, from, to, flags, pieces[i]));
482 moves.push(build_move(board, from, to, flags));
488 var them = swap_color(us);
489 var second_rank = {b: RANK_7, w: RANK_2};
491 var first_sq = SQUARES.a8;
492 var last_sq = SQUARES.h1;
493 var single_square = false;
495 /* do we want legal moves? */
496 var legal = (typeof options !== 'undefined' && 'legal' in options) ?
497 options.legal : true;
499 /* are we generating moves for a single square? */
500 if (typeof options !== 'undefined' && 'square' in options) {
501 if (options.square in SQUARES) {
502 first_sq = last_sq = SQUARES[options.square];
503 single_square = true;
510 for (var i = first_sq; i <= last_sq; i++) {
511 /* did we run off the end of the board */
512 if (i & 0x88) { i += 7; continue; }
514 var piece = board[i];
515 if (piece == null || piece.color !== us) {
519 if (piece.type === PAWN) {
520 /* single square, non-capturing */
521 var square = i + PAWN_OFFSETS[us][0];
522 if (board[square] == null) {
523 add_move(board, moves, i, square, BITS.NORMAL);
526 square = i + PAWN_OFFSETS[us][1];
527 if (second_rank[us] === rank(i) && board[square] == null) {
528 add_move(board, moves, i, square, BITS.BIG_PAWN);
533 for (var j = 2; j < 4; j++) {
534 square = i + PAWN_OFFSETS[us][j];
535 if (square & 0x88) continue;
537 if (board[square] != null &&
538 board[square].color === them) {
539 add_move(board, moves, i, square, BITS.CAPTURE);
540 } else if (square === ep_square) {
541 add_move(board, moves, i, ep_square, BITS.EP_CAPTURE);
545 for (var j = 0, len = PIECE_OFFSETS[piece.type].length; j < len; j++) {
546 var offset = PIECE_OFFSETS[piece.type][j];
551 if (square & 0x88) break;
553 if (board[square] == null) {
554 add_move(board, moves, i, square, BITS.NORMAL);
556 if (board[square].color === us) break;
557 add_move(board, moves, i, square, BITS.CAPTURE);
561 /* break, if knight or king */
562 if (piece.type === 'n' || piece.type === 'k') break;
568 /* check for castling if: a) we're generating all moves, or b) we're doing
569 * single square move generation on the king's square
571 if ((!single_square) || last_sq === kings[us]) {
572 /* king-side castling */
573 if (castling[us] & BITS.KSIDE_CASTLE) {
574 var castling_from = kings[us];
575 var castling_to = castling_from + 2;
577 if (board[castling_from + 1] == null &&
578 board[castling_to] == null &&
579 !attacked(them, kings[us]) &&
580 !attacked(them, castling_from + 1) &&
581 !attacked(them, castling_to)) {
582 add_move(board, moves, kings[us] , castling_to,
587 /* queen-side castling */
588 if (castling[us] & BITS.QSIDE_CASTLE) {
589 var castling_from = kings[us];
590 var castling_to = castling_from - 2;
592 if (board[castling_from - 1] == null &&
593 board[castling_from - 2] == null &&
594 board[castling_from - 3] == null &&
595 !attacked(them, kings[us]) &&
596 !attacked(them, castling_from - 1) &&
597 !attacked(them, castling_to)) {
598 add_move(board, moves, kings[us], castling_to,
604 /* return all pseudo-legal moves (this includes moves that allow the king
611 /* filter out illegal moves */
612 var legal_moves = [];
613 for (var i = 0, len = moves.length; i < len; i++) {
615 if (!king_attacked(us)) {
616 legal_moves.push(moves[i]);
624 /* convert a move from 0x88 coordinates to Standard Algebraic Notation
627 function move_to_san(move) {
630 if (move.flags & BITS.KSIDE_CASTLE) {
632 } else if (move.flags & BITS.QSIDE_CASTLE) {
635 var disambiguator = get_disambiguator(move);
637 if (move.piece !== PAWN) {
638 output += move.piece.toUpperCase() + disambiguator;
641 if (move.flags & (BITS.CAPTURE | BITS.EP_CAPTURE)) {
642 if (move.piece === PAWN) {
643 output += algebraic(move.from)[0];
648 output += algebraic(move.to);
650 if (move.flags & BITS.PROMOTION) {
651 output += '=' + move.promotion.toUpperCase();
657 if (in_checkmate()) {
668 function attacked(color, square) {
669 for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
670 /* did we run off the end of the board */
671 if (i & 0x88) { i += 7; continue; }
673 /* if empty square or wrong color */
674 if (board[i] == null || board[i].color !== color) continue;
676 var piece = board[i];
677 var difference = i - square;
678 var index = difference + 119;
680 if (ATTACKS[index] & (1 << SHIFTS[piece.type])) {
681 if (piece.type === PAWN) {
682 if (difference > 0) {
683 if (piece.color === WHITE) return true;
685 if (piece.color === BLACK) return true;
690 /* if the piece is a knight or a king */
691 if (piece.type === 'n' || piece.type === 'k') return true;
693 var offset = RAYS[index];
697 while (j !== square) {
698 if (board[j] != null) { blocked = true; break; }
702 if (!blocked) return true;
709 function king_attacked(color) {
710 return attacked(swap_color(color), kings[color]);
713 function in_check() {
714 return king_attacked(turn);
717 function in_checkmate() {
718 return in_check() && generate_moves().length === 0;
721 function in_stalemate() {
722 return !in_check() && generate_moves().length === 0;
725 function insufficient_material() {
731 for (var i = SQUARES.a8; i<= SQUARES.h1; i++) {
732 sq_color = (sq_color + 1) % 2;
733 if (i & 0x88) { i += 7; continue; }
735 var piece = board[i];
737 pieces[piece.type] = (piece.type in pieces) ?
738 pieces[piece.type] + 1 : 1;
739 if (piece.type === BISHOP) {
740 bishops.push(sq_color);
747 if (num_pieces === 2) { return true; }
749 /* k vs. kn .... or .... k vs. kb */
750 else if (num_pieces === 3 && (pieces[BISHOP] === 1 ||
751 pieces[KNIGHT] === 1)) { return true; }
753 /* kb vs. kb where any number of bishops are all on the same color */
754 else if (num_pieces === pieces[BISHOP] + 2) {
756 var len = bishops.length;
757 for (var i = 0; i < len; i++) {
760 if (sum === 0 || sum === len) { return true; }
766 function in_threefold_repetition() {
767 /* TODO: while this function is fine for casual use, a better
768 * implementation would use a Zobrist key (instead of FEN). the
769 * Zobrist key would be maintained in the make_move/undo_move functions,
770 * avoiding the costly that we do below.
774 var repetition = false;
777 var move = undo_move();
783 /* remove the last two fields in the FEN string, they're not needed
784 * when checking for draw by rep */
785 var fen = generate_fen().split(' ').slice(0,4).join(' ');
787 /* has the position occurred three or move times */
788 positions[fen] = (fen in positions) ? positions[fen] + 1 : 1;
789 if (positions[fen] >= 3) {
796 make_move(moves.pop());
802 function push(move) {
805 kings: {b: kings.b, w: kings.w},
807 castling: {b: castling.b, w: castling.w},
808 ep_square: ep_square,
809 half_moves: half_moves,
810 move_number: move_number
814 function make_move(move) {
816 var them = swap_color(us);
819 board[move.to] = board[move.from];
820 board[move.from] = null;
822 /* if ep capture, remove the captured pawn */
823 if (move.flags & BITS.EP_CAPTURE) {
824 if (turn === BLACK) {
825 board[move.to - 16] = null;
827 board[move.to + 16] = null;
831 /* if pawn promotion, replace with new piece */
832 if (move.flags & BITS.PROMOTION) {
833 board[move.to] = {type: move.promotion, color: us};
836 /* if we moved the king */
837 if (board[move.to].type === KING) {
838 kings[board[move.to].color] = move.to;
840 /* if we castled, move the rook next to the king */
841 if (move.flags & BITS.KSIDE_CASTLE) {
842 var castling_to = move.to - 1;
843 var castling_from = move.to + 1;
844 board[castling_to] = board[castling_from];
845 board[castling_from] = null;
846 } else if (move.flags & BITS.QSIDE_CASTLE) {
847 var castling_to = move.to + 1;
848 var castling_from = move.to - 2;
849 board[castling_to] = board[castling_from];
850 board[castling_from] = null;
853 /* turn off castling */
857 /* turn off castling if we move a rook */
859 for (var i = 0, len = ROOKS[us].length; i < len; i++) {
860 if (move.from === ROOKS[us][i].square &&
861 castling[us] & ROOKS[us][i].flag) {
862 castling[us] ^= ROOKS[us][i].flag;
868 /* turn off castling if we capture a rook */
869 if (castling[them]) {
870 for (var i = 0, len = ROOKS[them].length; i < len; i++) {
871 if (move.to === ROOKS[them][i].square &&
872 castling[them] & ROOKS[them][i].flag) {
873 castling[them] ^= ROOKS[them][i].flag;
879 /* if big pawn move, update the en passant square */
880 if (move.flags & BITS.BIG_PAWN) {
882 ep_square = move.to - 16;
884 ep_square = move.to + 16;
890 /* reset the 50 move counter if a pawn is moved or a piece is captured */
891 if (move.piece === PAWN) {
893 } else if (move.flags & (BITS.CAPTURE | BITS.EP_CAPTURE)) {
899 if (turn === BLACK) {
902 turn = swap_color(turn);
905 function undo_move() {
906 var old = history.pop();
907 if (old == null) { return null; }
912 castling = old.castling;
913 ep_square = old.ep_square;
914 half_moves = old.half_moves;
915 move_number = old.move_number;
918 var them = swap_color(turn);
920 board[move.from] = board[move.to];
921 board[move.from].type = move.piece; // to undo any promotions
922 board[move.to] = null;
924 if (move.flags & BITS.CAPTURE) {
925 board[move.to] = {type: move.captured, color: them};
926 } else if (move.flags & BITS.EP_CAPTURE) {
929 index = move.to - 16;
931 index = move.to + 16;
933 board[index] = {type: PAWN, color: them};
937 if (move.flags & (BITS.KSIDE_CASTLE | BITS.QSIDE_CASTLE)) {
938 var castling_to, castling_from;
939 if (move.flags & BITS.KSIDE_CASTLE) {
940 castling_to = move.to + 1;
941 castling_from = move.to - 1;
942 } else if (move.flags & BITS.QSIDE_CASTLE) {
943 castling_to = move.to - 2;
944 castling_from = move.to + 1;
947 board[castling_to] = board[castling_from];
948 board[castling_from] = null;
954 /* this function is used to uniquely identify ambiguous moves */
955 function get_disambiguator(move) {
956 var moves = generate_moves();
958 var from = move.from;
960 var piece = move.piece;
966 for (var i = 0, len = moves.length; i < len; i++) {
967 var ambig_from = moves[i].from;
968 var ambig_to = moves[i].to;
969 var ambig_piece = moves[i].piece;
971 /* if a move of the same piece type ends on the same to square, we'll
972 * need to add a disambiguator to the algebraic notation
974 if (piece === ambig_piece && from !== ambig_from && to === ambig_to) {
977 if (rank(from) === rank(ambig_from)) {
981 if (file(from) === file(ambig_from)) {
987 if (ambiguities > 0) {
988 /* if there exists a similar moving piece on the same rank and file as
989 * the move in question, use the square as the disambiguator
991 if (same_rank > 0 && same_file > 0) {
992 return algebraic(from);
994 /* if the moving piece rests on the same file, use the rank symbol as the
997 else if (same_file > 0) {
998 return algebraic(from).charAt(1);
1000 /* else use the file symbol */
1002 return algebraic(from).charAt(0);
1010 var s = ' +------------------------+\n';
1011 for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
1012 /* display the rank */
1013 if (file(i) === 0) {
1014 s += ' ' + '87654321'[rank(i)] + ' |';
1018 if (board[i] == null) {
1021 var piece = board[i].type;
1022 var color = board[i].color;
1023 var symbol = (color === WHITE) ?
1024 piece.toUpperCase() : piece.toLowerCase();
1025 s += ' ' + symbol + ' ';
1028 if ((i + 1) & 0x88) {
1033 s += ' +------------------------+\n';
1034 s += ' a b c d e f g h\n';
1039 /*****************************************************************************
1041 ****************************************************************************/
1050 function algebraic(i){
1051 var f = file(i), r = rank(i);
1052 return 'abcdefgh'.substring(f,f+1) + '87654321'.substring(r,r+1);
1055 function swap_color(c) {
1056 return c === WHITE ? BLACK : WHITE;
1059 function is_digit(c) {
1060 return '0123456789'.indexOf(c) !== -1;
1063 /* pretty = external move object */
1064 function make_pretty(ugly_move) {
1065 var move = clone(ugly_move);
1066 move.san = move_to_san(move);
1067 move.to = algebraic(move.to);
1068 move.from = algebraic(move.from);
1072 for (var flag in BITS) {
1073 if (BITS[flag] & move.flags) {
1074 flags += FLAGS[flag];
1082 function clone(obj) {
1083 var dupe = (obj instanceof Array) ? [] : {};
1085 for (var property in obj) {
1086 if (typeof property === 'object') {
1087 dupe[property] = clone(obj[property]);
1089 dupe[property] = obj[property];
1096 function trim(str) {
1097 return str.replace(/^\s+|\s+$/g, '');
1100 /*****************************************************************************
1101 * DEBUGGING UTILITIES
1102 ****************************************************************************/
1103 function perft(depth) {
1104 var moves = generate_moves({legal: false});
1108 for (var i = 0, len = moves.length; i < len; i++) {
1109 make_move(moves[i]);
1110 if (!king_attacked(color)) {
1111 if (depth - 1 > 0) {
1112 var child_nodes = perft(depth - 1);
1113 nodes += child_nodes;
1125 /***************************************************************************
1126 * PUBLIC CONSTANTS (is there a better way to do this?)
1127 **************************************************************************/
1136 SQUARES: (function() {
1137 /* from the ECMA-262 spec (section 12.6.4):
1138 * "The mechanics of enumerating the properties ... is
1139 * implementation dependent"
1140 * so: for (var sq in SQUARES) { keys.push(sq); } might not be
1144 for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
1145 if (i & 0x88) { i += 7; continue; }
1146 keys.push(algebraic(i));
1152 /***************************************************************************
1154 **************************************************************************/
1155 load: function(fen) {
1163 moves: function(options) {
1164 /* The internal representation of a chess move is in 0x88 format, and
1165 * not meant to be human-readable. The code below converts the 0x88
1166 * square coordinates to algebraic coordinates. It also prunes an
1167 * unnecessary move keys resulting from a verbose call.
1170 var ugly_moves = generate_moves(options);
1173 for (var i = 0, len = ugly_moves.length; i < len; i++) {
1175 /* does the user want a full move object (most likely not), or just
1178 if (typeof options !== 'undefined' && 'verbose' in options &&
1180 moves.push(make_pretty(ugly_moves[i]));
1182 moves.push(move_to_san(ugly_moves[i]));
1189 in_check: function() {
1193 in_checkmate: function() {
1194 return in_checkmate();
1197 in_stalemate: function() {
1198 return in_stalemate();
1201 in_draw: function() {
1202 return half_moves >= 100 ||
1204 insufficient_material() ||
1205 in_threefold_repetition();
1208 insufficient_material: function() {
1209 return insufficient_material();
1212 in_threefold_repetition: function() {
1213 return in_threefold_repetition();
1216 game_over: function() {
1217 return half_moves >= 100 ||
1220 insufficient_material() ||
1221 in_threefold_repetition();
1224 validate_fen: function(fen) {
1225 return validate_fen(fen);
1229 return generate_fen();
1232 pgn: function(options) {
1233 /* using the specification from http://www.chessclub.com/help/PGN-spec
1234 * example for html usage: .pgn({ max_width: 72, newline_char: "<br />" })
1236 var newline = (typeof options === 'object' &&
1237 typeof options.newline_char === 'string') ?
1238 options.newline_char : '\n';
1239 var max_width = (typeof options === 'object' &&
1240 typeof options.max_width === 'number') ?
1241 options.max_width : 0;
1243 var header_exists = false;
1245 /* add the PGN header headerrmation */
1246 for (var i in header) {
1247 /* TODO: order of enumerated properties in header object is not
1248 * guaranteed, see ECMA-262 spec (section 12.6.4)
1250 result.push('[' + i + ' \"' + header[i] + '\"]' + newline);
1251 header_exists = true;
1254 if (header_exists && history.length) {
1255 result.push(newline);
1258 /* pop all of history onto reversed_history */
1259 var reversed_history = [];
1260 while (history.length > 0) {
1261 reversed_history.push(undo_move());
1265 var move_string = '';
1266 var pgn_move_number = 1;
1268 /* build the list of moves. a move_string looks like: "3. e3 e6" */
1269 while (reversed_history.length > 0) {
1270 var move = reversed_history.pop();
1272 /* if the position started with black to move, start PGN with 1. ... */
1273 if (pgn_move_number === 1 && move.color === 'b') {
1274 move_string = '1. ...';
1276 } else if (move.color === 'w') {
1277 /* store the previous generated move_string if we have one */
1278 if (move_string.length) {
1279 moves.push(move_string);
1281 move_string = pgn_move_number + '.';
1285 move_string = move_string + ' ' + move_to_san(move);
1289 /* are there any other leftover moves? */
1290 if (move_string.length) {
1291 moves.push(move_string);
1294 /* is there a result? */
1295 if (typeof header.Result !== 'undefined') {
1296 moves.push(header.Result);
1299 /* history should be back to what is was before we started generating PGN,
1300 * so join together moves
1302 if (max_width === 0) {
1303 return result.join('') + moves.join(' ');
1306 /* wrap the PGN output at max_width */
1307 var current_width = 0;
1308 for (var i = 0; i < moves.length; i++) {
1309 /* if the current move will push past max_width */
1310 if (current_width + moves[i].length > max_width && i !== 0) {
1312 /* don't end the line with whitespace */
1313 if (result[result.length - 1] === ' ') {
1317 result.push(newline);
1319 } else if (i !== 0) {
1323 result.push(moves[i]);
1324 current_width += moves[i].length;
1327 return result.join('');
1330 load_pgn: function(pgn, options) {
1331 function mask(str) {
1332 return str.replace(/\\/g, '\\');
1335 /* convert a move from Standard Algebraic Notation (SAN) to 0x88
1338 function move_from_san(move) {
1339 var moves = generate_moves();
1340 for (var i = 0, len = moves.length; i < len; i++) {
1341 /* strip off any trailing move decorations: e.g Nf3+?! */
1342 if (move.replace(/[+#?!=]+$/,'') ==
1343 move_to_san(moves[i]).replace(/[+#?!=]+$/,'')) {
1350 function get_move_obj(move) {
1351 return move_from_san(trim(move));
1354 function has_keys(object) {
1355 var has_keys = false;
1356 for (var key in object) {
1362 function parse_pgn_header(header, options) {
1363 var newline_char = (typeof options === 'object' &&
1364 typeof options.newline_char === 'string') ?
1365 options.newline_char : '\r?\n';
1366 var header_obj = {};
1367 var headers = header.split(new RegExp(mask(newline_char)));
1371 for (var i = 0; i < headers.length; i++) {
1372 key = headers[i].replace(/^\[([A-Z][A-Za-z]*)\s.*\]$/, '$1');
1373 value = headers[i].replace(/^\[[A-Za-z]+\s"(.*)"\]$/, '$1');
1374 if (trim(key).length > 0) {
1375 header_obj[key] = value;
1382 var newline_char = (typeof options === 'object' &&
1383 typeof options.newline_char === 'string') ?
1384 options.newline_char : '\r?\n';
1385 var regex = new RegExp('^(\\[(.|' + mask(newline_char) + ')*\\])' +
1386 '(' + mask(newline_char) + ')*' +
1387 '1.(' + mask(newline_char) + '|.)*$', 'g');
1389 /* get header part of the PGN file */
1390 var header_string = pgn.replace(regex, '$1');
1392 /* no info part given, begins with moves */
1393 if (header_string[0] !== '[') {
1399 /* parse PGN header */
1400 var headers = parse_pgn_header(header_string, options);
1401 for (var key in headers) {
1402 set_header([key, headers[key]]);
1405 /* delete header to get the moves */
1406 var ms = pgn.replace(header_string, '').replace(new RegExp(mask(newline_char), 'g'), ' ');
1408 /* delete comments */
1409 ms = ms.replace(/(\{[^}]+\})+?/g, '');
1411 /* delete move numbers */
1412 ms = ms.replace(/\d+\./g, '');
1415 /* trim and get array of moves */
1416 var moves = trim(ms).split(new RegExp(/\s+/));
1418 /* delete empty entries */
1419 moves = moves.join(',').replace(/,,+/g, ',').split(',');
1422 for (var half_move = 0; half_move < moves.length - 1; half_move++) {
1423 move = get_move_obj(moves[half_move]);
1425 /* move not possible! (don't clear the board to examine to show the
1426 * latest valid position)
1435 /* examine last move */
1436 move = moves[moves.length - 1];
1437 if (POSSIBLE_RESULTS.indexOf(move) > -1) {
1438 if (has_keys(header) && typeof header.Result === 'undefined') {
1439 set_header(['Result', move]);
1443 move = get_move_obj(move);
1453 header: function() {
1454 return set_header(arguments);
1465 move: function(move) {
1466 /* The move function can be called with in the following parameters:
1468 * .move('Nxb7') <- where 'move' is a case-sensitive SAN string
1470 * .move({ from: 'h7', <- where the 'move' is a move object (additional
1471 * to :'h8', fields are ignored)
1475 var move_obj = null;
1476 var moves = generate_moves();
1478 if (typeof move === 'string') {
1479 /* convert the move string to a move object */
1480 for (var i = 0, len = moves.length; i < len; i++) {
1481 if (move === move_to_san(moves[i])) {
1482 move_obj = moves[i];
1486 } else if (typeof move === 'object') {
1487 /* convert the pretty move object to an ugly move object */
1488 for (var i = 0, len = moves.length; i < len; i++) {
1489 if (move.from === algebraic(moves[i].from) &&
1490 move.to === algebraic(moves[i].to) &&
1491 (!('promotion' in moves[i]) ||
1492 move.promotion === moves[i].promotion)) {
1493 move_obj = moves[i];
1499 /* failed to find move */
1504 /* need to make a copy of move because we can't generate SAN after the
1507 var pretty_move = make_pretty(move_obj);
1509 make_move(move_obj);
1515 var move = undo_move();
1516 return (move) ? make_pretty(move) : null;
1523 put: function(piece, square) {
1524 return put(piece, square);
1527 get: function(square) {
1531 remove: function(square) {
1532 return remove(square);
1535 perft: function(depth) {
1536 return perft(depth);
1539 square_color: function(square) {
1540 if (square in SQUARES) {
1541 var sq_0x88 = SQUARES[square];
1542 return ((rank(sq_0x88) + file(sq_0x88)) % 2 === 0) ? 'light' : 'dark';
1548 history: function(options) {
1549 var reversed_history = [];
1550 var move_history = [];
1551 var verbose = (typeof options !== 'undefined' && 'verbose' in options &&
1554 while (history.length > 0) {
1555 reversed_history.push(undo_move());
1558 while (reversed_history.length > 0) {
1559 var move = reversed_history.pop();
1561 move_history.push(make_pretty(move));
1563 move_history.push(move_to_san(move));
1568 return move_history;