chessgame-lib-js
Version:
A javascript library for chess logic.
1 lines • 8.39 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["chessgame-lib-js"]=e():t["chessgame-lib-js"]=e()}(this,(()=>(()=>{"use strict";var t={d:(e,s)=>{for(var i in s)t.o(s,i)&&!t.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:s[i]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{START_POSITION:()=>s,default:()=>i});const s="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",i=class{board=[["r","n","b","q","k","b","n","r"],["p","p","p","p","p","p","p","p"],["","","","","","","",""],["","","","","","","",""],["","","","","","","",""],["","","","","","","",""],["P","P","P","P","P","P","P","P"],["R","N","B","Q","K","B","N","R"]];candidates={white:[],black:[]};turn=!0;castle_k=!0;castle_q=!0;castle_K=!0;castle_Q=!0;halfmoves=0;moves=1;enpassant="-";promotion="";constructor(t=null){t&&this.loadFEN(t),this.calculateCandidates(),this.verifyCandidates()}promote(t){if(["Q","R","B","N","q","r","b","n"].includes(t)){t=this.turn?t.toUpperCase():t.toLowerCase();const[e,s]=this.getCoordsByName(this.promotion);this.board[e][s]=t,this.promotion=""}}makeMove(t,e=null){if(""!==this.promotion)return!1;if(-1===this.candidates[this.turn?"white":"black"].indexOf(t))return!1;const[s,i,a]=this.moveToArray(t);let h=`${s.toUpperCase()}${i}-${a}`;"e1-g1"===t?(this.movePiece(t),this.movePiece("h1-f1")):"e1-c1"===t?(this.movePiece(t),this.movePiece("a1-d1")):"e8-g8"===t?(this.movePiece(t),this.movePiece("h8-f8")):"e8-c8"===t?(this.movePiece(t),this.movePiece("a8-d8")):this.movePiece(t),"e1"===i?(this.castle_K=!1,this.castle_Q=!1):"a1"===i?this.castle_Q=!1:"h1"===i?this.castle_K=!1:"e8"===i?(this.castle_k=!1,this.castle_q=!1):"a8"===i?this.castle_q=!1:"h8"===i&&(this.castle_k=!1);const r=this.enpassant;if("p"===s&&"7"===i[1]&&"5"===a[1]?this.enpassant=`${i[0]}6`:"P"===s&&"2"===i[1]&&"4"===a[1]?this.enpassant=`${i[0]}3`:this.enpassant="-",("p"===s&&"1"===a[1]||"P"===s&&"8"===a[1])&&(this.promotion=a,e&&this.promote(e)),"p"===s||"P"===s||""!==this.getFieldByName(a)?this.halfmoves=0:this.halfmoves++,this.turn||this.moves++,a===r){let t=this.getCoordsByName(i),e=this.getCoordsByName(a);this.board[t[0]][e[1]]=""}return this.turn=!this.turn,this.calculateCandidates(),this.verifyCandidates(),h}moveToArray(t){let[e,s]=t.split("-");var i;return(i=e[0]).charCodeAt(0)>=65&&i.charCodeAt(0)<=90&&(e=e.substring(1)),[this.getFieldByName(e),e,s]}calculateCandidates(){const t={white:[],black:[]};this.board.map(((e,s)=>{e.map(((e,i)=>{if(""===e)return;let a=[s,i],h=[0,0],r=this.isWhite(e);const o=()=>{this.isOnBoard(h)&&t[r?"white":"black"].push(`${this.getNameByCoords(a)}-${this.getNameByCoords(h)}`)},l=t=>(h=this.sumVectors(a,t),!(!this.isOnBoard(h)||r&&this.isWhite(this.getFieldByVector(h))||!r&&this.isBlack(this.getFieldByVector(h))||(r&&this.isBlack(this.getFieldByVector(h))||!r&&this.isWhite(this.getFieldByVector(h))?(o(),1):(o(),0))));switch(e){case"p":h=this.sumVectors(a,[1,0]),this.isOnBoard(h)&&""===this.getFieldByVector(h)&&(o(),1===s&&(h=this.sumVectors(a,[2,0]),""===this.getFieldByVector(h)&&o())),h=this.sumVectors(a,[1,1]),this.isOnBoard(h)&&(this.isWhite(this.getFieldByVector(h))||this.getNameByCoords(h)===this.enpassant)&&o(),h=this.sumVectors(a,[1,-1]),this.isOnBoard(h)&&(this.isWhite(this.getFieldByVector(h))||this.getNameByCoords(h)===this.enpassant)&&o();break;case"P":h=this.sumVectors(a,[-1,0]),this.isOnBoard(h)&&""===this.getFieldByVector(h)&&(o(),6===s&&(h=this.sumVectors(a,[-2,0]),""===this.getFieldByVector(h)&&o())),h=this.sumVectors(a,[-1,1]),this.isOnBoard(h)&&(this.isBlack(this.getFieldByVector(h))||this.getNameByCoords(h)===this.enpassant)&&o(),h=this.sumVectors(a,[-1,-1]),this.isOnBoard(h)&&(this.isBlack(this.getFieldByVector(h))||this.getNameByCoords(h)===this.enpassant)&&o();break;case"R":case"r":for(let t=1;t<8&&l([t,0]);t++);for(let t=1;t<8&&l([-t,0]);t++);for(let t=1;t<8&&l([0,t]);t++);for(let t=1;t<8&&l([0,-t]);t++);break;case"b":case"B":for(let t=1;t<8&&l([t,t]);t++);for(let t=1;t<8&&l([t,-t]);t++);for(let t=1;t<8&&l([-t,t]);t++);for(let t=1;t<8&&l([-t,-t]);t++);break;case"n":case"N":l([2,1]),l([2,-1]),l([-2,1]),l([-2,-1]),l([1,2]),l([1,-2]),l([-1,2]),l([-1,-2]);break;case"q":case"Q":for(let t=1;t<8&&l([t,0]);t++);for(let t=1;t<8&&l([-t,0]);t++);for(let t=1;t<8&&l([0,t]);t++);for(let t=1;t<8&&l([0,-t]);t++);for(let t=1;t<8&&l([t,t]);t++);for(let t=1;t<8&&l([t,-t]);t++);for(let t=1;t<8&&l([-t,t]);t++);for(let t=1;t<8&&l([-t,-t]);t++);break;case"k":case"K":l([-1,-1]),l([-1,0]),l([-1,1]),l([0,-1]),l([0,1]),l([1,-1]),l([1,0]),l([1,1])}}))})),this.castle_k&&""===this.getFieldByName("f8")&&""===this.getFieldByName("g8")&&0===t.white.map((t=>{if("f8"===t.split("-")[1]||"g8"===t.split("-")[1]||"e8"===t.split("-")[1])return t})).filter(Boolean).length&&t.black.push("e8-g8"),this.castle_q&&""===this.getFieldByName("b8")&&""===this.getFieldByName("c8")&&""===this.getFieldByName("d8")&&0===t.white.map((t=>{if("b8"===t.split("-")[1]||"c8"===t.split("-")[1]||"d8"===t.split("-")[1]||"e8"===t.split("-")[1])return t})).filter(Boolean).length&&t.black.push("e8-c8"),this.castle_k&&""===this.getFieldByName("f1")&&""===this.getFieldByName("g1")&&0===t.black.map((t=>{if("f1"===t.split("-")[1]||"g1"===t.split("-")[1]||"e1"===t.split("-")[1])return t})).filter(Boolean).length&&t.white.push("e1-g1"),this.castle_q&&""===this.getFieldByName("b1")&&""===this.getFieldByName("c1")&&""===this.getFieldByName("d1")&&0===t.black.map((t=>{if("b1"===t.split("-")[1]||"c1"===t.split("-")[1]||"d1"===t.split("-")[1]||"e1"===t.split("-")[1])return t})).filter(Boolean).length&&t.white.push("e1-c1"),this.candidates=t}verifyCandidates(){let t=[...this.candidates[this.turn?"white":"black"]],e=this.board.map((t=>t.slice())),s=JSON.stringify(this.candidates);t.map((t=>{this.movePiece(t),this.calculateCandidates(),this.isCheck()&&(this.candidates=JSON.parse(s),this.candidates[this.turn?"white":"black"].splice(this.candidates[this.turn?"white":"black"].indexOf(t),1),s=JSON.stringify(this.candidates)),this.board=e.map((t=>t.slice())),this.candidates=JSON.parse(s)}))}movePiece(t){let[e,s,i]=this.moveToArray(t);const a=this.getCoordsByName(s),h=this.getCoordsByName(i);this.board[a[0]][a[1]]="",this.board[h[0]][h[1]]=e}isCheck(){const t=[...this.candidates[this.turn?"black":"white"]];let e="";return this.board.forEach(((t,s)=>{t.forEach(((t,i)=>{t===(this.turn?"K":"k")&&(e=this.getNameByCoords([s,i]))}))})),0!==t.filter((t=>t.split("-")[1]===e)).length}isMate(){return this.isCheck()&&0===this.candidates[this.turn?"white":"black"].length}getFEN(){let t="";return this.board.map(((e,s)=>{let i=0;e.map((e=>{""!==e?(i>0&&(t+=i,i=0),t+=e):i++})),i>0&&(t+=i),s<this.board.length-1&&(t+="/")})),t+=` ${this.turn?"w":"b"} ${this.castle_K?"K":""}${this.castle_Q?"Q":""}${this.castle_k?"k":""}${this.castle_q?"q":""}${this.castle_k||this.castle_q||this.castle_K||this.castle_Q?"":"-"} ${this.enpassant} ${this.halfmoves} ${this.moves}`,t}loadFEN(t){const e=[],[s,i,a,h,r,o]=t.split(" ");s.split("/").forEach(((t,s)=>{const i=[];t.split("").forEach((t=>{if(parseInt(t))for(let e=0;e<parseInt(t);e++)i.push("");else i.push(t)})),e.push(i)})),this.board=e,this.turn="w"===i,this.castle_k=!1,this.castle_q=!1,this.castle_K=!1,this.castle_Q=!1,a.split("").forEach((t=>{switch(t){case"K":this.castle_K=!0;break;case"Q":this.castle_Q=!0;break;case"k":this.castle_k=!0;break;case"q":this.castle_q=!0}})),this.enpassant=h,this.halfmoves=r,this.moves=o}isWhite(t){return t.charCodeAt(0)>=65&&t.charCodeAt(0)<=90}isBlack(t){return t.charCodeAt(0)>=97&&t.charCodeAt(0)<=122}isOnBoard([t,e]){return t>=0&&t<8&&e>=0&&e<8}getFieldByName(t){const[e,s]=this.getCoordsByName(t);return this.isOnBoard([e,s])?this.board[e][s]:""}getNameByCoords([t,e]){return`${String.fromCharCode(97+e)}${8-t}`}getCoordsByName(t){return 2===t.length?[8-+(t=t.toLowerCase())[1],t.charCodeAt(0)-97]:[-1,-1]}getFieldByVector(t){return this.isOnBoard(t)?this.board[t[0]][t[1]]:""}sumVectors(t,e){return t.map(((t,s)=>t+e[s]))}moveToString(t,e){return piece=this.getFieldByName(t),`${"P"!=piece.toUpperCase()?piece.toUpperCase():""}${t}-${e}`}};return e})()));