minesweeperjs
Version:
Free and easy to use minesweeper javascript library.
3 lines (2 loc) • 8.62 kB
JavaScript
/* MinesweeperJS v1.0.0 | by Donovan Trinh | Under MIT License | Date: 2020/10/02 | Find more : https://github.com/Dono7/MinesweeperJS */
!function r(h,o,a){function l(e,t){if(!o[e]){if(!h[e]){var i="function"==typeof require&&require;if(!t&&i)return i(e,!0);if(u)return u(e,!0);var n=new Error("Cannot find module '"+e+"'");throw n.code="MODULE_NOT_FOUND",n}var s=o[e]={exports:{}};h[e][0].call(s.exports,function(t){return l(h[e][1][t]||t)},s,s.exports,r,h,o,a)}return o[e].exports}for(var u="function"==typeof require&&require,t=0;t<a.length;t++)l(a[t]);return l}({1:[function(t,e,i){"use strict";function s(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}t("./Checker.js");var n=function(){function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.isRevealed=!1,this.isBomb=!1,this.isFlagged=!1,this.nb=0}var e,i,n;return e=t,(i=[{key:"inc",value:function(){this.nb++}},{key:"dec",value:function(){this.nb--}},{key:"bomb",value:function(){this.isBomb=!0}},{key:"unbomb",value:function(){this.isBomb=!1}},{key:"reveal",value:function(){return this.isRevealed=!0,this.nb}},{key:"toggleFlag",value:function(){this.isFlagged=!this.isFlagged}}])&&s(e.prototype,i),n&&s(e,n),t}();e.exports=n},{"./Checker.js":2}],2:[function(t,e,i){"use strict";function s(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var n=function(){function i(t){var e=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"";return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,i),this.n=e,this.v=t,this.ok=!0,this.msg="",this}var t,e,n;return t=i,(e=[{key:"required",value:function(){return!this.ok||void 0!==this.v&&null!==this.v||(this.ok=!1,this.msg="The parameter ".concat(this.n," is required"),this.fire()),this}},{key:"notEmpty",value:function(){return this.ok&&(this.v.length<1||""===this.v)&&(this.ok=!1,this.msg="The parameter ".concat(this.n," is required"),this.fire()),this}},{key:"int",value:function(){return this.ok&&!Number.isInteger(this.v)&&(this.ok=!1,this.msg="The parameter ".concat(this.n," must be an integer"),this.fire()),this}},{key:"string",value:function(){return this.ok&&!("string"==typeof this.v||this.v instanceof String)&&(this.ok=!1,this.msg="The parameter ".concat(this.n," must be a string"),this.fire()),this}},{key:"bool",value:function(){return this.ok&&"boolean"!=typeof this.v&&(this.ok=!1,this.msg="The parameter ".concat(this.n," must be a boolean value"),this.fire()),this}},{key:"def",value:function(){return this.ok&&void 0===this.v&&(this.ok=!1,this.msg="The parameter ".concat(this.n," should not be undefined"),this.fire()),this}},{key:"undef",value:function(){return this.ok&&void 0!==this.v&&(this.ok=!1,this.msg="The parameter ".concat(this.n," should be undefined"),this.fire()),this}},{key:"min",value:function(t){return this.ok&&this.v<t&&(this.ok=!1,this.msg="The parameter ".concat(this.n," must be greater than ").concat(t),this.fire()),this}},{key:"max",value:function(t){return this.ok&&this.v>t&&(this.ok=!1,this.msg="The parameter ".concat(this.n," must be smaller than ").concat(t),this.fire()),this}},{key:"between",value:function(t,e){return this.min(t).max(e)}},{key:"fire",value:function(){if(!this.ok&&""!=this.msg)throw new Error("[MinesweeperJS] ".concat(this.msg))}}])&&s(t.prototype,e),n&&s(t,n),i}();e.exports=n},{}],3:[function(t,e,i){"use strict";function f(t){return function(t){if(Array.isArray(t))return n(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||function(t,e){if(!t)return;if("string"==typeof t)return n(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);"Object"===i&&t.constructor&&(i=t.constructor.name);if("Map"===i||"Set"===i)return Array.from(t);if("Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i))return n(t,e)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var i=0,n=new Array(e);i<e;i++)n[i]=t[i];return n}function s(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var d=t("./Checker.js"),r=t("./Cell.js"),v=9,m=1e3,h=function(){function c(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=t.width,i=void 0===e?v:e,n=t.height,s=void 0===n?v:n,r=t.name,h=void 0===r?"Untitled":r,o=t.nbbombs,a=void 0===o?10:o,l=t.lives,u=void 0===l?0:l;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,c),new d(i,"width").int().between(v,m),new d(s,"width").int().between(v,m),new d(h,"name").string().notEmpty(),new d(a,"nbbomb").int().min(Math.max(1,Math.floor(i*s/100))),new d(u,"lives").int().between(0,a-1),this.width=i,this.height=s,this.name=h,this.nbbombs=a,this.lives=u,this.areBombsSet=!1,this.isEnded=!1,this.isWon=!1,this.revealedCells=0,this.flaggedCells=[]}var t,e,i;return t=c,(e=[{key:"initMap",value:function(){var t=this;return this.map=new Array(this.height).fill(0).map(function(){return new Array(t.width).fill(0).map(function(){return new r})}),this}},{key:"spawnBombs",value:function(t,e){var i=this,n=this.indexToCoord(t,e),s=n.x,r=n.y;if(this.areBombsSet)return this;for(var h,o=f(Array(this.width*this.height-2).keys()),a=r*this.width+s,l=[],u=0;u<this.nbbombs;u++)h=(h=o.splice(Math.floor(Math.random()*o.length),1)[0])==a?this.height*this.width-1:h,l.push(h);return l.forEach(function(t){return i.addBomb(t)}),this.areBombsSet=!0,this}},{key:"spawnRdmBombs",value:function(){new d(this.map,"map").def();var t=Math.floor(Math.random()*this.map.length),e=Math.floor(Math.random()*this.map[0].length);return this.spawnBombs(t,e)}},{key:"addBomb",value:function(t,e){for(var i=this.indexToCoord(t,e),n=i.x,s=i.y,r=n-1;r<=n+1;r++)for(var h=s-1;h<=s+1;h++)0<=r&&0<=h&&r<this.height&&h<this.width&&(r!=n||h!=s)&&this.map[r][h].inc();return this.map[n][s].bomb(),this}},{key:"show",value:function(t){var e=0<arguments.length&&void 0!==t&&t;new d(e,"reveal").def().bool(),new d(this.map,"map").def();var i="".concat(this.name," (").concat(this.width,"x").concat(this.height,", ").concat(this.nbbombs," bombs) \n")+"Lives left : ".concat(this.lives," , Flags : ").concat(this.flaggedCells.length," \n");return i+=this.isEnded?"Game Over : ".concat(this.isWon?"Won ! :)":"Lost :("," \n"):"",this.map.forEach(function(t){t.forEach(function(t){e&&(i+=t.isRevealed?"-":t.isFlagged?"*":" "),i+=t.isBomb?"x ":t.nb+" "}),i+="\n"}),console.log(i),this}},{key:"reveal",value:function(t,e){var i=this,n=this.indexToCoord(t,e),s=n.x,r=n.y;if(this.map[s][r].isRevealed||this.map[s][r].isFlagged)return this;var h,o=this.map[s][r].reveal();return this.map[s][r].isBomb?this.lives<1?(this.isWon=!1,this.isEnded=!0):this.lives--:this.revealedCells++,0==o&&(0<s&&this.reveal(s-1,r),0<r&&this.reveal(s,r-1),s<this.width-1&&this.reveal(s+1,r),r<this.height-1&&this.reveal(s,r+1)),this.revealedCells>=this.width*this.height-this.nbbombs&&(this.isWon=!0,this.isEnded=!0),this.flaggedCells.length==this.nbbombs&&(h=!0,this.flaggedCells.forEach(function(t){h=h&&i.map[t.x][t.y].isBomb}),h&&(this.isWon=!0,this.isEnded=!0)),this}},{key:"flag",value:function(t,e){if(this.flaggedCells.length>=this.nbbombs)return this;var i=this.indexToCoord(t,e),n=i.x,s=i.y;return this.map[n][s].isFlagged?this.flaggedCells=this.flaggedCells.filter(function(t){return!(t.x==n&&t.y==s)}):this.flaggedCells.push({x:n,y:s}),this.map[n][s].toggleFlag(),this}},{key:"indexToCoord",value:function(t,e){return new d(this.map,"map").def(),new d(this.width,"width").def(),new d(this.height,"height").def(),void 0!==e?(new d(t,"indexX").required().int().between(0,this.width-1),new d(e,"indexY").required().int().between(0,this.height-1)):(new d(t,"indexX").required().int().between(0,this.height*this.width-1),new d(e,"indexY").undef()),{x:e?t:Math.floor(t/this.width),y:e||t%this.width}}}])&&s(t.prototype,e),i&&s(t,i),c}();e.exports=h},{"./Cell.js":1,"./Checker.js":2}],4:[function(t,e,i){"use strict";var n=t("./Checker"),s=t("./Cell"),r=t("./Grid"),h=window||h;h.Checker=n,h.Cell=s,h.Grid=r,h.MsInstalled=function(){return console.log("MinesweeperJS is currently installed")}},{"./Cell":1,"./Checker":2,"./Grid":3}]},{},[4]);