Path: blob/main/projects/minesweeper/minesweeper.min.js
1834 views
function Minesweeper(e,t,n){var a,i,s,o,r,d,u,l,c,m,f,g,h,v,p,b,w,y,q,M,T,C,I=this,k=new function(){var e,t,n;function a(){var s=(new Date).getTime();n=setTimeout(a,1e3-(s-e-1e3*t)),t++,i()}function i(){var e=j(t);document.getElementById("seconds_hundreds").className="time"+e[0],document.getElementById("seconds_tens").className="time"+e[1],document.getElementById("seconds_ones").className="time"+e[2]}this.start=function(){e=(new Date).getTime()-1e3*t,a()},this.stop=function(){clearTimeout(n)},this.getTime=function(){return t},this.setTime=function(e){t=e,i()}};function R(e){$("#game-container, #game").width(e*(16*s+20)),$("#game").height(e*(16*i+30+26+6))}function N(e){return(e*s*16-6*Math.ceil(13*e)-2*e*6-26*e)/2}function _(){return i+"_"+s+"_"+o}function F(e,t){var n=0,a=!1,o=!1,d=!1;this.addToValue=function(e){n+=e},this.isMine=function(){return n<0},this.isFlagged=function(){return a},this.isMarked=function(){return o},this.isRevealed=function(){return d},this.isHidden=function(){return e<1||e>i||t<1||t>s},this.getRow=function(){return e},this.getCol=function(){return t},this.getValue=function(){return n},this.setRevealed=function(e){d=e},this.plantMine=function(){n-=10,m[e-1][t-1].addToValue(1),m[e-1][t].addToValue(1),m[e-1][t+1].addToValue(1),m[e][t-1].addToValue(1),m[e][t+1].addToValue(1),m[e+1][t-1].addToValue(1),m[e+1][t].addToValue(1),m[e+1][t+1].addToValue(1)},this.unplantMine=function(){n+=10,m[e-1][t-1].addToValue(-1),m[e-1][t].addToValue(-1),m[e-1][t+1].addToValue(-1),m[e][t-1].addToValue(-1),m[e][t+1].addToValue(-1),m[e+1][t-1].addToValue(-1),m[e+1][t].addToValue(-1),m[e+1][t+1].addToValue(-1)},this.setClass=function(n){document.getElementById(e+"_"+t).className=n},this.reveal1=function(){var e,t,n,a,i=[];for(i.push(this),this.pushed=!0;i.length>0;)if(!(n=i.pop()).isRevealed()&&!n.isFlagged()){if(n.isMine())return!1;if(!n.isFlagged()){if(n.setClass("square open"+n.getValue()),n.setRevealed(!0),!n.isHidden()&&0==--c)return x(),!0;if(0==n.getValue()&&!n.isHidden())for(e=-1;e<=1;e++)for(t=-1;t<=1;t++)(a=m[n.getRow()+e][n.getCol()+t]).pushed||a.isHidden()||a.isRevealed()||(i.push(a),a.pushed=!0)}}return V(),!0},this.reveal9=function(){if(d){var a,i,s,o=0,r=[];for(a=-1;a<=1;a++)for(i=-1;i<=1;i++)(s=m[e+a][t+i])!=this&&s.isFlagged()&&o++;if(o==n){for(a=-1;a<=1;a++)for(i=-1;i<=1;i++)(s=m[e+a][t+i])==this||s.reveal1()||r.push(s);r.length>0?D(r):V()}}},this.flag=function(e){d||(a?($("#marks").attr("checked")?(this.setClass("square question"),o=!0):(this.setClass("square blank"),e&&this._showFlagAnimation(!0)),a=!1,l++,O()):o?(this.setClass("square blank"),o=!1):(this.setClass("square bombflagged"),a=!0,l--,O(),e&&this._showFlagAnimation()),V())},this._showFlagAnimation=function(n){var a=$("#"+e+"_"+t),i=a.offset(),s=i.left+a.width()/2,o=i.top+a.height()/2,d=57*r*1.75,u=79*r*1.75,l={left:s-d/2,top:o-u/2,width:d+"px",height:u+"px",opacity:0},c={left:s,top:o,width:0,height:0,opacity:1};if(n){var m=l;l=c,c=m}var f=$('<img src="flag.png" class="flag-animation"></div>').css(l);$("body").append(f),setTimeout((function(){f.css(c)}),0),setTimeout((function(){f.remove()}),500)},this.serializeToObj=function(e){return e?d||a||o?[n,d?1:0,a?1:0,o?1:0]:n:{value:n,isRevealed:d,isFlagged:a,isMarked:o}},this.deserializeFromObj=function(e){n=e.value,a=e.isFlagged,o=e.isMarked,d=e.isRevealed}}function E(e){var t,n,a=[];for(t=0;t<=i+1;t++)for(a[t]=[],n=0;n<=s+1;n++)a[t][n]=m[t][n].serializeToObj(e);return a}function V(){var e=E();d={gridObj:e}}function B(e){var t,n,a,o,r=e.getRow(),d=e.getCol();if(!u&&!C){e.isMine()&&(v.splice(Math.floor(Math.random()*v.length),1)[0].plantMine(),e.unplantMine(),v.push(e));for(var l=[],c=0;c<v.length;c++)((o=v[c]).getRow()<r-1||o.getRow()>r+1||o.getCol()<d-1||o.getCol()>d+1)&&l.push(o);for(t=-1;t<=1;t++)for(n=-1;n<=1;n++)(a=m[r+t][d+n]).isMine()&&l.length>0&&(l.splice(Math.floor(Math.random()*l.length),1)[0].plantMine(),a.unplantMine())}return k.start(),1==r&&1==d||1==r&&d==s||r==i&&1==d||r==i&&d==s?1:1==r||r==i||1==d||d==s?2:3}function S(e){a>0&&(!function(){var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";p="";for(var t=0;t<3;t++)p+=e.charAt(Math.floor(Math.random()*e.length));p+=4*(Math.floor(225*Math.random())+25)+a;for(t=0;t<4;t++)p+=e.charAt(Math.floor(Math.random()*e.length))}(),$.post("start.php",{key:p,s:e}))}function O(){var e=j(l);document.getElementById("mines_hundreds").className="time"+e[0],document.getElementById("mines_tens").className="time"+e[1],document.getElementById("mines_ones").className="time"+e[2]}function j(e){return(e=Math.min(e,999))>=0?[Math.floor(e/100),Math.floor(e%100/10),e%10]:["-",Math.floor(-e%100/10),-e%10]}function D(e){var t,n,a,o;for(document.getElementById("face").className="facedead",k.stop(),g=!0,t=1;t<=i;t++)e:for(n=1;n<=s;n++)if(!(o=m[t][n]).isRevealed()){for(a=0;a<e.length;a++)if(o==e[a]){o.setClass("square bombdeath");continue e}o.isMine()&&!o.isFlagged()?o.setClass("square bombrevealed"):!o.isMine()&&o.isFlagged()&&o.setClass("square bombmisflagged")}}function x(){var t,n,o,r,d,c=!1;for(document.getElementById("face").className="facewin",k.stop(),g=!0,l=0,O(),t=1;t<=i;t++)for(n=1;n<=s;n++)(o=m[t][n]).isRevealed()||o.isFlagged()||o.setClass("square bombflagged");if(a>0){if(d=k.getTime(),!u){for(r=3;r>=0;r--)if(d<=e[r][a-1]){H(r+1,!0),c=!0;break}!c&&(1==a&&d<=10||2==a&&d<=50||3==a&&d<=150)&&H(1,!1)}I.onWin&&I.onWin(a,d)}}function H(e,t){var n,a,i,s,o=(new Date).getTime();switch(e){case 1:n="daily";break;case 2:n="weekly";break;case 3:n="monthly";break;case 4:n="all-time";break;default:n=""}i=z()&&localStorage.name?localStorage.name:"",a=t?prompt(k.getTime()+" is a new "+n+" high score! Please enter your name",i):prompt("Please enter your name to submit your score ("+k.getTime()+")",i),(a=$.trim(a).substring(0,25))&&z()&&(localStorage.name=a),s=Math.round(((new Date).getTime()-o)/1e3),$.post("win.php",{key:p,name:a,time:k.getTime(),s:s,i:e,h:t?1:0},(function(n){t&&I.onNewHighScore&&I.onNewHighScore(e)}))}function z(){try{return"localStorage"in window&&null!==window.localStorage}catch(e){return!1}}function G(e){return"square"==e.className.substring(0,6)}function A(e){var t={};return b?(t.left=1==e.button||3==e.button||4==e.button,t.right=2==e.button||3==e.button||4==e.button):(t.left=0==e.button||1==e.button,t.right=2==e.button||1==e.button),t}function K(e,t,n){e.isRevealed()||(e.isMarked()?e.setClass(n):e.isFlagged()||e.setClass(t))}function P(e,t,n){var a,i;for(a=-1;a<=1;a++)for(i=-1;i<=1;i++)K(m[e.getRow()+a][e.getCol()+i],t,n)}!function(){var e,t=!1;function n(t){if("touchmove"!==t.type||s(t)){var n=i(t);n==e||w||(q?(e&&P(f[e.id],"square blank","square question"),G(n)&&P(f[n.id],"square open0","square questionpressed")):(e&&K(f[e.id],"square blank","square question"),G(n)&&K(f[n.id],"square open0","square questionpressed"))),e=G(n)?n:void 0}}function a(e){if("touchmove"!==e.type||s(e)){var t=i(e);document.getElementById("face").className="face"==t.id?"facepressed":"facesmile"}}function i(e){if("touchmove"===e.type||"touchend"===e.type){var t=e.originalEvent.changedTouches[0];return document.elementFromPoint(t.clientX,t.clientY)}return e.target}function s(e){return!!M&&e.originalEvent.changedTouches[0].identifier===M}function o(){M&&(M=null,e&&(K(f[e.id],"square blank","square question"),e=void 0),g||(document.getElementById("face").className="facesmile"))}b=$.browser.msie&&parseFloat($.browser.version)<=7,$(document).bind("gesturestart",(function(e){T=!0,o()})),$(document).bind("gestureend",(function(e){T=!1})),$(document).bind("scroll",o),$(document).bind("touchstart",(function(a){if($(document).unbind("mousedown").unbind("mouseup"),!M&&!T)if(M=a.originalEvent.changedTouches[0].identifier,G(a.target)&&!g){var i=M,s=a.target;setTimeout((function(){i===M&&s===e&&(f[s.id].flag(!0),M=null,document.getElementById("face").className="facesmile")}),500),$(document).bind("touchmove",n),document.getElementById("face").className="faceooh",e=void 0,n(a)}else"face"==a.target.id&&(t=!0,$(document).bind("touchmove",n),document.getElementById("face").className="facepressed")})),$(document).bind("touchend",(function(e){if(s(e)){M=null,$(document).unbind("touchmove",n).unbind("touchmove",a),!t&&g||(document.getElementById("face").className="facesmile");var o=i(e);G(o)&&!g?(square=f[o.id],h||(squareTypeId=B(square)),square.isRevealed()?square.reveal9():square.isFlagged()?square.flag(!0):(square.reveal1()||D([square]),h||(S(squareTypeId),h=!0)),e.preventDefault()):"face"==o.id&&t&&I.newGame(),t=!1}})),$(document).mousedown((function(i){var s=A(i);if(y=s.left||y,q=s.right||q,i.ctrlKey&&G(i.target)&&!g)f[i.target.id].flag(),isMouseDownForCtrlClick=!0;else if(y)G(i.target)&&!g?(i.preventDefault(),$(document).bind("mousemove",n),document.getElementById("face").className="faceooh",e=void 0,n(i)):"face"==i.target.id&&(i.preventDefault(),t=!0,$(document).bind("mousemove",a),document.getElementById("face").className="facepressed");else if(q)return G(i.target)&&!g&&f[i.target.id].flag(),!1})),$(document).on("contextmenu",(function(e){var t=$(e.target);t.is("#game")||t.closest("#game").length>0||(q=!1)})),$(document).mouseup((function(e){var i,s,o=A(e);if(isMouseDownForCtrlClick)return y=!1,q=!1,void(isMouseDownForCtrlClick=!1);o.left&&(y=!1,$(document).unbind("mousemove",n).unbind("mousemove",a),!t&&g||(document.getElementById("face").className="facesmile"),G(e.target)&&!g?(i=f[e.target.id],q?(w=!0,P(f[e.target.id],"square blank","square question"),i.reveal9()):(w||(h||(s=B(i)),i.reveal1()||D([i]),h||(S(s),h=!0)),w=!1)):"face"==e.target.id&&t&&I.newGame(),t=!1),o.right&&(q=!1,G(e.target)&&!g&&(y?(i=f[e.target.id],w=!0,P(i,"square blank","square question"),i.reveal9()):w=!1,g||(document.getElementById("face").className="facesmile")))})),$(document).keydown((function(e){113==e.which?I.newGame():32==e.which?(hoveredSquareId&&!g&&(square=f[hoveredSquareId],square.isRevealed()?square.reveal9():square.flag()),e.preventDefault()):90==e.which&&!e.shiftKey&&(window.navigator&&window.navigator.platform&&-1!==window.navigator.platform.toLowerCase().indexOf("mac")?e.metaKey:e.ctrlKey)&&"facedead"==document.getElementById("face").className&&I.newGame(d)})),$("#game").mouseover((function(e){G(e.target)&&(hoveredSquareId=e.target.id)})),$("#game").mouseout((function(e){G(e.target)&&(hoveredSquareId=e.target.id)&&(hoveredSquareId="")}))}(),this.newGame=function(e,n){var d,p,b,I,E,B;for(I=_(),B=t(),a=B.gameTypeId,i=B.numRows,s=B.numCols,o=B.numMines,r=B.zoom,n&&(void 0!==n.gameTypeId&&(a=n.gameTypeId),void 0!==n.numRows&&(i=n.numRows),void 0!==n.numCols&&(s=n.numCols),void 0!==n.numMines&&(o=n.numMines)),E=_()!=I,R(r),E&&function(){var e,t,n=[],a=N(r);for(n.push('<div class="bordertl"></div>'),t=0;t<s;t++)n.push('<div class="bordertb"></div>');for(n.push('<div class="bordertr"></div>'),n.push('<div class="borderlrlong"></div>','<div class="time0" id="mines_hundreds"></div>','<div class="time0" id="mines_tens"></div>','<div class="time0" id="mines_ones"></div>','<div class="facesmile" style="margin-left:',Math.floor(a),"px; margin-right: ",Math.ceil(a),'px;" id="face"></div>','<div class="time0" id="seconds_hundreds"></div>','<div class="time0" id="seconds_tens"></div>','<div class="time0" id="seconds_ones"></div>','<div class="borderlrlong"></div>'),n.push('<div class="borderjointl"></div>'),t=0;t<s;t++)n.push('<div class="bordertb"></div>');for(n.push('<div class="borderjointr"></div>'),e=1;e<=i;e++){for(n.push('<div class="borderlr"></div>'),t=1;t<=s;t++)n.push('<div class="square blank" id="',e,"_",t,'"></div>');n.push('<div class="borderlr"></div>')}for(n.push('<div class="borderbl"></div>'),t=0;t<s;t++)n.push('<div class="bordertb"></div>');for(n.push('<div class="borderbr"></div>'),t=0;t<=s+1;t++)n.push('<div class="square blank" style="display: none;" id="',0,"_",t,'"></div>');for(t=0;t<=s+1;t++)n.push('<div class="square blank" style="display: none;" id="',i+1,"_",t,'"></div>');for(e=1;e<=i;e++)n.push('<div class="square blank" style="display: none;" id="',e,"_",0,'"></div>'),n.push('<div class="square blank" style="display: none;" id="',e,"_",s+1,'"></div>');$("#game").html(n.join(""))}(),function(e){var t,n,a,r;for(m=[],f=[],v=[],a=0,t=0;t<=i+1;t++)for(m[t]=[],n=0;n<=s+1;n++)r=new F(t,n),m[t][n]=r,f[t+"_"+n]=r,r.isHidden()||(v[a++]=r);if(e){var d=e.gridObj;for(t=0;t<=i+1;t++)for(n=0;n<=s+1;n++)m[t][n].deserializeFromObj(d[t][n]);for(v=[],t=0;t<=i+1;t++)for(n=0;n<=s+1;n++)(r=m[t][n]).isHidden()||r.isMine()||v.push(r)}else for(a=0;a<o;a++)v.splice(Math.floor(Math.random()*v.length),1)[0].plantMine()}(e),V(),u=!!e,l=o,c=i*s-o,d=1;d<=i;d++)for(p=1;p<=s;p++)(b=m[d][p]).isFlagged()?(b.setClass("square bombflagged"),l--):b.isMarked()?b.setClass("square question"):b.isRevealed()?(b.setClass("square open"+b.getValue()),b.isHidden()||c--):b.setClass("square blank");k.stop(),u?n&&void 0!==n.time&&k.setTime(n.time):k.setTime(0),O(),g=!1,h=!1,w=!1,y=!1,q=!1,isMouseDownForCtrlClick=!1,M=null,T=!1,C=!1,$("#face")[0].className="facesmile",hoveredSquareId=""},this.resize=function(e){var t=N(e);R(e),$("#game-container").removeClass("z"+100*r).addClass("z"+100*e),$("#face").css({"margin-left":Math.floor(t)+"px","margin-right":Math.ceil(t)+"px"}),r=e},this.hasStartedPlaying=function(){return h},this.export_=function(){var e=E(!0),t=k.getTime(),n={version:1,gameTypeId:a,numRows:i,numCols:s,numMines:o,gridObj:e,time:t};return C=!0,btoa(JSON.stringify(n))},this.isImportable=function(e){try{return 1===JSON.parse(atob(e)).version}catch(e){return!1}},this.import_=function(e){var t,a,i,s,o=JSON.parse(atob(e)),r=[];for(t=0;t<=o.numRows+1;t++)for(r[t]=[],a=0;a<=o.numCols+1;a++)s="number"==typeof(i=o.gridObj[t][a])?{value:i,isRevealed:!1,isFlagged:!1,isMarked:!1}:{value:i[0],isRevealed:1===i[1],isFlagged:1===i[2],isMarked:1===i[3]},r[t][a]=s;var d={gridObj:r},u={gameTypeId:o.gameTypeId,numRows:o.numRows,numCols:o.numCols,numMines:o.numMines,time:o.time};n({gameTypeId:o.gameTypeId,numRows:o.numRows,numCols:o.numCols,numMines:o.numMines}),I.newGame(d,u)}}123