www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Small minesweeper game in D

reply Adam D. Ruppe <destructionator gmail.com> writes:
code here:
http://arsdnet.net/dcode/minesweeper.d

Here's a bit of fun. I saw this article on reddit: 
http://rkoutnik.com/articles/How-I-Interview.html

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:

http://arsdnet.net/dcode/minesweeper.d

It depends on my simpledisplay.d and color.d from here:
https://github.com/adamdruppe/arsd

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: http://arsdnet.net/minesweeper.png


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: https://en.wikipedia.org/wiki/Flood_fill

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....

...so 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

http://arsdnet.net/arsd/simpledisplay.html

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 :)
Dec 19 2015
next sibling parent reply stew <growlercab gmail.com> writes:
On Sunday, 20 December 2015 at 02:11:58 UTC, Adam D. Ruppe wrote:
 code here:
 http://arsdnet.net/dcode/minesweeper.d

 [...]
Nice! I love doing this kind of thing on a lazy Sunday. Pick any 80's arcade game and try to implement as much as possible in an hour. I have several languages I cycle through but the last 1-hour gamehack in D I did is here: "D-Invaders" under: http://dgame-dev.de/index.php?controller=wip The Github repo now has a bunch of extra commits since that initial hour to keep it compiling with Dgame and D. It isn't pretty, nor idiomatic, but I was learning D and Dgame at the same time in that hour :) I realised two things from that first hour with D, which have both proved true so far: a) (the most important for me) I can be as productive in D as I am in Python but still keep my static typing and native performance I get with C/C++. b) D is only going to keep improving. Cheers, Stew
Dec 19 2015
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 20 December 2015 at 06:26:43 UTC, stew wrote:
 "D-Invaders" under: http://dgame-dev.de/index.php?controller=wip
Nice! Dgame looks to be a pretty nice little lib too.
 a) (the most important for me) I can be as productive in D as I 
 am in Python but still keep my static typing and native 
 performance I get with C/C++.
My D productivity is so much higher than I am with ruby and python it isn't even a competition...
Dec 20 2015
prev sibling next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Sunday, 20 December 2015 at 02:11:58 UTC, Adam D. Ruppe wrote:
 code here:
 http://arsdnet.net/dcode/minesweeper.d

 [...]
The code looks easy to understand also. You might consider writing this up into a blog post.
Dec 20 2015
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 20 December 2015 at 17:24:41 UTC, jmh530 wrote:
 The code looks easy to understand also. You might consider 
 writing this up into a blog post.
I might if I had a blog... which I need to set up at some point but haven't yet (well, I used to have one but not for years). But what I posted here is the most of what I'd say anyway. I guess I could write a bit more about the code but most of it is IMO at least fairly straightforward.
Dec 20 2015
parent bubbasaur <bubba gmail.com> writes:
On Monday, 21 December 2015 at 02:28:27 UTC, Adam D. Ruppe wrote:
 On Sunday, 20 December 2015 at 17:24:41 UTC, jmh530 wrote:
 The code looks easy to understand also. You might consider 
 writing this up into a blog post.
I might if I had a blog... which I need to set up at some point but haven't yet (well, I used to have one but not for years).
You can write in "THIS WEEK IN D"... please do it! Bubba.
Dec 21 2015
prev sibling next sibling parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Sunday, 20 December 2015 at 02:11:58 UTC, Adam D. Ruppe wrote:
 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.
I had to install libglu1-mesa* on Ubuntu. Nicely done though!
Dec 20 2015
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 20 December 2015 at 20:58:25 UTC, Taylor Hillegeist 
wrote:
 I had to install libglu1-mesa* on Ubuntu. Nicely done though!
Huh, that's good to know! Everywhere else I tried on Linux it just worked but it does indeed require GL and GLU just because simpledisplay on Linux links to them, though I didn't actually use OpenGL for this program. (on Windows, opengl support is opt-in because the opengl32.lib and glu32.lib aren't included with dmd, I could do that on Linux too I suppose but installing glu isn't that hard anyway...)
Dec 20 2015
prev sibling parent reply wobbles <grogan.colin gmail.com> writes:
On Sunday, 20 December 2015 at 02:11:58 UTC, Adam D. Ruppe wrote:
 code here:
 http://arsdnet.net/dcode/minesweeper.d

 [...]
On Ubuntu 64 bit: $ dmd minesweeper.d simpledisplay.d color.d simpledisplay.d(4477): Error: cannot implicitly convert expression (XCreatePixmapCursor(this.display, pm, pm, & blackcolor, & blackcolor, 0u, 0u)) of type ulong to int $ dmd --version DMD64 D Compiler v2.069.2 I casted the problem away with cast(int)XCreatePixmapCursor(...) to play a couple games. Not really solving the problem though... Nice work though! 'Tis very cool. The game code is very simple to follow too. I'll try making a simple game using simpledisplay over the christmas. It looks quite nifty!
Dec 21 2015
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 21 December 2015 at 10:52:07 UTC, wobbles wrote:
 On Ubuntu 64 bit:
whoops, I always build 32 bit here and sometimes forget to test 64 bit before pushing. Fixed now.
 The game code is very simple to follow too.
 I'll try making a simple game using simpledisplay over the 
 christmas.
 It looks quite nifty!
Sweet, let me know how it goes!
Dec 21 2015