Minesweeper. Fixed height+length. Approx. 10-15 mines hidden. No timer. If you click on mine you loose (red cell) and gameboard is revealed. If you click on mine-free cell, this cell is colored green…
G=[];B="X";U=".";l=225;function $(b){return document.getElementById(b)}function m(b){return b?b.innerHTML:0}function n(b,d){if(b)b.innerHTML=d}function J(b,d){b.style.backgroundColor=d}function q(b,d){e=$(b+"#"+d);if(G[b][d]==B){J(e,"#f00");alert(B);for(b=0;b<15;b++)for(d=0;d<15;d++)n($(b+"#"+d),G[b][d])}else if(m(e)==U){J(e,"#5f5");n(e,G[b][d]);l--;a(b,d,0);l<=0&&alert("Won")}} function a(b,d,f){e=$(b+"#"+d);if(m(e)=="0"){o="";for(v=b-1;v<b+2;v++)for(w=d-1;w<d+2;w++){k=$(v+"#"+w);if(m(k)==U){J(k,"#5f5");n(k,G[v][w]);k&&l--;if(G[v][w]==0)o+=o.length>0?";"+v+";"+w:v+";"+w}}b=0;for(d=o.split(";");d.length>1&&b<d.length;b+=2)a(parseInt(d[b]),parseInt(d[b+1]),f+1)}}for(var r=0;r<15;r++){G[r]=[];for(var c=0;c<15;c++)G[r][c]=0}C=""; for(r=0;r<15;r++){for(c=0;c<15;c++){C+="<span id='"+r+"#"+c+"' onclick='q("+r+","+c+")' style='border:1px solid #aaa;'>.</span>";if(Math.random()<0.075){G[r][c]=B;l--;for(v=r-1;v<r+2;v++)for(w=c-1;w<c+2;w++)if(G[v]&&G[v][w]!=B)G[v][w]+=1}}C+="<br/>"}n(document.body,"<pre>"+C+"</pre>");
Rz1bXTtCPSJYIjtVPSIuIjtsPTIyNTtmdW5jdGlvbiAkKGIpe3JldHVybiBkb2N1bWVudC5nZXRFbGVtZW50QnlJZChiKX1mdW5jdGlvbiBtKGIpe3JldHVybiBiP2IuaW5uZXJIVE1MOjB9ZnVuY3Rpb24gbihiLGQpe2lmKGIpYi5pbm5lckhUTUw9ZH1mdW5jdGlvbiBKKGIsZCl7Yi5zdHlsZS5iYWNrZ3JvdW5kQ29sb3I9ZH1mdW5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N1bWVudC5ib2R5LCI8cHJlPiIrQysiPC9wcmU+Iik7
G=[]; // gameboard (holds state of cells)
B="X"; // represents bomb
U="."; // represents closed cell (status: undefined)
l=(15*15); // number of cells (l)eft (rows*cols)
//shortcut function to get element by id
function $(p) {
return document.getElementById(p);
}
//shortcut function to get innerHTML of given element p
function m(p) {
return p ? p.innerHTML : 0;
}
//shortcut function to set innerHTML of given element p
function n(p,v) {
if (p) p.innerHTML = v;
}
//shortcut function to set backgroundColor of given element p
function J(p,v){p.style.backgroundColor=v;}
// invoked when user clicks on a cell
function q(r, c) {
e = $(r+"#"+c);
if (G[r][c]==B) {
J(e,"#f00");
alert(B);
for (r=0; r<15; r++) {
for (c=0; c<15; c++) {
n($(r+"#"+c),G[r][c]);
}
}
} else {
if (m(e) == U) {
J(e,"#5f5");
n(e,G[r][c]);
l--;a(r,c,0);if (l<=0) {alert("Won");}
}
}
}
//reveal ("click") all cells around the given cell (r=row, c=col) with value = 0 (recursively).
function a(r, c, d) {
e = $(r+"#"+c);
if (m(e) == "0") { //cell has been clicked and bomb counter is 0
o = ""; // collect coordinates of other cells with bomb counter 0 to recursively invoke this method again
for (v=r-1; v<r+2; v++) {
for (w=c-1; w<c+2; w++) {
k = $(v+"#"+w);
if (m(k)==U) {
J(k,"#5f5");
n(k,G[v][w]);
if (k) l--;
if (G[v][w]==0) {
o += o.length>0?";"+v+";"+w:v+";"+w;
}
}
}
}
for (var i=0,p=o.split(";");p.length>1 && i<p.length;i+=2){
a(parseInt(p[i]),parseInt(p[i+1]),d+1);
}
}
}
// initialize memory representation of game board
for (var r=0;r<15;r++){G[r]=[];for (var c=0;c<15;c++)G[r][c]=0;}
// draw html representation of game board
C="";
for (var r=0; r<15; r++) {
for (var c=0; c<15; c++) {
C += "<span id='"+r+"#"+c+"' onclick='q("+r+","+c+")' style='border:1px solid #aaa;'>.</span>";
// place bombs (math.random<0,075 will result in approx. 10-15 bombs
if (Math.random()<0.075) {
G[r][c] = B;
l--;
// increment bomb counter in surrounding cells
for (v=r-1; v<r+2; v++) {
for (w=c-1; w<c+2; w++) {
if(G[v]&&G[v][w]!=B) G[v][w]=G[v][w]+1;
}
}
}
}
C+="<br/>";
}
n(document.body,"<pre>"+C+"</pre>");