Skip to content

algorithm: Minesweeper #1129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Search/Minesweeper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Author: IcarusTheFly (https://github.com/IcarusTheFly)
* Minesweeper explanation can be found in: https://en.wikipedia.org/wiki/Minesweeper_(video_game)
* This function will take a rectangular matrix filled with boolean values - the value for a cell
* with a mine will be true, otherwise it will be false.
* As a result it will return a rectangular matrix where each cell will have an integer that
* counts all the mines in the adjacent cells
* Two cells should share at least one corner to be considered adjacent
*/

/**
* @function minesweeper
* @description It counts the amount of mines surrounding every cell and returns a formatted matrix
* @param {boolean[][]} matrix
* @returns {number[][]} Matrix of numbers with the amount of mines surrounding each cell
*/

export const minesweeper = (matrix) => {
const arrResult = []
for (let x = 0; x < matrix.length; x++) {
const arrLine = []
for (let y = 0; y < matrix[x].length; y++) {
let minesInCell = 0
for (let xi = x - 1; xi <= x + 1; xi++) {
if (matrix[xi] !== undefined) {
for (let yi = y - 1; yi <= y + 1; yi++) {
if ((xi !== x || yi !== y) && matrix[xi][yi] === true) {
minesInCell++
}
}
}
}
arrLine.push(minesInCell)
}
arrResult.push(arrLine)
}
return arrResult
}
75 changes: 75 additions & 0 deletions Search/test/Minesweeper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { minesweeper } from '../Minesweeper'

describe('Testing minesweeper function', () => {
it('should return the expected 3x3 array', () => {
const input = [
[true, false, false],
[false, true, false],
[false, false, false]
]
const expectedOutput = [
[1, 2, 1],
[2, 1, 1],
[1, 1, 1]
]
expect(minesweeper(input)).toStrictEqual(expectedOutput)
})

it('should return the expected 3x4 array', () => {
const input = [
[true, false, false, true],
[false, false, true, false],
[true, true, false, true]
]
const expectedOutput = [
[0, 2, 2, 1],
[3, 4, 3, 3],
[1, 2, 3, 1]
]
expect(minesweeper(input)).toStrictEqual(expectedOutput)
})

it('should return the expected 5x2 array', () => {
const input = [
[true, false],
[true, false],
[false, true],
[false, false],
[false, false]
]
const expectedOutput = [
[1, 2],
[2, 3],
[2, 1],
[1, 1],
[0, 0]
]
expect(minesweeper(input)).toStrictEqual(expectedOutput)
})

it('should return the correct result when there are no mines', () => {
const input = [
[false, false, false],
[false, false, false]
]
const expectedOutput = [
[0, 0, 0],
[0, 0, 0]
]
expect(minesweeper(input)).toStrictEqual(expectedOutput)
})

it('should return the correct result when there are mines in every cell', () => {
const input = [
[true, true, true],
[true, true, true],
[true, true, true]
]
const expectedOutput = [
[3, 5, 3],
[5, 8, 5],
[3, 5, 3]
]
expect(minesweeper(input)).toStrictEqual(expectedOutput)
})
})