From 23adfc76d98346aff309b78792f587778957e5e4 Mon Sep 17 00:00:00 2001 From: Omar Ferreiro <27824673+IcarusTheFly@users.noreply.github.com> Date: Fri, 7 Oct 2022 07:15:25 +0200 Subject: [PATCH] algorithm: Minesweeper (#1129) --- Search/Minesweeper.js | 38 +++++++++++++++++ Search/test/Minesweeper.test.js | 75 +++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 Search/Minesweeper.js create mode 100644 Search/test/Minesweeper.test.js diff --git a/Search/Minesweeper.js b/Search/Minesweeper.js new file mode 100644 index 000000000..3de9fc917 --- /dev/null +++ b/Search/Minesweeper.js @@ -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 +} diff --git a/Search/test/Minesweeper.test.js b/Search/test/Minesweeper.test.js new file mode 100644 index 000000000..d5624c7d1 --- /dev/null +++ b/Search/test/Minesweeper.test.js @@ -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) + }) +})