kdb programming challenge – Minesweeper

AquaQ Admin kdb+ 2 Comments


Oops! Game over!

Happy New Year everyone! I hope everyone had a very good holiday and wouldn’t it be great to start the new year with a big bang right?? It has been a while since the last challenge and I’m sure my fans are dreading for one 🙂 Ok, enough of messing around. Let’s get down to business!

I’m sure everyone is familiar with the Minesweeper game. In this challenge, we are not making the game itself but just the grid. The best thing is that this challenge has 2 parts! Here are the followings:

1) Create a function that generates a random mine grid. (m n dimension with approximately x chance of mine spawns). For example:

// 3 x 3 grid with roughly 50{e673f69332cd905c29729b47ae3366d39dce868d0ab3fb1859a79a424737f2bd} chance of mine spawns
q)f[3 3;0.5]
" xx"
"x x"
" x"
q)avg"x"=raze f[3 3;0.5]
q)avg"x"=raze f[3 3;0.1]

2) Create another function that populates the grid with clues indicating number of mines around the neighboring cells. For example:

q)g f[3 3;0.5]
q)g f[2 3;0.5]

I find this challenge quite interesting. There are some nice techniques that can be used in this challenge. But of course I’ll let you crack on and hope you enjoy this challenge as much as I do! Answers to follow up by the end of this month!

AquaQ Adminkdb programming challenge – Minesweeper

Comments 2

  1. Kash

    borrowed code for g from Arthur.

    f:{x#(*/x)?,/(100-y;y:"i"$y*100)#'" x"}
    avg"x"=,//f[5 5;]'10000#0.5
    avg"x"=,//f[5 5;]'10000#0.3
    q)g f[5 5;0.3]

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax