📜 ⬆️ ⬇️

Minesweeper, or again 30 lines

In the course of the week, 30 lines on JS in Habré, decided to make a contribution.
There were no ideas, but they were suggested by one of the commentators (I don’t remember which one), who brought a whole list of games not yet written in 30 lines.
I submit to your court my creation - the good old sapper.
image

Link
The code itself:
(function (count,fileds) { function cl(id){x=document.getElementById(id);return x?(x.className.indexOf('bomb')!=-1?1:0):0;} var bombs=0; for(i=0;i<fileds;i++){ r=document.createElement('div'); if(Math.random()*fileds<count){r.className='bomb close',document.getElementById('text').innerHTML=(++bombs)+' bomb\'s';} else r.className='close'; r.id=Math.floor(i/10)+'_'+i%10; document.body.appendChild(r); } for(o=0;o<fileds;o++){ i=Math.floor(o/10),j=o%10,num=0,obj=document.getElementById(i+'_'+j); for(k=0;k<9;k++)num+=cl((i-(Math.floor(k/3)-1))+'_'+(j-(k%3-1))); obj.innerHTML=num==0?' ':num; obj.onclick=function(){mix=this.id.split('_'),open(mix[0],mix[1]);} obj.oncontextmenu=function(){this.className=this.className.indexOf('flag')!=-1?this.className.replace(/ flag/,''):this.className+' flag';return false;} } function open(i,j){ dom=document.getElementById(i+'_'+j); if(!dom||dom.className.indexOf('close')==-1)return; if(dom.className.indexOf('bomb')!=-1){ divs=document.getElementsByTagName('div'); for(i=0;i<divs.length;i++)divs[i].className=divs[i].className.indexOf('bomb')!=-1?'bomb':''; alert('You lose!'); } else { dom.className=''; var elems = document.getElementsByTagName('div'),len=0; for (ki in elems)if(elems[ki].className&&elems[ki].className.indexOf('close')!=-1)len++; if(len<=bombs)alert('You win!'); } if(dom.innerHTML==' ')for(var ks=0;ks<9;ks++)open(i-((Math.floor(ks/3)-1)),j-(((ks%3)-1))); } }(10,100)); 


The title, however, was a little misleading - it turned out 34 lines.
If you remove the installation of flags on mines, any messages, and even more stuff in the lines - then you can bring to 30, even less. But my goal was different - it was just interesting to write something fun and funny. (before attempts to bring the code to 30 lines, it was 54 lines;)

Thank you all for pushing to write this funny creation, and especially the authors of these games.
- Gonochka on JavaScript (30 lines of code)
- Tiny Arkanoid in JavaScript (30 lines of code)
- Tiny Excel in pure javascript (30 lines of code)
- Tiny Snake JavaScript (30 lines of code)
- Tetris in javascript (in 30+ lines)
And the last (of course not JS, but also "crazy")
- Drawing under Windows in C ++, or “Guys, I'm crazy too!” (30+ lines of code)
')
And one more variation of the sapper.
- Windows sapper in 50 lines, or as I did not fit in 30 lines

And even in the sandbox;)
- Tiny sapper in 50 lines

Source: https://habr.com/ru/post/202750/


All Articles