That first image really needs to be the game itself. I had such an instinct to click it as soon as I saw it and was sad when I noticed it was just an image. Overall the article is nice, if a bit dated, but at least it's a change from a new reactive library.
I was working on a multiplayer minesweeper game in elixir and react a couple years back to try and get a better understanding of otp.<p><a href="https://mines.gdf3.com" rel="nofollow">https://mines.gdf3.com</a>
<a href="https://github.com/slofurno/minesweepers-ex" rel="nofollow">https://github.com/slofurno/minesweepers-ex</a>
<a href="https://github.com/slofurno/minesweepers-front" rel="nofollow">https://github.com/slofurno/minesweepers-front</a><p>Wound up spending most of my time focusing on performance. Large boards (say 4000x4000) naively represented by a map in elixir consumed too much memory and trying to render 16 million dom elements in react didn't fly.<p>Never got around to basic ui stuff like setting player name and color, but I thought the bots were pretty cool.
I'm sure there's only a few ways to do it, but this code is really similar to the one I wrote back in December: <a href="https://github.com/mikehodgson/minesweeper" rel="nofollow">https://github.com/mikehodgson/minesweeper</a><p>It is playable here: <a href="https://sweeper.games" rel="nofollow">https://sweeper.games</a><p>No jQuery, just straight CSS/HTML/ES6
Google Chrome Labs released a very shiny typescript minesweeper on <a href="https://proxx.app" rel="nofollow">https://proxx.app</a>
The source code is on Github
<a href="https://github.com/GoogleChromeLabs/proxx" rel="nofollow">https://github.com/GoogleChromeLabs/proxx</a>
excellent post.<p>can i make a (hopefully useful) comment about programming style - something that someone shared with me a long time ago when reading my code that i have found to be very valuable over the years?<p>it can be incredibly beneficial (for readability, catching logic errors, etc.) to "exit early" from "if" statements. meaning, if you find that you're nesting "ifs" more than a couple of levels deep, the code <i>may</i> be a candidate for flattening.<p>so - your handleClick function could be rewritten (with stuff removed) as:<p><pre><code> var handleClick = function( id )
{
if ( gameOver ) return;
if ( ctrlIsPressed )
{
// do stuff...
return;
}
if ( cell.opened || cell.flagged ) return;
if ( cell.mined )
{
// do stuff...
return;
}
// else do stuff...
if ( cell.neighborMineCount > 0 )
{
// ...
return;
}
// else do final stuff...
}
</code></pre>
i may have missed something, but hopefully you get the point. this simple refactoring reduced the depth of the if statements from ~5 to 1. ...many of the other functions could be flattened just like this.<p>...and how do you know when something can be flattened?<p>if there is no code after the if statement and the end of the function - just swap the logic and return early.<p>e.g., this:<p><pre><code> var handleClick = function( id )
{
if ( !gameOver )
{
// ...lots of code and if statements...
}
// ...but no code after the block before returning from the function...
}
</code></pre>
...turns into this:<p><pre><code> var handleClick = function( id )
{
if ( gameOver ) return; // NOTE: logic check change...
// ...do the stuff in the block here...
}
</code></pre>
...and this is also a great pattern for checking input variables (and returning or throwing an exception) at the top of the function, ensuring that the code following it has valid input parameters.<p>since you're sharing your coding projects on your blog (which are excellent) - hopefully you can share this tidbit about coding style with your readers and they'd find it as useful as i have.
Here is my implementation from 2014 (with jquery...). Watch out. It has lots of bugs!<p><a href="http://www.ronilan.com/bugsweeper/" rel="nofollow">http://www.ronilan.com/bugsweeper/</a>
The game board is a well ordered grid, I can't fathom why you'd not organise it into an array. The cell object has 3 completely redundant fields. Most of the code just checks the mined parameter of cell objects, but for some reason there is also an isMined function. The code placing mines uses its own data structure to keep track of where mines have been placed, rather than the board data, and manages to be O(n^2) instead of O(n) because of that choice.<p>Overall it is code that takes a lot of unnecessary detours.
In a notebook:<p><a href="https://observablehq.com/@benjaminadk/minesweeper" rel="nofollow">https://observablehq.com/@benjaminadk/minesweeper</a>
Well. it is good tutorial but there is a little problem.<p>In minesweeper mines should be generated after first open. You can't hit mine at first click it must empty or number.
This was a lot of work and you should be really proud.<p>One suggestion which could simplify things. I noticed this bit of code:<p><pre><code> var getNumberColor = function( number )
{
var color = 'black';
if (number === 1)
{
color = 'blue';
}
if (number === 2)
{
color = 'green';
}
// etc
}
</code></pre>
You mention using a switch statement, but why not just use an array and look it up by the index?
Uh oh. Since folks are posting their own implementations, here’s mine, written as an exploratory exercise for Angular2:<p><a href="https://github.com/jonmellman/angular2-minesweeper" rel="nofollow">https://github.com/jonmellman/angular2-minesweeper</a><p><a href="https://jonmellman.com/minesweeper/" rel="nofollow">https://jonmellman.com/minesweeper/</a><p><i>Grimace</i>
I guess this is a good time to share my implementation too. Unfortunately it's pretty much desktop only, but includes a high scores board!<p><a href="https://github.com/reed-jones/minesweeper_js" rel="nofollow">https://github.com/reed-jones/minesweeper_js</a><p>And to play it:<p><a href="https://minesweeper.zone/" rel="nofollow">https://minesweeper.zone/</a>
I built a copy of Minesweeper using AngularJS for fun that may be instructive.<p>Demo: <a href="http://ceasarbautista.com/minesweeper/" rel="nofollow">http://ceasarbautista.com/minesweeper/</a><p>Source: <a href="https://github.com/Ceasar/minesweeper" rel="nofollow">https://github.com/Ceasar/minesweeper</a>
One weekend I was bored and made a React version of minesweeper. Needless to say I had a lot of fun.<p>Emoji minesweeper: <a href="https://codepen.io/nojvek/full/KjLxdx" rel="nofollow">https://codepen.io/nojvek/full/KjLxdx</a>
MSFT, our multiplayer Twitch Plays implementation of minesweeper, playable directly with your mouse (left click to dig, middle click to flag):<p>MineSweeper For Twitch (MSFT) [1]<p>----<p>[1] <a href="https://www.twitch.tv/bzh314" rel="nofollow">https://www.twitch.tv/bzh314</a>
cool tutorial, love all of the code examples. Some of that code is brutally unreadable with all of the nested statements and loops. I guess it works for the tutorial but ooof!
My friend (Math genius) made a video tutorial about writing Minesweeper in JS in a Notepad <a href="https://www.youtube.com/watch?v=OuahlosQ1m0" rel="nofollow">https://www.youtube.com/watch?v=OuahlosQ1m0</a><p><i></i>* It is in Czech :(