-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxiangqi-0.1.3.min.js
6 lines (6 loc) · 13.8 KB
/
xiangqi-0.1.3.min.js
1
2
3
4
5
6
/* @license
* Copyright (c) 2019, lengyanyu258 ([email protected])
* Released under the BSD license
* https://github.com/lengyanyu258/xiangqi.js/blob/master/LICENSE
*/
"use strict";var Xiangqi=function(e){var l="b",p="r",t=-1,g="p",m="c",y="r",q="n",A="b",w="a",N="k",i="pcrnbakPCRNBAK",r="rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR r - - 0 1",v=Object.freeze(["1-0","0-1","1/2-1/2","*"]),R=Object.freeze({b:[16,-1,1],r:[-16,-1,1]}),k=Object.freeze({c:[-16,16,-1,1],r:[-16,16,-1,1],n:[-33,-31,31,33,-18,14,-14,18],b:[-34,34,30,-30],a:[-17,17,15,-15],k:[-16,16,-1,1]}),o=Object.freeze({NORMAL:"n",CAPTURE:"c"}),_=Object.freeze({NORMAL:1,CAPTURE:2}),O=Object.freeze({a9:0,b9:1,c9:2,d9:3,e9:4,f9:5,g9:6,h9:7,i9:8,a8:16,b8:17,c8:18,d8:19,e8:20,f8:21,g8:22,h8:23,i8:24,a7:32,b7:33,c7:34,d7:35,e7:36,f7:37,g7:38,h7:39,i7:40,a6:48,b6:49,c6:50,d6:51,e6:52,f6:53,g6:54,h6:55,i6:56,a5:64,b5:65,c5:66,d5:67,e5:68,f5:69,g5:70,h5:71,i5:72,a4:80,b4:81,c4:82,d4:83,e4:84,f4:85,g4:86,h4:87,i4:88,a3:96,b3:97,c3:98,d3:99,e3:100,f3:101,g3:102,h3:103,i3:104,a2:112,b2:113,c2:114,d2:115,e2:116,f2:117,g2:118,h2:119,i2:120,a1:128,b1:129,c1:130,d1:131,e1:132,f1:133,g1:134,h1:135,i1:136,a0:144,b0:145,c0:146,d0:147,e0:148,f0:149,g0:150,h0:151,i0:152}),C=new Array(256),f={r:t,b:t},P=p,a=0,c=1,h=[],d={};function b(e){void 0===e&&(e=!1),C=new Array(256),f={r:t,b:t},P=p,a=0,c=1,h=[],e||(d={}),L(U())}function j(){E(r)}function E(e,r){if(void 0===r&&(r=!1),!x(e).valid)return!1;var n=e.split(/\s+/),t=n[0],i=0;b(r);for(var o=0;o<t.length;++o){var u=t.charAt(o);if("/"===u)i+=7;else if(-1!=="0123456789".indexOf(u))i+=parseInt(u,10);else{var s=u<"a"?p:l;$({type:u.toLowerCase(),color:s},Q(i)),i++}}return P=n[1],a=parseInt(n[4],10),c=parseInt(n[5],10),L(U()),!0}function x(e){var r={0:"No errors.",1:"FEN string must contain six space-delimited fields.",2:"6th field (move number) must be a positive integer.",3:"5th field (half move counter) must be a non-negative integer.",4:"4th field (en-passant square) should be '-'.",5:"3rd field (castling availability) should be '-'.",6:"2nd field (side to move) is invalid.",7:"1st field (piece positions) does not contain 10 '/'-delimited rows.",8:"1st field (piece positions) is invalid [consecutive numbers].",9:"1st field (piece positions) is invalid [invalid piece].",10:"1st field (piece positions) is invalid [row too large].",11:"1st field (piece positions) is invalid [each side has one king].",12:"1st field (piece positions) is invalid [each side has no more than 2 advisers].",13:"1st field (piece positions) is invalid [each side has no more than 2 bishops].",14:"1st field (piece positions) is invalid [each side has no more than 2 knights].",15:"1st field (piece positions) is invalid [each side has no more than 2 rooks].",16:"1st field (piece positions) is invalid [each side has no more than 2 cannons].",17:"1st field (piece positions) is invalid [each side has no more than 5 pawns].",18:"1st field (piece positions) is invalid [king should at palace].",19:"1st field (piece positions) is invalid [red adviser should at right position].",20:"1st field (piece positions) is invalid [black adviser should at right position].",21:"1st field (piece positions) is invalid [red bishop should at right position].",22:"1st field (piece positions) is invalid [black bishop should at right position].",23:"1st field (piece positions) is invalid [red pawn should at right position].",24:"1st field (piece positions) is invalid [black pawn should at right position]."};function n(e){return{valid:0===e,error_number:e,error:r[e]}}var t=e.split(/\s+/);if(6!==t.length)return n(1);if(isNaN(t[5])||parseInt(t[5],10)<=0)return n(2);if(isNaN(t[4])||parseInt(t[4],10)<0)return n(3);if(!/^-$/.test(t[3]))return n(4);if(!/^-$/.test(t[2]))return n(5);if(!/^([rb])$/.test(t[1]))return n(6);var i=t[0].split("/");if(10!==i.length)return n(7);var o,u={p:{number:0,squares:[]},P:{number:0,squares:[]},c:{number:0,squares:[]},C:{number:0,squares:[]},r:{number:0,squares:[]},R:{number:0,squares:[]},n:{number:0,squares:[]},N:{number:0,squares:[]},b:{number:0,squares:[]},B:{number:0,squares:[]},a:{number:0,squares:[]},A:{number:0,squares:[]},k:{number:0,squares:[]},K:{number:0,squares:[]}};for(o=0;o<i.length;o++){for(var s=0,a=!1,f=0;f<i[o].length;f++)if(isNaN(i[o][f])){try{++u[i[o][f]].number}catch(e){return n(9)}u[i[o][f]].squares.push(9-o<<4|s),s+=1,a=!1}else{if(a)return n(8);s+=parseInt(i[o][f],10),a=!0}if(9!==s)return n(10)}if(1!==u.k.number||1!==u.K.number)return n(11);if(2<u.a.number||2<u.A.number)return n(12);if(2<u.b.number||2<u.B.number)return n(13);if(2<u.n.number||2<u.N.number)return n(14);if(2<u.r.number||2<u.R.number)return n(15);if(2<u.c.number||2<u.C.number)return n(16);if(5<u.p.number||5<u.P.number)return n(17);if(Y(N,u.k.squares[0],p)||Y(N,u.K.squares[0],l))return n(18);for(o in u.a.squares)if(Y(w,u.a.squares[o],p))return n(19);for(o in u.A.squares)if(Y(w,u.A.squares[o],l))return n(20);for(o in u.b.squares)if(Y(A,u.b.squares[o],p))return n(21);for(o in u.B.squares)if(Y(A,u.B.squares[o],l))return n(22);for(o in u.p.squares)if(Y(g,u.p.squares[o],p))return n(23);for(o in u.P.squares)if(Y(g,u.P.squares[o],l))return n(24);return n(0)}function U(){for(var e=0,r="",n=O.a9;n<=O.i0;++n){if(null==C[n])e++;else{0<e&&(r+=e,e=0);var t=C[n].color,i=C[n].type;r+=t===p?i.toUpperCase():i.toLowerCase()}8<=H(n)&&(0<e&&(r+=e),n!==O.i0&&(r+="/"),e=0,n+=7)}return[r,P,"-","-",a,c].join(" ")}function I(e){for(var r=0;r<e.length;r+=2)"string"==typeof e[r]&&"string"==typeof e[r+1]&&(d[e[r]]=e[r+1]);return d}function L(e){0<h.length||(e!==r?(d.SetUp="1",d.FEN=e):(delete d.SetUp,delete d.FEN))}function u(e){var r=C[O[e]];return r?{type:r.type,color:r.color}:null}function $(e,r){if(!("type"in e&&"color"in e))return!1;if(-1===i.indexOf(e.type.toLowerCase()))return!1;if(!(r in O))return!1;var n=O[r];return(e.type!==N||f[e.color]===t||f[e.color]===n)&&(C[n]={type:e.type,color:e.color},e.type===N&&(f[e.color]=n),L(U()),!0)}function B(e){function r(e,r,n,t,i){var o,u,s,a;r.push((a={color:P,from:u=n,to:s=t,flags:i,piece:(o=e)[u].type},o[s]&&(a.captured=o[s].type),a))}var n,t,i,o=[],u=P,s=V(u),a=O.a9,f=O.i0,l=!(void 0!==e&&"legal"in e)||e.legal;if(void 0!==e&&"square"in e){if(!(e.square in O))return[];a=f=O[e.square]}for(n=a;n<=f;++n){var p=C[n];if(null!=p&&p.color===u){var c=p.type===g?R[u]:k[p.type];for(t=0,i=c.length;t<i&&(!(p.type===g&&0<t)||W(n,u));++t)for(var h=c[t],v=n,d=!1;!(J(v+=h)||p.type===q&&ee(n,t)||p.type===A&&(void 0,[-17,17,15,-15],null!=C[n+[-17,17,15,-15][t]]||W(v,u))||(p.type===w||p.type===N)&&Y(p.type,v,u));){if(null==C[v]){if(p.type===m&&d)continue;r(C,o,n,v,_.NORMAL)}else{if(p.type!==m){C[v].color===s&&r(C,o,n,v,_.CAPTURE);break}if(d){C[v].color===s&&r(C,o,n,v,_.CAPTURE);break}d=!0}if(p.type!==m&&p.type!==y)break}8<=H(n)&&(n+=7)}}if(!l)return o;var b=[];for(n=0,i=o.length;n<i;n++)X(o[n]),S(u)||b.push(o[n]),G();return b}function K(e,r){return Q(e.from)+Q(e.to)}function z(e){return e.replace(/=/,"").replace(/[+#]?[?!]*$/,"")}function S(e){var r,n,t,i=f[e],o=V(e);for(r=0,n=k[q].length;r<n;++r)if(t=i+k[q][r],null!=C[t]&&!J(t)&&C[t].color===o&&C[t].type===q&&!ee(t,r<4?3-r:3-r+8))return!0;for(r=0,n=k[y].length;r<n;++r){var u=k[y][r],s=!1;for(t=i+u;!J(t);t+=u){var a=C[t];if(null!=a){if(a.color===o)if(s){if(a.type===m)return!0}else if(a.type===y||a.type===N)return!0;if(s)break;s=!0}}}for(r=0,n=R[o].length;r<n;++r)if(t=i-R[o][r],null!=C[t]&&!J(t)&&C[t].color===o&&C[t].type===g)return!0;return!1}function n(){return S(P)}function s(){return n()&&0===B().length}function T(){return!n()&&0===B().length}function F(){var e={},r=0;for(var n in O){var t=C[O[n]];t&&(e[t.type]=t.type in e?e[t.type]+1:1,r++)}return 2===r||void 0===e[q]&&void 0===e[y]&&void 0===e[m]&&void 0===e[g]}function M(){for(var e=[],r={},n=!1;;){var t=G();if(!t)break;e.push(t)}for(;;){var i=U().split(" ").slice(0,2).join(" ");if(r[i]=i in r?r[i]+1:1,3<=r[i]&&(n=!0),!e.length)break;X(e.pop())}return n}function X(e){var r;r=e,h.push({move:r,kings:{b:f.b,r:f.r},turn:P,half_moves:a,move_number:c}),null!=C[e.to]&&C[e.to].type===N&&(f[C[e.to].color]=t),C[e.to]=C[e.from],C[e.from]=null,C[e.to].type===N&&(f[C[e.to].color]=e.to),e.flags&_.CAPTURE?a=0:a++,P===l&&c++,P=V(P)}function G(){var e=h.pop();if(null==e)return null;var r=e.move;f=e.kings,P=e.turn,a=e.half_moves,c=e.move_number;var n=V(P);return C[r.from]=C[r.to],C[r.from].type=r.piece,C[r.to]=null,0<(r.flags&_.CAPTURE)&&(C[r.to]={type:r.captured,color:n}),r}function Z(e,r){var n,t,i,o=z(e),u=o.match(/([a-i][0-9])-?([a-i][0-9])/);r&&u&&(n=u[1],t=u[2],i=u[3]);for(var s=B(),a=0,f=s.length;a<f;a++){if(o===z(K(s[a]))||r&&o===z(K(s[a])))return s[a];if(u&&(!n||n.toLowerCase()===s[a].piece)&&O[t]===s[a].from&&O[i]===s[a].to)return s[a]}return null}function D(e){return e>>4}function H(e){return 15&e}function Q(e){var r=H(e),n=D(e);return"abcdefghi".substring(r,r+1)+"9876543210".substring(n,n+1)}function V(e){return e===p?l:p}function W(e,r){return r===p?D(e)<5:4<D(e)}function J(e){return e<0||9<D(e)||8<H(e)}function Y(e,r,n){var t={};return e===g?(t=[0,2,4,6,8],n===p?6<D(r)||4<D(r)&&-1===t.indexOf(H(r)):D(r)<3||D(r)<5&&-1===t.indexOf(H(r))):(e===A?(t[p]=[146,150,112,116,120,82,86],t[l]=[2,6,32,36,40,66,70]):e===w?(t[p]=[147,149,132,115,117],t[l]=[3,5,20,35,37]):e===N&&(t[p]=[147,148,149,131,132,133,115,116,117],t[l]=[3,4,5,19,20,21,35,36,37]),-1===t[n].indexOf(r))}function ee(e,r){return null!=C[e+[-16,16,-1,1][Math.floor(r/2)]]}function re(e){var r=function e(r){var n=r instanceof Array?[]:{};for(var t in r)n[t]="object"==typeof t?e(r[t]):r[t];return n}(e);r.iccs=K(r),r.to=Q(r.to),r.from=Q(r.from);var n="";for(var t in _)0<(_[t]&r.flags)&&(n+=o[t]);return r.flags=n,r}function ne(e){return e.replace(/^\s+|\s+$/g,"")}return E(void 0===e?r:e),{RED:p,BLACK:l,PAWN:g,CANNON:m,ROOK:y,KNIGHT:q,BISHOP:A,ADVISER:w,KING:N,SQUARES:function(){for(var e=[],r=O.a9;r<=O.i0;r++)9!==H(r)?e.push(Q(r)):r+=6;return e}(),FLAGS:o,load:function(e){return E(e)},reset:function(){return j()},moves:function(e){for(var r=B(e),n=[],t=0,i=r.length;t<i;t++)void 0!==e&&"verbose"in e&&e.verbose?n.push(re(r[t])):n.push(K(r[t]));return n},in_check:function(){return n()},in_checkmate:function(){return s()},in_stalemate:function(){return T()},in_draw:function(){return 120<=a||F()},insufficient_material:function(){return F()},in_threefold_repetition:function(){return M()},game_over:function(){return 120<=a||s()||T()||F()||M()||f[V(P)]===t},validate_fen:function(e){return x(e)},fen:function(){return U()},board:function(){for(var e=[],r=[],n=O.a9;n<=O.i0;n++)null==C[n]?r.push(null):r.push({type:C[n].type,color:C[n].color}),8&n&&(e.push(r),r=[],n+=7);return e},pgn:function(e){var r,n="object"==typeof e&&"string"==typeof e.newline_char?e.newline_char:"\n",t="object"==typeof e&&"number"==typeof e.max_width?e.max_width:0,i=[],o=!1;for(r in d)i.push("["+r+' "'+d[r]+'"]'+n),o=!0;o&&h.length&&i.push(n);for(var u=[];0<h.length;)u.push(G());for(var s=[],a="";0<u.length;){var f=u.pop();h.length||"b"!==f.color?"b"!==f.color&&(a.length&&s.push(a),a=c+"."):a=c+". ...",a=a+" "+K(f),X(f)}if(a.length&&s.push(a),void 0!==d.Result&&s.push(d.Result),0===t)return i.join("")+s.join(" ");var l=0;for(r=0;r<s.length;r++)l+s[r].length>t&&0!==r?(" "===i[i.length-1]&&i.pop(),i.push(n),l=0):0!==r&&(i.push(" "),l++),i.push(s[r]),l+=s[r].length;return i.join("")},load_pgn:function(e,r){var n=void 0!==r&&"sloppy"in r&&r.sloppy;function a(e){return e.replace(/\\/g,"\\")}var t="object"==typeof r&&"string"==typeof r.newline_char?r.newline_char:"\r?\n",i=new RegExp("^(\\[((?:"+a(t)+")|.)*\\])(?:"+a(t)+"){2}"),o=i.test(e)?i.exec(e)[1]:"";j();var u=function(e,r){for(var n="object"==typeof r&&"string"==typeof r.newline_char?r.newline_char:"\r?\n",t={},i=e.split(new RegExp(a(n))),o="",u="",s=0;s<i.length;s++)o=i[s].replace(/^\[([A-Z][A-Za-z]*)\s.*\]$/,"$1"),u=i[s].replace(/^\[[A-Za-z]+\s"(.*)"\]$/,"$1"),0<ne(o).length&&(t[o]=u);return t}(o,r);for(var s in u)u.hasOwnProperty(s)&&I([s,u[s]]);if("1"===u.SetUp&&!("FEN"in u&&E(u.FEN,!0)))return!1;var f=e.replace(o,"").replace(new RegExp(a(t),"g")," ");f=f.replace(/({[^}]+})+?/g,"");for(var l=/(\([^()]+\))+?/g;l.test(f);)f=f.replace(l,"");var p=ne(f=(f=(f=f.replace(/\d+\.(\.\.)?/g,"")).replace(/\.\.\./g,"")).replace(/\$\d+/g,"")).split(new RegExp(/\s+/));p=p.join(",").replace(/,,+/g,",").split(",");for(var c="",h=0;h<p.length-1;h++){if(null==(c=Z(p[h],n)))return!1;X(c)}if(c=p[p.length-1],-1<v.indexOf(c))(function(e){for(var r in e)if(e.hasOwnProperty(r))return!0;return!1})(d)&&void 0===d.Result&&I(["Result",c]);else{if(null==(c=Z(c,n)))return!1;X(c)}return!0},header:function(){return I(arguments)},ascii:function(){return function(){for(var e=" +---------------------------+\n",r=O.a9;r<=O.i0;r++){if(0===H(r)&&(e+=" "+"9876543210"[D(r)]+" |"),null==C[r])e+=" . ";else{var n=C[r].type;e+=" "+(C[r].color===p?n.toUpperCase():n.toLowerCase())+" "}8&r&&(e+="|\n",r+=7)}return e+=" +---------------------------+\n",e+=" a b c d e f g h i\n"}()},turn:function(){return P},move:function(e,r){void 0!==r&&"sloppy"in r&&r.sloppy;var n=null;if("string"==typeof e)n=function(e){for(var r=B(),n=0,t=r.length;n<t;n++)if(e===K(r[n]))return r[n];return null}(e);else if("object"==typeof e)for(var t=B(),i=0,o=t.length;i<o;i++)if(e.from===Q(t[i].from)&&e.to===Q(t[i].to)&&!(""in t[i])){n=t[i];break}if(!n)return null;var u=re(n);return X(n),u},undo:function(){var e=G();return e?re(e):null},clear:function(){return b()},put:function(e,r){return $(e,r)},get:function(e){return u(e)},remove:function(e){return n=u(r=e),C[O[r]]=null,n&&n.type===N&&(f[n.color]=t),L(U()),n;var r,n},perft:function(e){return function e(r){for(var n=B({legal:!1}),t=0,i=0,o=n.length;i<o;i++)X(n[i]),S(P)||(0<r-1?t+=e(r-1):t++),G();return t}(e)},history:function(e){for(var r=[],n=[],t=(void 0!==e&&"verbose"in e&&e.verbose);0<h.length;)r.push(G());for(;0<r.length;){var i=r.pop();t?n.push(re(i)):n.push(K(i)),X(i)}return n}}};"undefined"!=typeof exports&&(exports.Xiangqi=Xiangqi),"undefined"!=typeof define&&define(function(){return Xiangqi});