33
0
Your goal is to check whether a completed Minesweeper board is valid. This means that each number is a correct count of mines in cells adjacent to it, including diagonals. The board does not wrap around.
As usual, you should give a function or program, and shortest code in bytes wins.
See also past challenges to generate, solve, and fully implement Minesweeper.
Input:
A single string like this: 02X2 13X2 X211
.
The rows of the minesweeper board are given separated by spaces. So, the above represents the 3x4 board:
02X2
13X2
X211
Each cell is a character:
X
for a mine, or a number0
through8
.All rows have the same length.
There are at least 3 rows and 3 columns.
The input doesn't start or end with a space, but you may include a newline at the end if you wish.
Output:
A consistent Truthy on correct boards, and a consistent Falsey value on incorrect boards. Consistent means that all Truthy outputs are the same and all Falsey outputs are the same.
Test cases
Each line is a separate test case.
True
:
02X2 13X2 X211
XXXX XXXX XXXX XXXX
XX4X2 5X6X4 XX6XX 4XX54 2X4XX
False
:
02X2 13X2 X212
XXXX XXXX X7XX XXXX
XX5X2 5X6X4 XX6XX 4XX54 2X5XX
You should probably specify the consistent falsy output must be distinct from the consistent truthy output ;-) – John Dvorak – 2014-12-13T06:45:58.037
@JanDvorak That should be implied by them being Truthy and Falsey respectively. – xnor – 2014-12-13T07:23:27.543
Not really. "truthy" and "falsy" are just two labels that you're letting us define. I can see no restriction they be actually truthy or falsy respectively in the language we use. The only word that may require them to be distinct is the "indicating" verb. I'm not sure it counts as a spec (it's still forbidden as a standard loophole, though). – John Dvorak – 2014-12-13T07:29:10.187
4@JanDvorak 'truthy' and 'falsy' are actually somewhat common terms if I'm not mistaken basically used to describe things (not necessarily bools) that evaluate to true or false when typed to bools. For example 0 is generally falsy and 1 is generally truthy. – KSab – 2014-12-13T07:30:06.503
@KSab I know of their standard usage. But I don't see anything in the spec saying that our chosen truthy value is actually truthy in the language we use. In Ruby
0
is truthy, yet it would be a perfectly valid falsy output in this challenge (right?) – John Dvorak – 2014-12-13T07:34:47.247@JanDvorak I linked to the meta post on Truthy/Falsey. Does that make the terms clear? – xnor – 2014-12-13T07:35:34.477
Posted several comments in that meta thread. Let's see how it goes. As for this question, is it OK if I emit a truthy value for invalid inputs and a falsy value for valid inputs? – John Dvorak – 2014-12-13T07:41:42.597
1@JanDvorak Nope, Truthy/Falsey has to match correct/incorrect. – xnor – 2014-12-13T07:42:29.410
Can't I even choose True as my falsy output and False as my truthy output? – John Dvorak – 2014-12-13T07:43:12.577
@JanDvorak Do you mean the text "True" and text "False"? Can your language convert those to bools or use them in an
if
? If so, they should act correctly. – xnor – 2014-12-13T07:44:33.097@MartinBüttner A 3x3 block of mines is valid; there's a test case like it. – xnor – 2014-12-13T09:20:24.877
@JanDvorak If you used Ruby and output
0
to mean false, I would say that you had incorrectly used a truthy value instead of a falsy one. – Kevin – 2014-12-14T21:22:53.353