this is a question about this codefight callenge.
The contestant is asked to check if a minesweeper field, represented as a 2D-Array is valid.
Details: Each cell of Minesweeper gameboard can be:
- a mine (appears as 9)
- or a number representing the number of mines in its surrounding cells (a cell is considered as surrounding another cell when this cell meets that cell on at least 1 corner) (appears as 0 - 8)
My approach (which works):
- loop through all items
- check neighbors for mines (and count number of mines, if there are any)
- compare number of found mines with number on tile
- return false, if numbers are unequal, else continue
Could someone please explain to me how this approach works?
minesweeper1 = g =>
!g.some((r,i) =>
r.some((c,j) =>
c < 9 && (f = d => d-- ? f(d) - ((g[i + ~-(d/3)] || 0)[j + d%3 - 1] > 8) : c)(9)
)
)
How much I understand:
- g is the 2D-array, representing the field
- .some will test, if an Element in the array will pass a test
- r are the single single rows in the field
- c is every single element in each row
- What are i and j? Counters?
- What is d?
- what is the advantage of writing code so cryptic?