code here:

Here's a bit of fun. I saw this article on reddit:

Short summary: when interviewing people for a code position, this author gives the candidate one hour to write as much as minesweeper as they can. He says nobody has ever finished it in an hour and nobody is expected to, but he just wants to see how far they get.

Nobody has finished it in an hour? CHALLENGE ACCEPTED.

I decided to give it a go over the last hour and hacked this together:

It depends on my simpledisplay.d and color.d from here:

Download just those two files, simpledisplay.d and color.d, to your current directory with minesweeper.d and compile:

dmd minesweeper.d simpledisplay.d color.d

And play the game! At least on Windows and Linux. On Mac, you need to install XQuartz since I don't have a working Cocoa implementation in simpledisplay.d right now. Fear not, it is on my todo list for 2016, so it will be there eventually.

My game isn't exactly fancy.... but it is playable. here's a screenshot:

I prolly could have used a downloaded icon set and made it look prettier, but I didn't want to look for one nor deal with copyright so I just drew text and rectangles.

Now, the interviewer used javascript, but I wanted another example for simpledisplay.d, so I used that instead. In JS, given the time constraint, I think I would have done it as an array of <img> tags with an onclick handler on the parent node which changes the src to a particular icon, or a class name for a css sprite. whatever. But the JS algorithm would be basically the same and I suspect I could do it in a similar amount of time since it is a simple program anyway. I just like D better.

I had to look up the flood fill which took seconds (I played real minesweeper and recognized it was the same algorithm MS Paint uses. I started to hack it myself but realized that I didn't really know it that well and figuring it out myself from principles, while doable, would likely eat my remaining time right there, so I Binged "paint fill algorithm".

Wikipedia popped right up with a description was good enough to run with:

I implemented the simple recursive version as a template with a few parameters to fit my needs here, while also being usable in my other side project, which has a MS Paint like graphics editor and needs this feature anyway!

Anyway, he rest was pretty straightforward. The complete game has a timer, a scoreboard, a UI for selecting size, etc. I have functions that could do that, but didn't have time to actually put on more UI than the basic game with mouse clicks.... indeed, I didn't finish it either. Challenge failed :(

But since it is playable i'm calling this a pretty decent success anyway! And now I have one more example to show off with simpledisplay.d, which I will might add to the documentation

along with the existing Pong game.

I didn't use any really special D features there, so not really a language showcase - like I said before, if I was doing it in Javascript, I probably would have done it largely the same - but hey, it is D, so I will post it here :)

Reply via email to