# kdb programming challenge – Minesweeper

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]
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!

kdb programming challenge – Minesweeper

```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" ```
```g:{.Q.n[2{+(0+':x)+(1_x),0}/x="x"]^x} ```