Skip to content

Commit 4c002c7

Browse files
committed
Minesweeper
1 parent a7d2bb1 commit 4c002c7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

CodeSignal/js/thecore/Minesweeper.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const directions = [
2+
[-1, -1], [-1, 0], [-1, 1],
3+
[0, -1], [0, 1],
4+
[1, -1], [1, 0], [1, 1]
5+
];
6+
7+
function solution(matrix) {
8+
9+
return matrix.map((row, y) => row.map((col, x) => directions.reduce((count, i) => {
10+
return count += !!(matrix[y + i[0]] && matrix[y + i[0]][x + i[1]])
11+
}, 0)));
12+
13+
}

CodeSignal/js/thecore/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# Minesweeper
2+
___
3+
In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.
4+
5+
[My solution - Click me and check the whole solution + comments ](https://github.com/PiotrSierant/HTML-CSS-JS/blob/main/CodeSignal/js/thecore/Minesweeper.js)
6+
```javascript
7+
const directions = [
8+
[-1,-1], [-1, 0], [-1, 1],
9+
[ 0,-1], [ 0, 1],
10+
[ 1,-1], [ 1, 0], [ 1, 1]
11+
];
12+
13+
function solution(matrix) {
14+
15+
return matrix.map((row, y) => row.map((col, x) => directions.reduce((count, i) => {
16+
return count += !!(matrix[y + i[0]] && matrix[y + i[0]][x + i[1]])
17+
}, 0)));
18+
19+
}
20+
```
21+
122
# Sudoku
223
___
324
Sudoku is a number-placement puzzle. The objective is to fill a` 9 × 9 `grid with digits so that each column, each row, and each of the nine` 3 × 3` sub-grids that compose the grid contains all of the digits from `1` to `9`.

0 commit comments

Comments
 (0)