Skip to content

Instantly share code, notes, and snippets.

@RahmatSaeedi
Created May 10, 2021 14:28
Show Gist options
  • Save RahmatSaeedi/45fef645da145f0d53eff7f817e5e46e to your computer and use it in GitHub Desktop.
Save RahmatSaeedi/45fef645da145f0d53eff7f817e5e46e to your computer and use it in GitHub Desktop.
CodeSignal - Arcade - Intro - JS - Minesweeper
function minesweeper(matrix) {
let height = matrix.length;
let width = matrix[0].length;
let outArray = Array.from(Array(height), () => new Array(width));
let mines = 0;
for(let i = 0; i < height; i++) {
for(let j = 0; j < width; j++) {
mines = 0;
if(i > 0) {
if(matrix[i-1][j-1]) mines += 1;
if(matrix[i-1][j]) mines += 1;
if(matrix[i-1][j+1]) mines += 1;
}
if(i < height - 1) {
if(matrix[i+1][j-1]) mines += 1;
if(matrix[i+1][j]) mines += 1;
if(matrix[i+1][j+1]) mines += 1;
}
if(matrix[i][j-1]) mines += 1;
//if(matrix[i][j]) mines += 1; As it seems you can't count the middle square! welp... I didn't know that!
if(matrix[i][j+1]) mines += 1;
outArray[i][j] = mines;
}
}
return outArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment