mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
algorithm: Minesweeper (#1129)
This commit is contained in:
38
Search/Minesweeper.js
Normal file
38
Search/Minesweeper.js
Normal file
@ -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
Search/test/Minesweeper.test.js
Normal file
75
Search/test/Minesweeper.test.js
Normal file
@ -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)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user