From 9d3c8c0e6e1a7ba43bf3dc19350d1dca68b657a3 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sun, 10 Feb 2019 11:16:07 +0800 Subject: Initial commit. --- minesweeper/ding.ogg | Bin 0 -> 7002 bytes minesweeper/explode.ogg | Bin 0 -> 17296 bytes minesweeper/index.html | 211 ++++++++++++++++++++++ minesweeper/minesweeper.js | 428 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 639 insertions(+) create mode 100644 minesweeper/ding.ogg create mode 100644 minesweeper/explode.ogg create mode 100644 minesweeper/index.html create mode 100644 minesweeper/minesweeper.js (limited to 'minesweeper') diff --git a/minesweeper/ding.ogg b/minesweeper/ding.ogg new file mode 100644 index 0000000..24701f1 Binary files /dev/null and b/minesweeper/ding.ogg differ diff --git a/minesweeper/explode.ogg b/minesweeper/explode.ogg new file mode 100644 index 0000000..0a10885 Binary files /dev/null and b/minesweeper/explode.ogg differ diff --git a/minesweeper/index.html b/minesweeper/index.html new file mode 100644 index 0000000..fc18254 --- /dev/null +++ b/minesweeper/index.html @@ -0,0 +1,211 @@ + + + + + +Chrisoft::Minesweeper + + + + + + + + + + + + + + + + + +
+
+
+
+ + +
Mines: +
+
+
+ + + + diff --git a/minesweeper/minesweeper.js b/minesweeper/minesweeper.js new file mode 100644 index 0000000..f4d652e --- /dev/null +++ b/minesweeper/minesweeper.js @@ -0,0 +1,428 @@ +//Chris Xiong 2017 +//Expat (MIT) License +var dimpx,dimx,dimy,cc,firstclick; +var cell=null; +var ismobile,effects=true,multimine=false,sfxe=true; +var dead,st,solved,animating,unc; +var dx=[-1,-1,-1, 0, 0, 1, 1, 1]; +var dy=[-1, 0, 1,-1, 1,-1, 0, 1]; +var classes=["one","two","three","four","five","six","seven","eight"]; +var queue=[],newqueue=[],flags=[],seed; +var mines=[],detx,dety,cmines=[],sfx=[]; +var ui={}; +function randomInt(min,max) +{ + seed*=214013;seed+=2531011;seed&=0xffffffff; + return min+(seed^seed>>15)%(max-min+1); +} +function setupui() +{ + ui.pbefx=document.getElementById('efx'); + ui.pbsfx=document.getElementById('sfx'); + ui.pbmul=document.getElementById('multim'); + ui.cbbsz=document.getElementById('bsize'); + ui.lbres=document.getElementById('result'); + ui.pbstt=document.getElementById('st'); + ui.lbmns=document.getElementById('mines'); + ui.cwctn=document.getElementById('containerDiv'); + ui.progress=document.getElementById('progress'); +} +function efxtoggle() +{ + effects=!effects; + ui.pbefx.classList.toggle('off'); + ui.pbefx.innerHTML=effects?"On":"Off"; + for(var i=0;i40||custx<3))||(custy&&(custy>40||custy<3))||(ccc&&custx&&custy&&(ccc>custx*custy-9))) + {console.log("Are you insane?");return;} + firstclick=true;dead=solved=false;for(var i=0;i<7;++i)flags[i]=cmines[i]=0; + ui.lbres.style.opacity="0";ui.lbres.style.zIndex="-999";animating=false; + for(;ui.cwctn.hasChildNodes();ui.cwctn.removeChild(ui.cwctn.lastChild)); + switch(ui.cbbsz.value) + { + case "1":dimx=9;dimy=9;cc=10;break; + case "2":dimx=16;dimy=16;cc=40;break; + case "3":dimx=16;dimy=30;cc=99;break; + } + if(custx&&custy&&ccc){dimx=custx;dimy=custy;cc=ccc;} + if((window.innerWidth>window.innerHeight)^(dimxwindow.innerHeight*0.8) + { + dimpx=window.innerHeight*(ismobile?0.9:0.8); + ui.cwctn.style.width=dimpx/dimx*dimy+"px"; + ui.cwctn.style.height=dimpx+"px"; + } + else + { + dimpx=window.innerWidth*(ismobile?0.9:0.8); + ui.cwctn.style.width=dimpx+"px"; + ui.cwctn.style.height=dimpx/dimy*dimx+"px"; + } + cell=new Array(dimx+4);for(var i=0;i=2||Math.abs(sy-j)>=2)s.push(i*dimy+j); + for(var i=0;i32)--cell[x][y].n; + if(rnd>57)--cell[x][y].n; + if(rnd>77)--cell[x][y].n; + if(rnd>87)--cell[x][y].n; + if(rnd>95)--cell[x][y].n; + } + ++cmines[-cell[x][y].n]; + for(var j=0;j<8;++j) + { + var cx=x+dx[j],cy=y+dy[j]; + if(cx>=0&&cx=0&&cy=0) + cell[cx][cy].n-=cell[x][y].n; + } + }st=new Date();updateflags(); +} +function mclick(e) +{ + e.preventDefault(); + if(dead||solved||animating)return; + var x=this.x,y=this.y; + if(ismobile) + { + if(multimine) + {if(cell[x][y].s>=0)flag(x,y);} + else{if(cell[x][y].s==1)deflag(x,y);else if(cell[x][y].s==0)flag(x,y);} + if(cell[x][y].s==-1)autodig(x,y); + } + else + { + if(cell[x][y].s==0)dig(x,y); + else if(cell[x][y].s==-1)autodig(x,y); + else if(multimine&&cell[x][y].s>0)deflag(x,y); + } +} +function rclick(e) +{ + e.preventDefault(); + if(dead||solved||animating)return; + var x=this.x,y=this.y; + if(ismobile) + { + if(cell[x][y].s==0)dig(x,y); + else if(multimine&&cell[x][y].s>0)deflag(x,y); + } + else + { + if(multimine) + {if(cell[x][y].s>=0)flag(x,y);} + else{if(cell[x][y].s==1)deflag(x,y);else if(cell[x][y].s==0)flag(x,y);} + } +} +function checksolved() +{ + for(var i=0;i=0&&cell[i][j].s!=-1)return false; + return true; +} +var pair=function(_x,_y){this.x=_x;this.y=_y;} +pair.prototype.toString=function() +{ + return this.x+","+this.y; +} +function bfs(s) +{ + q=newqueue.slice(0);newqueue=[]; + if(s)playsound('ding.ogg'); + for(var i=0;i0) + { + cell[x][y].span.innerHTML=cell[x][y].n; + cell[x][y].span.classList.add(classes[(cell[x][y].n-1)%8]); + } + cell[x][y].overlay.style.opacity="1"; + cell[x][y].overlay.style.transform="scale(1,1)"; + cell[x][y].overlay.classList.add("dug"); + ++unc; + ui.progress.style.width=100*unc/(dimx*dimy-cc)+"%"; + if(cell[x][y].n==0) + { + for(var j=0;j<8;++j) + { + var c=new pair(x+dx[j],y+dy[j]); + if(c.x>=0&&c.x=0&&c.y=0&&cx=0&&cy0)c+=cell[cx][cy].s; + } + if(c==cell[x][y].n) + { + newqueue=[]; + for(var i=0;i<8;++i) + { + var cx=x+dx[i],cy=y+dy[i]; + if(cx>=0&&cx=0&&cy6)cell[x][y].s=6;cell[x][y].span.innerHTML=cell[x][y].s;} + else{if(cell[x][y].s>1)cell[x][y].s=1;cell[x][y].span.innerHTML="X";} + ++flags[cell[x][y].s]; + cell[x][y].overlay.classList.add('flag'); + cell[x][y].overlay.style.opacity="1"; + cell[x][y].overlay.style.transform="scale(1,1)"; + updateflags(); +} +function deflag(x,y) +{ + --flags[cell[x][y].s];--cell[x][y].s;++flags[cell[x][y].s]; + cell[x][y].span.innerHTML=cell[x][y].s; + if(cell[x][y].s==0) + { + cell[x][y].span.innerHTML=""; + cell[x][y].classList.remove('flag'); + cell[x][y].classList.add('normal'); + cell[x][y].overlay.style.opacity="0"; + cell[x][y].overlay.style.transform="scale(2,2)"; + } + updateflags(); +} +function updateflags() +{ + if(multimine) + { + var s=""; + for(var i=1;i<7;++i)s=s+" x"+i+" "+flags[i]+"/"+cmines[i]; + ui.lbmns.innerHTML=s; + } + else + ui.lbmns.innerHTML=flags[1]+"/"+cmines[1]; +} +function cmp(x,y) +{ + return (Math.abs(x.x-detx)+Math.abs(x.y-dety))-(Math.abs(y.x-detx)+Math.abs(y.y-dety)); +} +function die(x,y) +{ + if(dead||cell[x][y].n>=0)return; + dead=true;detx=x;dety=y;mines.sort(cmp); + for(var i=0;i0&&cell[i][j].s!=-cell[i][j].n&&(i!=x||j!=y)) + { + cell[i][j].overlay.classList.remove('flag'); + cell[i][j].overlay.classList.add('flagw'); + cell[i][j].span.innerHTML=""+cell[i][j].span.innerHTML+""; + } + cell[x][y].overlay.classList.add('detonated'); + cell[x][y].overlay.classList.remove('flag'); + cell[x][y].overlay.style.opacity="1"; + cell[x][y].overlay.style.transform="scale(1,1)"; + if(multimine) + { + if(-cell[x][y].n!=cell[x][y].s&&cell[x][y].s>0) + cell[x][y].span.innerHTML=""+cell[x][y].s+""+(cell[x][y].n<0?-cell[x][y].n:""); + else cell[x][y].span.innerHTML=-cell[x][y].n; + } + var st=1;if(mines[1].toString()!=new pair(x,y).toString)st=0; + playsound('explode.ogg'); + if(effects)setTimeout(dieproc,10,st);else dieproc(st); + window.navigator.vibrate([200,100,300]); +} +function dieproc(p) +{ + if(!dead||p>=mines.length){if(p>=mines.length)showresult(0);ui.cwctn.style.transform="none";return;} + var x=mines[p].x,y=mines[p].y; + if(cell[x][y].n!=0&&cell[x][y].s==-cell[x][y].n)return dieproc(p+1); + if(sfxe&&(p%5==0))playsound('explode.ogg',.3*(1-p/cc/2)); + if(cell[x][y].s!=-cell[x][y].n) + { + cell[x][y].overlay.classList.add('mine'); + cell[x][y].overlay.classList.remove('flag'); + } + cell[x][y].overlay.style.opacity="1"; + cell[x][y].overlay.style.transform="scale(1,1)"; + if(multimine) + { + if(-cell[x][y].n!=cell[x][y].s&&cell[x][y].s>0) + cell[x][y].span.innerHTML=""+cell[x][y].s+""+(cell[x][y].n<0?-cell[x][y].n:""); + else cell[x][y].span.innerHTML=-cell[x][y].n; + } + if(effects) + ui.cwctn.style.transform="translate("+(Math.random()*10-5)+"px,"+(Math.random()*10-5)+"px)"; + if(!effects||(cc>40&&(p&1)))dieproc(p+1);else setTimeout(dieproc,10,p+1); +} +function systemtest() +{ + if(ui.pbstt.classList.contains("disabled")||solved||dead)return; + for(var i=0;iClick or tap anywhere in the dialog to close it."; + } + else + { + var cs=0,ccor=0; + for(var i=0;iClick or tap anywhere in the dialog to close it." + } +} -- cgit v1.2.3