Skip to content

Instantly share code, notes, and snippets.

@missinglink

missinglink/mines.js

Last active Oct 24, 2019
Embed
What would you like to do?
Javascript one-liner to calculate total minesweeper neighbours
var m = [
[' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' '],
['X',' ',' ',' ',' '],
[' ','X',' ',' ',' '],
[' ',' ','X',' ',' '],
]
var m = [
[' ',' ','X'],
[' ',' ','X'],
[' ','X',' '],
['X',' ',' '],
]
// Polyfill for ECMA 2019
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
if (!Array.prototype.flat) {
Array.prototype.flat = function () {
return this.reduce((acc, val) => acc.concat(val), []);
}
}
s = m.map(
(r,i) => r.map(
(c,j) => c=='X' ? c :
[,...m].splice(i,3).map(r=>[,...r].splice(j,3)).flat().filter(v=>v=='X').length
)
)
s.forEach(r => console.log('| ' + r.join(' | ') + ' |'))
| 0 | 2 | X |
| 1 | 3 | X |
| 2 | X | 2 |
| X | 2 | 1 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.