(window.webpackJsonp=window.webpackJsonp||[]).push([[0],{17:function(e,n,t){e.exports=t(29)},28:function(e,n,t){},29:function(e,n,t){"use strict";t.r(n);t(18);var i=t(0),r=t.n(i),a=t(9),o=t.n(a),l=t(32),c=t(10),s=t(11),u=t(15),p=t(12),f=t(16),k=t(14),v=t(2),m=t(5),y=function(e,n){return{x:120*n,y:960-120*e-120}},d=function(e){var n=e.gamePlayState,t=e.onSelectSquare,i=[0,1,2,3,4,5,6,7],a=!1;return r.a.createElement(r.a.Fragment,null,[0,1,2,3,4,5,6,7].map(function(e){return a=!a,i.map(function(i){var o=(a=!a)?"White":"Black";return r.a.createElement(h,{rank:e,file:i,player:o,gamePlayState:n,onSelectSquare:t})})}))},h=function(e){var n=e.rank,t=e.file,a=e.player,o=e.gamePlayState,l=e.onSelectSquare,c="White"===a?"#fff3b0":"#9e2a2b",s=o.selectedSquare,u=s&&s.file===t&&s.rank===n,p=y(n,t),f=p.x,k=p.y;return r.a.createElement(i.Fragment,{key:"Frag".concat(n,",").concat(t)},r.a.createElement("rect",{key:"Square".concat(n,",").concat(t),x:f,y:k,width:120,height:120,onClick:function(){return l(n,t)},style:{fill:c,stroke:"black",strokeWidth:u?5:2,opacity:1}}),r.a.createElement("text",{key:"Info".concat(n,",").concat(t),x:f+5,y:k+15,fill:"black"},"R: ",n,", F: ",t))},g=function(e){var n,t=e.piece,i=e.gamePlayState,a=e.onSelectPiece,o=e.x,l=e.y;switch(t.type){case"Pawn":n=v.d;break;case"Rook":n=v.f;break;case"Knight":n=v.c;break;case"Bishop":n=v.a;break;case"Queen":n=v.e;break;case"King":n=v.b;break;default:n=v.g}var c="White"===t.player?"#e09f3e":"black";return i.selectedPiece&&i.selectedPiece.id===t.id&&(c="green"),r.a.createElement(m.a,{key:t.id,onClick:function(){return a(t)},icon:n,width:84,height:84,x:o,y:l,style:{color:c}})},B=t(3),b=function(e,n){switch(n.type){case"Pawn":return C(e,n);case"Knight":return P(e,n);case"Rook":return w(e,n);case"Bishop":return W(e,n);case"King":return E(e,n);case"Queen":return K(e,n)}},C=function(e,n){var t=[],i="White"===n.player?1:-1,r=n.position.rank+i,a=G(e,r,n.position.file);a||(t.push({rank:r,file:n.position.file}),0===n.moveCount&&((a=G(e,r+=i,n.position.file))||t.push({rank:r,file:n.position.file}))),r=n.position.rank+i;var o=n.position.file+1;return(a=G(e,r,o))&&a.player!==n.player&&t.push({rank:r,file:o}),r=n.position.rank+i,o=n.position.file-1,(a=G(e,r,o))&&a.player!==n.player&&t.push({rank:r,file:o}),t},P=function(e,n){var t=M(n.position,[1,-1],[2,-2]),i=M(n.position,[2,-2],[1,-1]);return t.concat(i).reduce(function(t,i){if(q(i))return t;var r=G(e,i.rank,i.file);return r&&r.player===n.player||t.push(i),t},[])},w=function(e,n){var t=x(e,n);return t=t.concat(O(e,n))},W=function(e,n){var t=R(e,n,!0,!0);return t=(t=(t=t.concat(R(e,n,!0,!1))).concat(R(e,n,!1,!0))).concat(R(e,n,!1,!1))},E=function(e,n){var t=M(n.position,[1,0,-1],[1,0,-1]).reduce(function(t,i){if(q(i))return t;var r=G(e,i.rank,i.file);return r&&r.player===n.player||t.push(i),t},[]);return t=t.concat(S(e,n))},S=function(e,n){var t=[];if(n.moveCount>0)return t;if("White"===n.player){var i=j(e,"White Kings Rook");i&&0===i.moveCount&&null===G(e,0,1)&&null===G(e,0,2)&&null===G(e,0,3)&&t.push({rank:0,file:2}),(i=j(e,"White Queens Rook"))&&0===i.moveCount&&null===G(e,0,5)&&null===G(e,0,6)&&t.push({rank:0,file:6})}else{var r=j(e,"Black Kings Rook");r&&0===r.moveCount&&null===G(e,7,5)&&null===G(e,7,6)&&t.push({rank:7,file:6}),(r=j(e,"Black Queens Rook"))&&0===r.moveCount&&null===G(e,7,1)&&null===G(e,7,2)&&null===G(e,7,3)&&t.push({rank:7,file:2})}return t},K=function(e,n){var t=R(e,n,!0,!0);return t=(t=(t=(t=(t=t.concat(R(e,n,!0,!1))).concat(R(e,n,!1,!0))).concat(R(e,n,!1,!1))).concat(x(e,n))).concat(O(e,n))},x=function(e,n){for(var t=[],i=n.position.rank+1;i<8;i++){var r={rank:i,file:n.position.file},a=G(e,r.rank,r.file);if(a&&a.player===n.player||t.push(r),a)break}for(var o=n.position.rank-1;o>=0;o--){var l={rank:o,file:n.position.file},c=G(e,l.rank,l.file);if(c&&c.player===n.player||t.push(l),c)break}return t},O=function(e,n){for(var t=[],i=n.position.file+1;i<8;i++){var r={rank:n.position.rank,file:i},a=G(e,r.rank,r.file);if(a&&a.player===n.player||t.push(r),a)break}for(var o=n.position.file-1;o>=0;o--){var l={rank:n.position.rank,file:o},c=G(e,l.rank,l.file);if(c&&c.player===n.player||t.push(l),c)break}return t},R=function(e,n,t,i){for(var r=[],a=t?1:-1,o=i?1:-1,l=1;l<8;l++){var c={rank:n.position.rank+l*a,file:n.position.file+l*o};if(q(c))break;var s=G(e,c.rank,c.file);if(s&&s.player===n.player||r.push(c),s)break}return r},M=function(e,n,t){var i=[];return n.forEach(function(n){t.forEach(function(t){var r={rank:e.rank+n,file:e.file+t};r.rank>=0&&r.rank<8&&r.file>=0&&r.file<8&&(e.file!==r.file||e.rank!==r.rank)&&i.push(r)})}),i},j=function(e,n){return e.find(function(e){return e.id===n})},Q=function(e,n){var t=b(e,n),i=!0,r=!1,a=void 0;try{for(var o,l=t[Symbol.iterator]();!(i=(o=l.next()).done);i=!0){var c=o.value,s={currentBoard:{pieces:e},moves:[],currentState:"Playing",playersTurn:n.player};if(F(s,n,c),!T(s.currentBoard.pieces,n.player))return!0}}catch(u){r=!0,a=u}finally{try{i||null==l.return||l.return()}finally{if(r)throw a}}return!1},T=function(e,n){var t=e.find(function(e){return e.player===n&&"King"===e.type});if(!t)throw Error("Failed to find king");var i=e.filter(function(e){return e.player!==n&&!e.isOffBoard}),r=!0,a=!1,o=void 0;try{for(var l,c=i[Symbol.iterator]();!(r=(l=c.next()).done);r=!0){var s=l.value;if(b(e,s).find(function(e){return e.rank===t.position.rank&&e.file===t.position.file}))return s}}catch(u){a=!0,o=u}finally{try{r||null==c.return||c.return()}finally{if(a)throw o}}return null},q=function(e){return e.file<0||e.file>7||(e.rank<0||e.rank>7)},G=function(e,n,t){return q({rank:n,file:t})?null:e.find(function(e){return!e.isOffBoard&&e.position.file===t&&e.position.rank===n})||null},I=function(){for(var e=[{id:"White Queens Rook",type:"Rook",position:{rank:0,file:0},player:"White",moveCount:0},{id:"White Queens Knight",type:"Knight",position:{rank:0,file:1},player:"White",moveCount:0},{id:"White Queens Bishop",type:"Bishop",position:{rank:0,file:2},player:"White",moveCount:0},{id:"White Queen",type:"Queen",position:{rank:0,file:3},player:"White",moveCount:0},{id:"White King",type:"King",position:{rank:0,file:4},player:"White",moveCount:0},{id:"White Kings Bishop",type:"Bishop",position:{rank:0,file:5},player:"White",moveCount:0},{id:"White Kings Knight",type:"Knight",position:{rank:0,file:6},player:"White",moveCount:0},{id:"White Kings Rook",type:"Rook",position:{rank:0,file:7},player:"White",moveCount:0},{id:"Black Queens Rook",type:"Rook",position:{rank:7,file:0},player:"Black",moveCount:0},{id:"Black Queens Knight",type:"Knight",position:{rank:7,file:1},player:"Black",moveCount:0},{id:"Black Queens Bishop",type:"Bishop",position:{rank:7,file:2},player:"Black",moveCount:0},{id:"Black Queen",type:"Queen",position:{rank:7,file:3},player:"Black",moveCount:0},{id:"Black King",type:"King",position:{rank:7,file:4},player:"Black",moveCount:0},{id:"Black Kings Bishop",type:"Bishop",position:{rank:7,file:5},player:"Black",moveCount:0},{id:"Black Kings Knight",type:"Knight",position:{rank:7,file:6},player:"Black",moveCount:0},{id:"Black Kings Rook",type:"Rook",position:{rank:7,file:7},player:"Black",moveCount:0}],n=0;n<8;n++)e.push({id:"White Pawn ".concat(n),type:"Pawn",position:{rank:1,file:n},player:"White",moveCount:0}),e.push({id:"Black Pawn ".concat(n),type:"Pawn",position:{rank:6,file:n},player:"Black",moveCount:0});return{pieces:e}},F=function(e,n,t){if(!N(e,n,t)){var i=G(e.currentBoard.pieces,t.rank,t.file),r={player:n.player,pieceMoves:[{piece:n,startPosition:{rank:n.position.rank,file:n.position.file},endPosition:t}]};i&&(i.isOffBoard=!0,r.pieceMoves.push({piece:i,startPosition:{rank:i.position.rank,file:i.position.file},endPosition:{rank:-1,file:-1}})),e.moves.push(r),n.position=t,n.moveCount=n.moveCount+1}},N=function(e,n,t){if(0!==n.moveCount)return!1;var i={rank:n.position.rank,file:n.position.file},r={rank:0,file:0},a=null;if("King"===n.type&&"White"===n.player&&0===t.rank&&2===t.file)a="White Queens Rook",r={rank:0,file:3};else if("King"===n.type&&"White"===n.player&&0===t.rank&&6===t.file)a="White Kings Rook",r={rank:0,file:5};else if("King"===n.type&&"Black"===n.player&&7===t.rank&&2===t.file)a="Black Queens Rook",r={rank:7,file:3};else{if("King"!==n.type||"Black"!==n.player||7!==t.rank||6!==t.file)return!1;a="Black Kings Rook",r={rank:7,file:5}}var o=j(e.currentBoard.pieces,a);if(!o||0!==o.moveCount)return!1;var l={player:n.player,pieceMoves:[{piece:n,startPosition:i,endPosition:t},{piece:o,startPosition:o.position,endPosition:r}]};return e.moves.push(l),n.position=t,n.moveCount=n.moveCount+1,o.position=r,o.moveCount=o.moveCount+1,!0},A={messages:[]},D=function(e,n){switch(console.log("gamePlayStateReducer",n),n.type){case"setGame":var t=Object(B.a)({},e);return t.game=n.game,t;case"movePiece":return J(e,n.piece,n.position);case"selectPiece":var i=Object(B.a)({},e);return i.selectedPiece=n.piece,i.possibleMoves=void 0,i;case"selectSquare":var r=Object(B.a)({},e);return r.selectedSquare=n.square,r;case"setPossibleMoves":var a=Object(B.a)({},e);return a.possibleMoves=n.moves,a;case"undoMove":return e.game&&(e.game.moves&&0!==e.game.moves.length)?(L(e.game),e.selectedPiece=void 0,e.possibleMoves=void 0,Object(B.a)({},e)):e;case"redoMove":return Object(B.a)({},e);case"saveGame":return e.game&&(o=e.game,console.log("saveChessGame",o)),Object(B.a)({},e);default:return console.warn("gamePlayStateReducer: unknown action:"+n.type),e}var o},J=function(e,n,t){if(!e.game)return e;var i=Object(B.a)({},e);if(!i.game)return e;F(e.game,n,t),i.messages=[],i.selectedPiece=void 0,i.possibleMoves=void 0,i.game.start||(i.game.start=new Date);var r=T(i.game.currentBoard.pieces,n.player);if(r)return i.game.inCheck?i.messages.push("Invalid move. ".concat(n.player," must move out of check")):i.messages.push("Invalid move. This move would put you in Check. Piece ".concat(r.id," can take ").concat(n.player," king")),L(e.game),i.game.playersTurn=n.player,i;var a,o,l=z(i.game.playersTurn);return a=i.game.currentBoard.pieces,o=l,a.filter(function(e){return e.player!==o&&!e.isOffBoard}).reduce(function(e,n){return e||(e=b(a,n).length>0)},!1)?function(e,n){if(!T(e,n))return!1;var t=e.filter(function(e){return e.player!==n&&!e.isOffBoard}),i=!0,r=!1,a=void 0;try{for(var o,l=t[Symbol.iterator]();!(i=(o=l.next()).done);i=!0){var c=o.value;if(Q(e,c))return!1}}catch(s){r=!0,a=s}finally{try{i||null==l.return||l.return()}finally{if(r)throw a}}return!0}(i.game.currentBoard.pieces,l)?(i.messages.push("Checkmate. Player ".concat(l," is in checkmate")),i.game.currentState="Black"===l?"BlackInCheckmate":"WhiteInCheckmate",i):((r=T(i.game.currentBoard.pieces,l))?i.messages.push("Check. Player ".concat(l," is in check")):i.game.inCheck=void 0,i.game.playersTurn=l,i):(i.messages.push("Game is a draw ".concat(l," has no possible moves")),i.game.currentState="Draw",i)},L=function(e){if(e.moves&&0!==e.moves.length){var n=e.moves.pop();n&&n.pieceMoves.forEach(function(e){e.piece.position=e.startPosition,e.piece.isOffBoard=!1,e.piece.moveCount=0===e.piece.moveCount?0:e.piece.moveCount-1}),e.playersTurn=z(e.playersTurn),e.currentState="Playing"}},z=function(e){return"White"===e?"Black":"White"},U=function(){var e=Object(i.useReducer)(D,A),n=Object(k.a)(e,2),t=n[0],a=n[1];Object(i.useEffect)(function(){var e={currentBoard:I(),moves:[],playersTurn:"White",currentState:"Playing"};a({type:"setGame",game:e})},[]);var o=function(e){e.isOffBoard||t.game&&t.game.playersTurn===e.player&&(a({type:"selectPiece",piece:e}),a({type:"setPossibleMoves",moves:b(t.game.currentBoard.pieces,e)}))},l=function(e,n){if(t.game){if(t.selectedPiece&&t.possibleMoves)if(t.possibleMoves.find(function(t){return t.rank===e&&t.file===n}))return void a({type:"movePiece",piece:t.selectedPiece,position:{rank:e,file:n}});var i=G(t.game.currentBoard.pieces,e,n);i&&(t.selectedPiece&&i.id===t.selectedPiece.id||o(i))}};return r.a.createElement("div",null,t.game&&r.a.createElement("div",{style:{position:"absolute",left:"20px",fontSize:"24px",maxWidth:"300px"}},r.a.createElement("div",null,"Player : ",t.game.playersTurn),r.a.createElement("div",null,"Moves : ",t.game.moves.length),r.a.createElement("div",null,t.messages.map(function(e,n){return r.a.createElement("div",{key:n},e)})),r.a.createElement("button",{title:"undo",onClick:function(){a({type:"undoMove"})},style:{padding:"2px",margin:"2px"}},r.a.createElement(m.a,{icon:v.h})),r.a.createElement("button",{title:"save",onClick:function(){a({type:"saveGame"})},style:{padding:"2px",margin:"2px"}},"Save")),t.game&&t.game.currentBoard&&r.a.createElement("div",{style:{position:"absolute",left:"400px",top:"30px"}},r.a.createElement("svg",{key:"board",width:1440,height:960},r.a.createElement(d,{onSelectSquare:l,gamePlayState:t}),function(e){var n=0,i=0;return e.map(function(e){var a=y(e.position.rank,e.position.file),l=a.x,c=a.y;return l+=120*(1-.7)/2,c+=120*(1-.7)/2,e.isOffBoard&&("Black"===e.player?(l=960+i%4*120+120*(1-.7)/2,c=840-120*Math.floor(i/4)+120*(1-.7)/2,i++):(l=960+n%4*120+120*(1-.7)/2,c=120*Math.floor(n/4)+120*(1-.7)/2,n++)),r.a.createElement(g,{piece:e,gamePlayState:t,onSelectPiece:o,x:l,y:c})})}(t.game.currentBoard.pieces),t.selectedPiece&&(t.game.currentBoard,t.selectedPiece,t.possibleMoves&&t.possibleMoves.map(function(e,n){var t=y(e.rank,e.file),i=t.x,a=t.y;return i+=60,a+=60,r.a.createElement("circle",{key:"PossibleMove".concat(n),cy:a,cx:i,r:"20",fill:"blue",onClick:function(){return l(e.rank,e.file)}})})))))},$=(t(28),function(e){function n(){return Object(c.a)(this,n),Object(u.a)(this,Object(p.a)(n).apply(this,arguments))}return Object(f.a)(n,e),Object(s.a)(n,[{key:"render",value:function(){return r.a.createElement(U,null)}}]),n}(i.Component));$.displayName=$.name;var H=Boolean("localhost"===window.location.hostname||"[::1]"===window.location.hostname||window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));function V(e){navigator.serviceWorker.register(e).then(function(e){e.onupdatefound=function(){var n=e.installing;n.onstatechange=function(){"installed"===n.state&&(navigator.serviceWorker.controller?console.log("New content is available; please refresh."):console.log("Content is cached for offline use."))}}}).catch(function(e){console.error("Error during service worker registration:",e)})}var X=document.getElementsByTagName("base")[0].getAttribute("href"),Y=document.getElementById("root");o.a.render(r.a.createElement(l.a,{basename:X},r.a.createElement($,null)),Y),function(){if("serviceWorker"in navigator){if(new URL("",window.location).origin!==window.location.origin)return;window.addEventListener("load",function(){var e="".concat("","/service-worker.js");H?function(e){fetch(e).then(function(n){404===n.status||-1===n.headers.get("content-type").indexOf("javascript")?navigator.serviceWorker.ready.then(function(e){e.unregister().then(function(){window.location.reload()})}):V(e)}).catch(function(){console.log("No internet connection found. App is running in offline mode.")})}(e):V(e)})}}()}},[[17,1,2]]]); //# sourceMappingURL=main.15c183ed.chunk.js.map