<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>Minesweeper Bomb Generation And Tile Revealing | tait.tech</title><linkrel="stylesheet"href="/assets/css/style.css"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metaname="author"content="Tait Hoyem"><metaname="keywords"content=""><metaname="description"content=""><linkrel="stylesheet"href="/assets/css/katex.css"></head><body><divid="wrapper"><header><nav><inputtype="checkbox"id="menu"><labelfor="menu">☰</label><divclass="menu-content"><ahref="/"class="nav-link">Home</a><ahref="/blog/"class="nav-link">Blog</a><ahref="https://github.com/TTWNO/"class="nav-link"target="_blank"rel="noopener noreferrer">Github</a></div></nav></header><main><article><header><h1class="post-title">Minesweeper Bomb Generation And Tile Revealing</h1><timedatetime="20-09-12"class="post-date">Saturday, September 12 2020</time></header><hr><p>When I was creating a little Minesweeper game, I got confused at some points. My bomb generation didn’t look quite right, and I for sure didn’t quite get the whole cascading tile reveal thing. With a bit of internet research, I found what I was looking for. I’ll explain it all in one place for my own research purposes.</p><h2id="bomb-generation">Bomb Generation</h2><p>When I started this project I attempted to use a random bomb generator. By this I mean on each square, before it gets generated, give it a one in 15 change of being a bomb. Personally, I’m not sure why this never looked right. Something about the layout of the bombs did not mimic the classic Minesweeper game.</p><p>After looking at some open source Minesweeper examples, I started to get the idea. I wrote some mathematical statements describing the generation of bombs and how to get their x,y position from an appropriate number. For those non-mathy people, don’t leave just yet; there will be code equivalents to the math.</p><p>W and H are the width and height of the board respectively.</p><p><spanclass="katex-display"><spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mnmathvariant="italic">0</mn><mo>≤</mo><mi>r</mi><mo>≤</mo><mtext>W</mtext><mo>×</mo><mtext>H</mtext></mrow><annotationencoding="application/x-tex"> \it 0 \leq r \leq \text W \times \text H </annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8193em;vertical-align:-0.13597em;"></span><spanclass="mord"><spanclass="mord mathit">0</span><spanclass="mspace"style="margin-right:0.2777777777777778em;"></span><spanclass="mrel">≤</span><spanclass="mspace"style="margin-right:0.2777777777777778em;"></span><spanclass="mord mathit">r</span><spanclass="mspace"style="margin-right:0.2777777777777778em;"></span><spanclass="mrel">≤</span><spanclass="mspace"style="margin-right:0.2777777777777778em;"></span><spanclass="mord text"><spanclass="mord">W</span></span><spanclass="mspace"style="margin-right:0.2222222222222222em;"></span><spanclass="mbin">×</span><spanclass="mspace"style="margin-right:0.2222222222222222em;"></span><spanclass="mord text"><spanclass="mord">H</span></span></span></span></span></span></span><spanclass="katex-display"><spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo>=</mo><mi>r</mi><mtext></mtext><molspace="0.22em"rspace="0.22em"><mrow><mimathvariant="normal">m</mi><mimathvariant="normal">o</mi><mimathvariant="normal">d</mi></mrow></mo><mtext></mtext><mtext>W</mtext></mrow><annotationencoding="application/x-tex"> \it x = r \bmod \text W </annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:0.69444em;vertical-align:0em;"></span><spanclass="mord"><spanclass="mord mathit">x</span><spanclass="mspace"style="margin-right:0.2777777777777778em;"></span><spanclass="mrel"
</code></pre></div></div><p>So that’s that, we can put this in a big ‘ol for loop and generate an arbitrary <em>n</em> number of bombs given a width and height of a Minesweeper board.</p><h2id="cascading-tile-revealing">Cascading Tile Revealing</h2><p>This one is hard to describe; I am adapting this from <ahref="https://leetcode.com/problems/minesweeper/">leetcode.com</a>. Whenever a player clicks a tile, the following logic should be used:</p><ol><li>If a mine is revealed, the game is over. (obviously)</li><li>If a tile with <em>no</em> adjacent mines is revealed, recursively reveal all eight adjacent tiles.</li><li>If a tile with one or more adjacent mines is revealed, display the number of mines next to it.</li></ol><p>Here is the code in Python for this algorithm.</p><divclass="language-python highlighter-rouge"><divclass="highlight"><preclass="highlight"><code><spanclass="k">def</span><spanclass="nf">reveal_square</span><spanclass="p">(</span><spanclass="n">x</span><spanclass="p">,</span><spanclass="n">y</span><spanclass="p">,</span><spanclass="n">board</span><spanclass="p">,</span><spanclass="n">alread_revealed</span><spanclass="p">):</span>
</code></pre></div></div><p>This has no checks for valid squares, but it’s the general idea. This function returns an array of tile coordinates which should be revealed.</p><h2id="conclusion">Conclusion</h2><p>I wrote this because in the first place because I was writing my own Minesweeper game. I hope that this helps you with getting the general idea of a Minesweeper game. The completed version of this game is available on my <ahref="https://lamegames.tait.tech/">lamegames</a> site. Let me know what you think!</p><p>Happy hacking!</p></article></main><hr><footer> This page is mirrored on <ahref="https://beta.tait.tech/2020/09/12/minesweeper.html">beta.tait.tech</a>. </footer></div></body></html>