Created
March 6, 2017 02:55
-
-
Save Underdoge/b2e56b4aa6132814f1ab99772e75ef6e to your computer and use it in GitHub Desktop.
minesweeper
This file contains hidden or 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) { | |
var sum=0; | |
var mines=[],line=[]; | |
for (var y=0;y<matrix.length;y++){ | |
line=[]; | |
for (var x=0;x<matrix[0].length;x++){ | |
sum=0; | |
if(y-1>=0){ | |
if(matrix[y-1][x]) | |
sum++; | |
} | |
if(x-1>=0&&y-1>=0){ | |
if(matrix[y-1][x-1]) | |
sum++; | |
} | |
if(x-1>=0){ | |
if(matrix[y][x-1]) | |
sum++; | |
} | |
if(x+1<matrix[0].length&&y-1>=0){ | |
if(matrix[y-1][x+1]) | |
sum++; | |
} | |
if(y+1<matrix.length&&x-1>=0){ | |
if(matrix[y+1][x-1]) | |
sum++; | |
} | |
if(x+1<matrix[0].length){ | |
if(matrix[y][x+1]) | |
sum++; | |
} | |
if(y+1<matrix.length){ | |
if(matrix[y+1][x]) | |
sum++ | |
} | |
if(x+1<matrix[0].length&&y+1<matrix.length){ | |
if(matrix[y+1][x+1]) | |
sum++; | |
} | |
line.push(sum); | |
} | |
mines.push(line); | |
} | |
return mines; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment