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]