Use pure JavaScript to implement classic minesweeper games

Source: Internet
Author: User
Tags continue split

use pure JavaScript to implement classic minesweeper games

This article to share is a person just started to learn JavaScript when the imitation of the Windows classic Minesweeper game code, was just written down, no comment, here to make up, the need for small partners can refer to.

It was written a long time ago did not write the comments just add (NI, a lot of themselves do not know the ...)

The lack of place is originally want to write a game ranking statistics, and so on, and then add (as if every time to say so and then wait a long time ...)

There is no implementation: Click on the first grid can not be the function of thunder

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262-2 63 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298-299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329-3 331 332 333 334 335 336 <style> Ul{padding:0;list-style:none} #mine {overflow:hidden;width:30px;height:30px;border:1px solid #966;} # Mine li{float:left;width:30px;height:30px;line-height:30px;text-align:center;font-size:14px;color: #222;} #mine. Mine_01{background:url (mine.gif) no-repeat #mine. Mine_02{background:url (mine.gif) -30px 0 no-repeat; #mine. Mine_ 03{background:url (mine.gif) -60px 0 no-repeat; #mine. Mine_04{background:url (mine.gif) -90px 0 no-repeat;} #mine. mine_ 05{background:url (mine.gif) -120px 0 no-repeat; #mine. Mine_06{background:url (mine.gif) -150px 0 no-repeat;} #mine. Mine_07{background:url (mine.gif) -180px 0 no-repeat;}   #count {font-size:12px;} #time {color: #900; Font-weight: Bold;} </style> <select id= "WH" > <option value= "8*10" >8*10</option> <option value= "10*10" >10* 10</option> <option value= "12*12" >12*12</option> </select> <button id= ' ready ' > Start again </button> <div id= "Count" > Timing: <span id= "Time" >0</span> </div>   <ul id= "Mine" >   </ul>   ie6+ FF oprea Google Opera earlier version default does not support Document.on ContextMenu event did not find a good workaround <script> var $ = function (ID) {return document.getElementById (ID)}; Window.onload=function Ready () {var v=$ (' WH '). Value.split (' * ') Setminefield (number (v[0)), number (v[1));   $ (' WH '). onchange=$ (' ready '). Onclick=function () {v=$ (' WH '). Value.split (' * ') Setminefield (number (v[0)), number (v[1))} //---------------------------------------------------mined area var minefield={_minew:30,//Each ray's width must be synchronized with the style _mineh:30, _ minefieldblock:$ (' Mine '), _minefieldele:$ (' Mine '). getElementsByTagName (' Li '), _time:$ (' time '),   status:0,// Minefield status 0 hasn't started yet. 1 started already at the time 2 game ended minenum:0,//Thunder number clearplace:0,//statistical sweep of the lattice; x:0,//Column y:0,//Row density:0.2,//ray density minefield density not exceeding 0.5 in some sizes of mined areas too high settings cannot generate Minemap array minemap:[],//Minefield two-dimensional diagram 0: not Ray-1: Ray time:- 1,//Chronograph s Debug:false//debug Mode}//minefield object End   function Timedcount () {if (minefield.status!=1) {return false} m Inefield. time++; Minefield._time.innerhtml=minefield.time; SetTimeout ("Timedcount ()", 1000); }//--------------------------------------The state of the Thunder set function Setthismine (str,index) {  var allmine=minefield._ Minefieldele; Set whether the Thunder is inserted flag if (str== ' setOn ') {var thismine=allmine[index]; thismine.on=thismine.on<2?thismine.on+1:0; if ( thismine.on==1) {thismine.classname= ' mine_03 ';} Flag else if (thismine.on==2) {thismine.classname= ' mine_04 '}//cancel flag else if (thismine.on==0) {thismine.classname= ' Mine_ 01 '}//cancellation flag return false; }//Display grid if (str== ' show ') {if (Object.prototype.toString.call (index) = = ' [Object Array] ') {//display a large minefield.clearplace =minefield.clearplace+index.length; for (Var i=0;i<index.length;i++) {var thismine=allmine[index[i]]; thismine.innerhtml=minefield.minemap[ Thismine.index]; Thismine.classname= ' mine_02 '; thismine.on=3; } else{//displays a single non-mined area minefield.clearplace++; allmine[index].on=3; Allmine[index].innerhtml=minefield.minemap[index]; Allmine[index].classname= ' mine_02 '; } if (minefield.clearplace==minefield.x*minefield.y-minefield.minenum) {//Congratulations to you alert (' Congratulations '); for (Var i=0;i<allmine.length;i++) {if ( Minefield.minemap[allmine[i].index]==-1) {allmine[i].classname= ' mine_07 ';}} minefield.status=2; return false; }//Show All Thunder if (str== ' Peng ') {for (Var i=0;i<allmine.length;i++) {if (minefield.minemap[allmine[i].index]==-1) { Allmine[i].classname=i==index? ' Mine_06 ': ' mine_05 '; }} minefield.status=2; }//-----------------------------------------------set number of rows and columns function Setminefield (a,b) {var Thisminefiele=minefield . _minefieldblock; Divbox=document.createelement ("div"), num=a*b,k=0; Thisminefiele.innerhtml= '; The parameters of mined area are adjusted minefield.x=a;//several rows of minefield.y=b;//have several lines Minefield.minenum=math.floor (a*b*minefield.density); minefield.status=0; Minefield.time=-1; minefield.clearplace=0; minefield.minemap.length=0; minefield._time.innerhtml=0;  //Generate mined area map setminemap (); Generate mined area (create Li) while (k<num) {var newli=document.createelement ("Li"); if (Minefield.debug) newli.innerhtml= minefield.minemap[k];//Cheating newli.classname= ' mine_01 '; Divbox.appendchild (NEWLI); k++; } thisminefiele.style.height=minefield._minew*b+ ' px '; thisminefiele.style.width=minefield._mineh*a+ ' px '; thisminefiele.innerhtml=divbox.innerhtml; Divbox=null; SetEvent ()//event}//-----------------------------------generate mined area Map function Setminemap () {var num=minefield.x*minefield.y , Minenum=minefield.minenum, Interval=math.floor (Num/minenum); for (Var i=0;i<num;i++) {if (i%interval==0&&i<minenum*interval) {minefield.minemap[i]=-1} else{minefield.minemap[i]=0;} Lightning equidistant distribution and array} mineField.mineMap.sort (function () {return 0.5-math.random ()})//disturb array  //judge whether the squares are all thunder if it is, then regenerate the mined areas from and to avoid the existence of a minefield of br=0,//var x=minefield.x, L_t,t,r_t,l,r,l_b,b,r_b; for (Var i=0;i<num;i++) {Br=math.ceil (i+1)/x); l_t = i-x-1; T = i-X; r_t = i-x+1; L = i-1; R = i+1; L_b = i+x-1; B = i+x; R_b = i+x+1; If you're in the minefield and you're on the line and he's not Ray, go to the next square check if (L_t>=0&&math.ceil (l_t+1)/x) ==br-1&&minefield.minemap[l_ t]==0) {Continue} if (T >=0&Amp;&math.ceil ((T + 1)/x) ==br-1&&minefield.minemap[t]==0) {continue} if (R_t>=0&&math.ceil ( r_t+1)/x ==br-1&&minefield.minemap[r_t]==0) {continue}   if (L>=0 &&math.ceil ((l+1)/x) ==br &&minefield.minemap[l]==0) {Continue} if (R<num&&math.ceil (r+1)/x) ==br&& minefield.minemap[r]==0) {Continue}   if (L_b<num&&math.ceil (l_b+1)/x) ==br+1&& minefield.minemap[l_b]==0) {Continue} if (b <num&&math.ceil ((b + 1)/x) ==br+1&&minefield.minemap[b ]==0) {Continue} if (R_b<num&&math.ceil (r_b+1)/x) ==br+1&&minefield.minemap[r_b]==0) {Continue}   Setminemap (); return false}//Statistics the number of mines around non-lightning squares for (i=0;i<num;i++) {if (minefield.minemap[i]==-1) {continue} var thisminenum=0 br= Math.ceil ((i+1)/x); l_t = i-x-1; T = i-X; r_t = i-x+1; L = i-1; R = i+1; L_b = i+x-1; B = i+x; R_b = i+x+1;   if (L_t>=0&&math.ceil (l_t+1)/x) ==br-1&&minefield.minemap[l_t]==-1) {thisMineNum++;}if (t >=0&&math.ceil ((t + 1)/x) ==br-1&&minefield.minemap[t]==-1) {thisminenum++;} if (R_T>=0 &&math.ceil ((r_t+1)/x) ==br-1&&minefield.minemap[r_t]==-1) {thisminenum++;}   if (L>=0 & &math.ceil ((l+1)/x) ==br&&minefield.minemap[l]==-1) {thisminenum++;} if (R<num&&math.ceil ( r+1)/x) ==br&&minefield.minemap[r]==-1) {thisminenum++;}   if (L_b<num&&math.ceil (L_B+1)/x ==br+1&&minefield.minemap[l_b]==-1) {thisminenum++} if (b <num&&math.ceil ((b + 1)/x) ==br+1& &minefield.minemap[b]==-1) {thisminenum++} if (R_b<num&&math.ceil (r_b+1)/x) ==br+1&& Minefield.minemap[r_b]==-1) {thisminenum++;}   Minefield.minemap[i]=thisminenum; }//----------------------------------mined area event function SetEvent () {var thisminefiele=minefield._minefieldblock, Allmine=minefield._minefieldele, Imax=minefield.x*minefield.y; for (Var i=0;i<imax;i++) {allmine[i].index=i; allmine[i].on=0;//0 is plug for default 1Flag 2 is a question mark 3 for display} thisminefiele.onmouseup=function (e) {if (minefield.status==2) {return false;} if (minefield.status==0) { Minefield.status=1;timedcount ();} var e=e| | Window.event, thisobj=e.target| | E.srcelement; if (thisobj.nodename== ' UL ' | | Thisobj.on==3) {return false;}   var Btn=getbutton (e); if (btn== 0) {//left arrow if (Thisobj.on==1) {return false;} You can't point the flag.   if (minefield.minemap[thisobj.index]==-1) {//Point Ray Setthismine (' Peng ', Thisobj.index);} else if (minefield.minemap[thisobj.index]==0) {//point to open space opens a large//alert (' You're lucky to be good luck ') var allshowmine=minesshow (Thisobj.index ); Setthismine (' show ', allshowmine)}else{//displays a lattice setthismine (' shows ', Thisobj.index)}} if (btn== 2) {//Right button Setthismine (' SetOn ', thisobj.index); A large function minesshow (I) {var allmine=minefield._minefieldele is displayed when the space is pressed)}}//-------------------------------- allshowmine=[i];//Save the subscript   allmine[i].on=3 of the Thunder to be displayed;   Look (i);//Check the surrounding grid function of the query subscript I (AllI) {var _alli=[]; if (Object.prototype.toString.call (allI) = = "[Object Array] {for (Var i=0;i<alli.length;i++) {f (Alli[i]}} else{f (AllI)} function f (thisi) {var text, x=minefield.x, BR, NUM=X*MINEFIELD.Y, l_t , T,r_t,l,r,l_b,b,r_b;   text= ' _ ' +allshowmine.join ('_') + ' _ ';//To determine whether the subscript has been written to the array Br=math.ceil ((thisi+1)/x); l_t = thisi-x-1; T = Thisi-x; r_t = thisi-x+1; L = thisI-1; R = thisi+1; L_b = thisi+x-1; B = thisi+x; R_b = thisi+x+1; Upper left corner of the square if it is in the minefield is in the previous line is not open the lattice is also a space then write such as _alli array for the next retrieval if (L_t>=0&&math.ceil (l_t+1)/x) ==br-1&& allmine[l_t].on==0&&minefield.minemap[l_t] = = 0) {_alli.push (l_t);} if (T >=0&&math.ceil ((t + 1)/x) ==br-1&&allmine[t].on==0&&minefield.minemap[t] = = 0) {_alli.push (T);} if (r_t>=0&& Math.ceil ((r_t+1)/x) ==br-1&&allmine[r_t].on==0&&minefield.minemap[r_t] = = 0) {_allI.push (R_T);}   if (L>=0&&math.ceil (l+1)/x) ==br&&allmine[l].on==0&&minefield.minemap[l] = = 0) {_ Alli.push (L);} if (R<num&&math.ceil (r+1)/x) ==br&&allmine[r].on==0&amP;&minefield.minemap[r] = = 0) {_alli.push (R);}   if (L_b<num&&math.ceil (l_b+1)/x) ==br+1& &allmine[l_b].on==0&&minefield.minemap[l_b] = = 0) {_alli.push (l_b);} if (b <num&&math.ceil (b +1)/x) ==br+1&&allmine[b].on==0&&minefield.minemap[b] = = 0) {_alli.push (B);} if (r_b<num& &math.ceil ((r_b+1)/x) ==br+1&&allmine[r_b].on==0&&minefield.minemap[r_b] = = 0) {_allI.push (R_B );} ------------------------------------------------//upper left corner of the grid if the minefield is in the previous line is not open the lattice is not written allshowmine array, then write it and marked in advance to open the lattice if (L_t>=0&&math.ceil (l_t+1)/x) ==br-1&&allmine[l_t].on==0&&text.indexof ( ' _ ' +l_t+ ' _ ') = = 1) {Allshowmine.push (l_t); allmine[l_t].on=3} if (t >=0&&math.ceil ((t + 1)/x) ==br-1& &allmine[t].on==0&&text.indexof (' _ ' +t+ ' _ ') = = 1) {Allshowmine.push (T); allmine[t].on=3} if (R_T>=0 &&math.ceil ((r_t+1)/x) ==br-1&&allmine[r_t].on==0&&text.indexof (' _ ' +R_T+ ' _ ') = =-1) {Allshowmine.push (r_t); allmine[r_t].on=3}   if (L>=0&&math.ceil (l+1)/x) ==br&&allmine[ L].on==0&&text.indexof (' _ ' +l+ ' _ ') = = 1) {Allshowmine.push (L); allmine[l].on=3} if (r<num&& Math.ceil ((r+1)/x) ==br&&allmine[r].on==0&&text.indexof (' _ ' +r+ ' _ ') = = 1) {Allshowmine.push (R); Allmine[r].on=3}   if (L_b<num&&math.ceil (l_b+1)/x) ==br+1&&allmine[l_b].on==0&& Text.indexof (' _ ' +l_b+ ' _ ') = = 1) {Allshowmine.push (l_b); allmine[l_b].on=3} if (b <num&&math.ceil ((b + 1)/x) ==br+1&&allmine[b].on==0&&text.indexof (' _ ' +b+ ' _ ') = = 1) {Allshowmine.push (B); Allmine[b].on=3} if ( R_b<num&&math.ceil ((r_b+1)/x) ==br+1&&allmine[r_b].on==0&&text.indexof (' _ ' +R_B+ ' _ ') = =-1) {Allshowmine.push (r_b); allmine[r_b].on=3}   if (_alli.length!=0) {(_alli)}}} return Allshowmine}//------ --------------------------------document.oncontextmenu=function () {return false;} Prohibit right-click menu function gEtbutton (e) {var Btn; if (document.implementation.hasFeature (' mouseevents ', ' 2.0 ')) {Btn=e.button;} else{switch ( E.button) {case 0:case 1:case 3:case 5:case 7:btn=0; the case 2:case 6:btn=2; the case 4:btn=9; the break;}} return Btn; } </script>

The above is the entire contents of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.