gomoku-tools
Version:
Tools for handling gomoku games
1 lines • 8.51 kB
JavaScript
!function(t){if("object"==typeof exports)module.exports=t();else if("function"==typeof define&&define.amd)define(t);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self),o.gomokuTools=t()}}(function(){return function t(o,n,e){function i(s,u){if(!n[s]){if(!o[s]){var a="function"==typeof require&&require;if(!u&&a)return a(s,!0);if(r)return r(s,!0);throw new Error("Cannot find module '"+s+"'")}var f=n[s]={exports:{}};o[s][0].call(f.exports,function(t){var n=o[s][1][t];return i(n?n:t)},f,f.exports,t,o,n,e)}return n[s].exports}for(var r="function"==typeof require&&require,s=0;s<e.length;s++)i(e[s]);return i}({1:[function(t,o){(function(n){var e=t("./src/index.js");n.gomokuTools=o.exports={Gomoku:function(t){return t=t||{},t.strategy="gomoku",new e(t)},TicTacToe:function(t){return t=t||{},t.strategy="ticTacToe",new e(t)},Renju:function(t){return t=t||{},t.strategy="renju",new e(t)}}}).call(this,"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./src/index.js":11}],2:[function(t,o){function n(t){return"function"==typeof t&&r.test(t)}var e=Object.prototype,i=e.toString,r=RegExp("^"+String(i).replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/toString| for [^\]]+/g,".*?")+"$");o.exports=n},{}],3:[function(t,o){var n={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1};o.exports=n},{}],4:[function(t,o){var n=t("lodash._objecttypes"),e=Object.prototype,i=e.hasOwnProperty,r=function(t){var o,e=t,r=[];if(!e)return r;if(!n[typeof t])return r;for(o in e)i.call(e,o)&&r.push(o);return r};o.exports=r},{"lodash._objecttypes":3}],5:[function(t,o){var n=t("lodash.keys"),e=t("lodash._objecttypes"),i=function(t,o,i){var r,s=t,u=s;if(!s)return u;for(var a=arguments,f=0,c="number"==typeof i?2:a.length;++f<c;)if(s=a[f],s&&e[typeof s])for(var l=-1,h=e[typeof s]&&n(s),p=h?h.length:0;++l<p;)r=h[l],"undefined"==typeof u[r]&&(u[r]=s[r]);return u};o.exports=i},{"lodash._objecttypes":3,"lodash.keys":7}],6:[function(t,o){function n(t){return!(!t||!e[typeof t])}var e=t("lodash._objecttypes");o.exports=n},{"lodash._objecttypes":3}],7:[function(t,o){var n=t("lodash._isnative"),e=t("lodash.isobject"),i=t("lodash._shimkeys"),r=n(r=Object.keys)&&r,s=r?function(t){return e(t)?r(t):[]}:i;o.exports=s},{"lodash._isnative":2,"lodash._shimkeys":4,"lodash.isobject":6}],8:[function(t,o){function n(t,o,n){t=+t||0,n="number"==typeof n?n:+n||1,null==o&&(o=t,t=0);for(var r=-1,s=i(0,e((o-t)/(n||1))),u=Array(s);++r<s;)u[r]=t,t+=n;return u}var e=Math.ceil,i=Math.max;o.exports=n},{}],9:[function(t,o){o.exports=t("./lib/pad")},{"./lib/pad":10}],10:[function(t,o){o.exports=function(t,o,n){var e,i,r,s,u;for(null==n&&(n=" "),r="number"==typeof t,r&&(u=[t,o],o=u[0],t=u[1]),t=t.toString(),i="",o-=t.length,e=s=0;o>=0?o>s:s>o;e=o>=0?++s:--s)i+=n;return r?i+t:t+i}},{}],11:[function(t,o){function n(t,o){return Array.isArray(t)?(o=o||{},o.moves=t):o=t,o=f({},o,c)}function e(t,o){o=n(t,o),this.strategy=new r[o.strategy](o),this.config=this.strategy.config,this._applyFunctions(),this.position=a.generateEmptyPosition(this.strategy.config.cellsX,this.strategy.config.cellsY),this.reset(),o.moves&&this.moveTo.apply(this,o.moves)}var i=t("./tools/asciiBoard"),r=t("./strategies"),s=t("./tools/transforms"),u=t("./tools/normalize"),a=t("./tools/utils"),f=t("lodash.defaults"),c={strategy:"gomoku"};e.prototype={getPosition:function(){return a.clonePosition(this.position)},reset:function(){return this.history=[],this.undoHistory=[],a.emptyPosition(this.position),this.move=1,this},setPosition:function(t){return this.position=a.clonePosition(t),this},moveTo:function(){return this.undoHistory=[],Array.prototype.map.call(arguments,function(t){"string"==typeof t&&(t=this.strategy.toPoint(t)),this.has(t)||(this.history.push(t),this.updatePoint(t[0],t[1],this.getNextMove()))},this),this},has:function(t){return t=this.strategy.toPoint(t),0!==this.position[t[0]][t[1]]},forward:function(){if(this.undoHistory.length){var t=this.undoHistory.pop();this.history.push(t),this.updatePoint(t[0],t[1],this.getNextMove())}return this},jumpToMove:function(t){for(;t<this.history.length&&t>0;)this.back();for(;t>this.history.length;)this.forward();return this},back:function(){if(this.history.length){this.getPreviousMove();var t=this.history.pop();this.undoHistory.push(t),this.updatePoint(t[0],t[1],0)}return this},updatePoint:function(t,o,n){return this.position[t][o]=n,this},getHistory:function(){return this.history.map(this.strategy.toXY.bind(this.strategy))},getNextMove:function(){var t=this.move;return this.move=1===this.move?2:1,t},getPreviousMove:function(){return this.getNextMove()},ascii:function(){return i(this.getPosition())},_applyFunctions:function(){var t=function(t){return function(){this.position=t(this.position)}.bind(this)}.bind(this);this.transform={horizontal:t(s.horizontal),vertical:t(s.vertical),clockwise:t(s.clockwise),counterClockwise:t(s.counterClockwise),diagonalFromLeftTopToRightBottom:t(s.diagonalFromLeftTopToRightBottom),diagonalFromRightTopToLeftBottom:t(s.diagonalFromRightTopToLeftBottom),normalizeBasic:t(u.basic)}},clone:function(){var t=new e(this.config);return t.setPosition(this.position),t}},o.exports=e},{"./strategies":13,"./tools/asciiBoard":16,"./tools/normalize":17,"./tools/transforms":18,"./tools/utils":19,"lodash.defaults":5}],12:[function(t,o){function n(t){this.config=i({},t,this.defaults)}var e=t("./mnk"),i=t("lodash.defaults");n.prototype=new e,n.prototype.defaults={cellsX:15,cellsY:15,k:5},o.exports=n},{"./mnk":14,"lodash.defaults":5}],13:[function(t,o){o.exports={gomoku:t("./gomoku"),mnk:t("./mnk"),ticTacToe:t("./ticTacToe")}},{"./gomoku":12,"./mnk":14,"./ticTacToe":15}],14:[function(t,o){function n(){}n.prototype.toPoint=function(t){if("string"!=typeof t)return t;var o=t.toUpperCase().charCodeAt(0)-65,n=this.config.cellsY-t.substr(1);return[n,o]},n.prototype.toXY=function(t){if("string"==typeof t)return t;var o=this.config.cellsY-t[0],n=String.fromCharCode(65+t[1]);return n+o},o.exports=n},{}],15:[function(t,o){function n(t){this.config=i({},t,this.defaults)}var e=t("./mnk"),i=t("lodash.defaults");n.prototype=new e,n.prototype.defaults={cellsX:3,cellsY:3,k:3},o.exports=n},{"./mnk":14,"lodash.defaults":5}],16:[function(t,o){function n(t){return t>9?t:t+" "}var e={0:" ",1:"x",2:"o"},i={paddingRight:" "};o.exports=function(t){for(var o=i.paddingRight+" ",r=0;r<t.length;r++)o+=" "+String.fromCharCode(65+r)+" ";o+="\n";for(var s=0;s<t.length;s++){o+=i.paddingRight+n(t.length-s)+" ";for(var u=0;u<t.length;u++)o+=" "+e[t[s][u]]+" ";o+="\n"}return o}},{}],17:[function(t,o){function n(t,o){return r.stringify(t)>r.stringify(o)?-1:t===o?0:1}function e(t,o){for(var n=o[0],e=1;e<o.length;e++)t(n,o[e])<0&&(n=o[e]);return n}var i=t("./transforms"),r=t("./utils");o.exports={basic:function(t){var o=[t,i.horizontal(t),i.vertical(t),i.clockwise(t),i.clockwise(i.clockwise(t)),i.counterClockwise(t),i.diagonalFromLeftTopToRightBottom(t),i.diagonalFromRightTopToLeftBottom(t)];return e(n,o)}}},{"./transforms":18,"./utils":19}],18:[function(t,o){function n(t,o,n){for(var e=[],i=t.length-1,r=0;r<t.length;r++)for(var s=0;s<t[r].length;s++){var u=o(r,s,i),a=n(r,s,i);e[u]||(e[u]=[]),e[u][a]=t[r][s]}return e}var e=(t("./utils"),t("./asciiBoard"),t("pad")),i={x:function(t){return t},y:function(t,o){return o},negX:function(t,o,n){return n-t},negY:function(t,o,n){return n-o}};o.exports={horizontal:function(t){return n(t,i.x,i.negY)},vertical:function(t){return n(t,i.negX,i.y)},clockwise:function(t){return n(t,i.y,i.negX)},clockwise45:function(t){var o=t.length,n=t[0].length,i=o+n-1;return t.map(function(t){return o--,e(e(o+n,t.join(""),"0"),i,"0").split("")})},counterClockwise45:function(t){var o=t.length,n=t[0].length,i=o+n-1;return t.map(function(t){return o--,e(i,e(t.join(""),o+n,"0"),"0").split("")})},counterClockwise:function(t){return n(t,i.negY,i.x)},diagonalFromLeftTopToRightBottom:function(t){return n(t,i.negY,i.negX)},diagonalFromRightTopToLeftBottom:function(t){return n(t,i.y,i.x)}}},{"./asciiBoard":16,"./utils":19,pad:9}],19:[function(t,o){var n=t("lodash.range"),e=/,/g;o.exports={clonePosition:function(t){return t.map(function(t){return t.slice()})},generateEmptyPosition:function(t,o){return n(o).map(n.bind(null,0,t,0))},emptyPosition:function(t){return t.reduce(function(t,o,n){return o.reduce(function(t,o,e){return t[n][e]=0,t},t)},t)},stringify:function(t){return t.join("").replace(e,"")}}},{"lodash.range":8}]},{},[1])(1)});