/**
 * Minified by jsDelivr using Terser v5.37.0.
 * Original file: /npm/chess-js@2.0.0/build/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";function __extends(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);function o(){this.constructor=t}t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}Object.defineProperty(exports,"__esModule",{value:!0});var Coordinates=function(){function t(t,e){this.col=t,this.row=e}return t.from=function(t,e){return new this(t,e)},t.fromIndex=function(t){return new this(t%8,Math.floor(t/8))},t.prototype.buildPath=function(t){for(var e=t(this),r=[];null!==e;)r.push(e),e=t(e);return r},Object.defineProperty(t.prototype,"index",{get:function(){return 8*this.row+this.col},enumerable:!0,configurable:!0}),t.prototype.up=function(){return this.row<=0?null:t.from(this.col,this.row-1)},t.prototype.down=function(){return this.row>=7?null:t.from(this.col,this.row+1)},t.prototype.right=function(){return this.col>=7?null:t.from(this.col+1,this.row)},t.prototype.left=function(){return this.col<=0?null:t.from(this.col-1,this.row)},t.prototype.upleft=function(){var t=this.up();return t&&t.left()},t.prototype.upright=function(){var t=this.up();return t&&t.right()},t.prototype.downleft=function(){var t=this.down();return t&&t.left()},t.prototype.downright=function(){var t=this.down();return t&&t.right()},t.prototype.upPath=function(){return this.buildPath((function(t){return t.up()}))},t.prototype.downPath=function(){return this.buildPath((function(t){return t.down()}))},t.prototype.leftPath=function(){return this.buildPath((function(t){return t.left()}))},t.prototype.rightPath=function(){return this.buildPath((function(t){return t.right()}))},t.prototype.upleftPath=function(){return this.buildPath((function(t){return t.upleft()}))},t.prototype.uprightPath=function(){return this.buildPath((function(t){return t.upright()}))},t.prototype.downleftPath=function(){return this.buildPath((function(t){return t.downleft()}))},t.prototype.downrightPath=function(){return this.buildPath((function(t){return t.downright()}))},t}();!function(t){t[t.None=0]="None",t[t.CPU=1]="CPU",t[t.Human=2]="Human"}(exports.Player||(exports.Player={}));var extractWeight=function(t){var e=t.piece,r=t.coordinates;return e.weight(r)},sum=function(t,e){return t+e},score=function(t,e){return void 0===e&&(e=0),+t.fieldsByPlayer(exports.Player.CPU).map(extractWeight).reduce(sum,0).toFixed(2)-+t.fieldsByPlayer(exports.Player.Human).map(extractWeight).reduce(sum,0).toFixed(2)-e},Move=function(){function t(t,e){this.from=t,this.to=e}return t.sortBy=function(t,e){return function(r,o){return t===exports.Player.CPU?score(e.makeMove(o))-score(e.makeMove(r)):score(e.makeMove(r))-score(e.makeMove(o))}},t}(),EvaluatedMove=function(t){function e(e,r,o){var n=t.call(this,e,r)||this;return n.score=o,n}return __extends(e,t),e.from=function(t,r){return new e(t.from,t.to,r)},e.sortBy=function(t){return function(e,r){return t===exports.Player.CPU?r.score-e.score:e.score-r.score}},e}(Move),Field=function(){function t(t,e){void 0===e&&(e=null),this._coordinates=t,this._piece=e}return Object.defineProperty(t.prototype,"piece",{get:function(){return this._piece},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"coordinates",{get:function(){return this._coordinates},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"isEmpty",{get:function(){return null==this._piece},enumerable:!0,configurable:!0}),t.prototype.clone=function(e){void 0===e&&(e=null);var r=this._coordinates,o=r.col,n=r.row,i=this._piece;return new t(Coordinates.from(o,n),i&&i.clone()||e)},t.prototype.toString=function(){var t=this._coordinates,e=t.row,r=t.col;return"["+(this.isEmpty?" ":this._piece.toString())+("("+r+","+e+")")+"]"},t.prototype.possibleMoves=function(t){var e=this;return this.isEmpty?[]:this._piece.possibleMoves(this._coordinates,t).map((function(t){return new Move(e._coordinates,t)}))},t}(),Piece=function(){function t(t){this.player=t,this._weight=0}return t.prototype.possibleMoves=function(t,e){return[]},t.prototype.weight=function(t){return this._weight},t.prototype.toString=function(){return""},t.prototype.clone=function(){throw new Error("not implemented in abstract class")},t.prototype.possiblePathMoves=function(t,e,r){void 0===r&&(r=!0);for(var o=[],n=0,i=t;n<i.length;n++){var u=i[n],s=e.at(u);if(!s.isEmpty){if(s.piece.player!==this.player){r&&o.push(u);break}break}o.push(u)}return o},t}(),Pawn=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e._weight=1,e}return __extends(e,t),e.prototype.toString=function(){return this.player==exports.Player.Human?"♙":"♟"},e.prototype.weight=function(t){var e=t.row;return this.player===exports.Player.Human?this._weight-.01*e:this._weight-.01*(7-e)},e.prototype.possibleMoves=function(t,e){var r=this,o=this.player===exports.Player.Human,n=[];if(o){var i=t.up();if(null!==i&&e.at(i).isEmpty&&(n.push(i),6===t.row)){var u=i.up();e.at(u).isEmpty&&n.push(u)}}else{var s=t.down();if(null!==s&&e.at(s).isEmpty&&(n.push(s),1===t.row)){var a=s.down();e.at(a).isEmpty&&n.push(a)}}var p=o?[t.upleft(),t.upright()]:[t.downleft(),t.downright()];return p=p.filter((function(t){return null!==t&&!e.at(t).isEmpty&&e.at(t).piece.player!==r.player})),n.concat(p)},e.prototype.clone=function(){return new e(this.player)},e}(Piece),Rook=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e._weight=2,e}return __extends(e,t),e.prototype.toString=function(){return this.player==exports.Player.Human?"♖":"♜"},e.prototype.clone=function(){return new e(this.player)},e.prototype.possibleMoves=function(t,e){return this.possiblePathMoves(t.upPath(),e).concat(this.possiblePathMoves(t.downPath(),e),this.possiblePathMoves(t.leftPath(),e),this.possiblePathMoves(t.rightPath(),e))},e}(Piece),Knight=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e._weight=3,e}return __extends(e,t),e.prototype.toString=function(){return this.player==exports.Player.Human?"♘":"♞"},e.prototype.clone=function(){return new e(this.player)},e.prototype.possibleMoves=function(t,e){var r=this;return[t.up()&&t.up().upleft(),t.up()&&t.up().upright(),t.right()&&t.right().upright(),t.right()&&t.right().downright(),t.down()&&t.down().downright(),t.down()&&t.down().downleft(),t.left()&&t.left().downleft(),t.left()&&t.left().upleft()].filter((function(t){return null!==t&&(e.at(t).isEmpty||e.at(t).piece.player!==r.player)}))},e}(Piece),Bishop=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e._weight=2,e}return __extends(e,t),e.prototype.toString=function(){return this.player==exports.Player.Human?"♗":"♝"},e.prototype.clone=function(){return new e(this.player)},e.prototype.possibleMoves=function(t,e){return this.possiblePathMoves(t.upleftPath(),e).concat(this.possiblePathMoves(t.uprightPath(),e),this.possiblePathMoves(t.downleftPath(),e),this.possiblePathMoves(t.downrightPath(),e))},e}(Piece),Queen=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e._weight=5,e}return __extends(e,t),e.prototype.toString=function(){return this.player==exports.Player.Human?"♕":"♛"},e.prototype.clone=function(){return new e(this.player)},e.prototype.possibleMoves=function(t,e){return this.possiblePathMoves(t.upPath(),e).concat(this.possiblePathMoves(t.uprightPath(),e),this.possiblePathMoves(t.rightPath(),e),this.possiblePathMoves(t.downrightPath(),e),this.possiblePathMoves(t.downPath(),e),this.possiblePathMoves(t.downleftPath(),e),this.possiblePathMoves(t.leftPath(),e),this.possiblePathMoves(t.upleftPath(),e))},e}(Piece),King=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e._weight=8,e}return __extends(e,t),e.prototype.toString=function(){return this.player==exports.Player.Human?"♔":"♚"},e.prototype.clone=function(){return new e(this.player)},e.prototype.possibleMoves=function(t,e){var r=this;return[t.up(),t.upright(),t.right(),t.downright(),t.down(),t.downleft(),t.left(),t.upleft()].filter((function(t){return null!==t&&(e.at(t).isEmpty||e.at(t).piece.player!==r.player)}))},e}(Piece),deserialize=function(t){switch(t){case"♟":return new Pawn(exports.Player.CPU);case"♜":return new Rook(exports.Player.CPU);case"♞":return new Knight(exports.Player.CPU);case"♝":return new Bishop(exports.Player.CPU);case"♛":return new Queen(exports.Player.CPU);case"♚":return new King(exports.Player.CPU);case"♙":return new Pawn(exports.Player.Human);case"♖":return new Rook(exports.Player.Human);case"♘":return new Knight(exports.Player.Human);case"♗":return new Bishop(exports.Player.Human);case"♕":return new Queen(exports.Player.Human);case"♔":return new King(exports.Player.Human);default:return null}},opponent=function(t){var e=exports.Player.CPU,r=exports.Player.Human,o=exports.Player.None;switch(t){case e:return r;case r:return e;default:return o}},Board=function(){function t(t){this._fields=t}return Object.defineProperty(t.prototype,"fields",{get:function(){return this._fields},enumerable:!0,configurable:!0}),t.singleRowGenerator=function(){return Array.apply(null,{length:8}).map(Number.call,Number)},t.coordGenerator=function(e){return t.singleRowGenerator().map((function(t){return Coordinates.from(t,e)}))},t.piecesRow=function(t,e){return[new Rook(e),new Knight(e),new Bishop(e),new Queen(e),new King(e),new Bishop(e),new Knight(e),new Rook(e)].map((function(e,r){return new Field(t[r],e)}))},t.pawnRow=function(t,e){return t.slice().map((function(t){return new Field(t,new Pawn(e))}))},t.emptyRow=function(t){return t.slice().map((function(t){return new Field(t)}))},t.emptyGame=function(){var e=t.emptyRow(t.coordGenerator(0)).concat(t.emptyRow(t.coordGenerator(1)),t.emptyRow(t.coordGenerator(2)),t.emptyRow(t.coordGenerator(3)),t.emptyRow(t.coordGenerator(4)),t.emptyRow(t.coordGenerator(5)),t.emptyRow(t.coordGenerator(6)),t.emptyRow(t.coordGenerator(7)));return new t(e)},t.fromJSON=function(e){return new t(e.map((function(t,e){return new Field(Coordinates.fromIndex(e),deserialize(t))})))},t.newGame=function(){var e=t.piecesRow(t.coordGenerator(0),exports.Player.CPU).concat(t.pawnRow(t.coordGenerator(1),exports.Player.CPU),t.emptyRow(t.coordGenerator(2)),t.emptyRow(t.coordGenerator(3)),t.emptyRow(t.coordGenerator(4)),t.emptyRow(t.coordGenerator(5)),t.pawnRow(t.coordGenerator(6),exports.Player.Human),t.piecesRow(t.coordGenerator(7),exports.Player.Human));return new t(e)},t.prototype.at=function(t){return this.fields[t.index]},t.prototype.clone=function(){return new t(this.fields.map((function(t){return t.clone()})))},t.prototype.setAt=function(t,e){var r=t.row,o=t.col,n=t.index,i=this.clone();return i._fields[n]=new Field(Coordinates.from(o,r),e),i},t.prototype.emptyAt=function(t){return this.setAt(t,null)},t.prototype.makeMove=function(t){var e=t.from,r=t.to,o=this.at(e),n=o.isEmpty,i=o.piece;if(n)throw new Error("can`t make move from empty field");return this.emptyAt(e).setAt(r,i.clone())},t.prototype.toString=function(){var t="";return this.fields.forEach((function(e,r){t+=""+e+((r+1)%8==0?"\n":"")})),t},Object.defineProperty(t.prototype,"gameOver",{get:function(){return this.winner!==exports.Player.None},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"winner",{get:function(){var t=this.fieldsByPlayer(exports.Player.CPU).concat(this.fieldsByPlayer(exports.Player.Human)).filter((function(t){return t.piece instanceof King})).map((function(t){return t.piece})),e=t[0],r=t[1];return e&&r?exports.Player.None:e.player},enumerable:!0,configurable:!0}),t.prototype.fieldsByPlayer=function(t){return this.fields.filter((function(e){return!e.isEmpty&&e.piece.player===t}))},t.prototype.possibleMoves=function(t){var e=this;return this.fieldsByPlayer(t).map((function(t){return t.possibleMoves(e)})).reduce((function(t,e){return t.concat(e)}),[])},t.prototype.toJSON=function(){return this.fields.map((function(t){var e=t.isEmpty,r=t.piece;return e?null:""+r}))},t}(),DEFAULT_DEPTH=4,MAX_SCORE=Board.newGame().fieldsByPlayer(exports.Player.CPU).map(extractWeight).reduce(sum),cpu=function(t,e,r){void 0===e&&(e=!1),void 0===r&&(r=DEFAULT_DEPTH);var o=e?alphabeta(t,r):minimax(t,r);return t.makeMove(o)},minimax=function(t,e,r,o){void 0===e&&(e=DEFAULT_DEPTH),void 0===r&&(r=exports.Player.CPU);var n=t.gameOver,i=t.winner;if(n){if(i===exports.Player.CPU)return EvaluatedMove.from(o,MAX_SCORE-e);if(i===exports.Player.Human)return EvaluatedMove.from(o,e-MAX_SCORE)}if(e<=0)return EvaluatedMove.from(o,score(t,e));var u=opponent(r),s=t.possibleMoves(r).map((function(r){var o=t.makeMove(r),n=minimax(o,e-1,u,r).score;return EvaluatedMove.from(r,n)})),a=s.sort(EvaluatedMove.sortBy(r))[0];return a||EvaluatedMove.from(o,score(t,e))},alphabeta=function(t,e,r,o,n,i){void 0===e&&(e=DEFAULT_DEPTH),void 0===r&&(r=exports.Player.CPU),void 0===n&&(n=-1/0),void 0===i&&(i=1/0);var u=t.gameOver,s=t.winner;if(u){if(s===exports.Player.CPU)return EvaluatedMove.from(o,MAX_SCORE-e);if(s===exports.Player.Human)return EvaluatedMove.from(o,e-MAX_SCORE)}if(e<=0)return EvaluatedMove.from(o,score(t,e));for(var a=opponent(r),p=t.possibleMoves(r).sort(Move.sortBy(r,t)),l=r===exports.Player.CPU,c=[],f=0,h=p;f<h.length;f++){var y=h[f],d=t.makeMove(y),P=alphabeta(d,e-1,a,y,n,i).score;if(c.push(EvaluatedMove.from(y,P)),l?P>n&&(n=P):P<i&&(i=P),n>=i)break}var w=c.sort(EvaluatedMove.sortBy(r))[0];return w||EvaluatedMove.from(o,score(t,e))};exports.Board=Board,exports.opponent=opponent,exports.Field=Field,exports.Coordinates=Coordinates,exports.Move=Move,exports.EvaluatedMove=EvaluatedMove,exports.cpu=cpu,exports.minimax=minimax,exports.alphabeta=alphabeta,exports.Pawn=Pawn,exports.Rook=Rook,exports.Knight=Knight,exports.Bishop=Bishop,exports.Queen=Queen,exports.King=King,exports.Piece=Piece,exports.extractWeight=extractWeight,exports.score=score,exports.sum=sum;
//# sourceMappingURL=/sm/52d5c07f84a580b44c2279a78083bcfc6e5e5be1bb3a16aae3cccd1b71f2c9c2.map