54
2
Minesweeper is a popular puzzle game where you must discover which tiles are "mines" without clicking on those tiles. Each tile is either a mine (represented by *
) or a clue, i.e. a number from 0 to 8 representing how many of the 8 neighboring tiles are mines. Your task today is to take a board containing the mines and fill in all of the clues. For example, look at the following 5x4 board, with 5 mines:
*
* *
*
*
After filling in the clues, the board will look like this:
2*211
*33*1
12*32
0112*
Details
You must write either a full-program or a function that takes in a grid of characters containing only spaces and asterisks and outputs another grid where each space is replaced with the number of adjacent mines (asterisks). Any of these are acceptable formats for your grids:
A string with newlines in it
A 2D list of characters/single character strings
A list of strings
You can assume the grid will be at least 1x1, although it is possible for it to be all mines or all spaces.
The input grid will always be padded with the appropriate number of spaces. As usual, this is code-golf, so standard loopholes apply and the shortest answer in bytes wins!
Sample IO
So that you can see the whitespace, I will show all sample IO with brackets around it.
Input:
[ * ]
[* ]
[ ]
[ ]
[ ** ]
[ * * ]
Output:
[1101*1]
[*10111]
[110000]
[012210]
[12**21]
[1*33*1]
Input:
[****]
[****]
Output:
[****]
[****]
Input:
[ ]
[ ]
[ ]
[ ]
Output:
[000]
[000]
[000]
[000]
Input:
[* ]
[** ]
[ ]
[ *]
Ouput:
[*310]
[**10]
[2221]
[001*]
Input:
[** ]
[* *]
[ * ]
[ ]
[* ]
[**** ]
Output:
[**1011]
[*4211*]
[12*111]
[121100]
[*43210]
[****10]
Input:
[ * ]
[ * ]
[ * ]
[** *** ]
[ *** ]
[ ]
[ ** ]
[ * * ]
[* ** ]
[ ** ]
Output:
[00001*1111]
[00002221*1]
[22102*4321]
[**102***31]
[221013***1]
[0000013542]
[0112111**1]
[12*2*12442]
[*212112**2]
[1100002**2]
2FYI, I made all of the sample IO by hand, so it's possible there are some minor errors in there. Let me know if something looks off and I'll try to fix it ASAP. – James – 2016-10-23T04:45:19.210
Related. – xnor – 2016-10-23T04:50:55.897
1Can the grid be non-square ? – Ton Hospel – 2016-10-23T08:56:39.927
Can the mines represented by another character? – Akangka – 2016-10-23T11:58:05.823
@ChristianIrwan No, the mines will always be an asterisk. – James – 2016-10-23T12:59:09.923
Can my output contain leading blank lines? – Lynn – 2016-10-23T13:19:42.260
Can we take I/O with brackets around it? – Destructible Lemon – 2016-10-23T22:21:01.280
@Lynn I guess that's fine. – James – 2016-10-24T15:19:16.343
@DestructibleWatermelon Re brackets around input: I don't see why that would make any difference, but I guess I don't have any problem with it. – James – 2016-10-24T15:19:50.210
Your first example says "look at the following 5x5 board", but in both the asterisk display and the numbers below it have 5 columns and 4 rows. – Cody – 2016-10-24T23:14:26.377