kdb programming challenge – Minesweeper

WooiKent Lee kdb+ 2 Comments

boom

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% chance of mine spawns
q)f[3 3;0.5]
" xx"
"x x"
" x"
q)avg"x"=raze f[3 3;0.5]
0.5555556
q)avg"x"=raze f[3 3;0.1]
0.1111111

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]
"2xx"
"x5x"
"13x"
q)g f[2 3;0.5]
"13x"
"x3x"

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!

WooiKent Leekdb 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
    0.500612
    avg"x"=,//f[5 5;]'10000#0.3
    0.300876
    g:{(,/'$:2{+(0+':x)+(1_x),0}/x="x")^x}
    q)g f[5 5;0.3]
    "xx12x"
    "x313x"
    "2212x"
    "1x111"
    "11100"

    1. Post
      Author

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