Created
May 10, 2021 14:28
-
-
Save RahmatSaeedi/45fef645da145f0d53eff7f817e5e46e to your computer and use it in GitHub Desktop.
CodeSignal - Arcade - Intro - JS - Minesweeper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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