Skip to content

Commit 23adfc7

Browse files
authored
algorithm: Minesweeper (#1129)
1 parent c365e82 commit 23adfc7

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

Search/Minesweeper.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Author: IcarusTheFly (https://github.com/IcarusTheFly)
3+
* Minesweeper explanation can be found in: https://en.wikipedia.org/wiki/Minesweeper_(video_game)
4+
* This function will take a rectangular matrix filled with boolean values - the value for a cell
5+
* with a mine will be true, otherwise it will be false.
6+
* As a result it will return a rectangular matrix where each cell will have an integer that
7+
* counts all the mines in the adjacent cells
8+
* Two cells should share at least one corner to be considered adjacent
9+
*/
10+
11+
/**
12+
* @function minesweeper
13+
* @description It counts the amount of mines surrounding every cell and returns a formatted matrix
14+
* @param {boolean[][]} matrix
15+
* @returns {number[][]} Matrix of numbers with the amount of mines surrounding each cell
16+
*/
17+
18+
export const minesweeper = (matrix) => {
19+
const arrResult = []
20+
for (let x = 0; x < matrix.length; x++) {
21+
const arrLine = []
22+
for (let y = 0; y < matrix[x].length; y++) {
23+
let minesInCell = 0
24+
for (let xi = x - 1; xi <= x + 1; xi++) {
25+
if (matrix[xi] !== undefined) {
26+
for (let yi = y - 1; yi <= y + 1; yi++) {
27+
if ((xi !== x || yi !== y) && matrix[xi][yi] === true) {
28+
minesInCell++
29+
}
30+
}
31+
}
32+
}
33+
arrLine.push(minesInCell)
34+
}
35+
arrResult.push(arrLine)
36+
}
37+
return arrResult
38+
}

Search/test/Minesweeper.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { minesweeper } from '../Minesweeper'
2+
3+
describe('Testing minesweeper function', () => {
4+
it('should return the expected 3x3 array', () => {
5+
const input = [
6+
[true, false, false],
7+
[false, true, false],
8+
[false, false, false]
9+
]
10+
const expectedOutput = [
11+
[1, 2, 1],
12+
[2, 1, 1],
13+
[1, 1, 1]
14+
]
15+
expect(minesweeper(input)).toStrictEqual(expectedOutput)
16+
})
17+
18+
it('should return the expected 3x4 array', () => {
19+
const input = [
20+
[true, false, false, true],
21+
[false, false, true, false],
22+
[true, true, false, true]
23+
]
24+
const expectedOutput = [
25+
[0, 2, 2, 1],
26+
[3, 4, 3, 3],
27+
[1, 2, 3, 1]
28+
]
29+
expect(minesweeper(input)).toStrictEqual(expectedOutput)
30+
})
31+
32+
it('should return the expected 5x2 array', () => {
33+
const input = [
34+
[true, false],
35+
[true, false],
36+
[false, true],
37+
[false, false],
38+
[false, false]
39+
]
40+
const expectedOutput = [
41+
[1, 2],
42+
[2, 3],
43+
[2, 1],
44+
[1, 1],
45+
[0, 0]
46+
]
47+
expect(minesweeper(input)).toStrictEqual(expectedOutput)
48+
})
49+
50+
it('should return the correct result when there are no mines', () => {
51+
const input = [
52+
[false, false, false],
53+
[false, false, false]
54+
]
55+
const expectedOutput = [
56+
[0, 0, 0],
57+
[0, 0, 0]
58+
]
59+
expect(minesweeper(input)).toStrictEqual(expectedOutput)
60+
})
61+
62+
it('should return the correct result when there are mines in every cell', () => {
63+
const input = [
64+
[true, true, true],
65+
[true, true, true],
66+
[true, true, true]
67+
]
68+
const expectedOutput = [
69+
[3, 5, 3],
70+
[5, 8, 5],
71+
[3, 5, 3]
72+
]
73+
expect(minesweeper(input)).toStrictEqual(expectedOutput)
74+
})
75+
})

0 commit comments

Comments
 (0)