js-chess-engine 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,12 +8,12 @@ jobs:
8
8
 
9
9
  strategy:
10
10
  matrix:
11
- node-version: [12.x, 14.x]
11
+ node-version: [18.x, 20.x]
12
12
 
13
13
  steps:
14
14
  - uses: actions/checkout@v2
15
15
  - name: Use Node.js ${{ matrix.node-version }}
16
- uses: actions/setup-node@v1
16
+ uses: actions/setup-node@v2
17
17
  with:
18
18
  node-version: ${{ matrix.node-version }}
19
19
  - run: npm install
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 20
package/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ ## [1.0.3](https://github.com/josefjadrny/js-chess-engine/compare/v1.0.2...v1.0.3) (2025-09-01)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Add nodejs 20 support ([559244d](https://github.com/josefjadrny/js-chess-engine/commit/559244d6cd4c67fea15e6bfe6b55c862e728cd62))
7
+ * getHistory() check property consistently returning false ([#31](https://github.com/josefjadrny/js-chess-engine/issues/31)) ([05845d1](https://github.com/josefjadrny/js-chess-engine/commit/05845d1506ca377ad7dcbc46afe2bc88608cd127)), closes [#28](https://github.com/josefjadrny/js-chess-engine/issues/28)
8
+
9
+
10
+
11
+ ## [1.0.2](https://github.com/josefjadrny/js-chess-engine/compare/v1.0.1...v1.0.2) (2021-10-24)
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * Fixed FEN parsing ([#23](https://github.com/josefjadrny/js-chess-engine/issues/23)) ([14ebe50](https://github.com/josefjadrny/js-chess-engine/commit/14ebe506e300e2a22fed81696f76ef117c00c2d7))
17
+
18
+
19
+
1
20
  ## [1.0.1](https://github.com/josefjadrny/js-chess-engine/compare/v0.11.3...v1.0.1) (2021-10-16)
2
21
 
3
22
 
@@ -9,7 +28,7 @@
9
28
 
10
29
  # [1.0.0](https://github.com/josefjadrny/js-chess-engine/compare/v0.11.3...v1.0.0) (2021-10-13)
11
30
 
12
- FIX
31
+
13
32
 
14
33
  ## [0.11.3](https://github.com/josefjadrny/js-chess-engine/compare/v0.11.2...v0.11.3) (2021-03-29)
15
34
 
package/README.md CHANGED
@@ -27,7 +27,7 @@ game.printToConsole()
27
27
  ```
28
28
  more about [importing](#import) this library.
29
29
  ## Examples
30
- **js-chess-engine-app** - React application example with js-chess-engine REST API backend (without persistent storage) - [GitHub](https://github.com/josefjadrny/js-chess-engine-app) or [LIVE DEMO](http://chess.nadsenyvyvojar.cz/)
30
+ **js-chess-engine-app** - React application example with js-chess-engine REST API backend (without persistent storage) - [GitHub](https://github.com/josefjadrny/js-chess-engine-app) or [LIVE DEMO](http://chess.josefjadrny.info/)
31
31
 
32
32
  **More examples**<BR/>
33
33
  [Simple Fastify server](example/server.mjs) <BR/>
@@ -312,6 +312,14 @@ When a move is recognized as a castling - played with a king across two chess fi
312
312
 
313
313
  <BR/>
314
314
 
315
+ ## Collaboration
316
+
317
+ **Collaborators are welcome.** Please do not forget to check ESLint and run tests before submitting a new pull request (`npm run test`).
318
+
319
+ If it is possible, also use commit message prefixes like `feat: ` or `fix: ` - changelog is generated from this.
320
+
321
+ <BR/>
322
+
315
323
  ## TODO
316
324
  - Calculation and result caching
317
325
  - Forsyth–Edwards Notation (FEN) validation
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("js-chess-engine",[],e):"object"==typeof exports?exports["js-chess-engine"]=e():t["js-chess-engine"]=e()}(this,(function(){return function(t){var e={};function i(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,i),o.l=!0,o.exports}return i.m=t,i.c=e,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)i.d(n,o,function(e){return t[e]}.bind(null,o));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){"use strict";i.r(e),i.d(e,"Game",(function(){return $})),i.d(e,"moves",(function(){return J})),i.d(e,"status",(function(){return V})),i.d(e,"getFen",(function(){return Y})),i.d(e,"move",(function(){return z})),i.d(e,"aiMove",(function(){return X}));const n=["A","B","C","D","E","F","G","H"],o=["1","2","3","4","5","6","7","8"],s={KING_W:"K",QUEEN_W:"Q",ROOK_W:"R",BISHOP_W:"B",KNIGHT_W:"N",PAWN_W:"P",KING_B:"k",QUEEN_B:"q",ROOK_B:"r",BISHOP_B:"b",KNIGHT_B:"n",PAWN_B:"p"},r="black",c="white",l=[0,1,2,3,4],a={0:1,1:2,2:2,3:3,4:3,5:4},u={0:2,1:2,2:4,3:4,4:5,5:5},h={fullMove:1,halfMove:0,enPassant:null,isFinished:!1,checkMate:!1,check:!1,turn:c},g=Object.assign({pieces:{E1:"K",D1:"Q",A1:"R",H1:"R",C1:"B",F1:"B",B1:"N",G1:"N",A2:"P",B2:"P",C2:"P",D2:"P",E2:"P",F2:"P",G2:"P",H2:"P",E8:"k",D8:"q",A8:"r",H8:"r",C8:"b",F8:"b",B8:"n",G8:"n",A7:"p",B7:"p",C7:"p",D7:"p",E7:"p",F7:"p",G7:"p",H7:"p"},castling:{whiteShort:!0,blackShort:!0,whiteLong:!0,blackLong:!0}},h),f={UP:{A1:"A2",A2:"A3",A3:"A4",A4:"A5",A5:"A6",A6:"A7",A7:"A8",A8:null,B1:"B2",B2:"B3",B3:"B4",B4:"B5",B5:"B6",B6:"B7",B7:"B8",B8:null,C1:"C2",C2:"C3",C3:"C4",C4:"C5",C5:"C6",C6:"C7",C7:"C8",C8:null,D1:"D2",D2:"D3",D3:"D4",D4:"D5",D5:"D6",D6:"D7",D7:"D8",D8:null,E1:"E2",E2:"E3",E3:"E4",E4:"E5",E5:"E6",E6:"E7",E7:"E8",E8:null,F1:"F2",F2:"F3",F3:"F4",F4:"F5",F5:"F6",F6:"F7",F7:"F8",F8:null,G1:"G2",G2:"G3",G3:"G4",G4:"G5",G5:"G6",G6:"G7",G7:"G8",G8:null,H1:"H2",H2:"H3",H3:"H4",H4:"H5",H5:"H6",H6:"H7",H7:"H8",H8:null},DOWN:{A1:null,A2:"A1",A3:"A2",A4:"A3",A5:"A4",A6:"A5",A7:"A6",A8:"A7",B1:null,B2:"B1",B3:"B2",B4:"B3",B5:"B4",B6:"B5",B7:"B6",B8:"B7",C1:null,C2:"C1",C3:"C2",C4:"C3",C5:"C4",C6:"C5",C7:"C6",C8:"C7",D1:null,D2:"D1",D3:"D2",D4:"D3",D5:"D4",D6:"D5",D7:"D6",D8:"D7",E1:null,E2:"E1",E3:"E2",E4:"E3",E5:"E4",E6:"E5",E7:"E6",E8:"E7",F1:null,F2:"F1",F3:"F2",F4:"F3",F5:"F4",F6:"F5",F7:"F6",F8:"F7",G1:null,G2:"G1",G3:"G2",G4:"G3",G5:"G4",G6:"G5",G7:"G6",G8:"G7",H1:null,H2:"H1",H3:"H2",H4:"H3",H5:"H4",H6:"H5",H7:"H6",H8:"H7"},LEFT:{A1:null,A2:null,A3:null,A4:null,A5:null,A6:null,A7:null,A8:null,B1:"A1",B2:"A2",B3:"A3",B4:"A4",B5:"A5",B6:"A6",B7:"A7",B8:"A8",C1:"B1",C2:"B2",C3:"B3",C4:"B4",C5:"B5",C6:"B6",C7:"B7",C8:"B8",D1:"C1",D2:"C2",D3:"C3",D4:"C4",D5:"C5",D6:"C6",D7:"C7",D8:"C8",E1:"D1",E2:"D2",E3:"D3",E4:"D4",E5:"D5",E6:"D6",E7:"D7",E8:"D8",F1:"E1",F2:"E2",F3:"E3",F4:"E4",F5:"E5",F6:"E6",F7:"E7",F8:"E8",G1:"F1",G2:"F2",G3:"F3",G4:"F4",G5:"F5",G6:"F6",G7:"F7",G8:"F8",H1:"G1",H2:"G2",H3:"G3",H4:"G4",H5:"G5",H6:"G6",H7:"G7",H8:"G8"},RIGHT:{A1:"B1",A2:"B2",A3:"B3",A4:"B4",A5:"B5",A6:"B6",A7:"B7",A8:"B8",B1:"C1",B2:"C2",B3:"C3",B4:"C4",B5:"C5",B6:"C6",B7:"C7",B8:"C8",C1:"D1",C2:"D2",C3:"D3",C4:"D4",C5:"D5",C6:"D6",C7:"D7",C8:"D8",D1:"E1",D2:"E2",D3:"E3",D4:"E4",D5:"E5",D6:"E6",D7:"E7",D8:"E8",E1:"F1",E2:"F2",E3:"F3",E4:"F4",E5:"F5",E6:"F6",E7:"F7",E8:"F8",F1:"G1",F2:"G2",F3:"G3",F4:"G4",F5:"G5",F6:"G6",F7:"G7",F8:"G8",G1:"H1",G2:"H2",G3:"H3",G4:"H4",G5:"H5",G6:"H6",G7:"H7",G8:"H8",H1:null,H2:null,H3:null,H4:null,H5:null,H6:null,H7:null,H8:null},UP_LEFT:{A1:null,A2:null,A3:null,A4:null,A5:null,A6:null,A7:null,A8:null,B1:"A2",B2:"A3",B3:"A4",B4:"A5",B5:"A6",B6:"A7",B7:"A8",B8:null,C1:"B2",C2:"B3",C3:"B4",C4:"B5",C5:"B6",C6:"B7",C7:"B8",C8:null,D1:"C2",D2:"C3",D3:"C4",D4:"C5",D5:"C6",D6:"C7",D7:"C8",D8:null,E1:"D2",E2:"D3",E3:"D4",E4:"D5",E5:"D6",E6:"D7",E7:"D8",E8:null,F1:"E2",F2:"E3",F3:"E4",F4:"E5",F5:"E6",F6:"E7",F7:"E8",F8:null,G1:"F2",G2:"F3",G3:"F4",G4:"F5",G5:"F6",G6:"F7",G7:"F8",G8:null,H1:"G2",H2:"G3",H3:"G4",H4:"G5",H5:"G6",H6:"G7",H7:"G8",H8:null},DOWN_RIGHT:{A1:null,A2:"B1",A3:"B2",A4:"B3",A5:"B4",A6:"B5",A7:"B6",A8:"B7",B1:null,B2:"C1",B3:"C2",B4:"C3",B5:"C4",B6:"C5",B7:"C6",B8:"C7",C1:null,C2:"D1",C3:"D2",C4:"D3",C5:"D4",C6:"D5",C7:"D6",C8:"D7",D1:null,D2:"E1",D3:"E2",D4:"E3",D5:"E4",D6:"E5",D7:"E6",D8:"E7",E1:null,E2:"F1",E3:"F2",E4:"F3",E5:"F4",E6:"F5",E7:"F6",E8:"F7",F1:null,F2:"G1",F3:"G2",F4:"G3",F5:"G4",F6:"G5",F7:"G6",F8:"G7",G1:null,G2:"H1",G3:"H2",G4:"H3",G5:"H4",G6:"H5",G7:"H6",G8:"H7",H1:null,H2:null,H3:null,H4:null,H5:null,H6:null,H7:null,H8:null},UP_RIGHT:{A1:"B2",A2:"B3",A3:"B4",A4:"B5",A5:"B6",A6:"B7",A7:"B8",A8:null,B1:"C2",B2:"C3",B3:"C4",B4:"C5",B5:"C6",B6:"C7",B7:"C8",B8:null,C1:"D2",C2:"D3",C3:"D4",C4:"D5",C5:"D6",C6:"D7",C7:"D8",C8:null,D1:"E2",D2:"E3",D3:"E4",D4:"E5",D5:"E6",D6:"E7",D7:"E8",D8:null,E1:"F2",E2:"F3",E3:"F4",E4:"F5",E5:"F6",E6:"F7",E7:"F8",E8:null,F1:"G2",F2:"G3",F3:"G4",F4:"G5",F5:"G6",F6:"G7",F7:"G8",F8:null,G1:"H2",G2:"H3",G3:"H4",G4:"H5",G5:"H6",G6:"H7",G7:"H8",G8:null,H1:null,H2:null,H3:null,H4:null,H5:null,H6:null,H7:null,H8:null},DOWN_LEFT:{A1:null,A2:null,A3:null,A4:null,A5:null,A6:null,A7:null,A8:null,B1:null,B2:"A1",B3:"A2",B4:"A3",B5:"A4",B6:"A5",B7:"A6",B8:"A7",C1:null,C2:"B1",C3:"B2",C4:"B3",C5:"B4",C6:"B5",C7:"B6",C8:"B7",D1:null,D2:"C1",D3:"C2",D4:"C3",D5:"C4",D6:"C5",D7:"C6",D8:"C7",E1:null,E2:"D1",E3:"D2",E4:"D3",E5:"D4",E6:"D5",E7:"D6",E8:"D7",F1:null,F2:"E1",F3:"E2",F4:"E3",F5:"E4",F6:"E5",F7:"E6",F8:"E7",G1:null,G2:"F1",G3:"F2",G4:"F3",G5:"F4",G6:"F5",G7:"F6",G8:"F7",H1:null,H2:"G1",H3:"G2",H4:"G3",H5:"G4",H6:"G5",H7:"G6",H8:"G7"}},C=[[0,0,0,0,0,0,0,0],[5,5,5,5,5,5,5,5],[1,1,2,3,3,2,1,1],[.5,.5,1,2.5,2.5,1,.5,.5],[0,0,0,2,2,0,0,0],[.5,0,1,0,0,1,0,.5],[.5,0,0,-2,-2,0,0,.5],[0,0,0,0,0,0,0,0]],P=[[-4,-3,-2,-2,-2,-2,-3,-4],[-3,-2,0,0,0,0,-2,-3],[-2,0,1,1.5,1.5,1,0,-2],[-2,.5,1.5,2,2,1.5,.5,-2],[-2,0,1.5,2,2,1.5,0,-2],[-2,.5,1,1.5,1.5,1,.5,-2],[-3,-2,0,.5,.5,0,-2,-3],[-4,-3,-2,-2,-2,-2,-3,-4]],p=[[-2,-1,-1,-1,-1,-1,-1,-2],[-1,0,0,0,0,0,0,-1],[-1,0,.5,1,1,.5,0,-1],[-1,.5,.5,1,1,.5,.5,-1],[-1,0,1,1,1,1,0,-1],[-1,1,1,1,1,1,1,-1],[-1,.5,0,0,0,0,.5,-1],[-2,-1,-1,-1,-1,-1,-1,-2]],E=[[0,0,0,0,0,0,0,0],[.5,1,1,1,1,1,1,.5],[-.5,0,0,0,0,0,0,-.5],[-.5,0,0,0,0,0,0,-.5],[-.5,0,0,0,0,0,0,-.5],[-.5,0,0,0,0,0,0,-.5],[-.5,0,0,0,0,0,0,-.5],[0,0,0,.5,.5,0,0,0]],B=[[-3,-4,-4,-5,-5,-4,-4,-3],[-3,-4,-4,-5,-5,-4,-4,-3],[-3,-4,-4,-5,-5,-4,-4,-3],[-3,-4,-4,-5,-5,-4,-4,-3],[-2,-3,-3,-4,-4,-3,-3,-2],[-1,-2,-2,-2,-2,-2,-2,-1],[2,2,0,0,0,0,2,2],[2,3,1,0,0,1,3,2]],F=[[-2,-1,-1,-.5,-.5,-1,-1,-2],[-1,0,0,0,0,0,0,-1],[-1,0,.5,.5,.5,.5,0,-1],[-.5,0,.5,.5,.5,.5,0,-.5],[0,0,.5,.5,.5,.5,0,-.5],[-1,.5,.5,.5,.5,.5,0,-1],[-1,0,.5,0,0,0,0,-1],[-2,-1,-1,-.5,-.5,-1,-1,-2]],G={P:C.slice().reverse(),p:C,N:P.slice().reverse(),n:P,B:p.slice().reverse(),b:p,R:E.slice().reverse(),r:E,K:B.slice().reverse(),k:B,Q:F.slice().reverse(),q:F};function D(t){return f.UP[t]}function A(t){return f.DOWN[t]}function H(t){return f.LEFT[t]}function b(t){return f.RIGHT[t]}function d(t){return f.UP_LEFT[t]}function k(t){return f.UP_RIGHT[t]}function v(t){return f.DOWN_LEFT[t]}function y(t){return f.DOWN_RIGHT[t]}function w(t){const e=d(t);return e?D(e):null}function O(t){const e=d(t);return e?H(e):null}function L(t){const e=k(t);return e?D(e):null}function M(t){const e=k(t);return e?b(e):null}function m(t){const e=v(t);return e?A(e):null}function K(t){const e=v(t);return e?H(e):null}function N(t){const e=y(t);return e?A(e):null}function S(t){const e=y(t);return e?b(e):null}function j(t,e){return e===c?f.UP[t]:f.DOWN[t]}function _(t,e){return e===c?f.UP_LEFT[t]:f.DOWN_RIGHT[t]}function R(t,e){return e===c?f.UP_RIGHT[t]:f.DOWN_LEFT[t]}function W(t,e){return e===c?f.DOWN_LEFT[t]:f.UP_RIGHT[t]}function U(t,e){return e===c?f.DOWN_RIGHT[t]:f.UP_LEFT[t]}function T(t){let e=0;switch(t){case"K":e=10;break;case"Q":e=9;break;case"R":e=5;break;case"B":case"N":e=3;break;case"P":e=1;break;case"k":e=10;break;case"q":e=9;break;case"r":e=5;break;case"b":case"n":e=3;break;case"p":e=1}return e}function I(t){return"string"==typeof t&&t.match("^[a-hA-H]{1}[1-8]{1}$")}const Q=-1e3,x=1e3;class q{constructor(t=JSON.parse(JSON.stringify(g))){if("object"==typeof t)this.configuration=Object.assign({},h,t);else{if("string"!=typeof t)throw new Error(`Unknown configuration type ${typeof config}.`);this.configuration=Object.assign({},h,function(t=""){const e={pieces:{}},i=t.split(" "),s=i[0],l=i[1],a=i[2],u=i[3],h=i[4],g=i[5];let f=0,C=o.length-1;for(let t=0;t<s.length;t++)["K","Q","R","B","N","P","k","q","r","b","n","p"].includes(s[t])?(e.pieces[`${n[f]}${o[C]}`]=s[t],f++):["1","2","3","4","5","6","7","8"].includes(s[t])?f+=parseInt(s[t]):"/"===s[t]&&(C--,f=0);return e.turn="b"===l?r:c,e.castling={whiteLong:!1,whiteShort:!1,blackLong:!1,blackShort:!1},a.includes("K")&&(e.castling.whiteShort=!0),a.includes("k")&&(e.castling.blackShort=!0),a.includes("Q")&&(e.castling.whiteLong=!0),a.includes("q")&&(e.castling.blackLong=!0),I(u)&&(e.enPassant=u.toUpperCase()),e.halfMove=parseInt(h),e.fullMove=parseInt(g),e}(t))}this.configuration.castling||(this.configuration.castling={whiteShort:!0,blackShort:!0,whiteLong:!0,blackLong:!0}),this.history=[]}getAttackingFields(t=this.getPlayingColor()){let e=[];for(const i in this.configuration.pieces){const n=this.getPiece(i);this.getPieceColor(n)===t&&(e=[...e,...this.getPieceMoves(n,i)])}return e}isAttackingKing(t=this.getPlayingColor()){let e=null;for(const i in this.configuration.pieces){const n=this.getPiece(i);if(this.isKing(n)&&this.getPieceColor(n)!==t){e=i;break}}return this.isPieceUnderAttack(e)}isPieceUnderAttack(t){const e=this.getPieceOnLocationColor(t),i=this.getEnemyColor(e);let n=!1,o=t,s=0;for(;D(o)&&!n;){o=D(o),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isRook(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;A(o)&&!n;){o=A(o),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isRook(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;H(o)&&!n;){o=H(o),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isRook(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;b(o)&&!n;){o=b(o),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isRook(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;R(o,e)&&!n;){o=R(o,e),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isBishop(t)||this.isQueen(t)||1===s&&(this.isKing(t)||this.isPawn(t)))&&(n=!0),t)break}for(o=t,s=0;_(o,e)&&!n;){o=_(o,e),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isBishop(t)||this.isQueen(t)||1===s&&(this.isKing(t)||this.isPawn(t)))&&(n=!0),t)break}for(o=t,s=0;U(o,e)&&!n;){o=U(o,e),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isBishop(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;W(o,e)&&!n;){o=W(o,e),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isBishop(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}o=L(t);let r=this.getPiece(o);return r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=M(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=O(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=w(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=m(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=K(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=N(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=S(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),n}hasPlayingPlayerCheck(){return this.isAttackingKing(this.getNonPlayingColor())}hasNonPlayingPlayerCheck(){return this.isAttackingKing(this.getPlayingColor())}getLowestValuePieceAttackingLocation(t,e=this.getPlayingColor()){let i=null;for(const n in this.configuration.pieces){const o=this.getPiece(n);this.getPieceColor(o)===e&&this.getPieceMoves(o,n).map(e=>{e===t&&(null===i||T(o)<i)&&(i=T(o))})}return i}getMoves(t=this.getPlayingColor(),e=null){const i={};let n=0;for(const e in this.configuration.pieces){const o=this.getPiece(e);if(this.getPieceColor(o)===t){const t=this.getPieceMoves(o,e);t.length&&n++,Object.assign(i,{[e]:t})}}const o=this.getAttackingFields(this.getNonPlayingColor());if(this.isLeftCastlingPossible(o)&&(this.isPlayingWhite()&&i.E1.push("C1"),this.isPlayingBlack()&&i.E8.push("C8")),this.isRightCastlingPossible(o)&&(this.isPlayingWhite()&&i.E1.push("G1"),this.isPlayingBlack()&&i.E8.push("G8")),e&&n>e)return i;const s={};for(const t in i)i[t].map(e=>{const i={pieces:Object.assign({},this.configuration.pieces),castling:Object.assign({},this.configuration.castling)},n=new q(i);n.move(t,e),(this.isPlayingWhite()&&!n.isAttackingKing(r)||this.isPlayingBlack()&&!n.isAttackingKing(c))&&(s[t]||(s[t]=[]),s[t].push(e))});return Object.keys(s).length||(this.configuration.isFinished=!0,this.hasPlayingPlayerCheck()&&(this.configuration.checkMate=!0)),s}isLeftCastlingPossible(t){if(this.isPlayingWhite()&&!this.configuration.castling.whiteLong)return!1;if(this.isPlayingBlack()&&!this.configuration.castling.blackLong)return!1;let e=null;if(this.isPlayingWhite()&&"K"===this.getPiece("E1")&&"R"===this.getPiece("A1")&&!t.includes("E1")?e="E1":this.isPlayingBlack()&&"k"===this.getPiece("E8")&&"r"===this.getPiece("A8")&&!t.includes("E8")&&(e="E8"),!e)return!1;let i=H(e);return!this.getPiece(i)&&!t.includes(i)&&(i=H(i),!this.getPiece(i)&&!t.includes(i)&&(i=H(i),!this.getPiece(i)))}isRightCastlingPossible(t){if(this.isPlayingWhite()&&!this.configuration.castling.whiteShort)return!1;if(this.isPlayingBlack()&&!this.configuration.castling.blackShort)return!1;let e=null;if(this.isPlayingWhite()&&"K"===this.getPiece("E1")&&"R"===this.getPiece("H1")&&!t.includes("E1")?e="E1":this.isPlayingBlack()&&"k"===this.getPiece("E8")&&"r"===this.getPiece("H8")&&!t.includes("E8")&&(e="E8"),!e)return!1;let i=b(e);return!this.getPiece(i)&&!t.includes(i)&&(i=b(i),!this.getPiece(i)&&!t.includes(i))}getPieceMoves(t,e){return this.isPawn(t)?this.getPawnMoves(t,e):this.isKnight(t)?this.getKnightMoves(t,e):this.isRook(t)?this.getRookMoves(t,e):this.isBishop(t)?this.getBishopMoves(t,e):this.isQueen(t)?this.getQueenMoves(t,e):this.isKing(t)?this.getKingMoves(t,e):[]}isPawn(t){return"P"===t.toUpperCase()}isKnight(t){return"N"===t.toUpperCase()}isRook(t){return"R"===t.toUpperCase()}isBishop(t){return"B"===t.toUpperCase()}isQueen(t){return"Q"===t.toUpperCase()}isKing(t){return"K"===t.toUpperCase()}getPawnMoves(t,e){const i=[],n=this.getPieceColor(t);let o=j(e,n);return o&&!this.getPiece(o)&&(i.push(o),o=j(o,n),function(t,e){if(t===c&&"2"===e[1])return!0;if(t===r&&"7"===e[1])return!0;return!1}(n,e)&&o&&!this.getPiece(o)&&i.push(o)),o=_(e,n),o&&(this.getPiece(o)&&this.getPieceOnLocationColor(o)!==n||o===this.configuration.enPassant)&&i.push(o),o=R(e,n),o&&(this.getPiece(o)&&this.getPieceOnLocationColor(o)!==n||o===this.configuration.enPassant)&&i.push(o),i}getKnightMoves(t,e){const i=[],n=this.getPieceColor(t);let o=L(e);return o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=M(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=w(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=O(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=K(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=m(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=S(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=N(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),i}getRookMoves(t,e){const i=[],n=this.getPieceColor(t);let o=e;for(;D(o);){o=D(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;A(o);){o=A(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;b(o);){o=b(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;H(o);){o=H(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}return i}getBishopMoves(t,e){const i=[],n=this.getPieceColor(t);let o=e;for(;d(o);){o=d(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;k(o);){o=k(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;v(o);){o=v(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;y(o);){o=y(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}return i}getQueenMoves(t,e){return[...this.getRookMoves(t,e),...this.getBishopMoves(t,e)]}getKingMoves(t,e){const i=[],n=this.getPieceColor(t);let o=e;return o=D(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=b(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=A(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=H(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=d(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=k(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=v(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=y(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),i}getPieceColor(t){return t.toUpperCase()===t?c:r}getPieceOnLocationColor(t){const e=this.getPiece(t);return e?e.toUpperCase()===e?c:r:null}getPiece(t){return this.configuration.pieces[t]}setPiece(t,e){if(!function(t){return Object.values(s).includes(t)}(e))throw new Error("Invalid piece "+e);if(!I(t))throw new Error("Invalid location "+t);this.configuration.pieces[t.toUpperCase()]=e}removePiece(t){if(!I(t))throw new Error("Invalid location "+t);delete this.configuration.pieces[t.toUpperCase()]}isEmpty(t){if(!I(t))throw new Error("Invalid location "+t);return!this.configuration.pieces[t.toUpperCase()]}getEnemyColor(t){return t===c?r:c}getPlayingColor(){return this.configuration.turn}getNonPlayingColor(){return this.isPlayingWhite()?r:c}isPlayingWhite(){return this.configuration.turn===c}isPlayingBlack(){return this.configuration.turn===r}addMoveToHistory(t,e){this.history.push({from:t,to:e,configuration:JSON.parse(JSON.stringify(this.configuration))})}move(t,e){const i=this.getPiece(t),n=this.getPiece(e);if(!i)throw new Error("There is no piece at "+t);var o,s;if(Object.assign(this.configuration.pieces,{[e]:i}),delete this.configuration.pieces[t],this.isPlayingWhite()&&this.isPawn(i)&&"8"===e[1]&&Object.assign(this.configuration.pieces,{[e]:"Q"}),this.isPlayingBlack()&&this.isPawn(i)&&"1"===e[1]&&Object.assign(this.configuration.pieces,{[e]:"q"}),this.isPawn(i)&&e===this.configuration.enPassant&&delete this.configuration.pieces[(o=e,s=this.getPlayingColor(),s===c?f.DOWN[o]:f.UP[o])],this.isPawn(i)&&this.isPlayingWhite()&&"2"===t[1]&&"4"===e[1]?this.configuration.enPassant=t[0]+"3":this.isPawn(i)&&this.isPlayingBlack()&&"7"===t[1]&&"5"===e[1]?this.configuration.enPassant=t[0]+"6":this.configuration.enPassant=null,"E1"===t&&Object.assign(this.configuration.castling,{whiteLong:!1,whiteShort:!1}),"E8"===t&&Object.assign(this.configuration.castling,{blackLong:!1,blackShort:!1}),"A1"===t&&Object.assign(this.configuration.castling,{whiteLong:!1}),"H1"===t&&Object.assign(this.configuration.castling,{whiteShort:!1}),"A8"===t&&Object.assign(this.configuration.castling,{blackLong:!1}),"H8"===t&&Object.assign(this.configuration.castling,{blackShort:!1}),this.isKing(i)){if("E1"===t&&"C1"===e)return this.move("A1","D1");if("E8"===t&&"C8"===e)return this.move("A8","D8");if("E1"===t&&"G1"===e)return this.move("H1","F1");if("E8"===t&&"G8"===e)return this.move("H8","F8")}this.configuration.turn=this.isPlayingWhite()?r:c,this.isPlayingWhite()&&this.configuration.fullMove++,this.configuration.halfMove++,(n||this.isPawn(i))&&(this.configuration.halfMove=0)}exportJson(){return{moves:this.getMoves(),pieces:this.configuration.pieces,turn:this.configuration.turn,isFinished:this.configuration.isFinished,check:this.hasPlayingPlayerCheck(),checkMate:this.configuration.checkMate,castling:this.configuration.castling,enPassant:this.configuration.enPassant,halfMove:this.configuration.halfMove,fullMove:this.configuration.fullMove}}calculateAiMove(t){return this.calculateAiMoves(t)[0]}calculateAiMoves(t){if(t=parseInt(t),!l.includes(t))throw new Error(`Invalid level ${t}. You can choose ${l.join(",")}`);this.shouldIncreaseLevel()&&t++;const e=[],i=this.calculateScore(this.getPlayingColor()),n=this.getMoves();for(const o in n)n[o].map(n=>{const s=this.getTestBoard(),r=Boolean(s.getPiece(n));s.move(o,n),e.push({from:o,to:n,score:s.testMoveScores(this.getPlayingColor(),t,r,r?s.calculateScore(this.getPlayingColor()):i,n).score+s.calculateScoreByPiecesLocation(this.getPlayingColor())+Math.floor(Math.random()*(this.configuration.halfMove>10?this.configuration.halfMove-10:1)*10)/10})});return e.sort((t,e)=>t.score<e.score?1:t.score>e.score?-1:0),e}shouldIncreaseLevel(){return this.getIngamePiecesValue()<50}getIngamePiecesValue(){let t=0;for(const e in this.configuration.pieces){t+=T(this.getPiece(e))}return t}getTestBoard(){const t={pieces:Object.assign({},this.configuration.pieces),castling:Object.assign({},this.configuration.castling),turn:this.configuration.turn,enPassant:this.configuration.enPassant};return new q(t)}testMoveScores(t,e,i,n,o,s=1){let r=null;if(s<u[e]&&this.hasPlayingPlayerCheck()?r=this.getMoves(this.getPlayingColor()):(s<a[e]||i&&s<u[e])&&(r=this.getMoves(this.getPlayingColor(),5)),this.configuration.isFinished)return{score:this.calculateScore(t)+(this.getPlayingColor()===t?s:-s),max:!0};if(!r){if(null!==n)return{score:n,max:!1};return{score:this.calculateScore(t),max:!1}}let c=this.getPlayingColor()===t?Q:x,l=!1;for(const i in r)l||r[i].map(o=>{if(l)return;const r=this.getTestBoard(),a=Boolean(r.getPiece(o));if(r.move(i,o),r.hasNonPlayingPlayerCheck())return;const u=r.testMoveScores(t,e,a,a?r.calculateScore(t):n,o,s+1);u.max&&(l=!0),c=this.getPlayingColor()===t?Math.max(c,u.score):Math.min(c,u.score)});return{score:c,max:!1}}calculateScoreByPiecesLocation(t=this.getPlayingColor()){const e={A:0,B:1,C:2,D:3,E:4,F:5,G:6,H:7};let i=0;for(const n in this.configuration.pieces){const o=this.getPiece(n);if(G[o]){const s=G[o][n[1]-1][e[n[0]]];i+=.5*(this.getPieceColor(o)===t?s:-s)}}return i}calculateScore(t=this.getPlayingColor()){let e=0;if(this.configuration.checkMate)return this.getPlayingColor()===t?Q:x;if(this.configuration.isFinished)return this.getPlayingColor()===t?x:Q;for(const i in this.configuration.pieces){const n=this.getPiece(i);this.getPieceColor(n)===t?e+=10*T(n):e-=10*T(n)}return e}}class ${constructor(t){this.board=new q(t)}move(t,e){t=t.toUpperCase(),e=e.toUpperCase();const i=this.board.getMoves();if(!i[t]||!i[t].includes(e))throw new Error(`Invalid move from ${t} to ${e} for ${this.board.getPlayingColor()}`);return this.board.addMoveToHistory(t,e),this.board.move(t,e),{[t]:e}}moves(t=null){return(t?this.board.getMoves()[t.toUpperCase()]:this.board.getMoves())||[]}setPiece(t,e){this.board.setPiece(t,e)}removePiece(t){this.board.removePiece(t)}aiMove(t=2){const e=this.board.calculateAiMove(t);return this.move(e.from,e.to)}getHistory(t=!1){return t?this.board.history.reverse():this.board.history}printToConsole(){!function(t){process.stdout.write("\n");let e=c;Object.assign([],o).reverse().map(i=>{process.stdout.write(""+i),n.map(n=>{switch(t.pieces[`${n}${i}`]){case"K":process.stdout.write("♚");break;case"Q":process.stdout.write("♛");break;case"R":process.stdout.write("♜");break;case"B":process.stdout.write("♝");break;case"N":process.stdout.write("♞");break;case"P":process.stdout.write("♟");break;case"k":process.stdout.write("♔");break;case"q":process.stdout.write("♕");break;case"r":process.stdout.write("♖");break;case"b":process.stdout.write("♗");break;case"n":process.stdout.write("♘");break;case"p":process.stdout.write("♙");break;default:process.stdout.write(e===c?"█":"░")}e=e===c?r:c}),e=e===c?r:c,process.stdout.write("\n")}),process.stdout.write(" "),n.map(t=>{process.stdout.write(""+t)}),process.stdout.write("\n")}(this.board.configuration)}exportJson(){return this.board.exportJson()}exportFEN(){return function(t){let e="";Object.assign([],o).reverse().map(i=>{let o=0;i<8&&(e+="/"),n.map(n=>{const s=t.pieces[`${n}${i}`];s?(o&&(e+=o.toString(),o=0),e+=s):o++}),e+=""+(o||"")}),e+=t.turn===c?" w ":" b ";const{whiteShort:i,whiteLong:s,blackLong:r,blackShort:l}=t.castling;return s||i||r||l?(i&&(e+="K"),s&&(e+="Q"),l&&(e+="k"),r&&(e+="q")):e+="-",e+=" "+(t.enPassant?t.enPassant.toLowerCase():"-"),e+=" "+t.halfMove,e+=" "+t.fullMove,e}(this.board.configuration)}}function J(t){if(!t)throw new Error("Configuration param required.");return new $(t).moves()}function V(t){if(!t)throw new Error("Configuration param required.");return new $(t).exportJson()}function Y(t){if(!t)throw new Error("Configuration param required.");return new $(t).exportFEN()}function z(t,e,i){if(!t)throw new Error("Configuration param required.");const n=new $(t);return n.move(e,i),"object"==typeof t?n.exportJson():n.exportFEN()}function X(t,e=2){if(!t)throw new Error("Configuration param required.");const i=new $(t).board.calculateAiMove(e);return{[i.from]:i.to}}}])}));
1
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("js-chess-engine",[],e):"object"==typeof exports?exports["js-chess-engine"]=e():t["js-chess-engine"]=e()}(this,(function(){return function(t){var e={};function i(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,i),o.l=!0,o.exports}return i.m=t,i.c=e,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)i.d(n,o,function(e){return t[e]}.bind(null,o));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){"use strict";i.r(e),i.d(e,"Game",(function(){return $})),i.d(e,"moves",(function(){return J})),i.d(e,"status",(function(){return V})),i.d(e,"getFen",(function(){return Y})),i.d(e,"move",(function(){return z})),i.d(e,"aiMove",(function(){return X}));const n=["A","B","C","D","E","F","G","H"],o=["1","2","3","4","5","6","7","8"],s={KING_W:"K",QUEEN_W:"Q",ROOK_W:"R",BISHOP_W:"B",KNIGHT_W:"N",PAWN_W:"P",KING_B:"k",QUEEN_B:"q",ROOK_B:"r",BISHOP_B:"b",KNIGHT_B:"n",PAWN_B:"p"},r="black",c="white",l=[0,1,2,3,4],a={0:1,1:2,2:2,3:3,4:3,5:4},u={0:2,1:2,2:4,3:4,4:5,5:5},h={fullMove:1,halfMove:0,enPassant:null,isFinished:!1,checkMate:!1,check:!1,turn:c},g=Object.assign({pieces:{E1:"K",D1:"Q",A1:"R",H1:"R",C1:"B",F1:"B",B1:"N",G1:"N",A2:"P",B2:"P",C2:"P",D2:"P",E2:"P",F2:"P",G2:"P",H2:"P",E8:"k",D8:"q",A8:"r",H8:"r",C8:"b",F8:"b",B8:"n",G8:"n",A7:"p",B7:"p",C7:"p",D7:"p",E7:"p",F7:"p",G7:"p",H7:"p"},castling:{whiteShort:!0,blackShort:!0,whiteLong:!0,blackLong:!0}},h),f={UP:{A1:"A2",A2:"A3",A3:"A4",A4:"A5",A5:"A6",A6:"A7",A7:"A8",A8:null,B1:"B2",B2:"B3",B3:"B4",B4:"B5",B5:"B6",B6:"B7",B7:"B8",B8:null,C1:"C2",C2:"C3",C3:"C4",C4:"C5",C5:"C6",C6:"C7",C7:"C8",C8:null,D1:"D2",D2:"D3",D3:"D4",D4:"D5",D5:"D6",D6:"D7",D7:"D8",D8:null,E1:"E2",E2:"E3",E3:"E4",E4:"E5",E5:"E6",E6:"E7",E7:"E8",E8:null,F1:"F2",F2:"F3",F3:"F4",F4:"F5",F5:"F6",F6:"F7",F7:"F8",F8:null,G1:"G2",G2:"G3",G3:"G4",G4:"G5",G5:"G6",G6:"G7",G7:"G8",G8:null,H1:"H2",H2:"H3",H3:"H4",H4:"H5",H5:"H6",H6:"H7",H7:"H8",H8:null},DOWN:{A1:null,A2:"A1",A3:"A2",A4:"A3",A5:"A4",A6:"A5",A7:"A6",A8:"A7",B1:null,B2:"B1",B3:"B2",B4:"B3",B5:"B4",B6:"B5",B7:"B6",B8:"B7",C1:null,C2:"C1",C3:"C2",C4:"C3",C5:"C4",C6:"C5",C7:"C6",C8:"C7",D1:null,D2:"D1",D3:"D2",D4:"D3",D5:"D4",D6:"D5",D7:"D6",D8:"D7",E1:null,E2:"E1",E3:"E2",E4:"E3",E5:"E4",E6:"E5",E7:"E6",E8:"E7",F1:null,F2:"F1",F3:"F2",F4:"F3",F5:"F4",F6:"F5",F7:"F6",F8:"F7",G1:null,G2:"G1",G3:"G2",G4:"G3",G5:"G4",G6:"G5",G7:"G6",G8:"G7",H1:null,H2:"H1",H3:"H2",H4:"H3",H5:"H4",H6:"H5",H7:"H6",H8:"H7"},LEFT:{A1:null,A2:null,A3:null,A4:null,A5:null,A6:null,A7:null,A8:null,B1:"A1",B2:"A2",B3:"A3",B4:"A4",B5:"A5",B6:"A6",B7:"A7",B8:"A8",C1:"B1",C2:"B2",C3:"B3",C4:"B4",C5:"B5",C6:"B6",C7:"B7",C8:"B8",D1:"C1",D2:"C2",D3:"C3",D4:"C4",D5:"C5",D6:"C6",D7:"C7",D8:"C8",E1:"D1",E2:"D2",E3:"D3",E4:"D4",E5:"D5",E6:"D6",E7:"D7",E8:"D8",F1:"E1",F2:"E2",F3:"E3",F4:"E4",F5:"E5",F6:"E6",F7:"E7",F8:"E8",G1:"F1",G2:"F2",G3:"F3",G4:"F4",G5:"F5",G6:"F6",G7:"F7",G8:"F8",H1:"G1",H2:"G2",H3:"G3",H4:"G4",H5:"G5",H6:"G6",H7:"G7",H8:"G8"},RIGHT:{A1:"B1",A2:"B2",A3:"B3",A4:"B4",A5:"B5",A6:"B6",A7:"B7",A8:"B8",B1:"C1",B2:"C2",B3:"C3",B4:"C4",B5:"C5",B6:"C6",B7:"C7",B8:"C8",C1:"D1",C2:"D2",C3:"D3",C4:"D4",C5:"D5",C6:"D6",C7:"D7",C8:"D8",D1:"E1",D2:"E2",D3:"E3",D4:"E4",D5:"E5",D6:"E6",D7:"E7",D8:"E8",E1:"F1",E2:"F2",E3:"F3",E4:"F4",E5:"F5",E6:"F6",E7:"F7",E8:"F8",F1:"G1",F2:"G2",F3:"G3",F4:"G4",F5:"G5",F6:"G6",F7:"G7",F8:"G8",G1:"H1",G2:"H2",G3:"H3",G4:"H4",G5:"H5",G6:"H6",G7:"H7",G8:"H8",H1:null,H2:null,H3:null,H4:null,H5:null,H6:null,H7:null,H8:null},UP_LEFT:{A1:null,A2:null,A3:null,A4:null,A5:null,A6:null,A7:null,A8:null,B1:"A2",B2:"A3",B3:"A4",B4:"A5",B5:"A6",B6:"A7",B7:"A8",B8:null,C1:"B2",C2:"B3",C3:"B4",C4:"B5",C5:"B6",C6:"B7",C7:"B8",C8:null,D1:"C2",D2:"C3",D3:"C4",D4:"C5",D5:"C6",D6:"C7",D7:"C8",D8:null,E1:"D2",E2:"D3",E3:"D4",E4:"D5",E5:"D6",E6:"D7",E7:"D8",E8:null,F1:"E2",F2:"E3",F3:"E4",F4:"E5",F5:"E6",F6:"E7",F7:"E8",F8:null,G1:"F2",G2:"F3",G3:"F4",G4:"F5",G5:"F6",G6:"F7",G7:"F8",G8:null,H1:"G2",H2:"G3",H3:"G4",H4:"G5",H5:"G6",H6:"G7",H7:"G8",H8:null},DOWN_RIGHT:{A1:null,A2:"B1",A3:"B2",A4:"B3",A5:"B4",A6:"B5",A7:"B6",A8:"B7",B1:null,B2:"C1",B3:"C2",B4:"C3",B5:"C4",B6:"C5",B7:"C6",B8:"C7",C1:null,C2:"D1",C3:"D2",C4:"D3",C5:"D4",C6:"D5",C7:"D6",C8:"D7",D1:null,D2:"E1",D3:"E2",D4:"E3",D5:"E4",D6:"E5",D7:"E6",D8:"E7",E1:null,E2:"F1",E3:"F2",E4:"F3",E5:"F4",E6:"F5",E7:"F6",E8:"F7",F1:null,F2:"G1",F3:"G2",F4:"G3",F5:"G4",F6:"G5",F7:"G6",F8:"G7",G1:null,G2:"H1",G3:"H2",G4:"H3",G5:"H4",G6:"H5",G7:"H6",G8:"H7",H1:null,H2:null,H3:null,H4:null,H5:null,H6:null,H7:null,H8:null},UP_RIGHT:{A1:"B2",A2:"B3",A3:"B4",A4:"B5",A5:"B6",A6:"B7",A7:"B8",A8:null,B1:"C2",B2:"C3",B3:"C4",B4:"C5",B5:"C6",B6:"C7",B7:"C8",B8:null,C1:"D2",C2:"D3",C3:"D4",C4:"D5",C5:"D6",C6:"D7",C7:"D8",C8:null,D1:"E2",D2:"E3",D3:"E4",D4:"E5",D5:"E6",D6:"E7",D7:"E8",D8:null,E1:"F2",E2:"F3",E3:"F4",E4:"F5",E5:"F6",E6:"F7",E7:"F8",E8:null,F1:"G2",F2:"G3",F3:"G4",F4:"G5",F5:"G6",F6:"G7",F7:"G8",F8:null,G1:"H2",G2:"H3",G3:"H4",G4:"H5",G5:"H6",G6:"H7",G7:"H8",G8:null,H1:null,H2:null,H3:null,H4:null,H5:null,H6:null,H7:null,H8:null},DOWN_LEFT:{A1:null,A2:null,A3:null,A4:null,A5:null,A6:null,A7:null,A8:null,B1:null,B2:"A1",B3:"A2",B4:"A3",B5:"A4",B6:"A5",B7:"A6",B8:"A7",C1:null,C2:"B1",C3:"B2",C4:"B3",C5:"B4",C6:"B5",C7:"B6",C8:"B7",D1:null,D2:"C1",D3:"C2",D4:"C3",D5:"C4",D6:"C5",D7:"C6",D8:"C7",E1:null,E2:"D1",E3:"D2",E4:"D3",E5:"D4",E6:"D5",E7:"D6",E8:"D7",F1:null,F2:"E1",F3:"E2",F4:"E3",F5:"E4",F6:"E5",F7:"E6",F8:"E7",G1:null,G2:"F1",G3:"F2",G4:"F3",G5:"F4",G6:"F5",G7:"F6",G8:"F7",H1:null,H2:"G1",H3:"G2",H4:"G3",H5:"G4",H6:"G5",H7:"G6",H8:"G7"}},C=[[0,0,0,0,0,0,0,0],[5,5,5,5,5,5,5,5],[1,1,2,3,3,2,1,1],[.5,.5,1,2.5,2.5,1,.5,.5],[0,0,0,2,2,0,0,0],[.5,0,1,0,0,1,0,.5],[.5,0,0,-2,-2,0,0,.5],[0,0,0,0,0,0,0,0]],P=[[-4,-3,-2,-2,-2,-2,-3,-4],[-3,-2,0,0,0,0,-2,-3],[-2,0,1,1.5,1.5,1,0,-2],[-2,.5,1.5,2,2,1.5,.5,-2],[-2,0,1.5,2,2,1.5,0,-2],[-2,.5,1,1.5,1.5,1,.5,-2],[-3,-2,0,.5,.5,0,-2,-3],[-4,-3,-2,-2,-2,-2,-3,-4]],p=[[-2,-1,-1,-1,-1,-1,-1,-2],[-1,0,0,0,0,0,0,-1],[-1,0,.5,1,1,.5,0,-1],[-1,.5,.5,1,1,.5,.5,-1],[-1,0,1,1,1,1,0,-1],[-1,1,1,1,1,1,1,-1],[-1,.5,0,0,0,0,.5,-1],[-2,-1,-1,-1,-1,-1,-1,-2]],E=[[0,0,0,0,0,0,0,0],[.5,1,1,1,1,1,1,.5],[-.5,0,0,0,0,0,0,-.5],[-.5,0,0,0,0,0,0,-.5],[-.5,0,0,0,0,0,0,-.5],[-.5,0,0,0,0,0,0,-.5],[-.5,0,0,0,0,0,0,-.5],[0,0,0,.5,.5,0,0,0]],B=[[-3,-4,-4,-5,-5,-4,-4,-3],[-3,-4,-4,-5,-5,-4,-4,-3],[-3,-4,-4,-5,-5,-4,-4,-3],[-3,-4,-4,-5,-5,-4,-4,-3],[-2,-3,-3,-4,-4,-3,-3,-2],[-1,-2,-2,-2,-2,-2,-2,-1],[2,2,0,0,0,0,2,2],[2,3,1,0,0,1,3,2]],F=[[-2,-1,-1,-.5,-.5,-1,-1,-2],[-1,0,0,0,0,0,0,-1],[-1,0,.5,.5,.5,.5,0,-1],[-.5,0,.5,.5,.5,.5,0,-.5],[0,0,.5,.5,.5,.5,0,-.5],[-1,.5,.5,.5,.5,.5,0,-1],[-1,0,.5,0,0,0,0,-1],[-2,-1,-1,-.5,-.5,-1,-1,-2]],G={P:C.slice().reverse(),p:C,N:P.slice().reverse(),n:P,B:p.slice().reverse(),b:p,R:E.slice().reverse(),r:E,K:B.slice().reverse(),k:B,Q:F.slice().reverse(),q:F};function D(t){return f.UP[t]}function A(t){return f.DOWN[t]}function H(t){return f.LEFT[t]}function b(t){return f.RIGHT[t]}function d(t){return f.UP_LEFT[t]}function k(t){return f.UP_RIGHT[t]}function v(t){return f.DOWN_LEFT[t]}function y(t){return f.DOWN_RIGHT[t]}function w(t){const e=d(t);return e?D(e):null}function O(t){const e=d(t);return e?H(e):null}function L(t){const e=k(t);return e?D(e):null}function m(t){const e=k(t);return e?b(e):null}function M(t){const e=v(t);return e?A(e):null}function K(t){const e=v(t);return e?H(e):null}function N(t){const e=y(t);return e?A(e):null}function S(t){const e=y(t);return e?b(e):null}function j(t,e){return e===c?f.UP[t]:f.DOWN[t]}function _(t,e){return e===c?f.UP_LEFT[t]:f.DOWN_RIGHT[t]}function W(t,e){return e===c?f.UP_RIGHT[t]:f.DOWN_LEFT[t]}function U(t,e){return e===c?f.DOWN_LEFT[t]:f.UP_RIGHT[t]}function R(t,e){return e===c?f.DOWN_RIGHT[t]:f.UP_LEFT[t]}function T(t){return{k:10,q:9,r:5,b:3,n:3,p:1}[t.toLowerCase()]||0}function I(t){return"string"==typeof t&&t.match("^[a-hA-H]{1}[1-8]{1}$")}const x=-1e3,Q=1e3;class q{constructor(t=JSON.parse(JSON.stringify(g))){if("object"==typeof t)this.configuration=Object.assign({},h,t);else{if("string"!=typeof t)throw new Error(`Unknown configuration type ${typeof config}.`);this.configuration=Object.assign({},h,function(t=""){const[e,i,s,l,a,u]=t.split(" "),h={pieces:Object.fromEntries(e.split("/").flatMap((t,e)=>{let i=0;return t.split("").reduce((t,s)=>{const r=s.match(/k|b|q|n|p|r/i);r&&(t.push([`${n[i]}${o[7-e]}`,r[0]]),i+=1);const c=s.match(/[1-8]/);return c&&(i+=Number(c)),t},[])}))};return h.turn="b"===i?r:c,h.castling={whiteLong:!1,whiteShort:!1,blackLong:!1,blackShort:!1},s.includes("K")&&(h.castling.whiteShort=!0),s.includes("k")&&(h.castling.blackShort=!0),s.includes("Q")&&(h.castling.whiteLong=!0),s.includes("q")&&(h.castling.blackLong=!0),I(l)&&(h.enPassant=l.toUpperCase()),h.halfMove=parseInt(a),h.fullMove=parseInt(u),h}(t))}this.configuration.castling||(this.configuration.castling={whiteShort:!0,blackShort:!0,whiteLong:!0,blackLong:!0}),this.history=[]}getAttackingFields(t=this.getPlayingColor()){let e=[];for(const i in this.configuration.pieces){const n=this.getPiece(i);this.getPieceColor(n)===t&&(e=[...e,...this.getPieceMoves(n,i)])}return e}isAttackingKing(t=this.getPlayingColor()){let e=null;for(const i in this.configuration.pieces){const n=this.getPiece(i);if(this.isKing(n)&&this.getPieceColor(n)!==t){e=i;break}}return this.isPieceUnderAttack(e)}isPieceUnderAttack(t){const e=this.getPieceOnLocationColor(t),i=this.getEnemyColor(e);let n=!1,o=t,s=0;for(;D(o)&&!n;){o=D(o),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isRook(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;A(o)&&!n;){o=A(o),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isRook(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;H(o)&&!n;){o=H(o),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isRook(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;b(o)&&!n;){o=b(o),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isRook(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;W(o,e)&&!n;){o=W(o,e),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isBishop(t)||this.isQueen(t)||1===s&&(this.isKing(t)||this.isPawn(t)))&&(n=!0),t)break}for(o=t,s=0;_(o,e)&&!n;){o=_(o,e),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isBishop(t)||this.isQueen(t)||1===s&&(this.isKing(t)||this.isPawn(t)))&&(n=!0),t)break}for(o=t,s=0;R(o,e)&&!n;){o=R(o,e),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isBishop(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}for(o=t,s=0;U(o,e)&&!n;){o=U(o,e),s++;const t=this.getPiece(o);if(t&&this.getPieceColor(t)===i&&(this.isBishop(t)||this.isQueen(t)||this.isKing(t)&&1===s)&&(n=!0),t)break}o=L(t);let r=this.getPiece(o);return r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=m(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=O(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=w(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=M(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=K(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=N(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),o=S(t),r=this.getPiece(o),r&&this.getPieceColor(r)===i&&this.isKnight(r)&&(n=!0),n}hasPlayingPlayerCheck(){return this.isAttackingKing(this.getNonPlayingColor())}hasNonPlayingPlayerCheck(){return this.isAttackingKing(this.getPlayingColor())}getLowestValuePieceAttackingLocation(t,e=this.getPlayingColor()){let i=null;for(const n in this.configuration.pieces){const o=this.getPiece(n);this.getPieceColor(o)===e&&this.getPieceMoves(o,n).map(e=>{e===t&&(null===i||T(o)<i)&&(i=T(o))})}return i}getMoves(t=this.getPlayingColor(),e=null){const i={};let n=0;for(const e in this.configuration.pieces){const o=this.getPiece(e);if(this.getPieceColor(o)===t){const t=this.getPieceMoves(o,e);t.length&&n++,Object.assign(i,{[e]:t})}}const o=this.getAttackingFields(this.getNonPlayingColor());if(this.isLeftCastlingPossible(o)&&(this.isPlayingWhite()&&i.E1.push("C1"),this.isPlayingBlack()&&i.E8.push("C8")),this.isRightCastlingPossible(o)&&(this.isPlayingWhite()&&i.E1.push("G1"),this.isPlayingBlack()&&i.E8.push("G8")),e&&n>e)return i;const s={};for(const t in i)i[t].map(e=>{const i={pieces:Object.assign({},this.configuration.pieces),castling:Object.assign({},this.configuration.castling)},n=new q(i);n.move(t,e),(this.isPlayingWhite()&&!n.isAttackingKing(r)||this.isPlayingBlack()&&!n.isAttackingKing(c))&&(s[t]||(s[t]=[]),s[t].push(e))});return Object.keys(s).length||(this.configuration.isFinished=!0,this.hasPlayingPlayerCheck()&&(this.configuration.checkMate=!0)),s}isLeftCastlingPossible(t){if(this.isPlayingWhite()&&!this.configuration.castling.whiteLong)return!1;if(this.isPlayingBlack()&&!this.configuration.castling.blackLong)return!1;let e=null;if(this.isPlayingWhite()&&"K"===this.getPiece("E1")&&"R"===this.getPiece("A1")&&!t.includes("E1")?e="E1":this.isPlayingBlack()&&"k"===this.getPiece("E8")&&"r"===this.getPiece("A8")&&!t.includes("E8")&&(e="E8"),!e)return!1;let i=H(e);return!this.getPiece(i)&&!t.includes(i)&&(i=H(i),!this.getPiece(i)&&!t.includes(i)&&(i=H(i),!this.getPiece(i)))}isRightCastlingPossible(t){if(this.isPlayingWhite()&&!this.configuration.castling.whiteShort)return!1;if(this.isPlayingBlack()&&!this.configuration.castling.blackShort)return!1;let e=null;if(this.isPlayingWhite()&&"K"===this.getPiece("E1")&&"R"===this.getPiece("H1")&&!t.includes("E1")?e="E1":this.isPlayingBlack()&&"k"===this.getPiece("E8")&&"r"===this.getPiece("H8")&&!t.includes("E8")&&(e="E8"),!e)return!1;let i=b(e);return!this.getPiece(i)&&!t.includes(i)&&(i=b(i),!this.getPiece(i)&&!t.includes(i))}getPieceMoves(t,e){return this.isPawn(t)?this.getPawnMoves(t,e):this.isKnight(t)?this.getKnightMoves(t,e):this.isRook(t)?this.getRookMoves(t,e):this.isBishop(t)?this.getBishopMoves(t,e):this.isQueen(t)?this.getQueenMoves(t,e):this.isKing(t)?this.getKingMoves(t,e):[]}isPawn(t){return"P"===t.toUpperCase()}isKnight(t){return"N"===t.toUpperCase()}isRook(t){return"R"===t.toUpperCase()}isBishop(t){return"B"===t.toUpperCase()}isQueen(t){return"Q"===t.toUpperCase()}isKing(t){return"K"===t.toUpperCase()}getPawnMoves(t,e){const i=[],n=this.getPieceColor(t);let o=j(e,n);return o&&!this.getPiece(o)&&(i.push(o),o=j(o,n),function(t,e){if(t===c&&"2"===e[1])return!0;if(t===r&&"7"===e[1])return!0;return!1}(n,e)&&o&&!this.getPiece(o)&&i.push(o)),o=_(e,n),o&&(this.getPiece(o)&&this.getPieceOnLocationColor(o)!==n||o===this.configuration.enPassant)&&i.push(o),o=W(e,n),o&&(this.getPiece(o)&&this.getPieceOnLocationColor(o)!==n||o===this.configuration.enPassant)&&i.push(o),i}getKnightMoves(t,e){const i=[],n=this.getPieceColor(t);let o=L(e);return o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=m(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=w(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=O(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=K(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=M(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=S(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=N(e),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),i}getRookMoves(t,e){const i=[],n=this.getPieceColor(t);let o=e;for(;D(o);){o=D(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;A(o);){o=A(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;b(o);){o=b(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;H(o);){o=H(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}return i}getBishopMoves(t,e){const i=[],n=this.getPieceColor(t);let o=e;for(;d(o);){o=d(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;k(o);){o=k(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;v(o);){o=v(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}for(o=e;y(o);){o=y(o);const t=this.getPieceOnLocationColor(o);if(this.getPieceOnLocationColor(o)!==n&&i.push(o),t)break}return i}getQueenMoves(t,e){return[...this.getRookMoves(t,e),...this.getBishopMoves(t,e)]}getKingMoves(t,e){const i=[],n=this.getPieceColor(t);let o=e;return o=D(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=b(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=A(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=H(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=d(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=k(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=v(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),o=e,o=y(o),o&&this.getPieceOnLocationColor(o)!==n&&i.push(o),i}getPieceColor(t){return t.toUpperCase()===t?c:r}getPieceOnLocationColor(t){const e=this.getPiece(t);return e?e.toUpperCase()===e?c:r:null}getPiece(t){return this.configuration.pieces[t]}setPiece(t,e){if(!function(t){return Object.values(s).includes(t)}(e))throw new Error("Invalid piece "+e);if(!I(t))throw new Error("Invalid location "+t);this.configuration.pieces[t.toUpperCase()]=e}removePiece(t){if(!I(t))throw new Error("Invalid location "+t);delete this.configuration.pieces[t.toUpperCase()]}isEmpty(t){if(!I(t))throw new Error("Invalid location "+t);return!this.configuration.pieces[t.toUpperCase()]}getEnemyColor(t){return t===c?r:c}getPlayingColor(){return this.configuration.turn}getNonPlayingColor(){return this.isPlayingWhite()?r:c}isPlayingWhite(){return this.configuration.turn===c}isPlayingBlack(){return this.configuration.turn===r}addMoveToHistory(t,e){const i=JSON.parse(JSON.stringify(this.configuration));i.check=this.hasPlayingPlayerCheck(),this.history.push({from:t,to:e,configuration:i})}move(t,e){const i=this.getPiece(t),n=this.getPiece(e);if(!i)throw new Error("There is no piece at "+t);var o,s;if(Object.assign(this.configuration.pieces,{[e]:i}),delete this.configuration.pieces[t],this.isPlayingWhite()&&this.isPawn(i)&&"8"===e[1]&&Object.assign(this.configuration.pieces,{[e]:"Q"}),this.isPlayingBlack()&&this.isPawn(i)&&"1"===e[1]&&Object.assign(this.configuration.pieces,{[e]:"q"}),this.isPawn(i)&&e===this.configuration.enPassant&&delete this.configuration.pieces[(o=e,s=this.getPlayingColor(),s===c?f.DOWN[o]:f.UP[o])],this.isPawn(i)&&this.isPlayingWhite()&&"2"===t[1]&&"4"===e[1]?this.configuration.enPassant=t[0]+"3":this.isPawn(i)&&this.isPlayingBlack()&&"7"===t[1]&&"5"===e[1]?this.configuration.enPassant=t[0]+"6":this.configuration.enPassant=null,"E1"===t&&Object.assign(this.configuration.castling,{whiteLong:!1,whiteShort:!1}),"E8"===t&&Object.assign(this.configuration.castling,{blackLong:!1,blackShort:!1}),"A1"===t&&Object.assign(this.configuration.castling,{whiteLong:!1}),"H1"===t&&Object.assign(this.configuration.castling,{whiteShort:!1}),"A8"===t&&Object.assign(this.configuration.castling,{blackLong:!1}),"H8"===t&&Object.assign(this.configuration.castling,{blackShort:!1}),this.isKing(i)){if("E1"===t&&"C1"===e)return this.move("A1","D1");if("E8"===t&&"C8"===e)return this.move("A8","D8");if("E1"===t&&"G1"===e)return this.move("H1","F1");if("E8"===t&&"G8"===e)return this.move("H8","F8")}this.configuration.turn=this.isPlayingWhite()?r:c,this.isPlayingWhite()&&this.configuration.fullMove++,this.configuration.halfMove++,(n||this.isPawn(i))&&(this.configuration.halfMove=0)}exportJson(){return{moves:this.getMoves(),pieces:this.configuration.pieces,turn:this.configuration.turn,isFinished:this.configuration.isFinished,check:this.hasPlayingPlayerCheck(),checkMate:this.configuration.checkMate,castling:this.configuration.castling,enPassant:this.configuration.enPassant,halfMove:this.configuration.halfMove,fullMove:this.configuration.fullMove}}calculateAiMove(t){return this.calculateAiMoves(t)[0]}calculateAiMoves(t){if(t=parseInt(t),!l.includes(t))throw new Error(`Invalid level ${t}. You can choose ${l.join(",")}`);this.shouldIncreaseLevel()&&t++;const e=[],i=this.calculateScore(this.getPlayingColor()),n=this.getMoves();for(const o in n)n[o].map(n=>{const s=this.getTestBoard(),r=Boolean(s.getPiece(n));s.move(o,n),e.push({from:o,to:n,score:s.testMoveScores(this.getPlayingColor(),t,r,r?s.calculateScore(this.getPlayingColor()):i,n).score+s.calculateScoreByPiecesLocation(this.getPlayingColor())+Math.floor(Math.random()*(this.configuration.halfMove>10?this.configuration.halfMove-10:1)*10)/10})});return e.sort((t,e)=>t.score<e.score?1:t.score>e.score?-1:0),e}shouldIncreaseLevel(){return this.getIngamePiecesValue()<50}getIngamePiecesValue(){let t=0;for(const e in this.configuration.pieces){t+=T(this.getPiece(e))}return t}getTestBoard(){const t={pieces:Object.assign({},this.configuration.pieces),castling:Object.assign({},this.configuration.castling),turn:this.configuration.turn,enPassant:this.configuration.enPassant};return new q(t)}testMoveScores(t,e,i,n,o,s=1){let r=null;if(s<u[e]&&this.hasPlayingPlayerCheck()?r=this.getMoves(this.getPlayingColor()):(s<a[e]||i&&s<u[e])&&(r=this.getMoves(this.getPlayingColor(),5)),this.configuration.isFinished)return{score:this.calculateScore(t)+(this.getPlayingColor()===t?s:-s),max:!0};if(!r){if(null!==n)return{score:n,max:!1};return{score:this.calculateScore(t),max:!1}}let c=this.getPlayingColor()===t?x:Q,l=!1;for(const i in r)l||r[i].map(o=>{if(l)return;const r=this.getTestBoard(),a=Boolean(r.getPiece(o));if(r.move(i,o),r.hasNonPlayingPlayerCheck())return;const u=r.testMoveScores(t,e,a,a?r.calculateScore(t):n,o,s+1);u.max&&(l=!0),c=this.getPlayingColor()===t?Math.max(c,u.score):Math.min(c,u.score)});return{score:c,max:!1}}calculateScoreByPiecesLocation(t=this.getPlayingColor()){const e={A:0,B:1,C:2,D:3,E:4,F:5,G:6,H:7};let i=0;for(const n in this.configuration.pieces){const o=this.getPiece(n);if(G[o]){const s=G[o][n[1]-1][e[n[0]]];i+=.5*(this.getPieceColor(o)===t?s:-s)}}return i}calculateScore(t=this.getPlayingColor()){let e=0;if(this.configuration.checkMate)return this.getPlayingColor()===t?x:Q;if(this.configuration.isFinished)return this.getPlayingColor()===t?Q:x;for(const i in this.configuration.pieces){const n=this.getPiece(i);this.getPieceColor(n)===t?e+=10*T(n):e-=10*T(n)}return e}}class ${constructor(t){this.board=new q(t)}move(t,e){t=t.toUpperCase(),e=e.toUpperCase();const i=this.board.getMoves();if(!i[t]||!i[t].includes(e))throw new Error(`Invalid move from ${t} to ${e} for ${this.board.getPlayingColor()}`);return this.board.addMoveToHistory(t,e),this.board.move(t,e),{[t]:e}}moves(t=null){return(t?this.board.getMoves()[t.toUpperCase()]:this.board.getMoves())||[]}setPiece(t,e){this.board.setPiece(t,e)}removePiece(t){this.board.removePiece(t)}aiMove(t=2){const e=this.board.calculateAiMove(t);return this.move(e.from,e.to)}getHistory(t=!1){return t?this.board.history.reverse():this.board.history}printToConsole(){!function(t){process.stdout.write("\n");let e=c;Object.assign([],o).reverse().map(i=>{process.stdout.write(""+i),n.map(n=>{switch(t.pieces[`${n}${i}`]){case"K":process.stdout.write("♚");break;case"Q":process.stdout.write("♛");break;case"R":process.stdout.write("♜");break;case"B":process.stdout.write("♝");break;case"N":process.stdout.write("♞");break;case"P":process.stdout.write("♟");break;case"k":process.stdout.write("♔");break;case"q":process.stdout.write("♕");break;case"r":process.stdout.write("♖");break;case"b":process.stdout.write("♗");break;case"n":process.stdout.write("♘");break;case"p":process.stdout.write("♙");break;default:process.stdout.write(e===c?"█":"░")}e=e===c?r:c}),e=e===c?r:c,process.stdout.write("\n")}),process.stdout.write(" "),n.map(t=>{process.stdout.write(""+t)}),process.stdout.write("\n")}(this.board.configuration)}exportJson(){return this.board.exportJson()}exportFEN(){return function(t){let e="";Object.assign([],o).reverse().map(i=>{let o=0;i<8&&(e+="/"),n.map(n=>{const s=t.pieces[`${n}${i}`];s?(o&&(e+=o.toString(),o=0),e+=s):o++}),e+=""+(o||"")}),e+=t.turn===c?" w ":" b ";const{whiteShort:i,whiteLong:s,blackLong:r,blackShort:l}=t.castling;return s||i||r||l?(i&&(e+="K"),s&&(e+="Q"),l&&(e+="k"),r&&(e+="q")):e+="-",e+=" "+(t.enPassant?t.enPassant.toLowerCase():"-"),e+=" "+t.halfMove,e+=" "+t.fullMove,e}(this.board.configuration)}}function J(t){if(!t)throw new Error("Configuration param required.");return new $(t).moves()}function V(t){if(!t)throw new Error("Configuration param required.");return new $(t).exportJson()}function Y(t){if(!t)throw new Error("Configuration param required.");return new $(t).exportFEN()}function z(t,e,i){if(!t)throw new Error("Configuration param required.");const n=new $(t);return n.move(e,i),"object"==typeof t?n.exportJson():n.exportFEN()}function X(t,e=2){if(!t)throw new Error("Configuration param required.");const i=new $(t).board.calculateAiMove(e);return{[i.from]:i.to}}}])}));
package/lib/Board.mjs CHANGED
@@ -695,7 +695,10 @@ export default class Board {
695
695
  }
696
696
 
697
697
  addMoveToHistory (from, to) {
698
- this.history.push({ from, to, configuration: JSON.parse(JSON.stringify(this.configuration)) })
698
+ const historyConfiguration = JSON.parse(JSON.stringify(this.configuration))
699
+ // Add computed check property that should be in the history
700
+ historyConfiguration.check = this.hasPlayingPlayerCheck()
701
+ this.history.push({ from, to, configuration: historyConfiguration })
699
702
  }
700
703
 
701
704
  move (from, to) {
package/lib/utils.mjs CHANGED
@@ -35,23 +35,8 @@ export function printToConsole (configuration) {
35
35
  }
36
36
 
37
37
  export function getPieceValue (piece) {
38
- let value = 0
39
- switch (piece) {
40
- case 'K': value = 10; break
41
- case 'Q': value = 9; break
42
- case 'R': value = 5; break
43
- case 'B': value = 3; break
44
- case 'N': value = 3; break
45
- case 'P': value = 1; break
46
- case 'k': value = 10; break
47
- case 'q': value = 9; break
48
- case 'r': value = 5; break
49
- case 'b': value = 3; break
50
- case 'n': value = 3; break
51
- case 'p': value = 1; break
52
- }
53
-
54
- return value
38
+ const values = { k: 10, q: 9, r: 5, b: 3, n: 3, p: 1 }
39
+ return values[piece.toLowerCase()] || 0
55
40
  }
56
41
 
57
42
  export function getFEN (configuration) {
@@ -98,29 +83,25 @@ export function getFEN (configuration) {
98
83
  }
99
84
 
100
85
  export function getJSONfromFEN (fen = '') {
101
- const configuration = {
102
- pieces: {},
103
- }
104
- const parts = fen.split(' ')
105
- const pieces = parts[0]
106
- const player = parts[1]
107
- const castlings = parts[2]
108
- const enPassant = parts[3]
109
- const halfmove = parts[4]
110
- const fullmove = parts[5]
86
+ const [board, player, castlings, enPassant, halfmove, fullmove] = fen.split(' ')
111
87
 
112
88
  // pieces
113
- let columnIndex = 0
114
- let rowIndex = ROWS.length - 1
115
- for (let index = 0; index < pieces.length; index++) {
116
- if (['K', 'Q', 'R', 'B', 'N', 'P', 'k', 'q', 'r', 'b', 'n', 'p'].includes(pieces[index])) {
117
- configuration.pieces[`${COLUMNS[columnIndex]}${ROWS[rowIndex]}`] = pieces[index]; columnIndex++
118
- } else if (['1', '2', '3', '4', '5', '6', '7', '8'].includes(pieces[index])) {
119
- columnIndex += parseInt(pieces[index])
120
- } else if (pieces[index] === '/') {
121
- rowIndex--
122
- columnIndex = 0
123
- }
89
+ const configuration = {
90
+ pieces: Object.fromEntries(board.split('/').flatMap((row, rowIdx) => {
91
+ let colIdx = 0
92
+ return row.split('').reduce((acc, sign) => {
93
+ const piece = sign.match(/k|b|q|n|p|r/i)
94
+ if (piece) {
95
+ acc.push([`${COLUMNS[colIdx]}${ROWS[7 - rowIdx]}`, piece[0]])
96
+ colIdx += 1
97
+ }
98
+ const squares = sign.match(/[1-8]/)
99
+ if (squares) {
100
+ colIdx += Number(squares)
101
+ }
102
+ return acc
103
+ }, [])
104
+ })),
124
105
  }
125
106
 
126
107
  // playing player
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-chess-engine",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Simple and fast Node.js chess engine without dependencies",
5
5
  "keywords": [
6
6
  "chess",
@@ -22,6 +22,9 @@
22
22
  "repository": "github:josefjadrny/js-chess-engine",
23
23
  "homepage": "https://github.com/josefjadrny/js-chess-engine#readme",
24
24
  "license": "MIT",
25
+ "engines": {
26
+ "node": ">=20 <21"
27
+ },
25
28
  "devDependencies": {
26
29
  "chai": "^4.2.0",
27
30
  "conventional-changelog-cli": "^2.0.34",
@@ -36,10 +39,10 @@
36
39
  "eslint-plugin-standard": "^4.0.1",
37
40
  "fastify": "^2.14.1",
38
41
  "fastify-cors": "^3.0.3",
39
- "mocha": "^7.1.2",
40
- "mocha-badge-generator": "^0.6.0",
42
+ "mocha": "^9.1.3",
43
+ "mocha-badge-generator": "^0.3.0",
41
44
  "nodemon": "^2.0.3",
42
45
  "webpack": "^4.43.0",
43
- "webpack-cli": "^3.3.11"
46
+ "webpack-cli": "^4.9.1"
44
47
  }
45
48
  }
package/test/badge.svg CHANGED
@@ -1 +1 @@
1
- <svg xmlns="http://www.w3.org/2000/svg" width="83" height="20"><defs><linearGradient id="smooth" x2="0" y2="100%"><stop offset="0" stop-color="#aaa" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><style>text{font-size:11px;font-family:Verdana,DejaVu Sans,Geneva,sans-serif}text.shadow{fill:#010101;fill-opacity:.3}text.high{fill:#fff}</style><mask id="round"><rect width="100%" height="100%" rx="3" fill="#fff"/></mask></defs><g id="bg" mask="url(#round)"><path fill="#696969" d="M0 0h41v20H0z"/><path fill="#4c1" d="M41 0h42v20H41z"/><path fill="url(#smooth)" d="M0 0h83v20H0z"/></g><g id="fg"><text class="shadow" x="5.5" y="15">Tests</text><text class="high" x="5" y="14">Tests</text><text class="shadow" x="46.5" y="15">55/55</text><text class="high" x="46" y="14">55/55</text></g></svg>
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="83" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><mask id="a"><rect width="83" height="20" rx="3" fill="#fff"/></mask><g mask="url(#a)"><path fill="#555" d="M0 0h41v20H0z"/><path fill="#4C1" d="M41 0h42v20H41z"/><path fill="url(#b)" d="M0 0h83v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,DejaVu Sans,Geneva,sans-serif" font-size="11"><text x="20.5" y="15" fill="#010101" fill-opacity=".3">Tests</text><text x="20.5" y="14">Tests</text><text x="61" y="15" fill="#010101" fill-opacity=".3">56/56</text><text x="61" y="14">56/56</text></g></svg>
@@ -539,7 +539,7 @@ describe('Should properly get history', function () {
539
539
  whiteLong: true,
540
540
  whiteShort: true,
541
541
  },
542
- check: false,
542
+ check: true,
543
543
  checkMate: false,
544
544
  enPassant: null,
545
545
  fullMove: 1,
@@ -631,6 +631,31 @@ describe('Should properly get history', function () {
631
631
 
632
632
  expect(game.getHistory()).to.be.deep.equal([expectedHistory1])
633
633
  })
634
+
635
+ it('should properly track check property through multiple moves', function () {
636
+ const game = new Game({
637
+ turn: 'white',
638
+ pieces: {
639
+ E1: 'K',
640
+ C3: 'R',
641
+ E8: 'k',
642
+ },
643
+ })
644
+
645
+ game.move('C3', 'E3')
646
+ game.move('E8', 'F8')
647
+ game.move('E3', 'F3')
648
+
649
+ const history = game.getHistory()
650
+
651
+ expect(history).to.have.length(3)
652
+ // eslint-disable-next-line no-unused-expressions
653
+ expect(history[0].configuration.check).to.be.false
654
+ // eslint-disable-next-line no-unused-expressions
655
+ expect(history[1].configuration.check).to.be.true
656
+ // eslint-disable-next-line no-unused-expressions
657
+ expect(history[2].configuration.check).to.be.false
658
+ })
634
659
  })
635
660
 
636
661
  describe('Set new piece should', function () {