Skip to content

Instantly share code, notes, and snippets.

@sujinleeme
Last active June 25, 2019 18:28
Show Gist options
  • Save sujinleeme/a5a0581969e896c378744071ccfaa8a7 to your computer and use it in GitHub Desktop.
Save sujinleeme/a5a0581969e896c378744071ccfaa8a7 to your computer and use it in GitHub Desktop.
class Minesweeper {
constructor(cols, rows, mines) {
this.cols = cols
this.rows = rows
this.mines = mines
}
drawGrid() {
let grid = []
for (let i = 0; i < this.rows; i++) {
grid[i] = []
for (let j = 0; j < this.cols; j++) {
grid[i][j] = 0
}
}
return grid
}
drawMine(grid) {
this.mines.forEach(mine => {
const [x, y] = mine
grid[y][x] = '*'
});
}
calculateProximity(grid) {
this.mines.forEach(([col, row]) => {
const neighbouringCells = this.findNeighbours(col, row)
neighbouringCells.forEach(([col, row]) => {
if (grid[row][col] !== '*') {
grid[row][col] += 1
}
})
})
}
findNeighbours(col, row) {
let cells = [
[col-1, row-1], [col, row-1], [col+1, row-1],
[col-1, row], [col+1, row],
[col-1, row+1], [col, row+1], [col+1, row+1]
]
return cells.filter(([col, row]) => {
return (row > -1 &&
col > -1 &&
row <= (this.rows - 1) &&
col <= (this.cols -1)
)
})
}
changeZeroToDot(gridv) {
grid.forEach((rowCells, rowIndex) => {
rowCells.forEach((cell, colIndex) => {
if (Number.isInteger(cell)) {
if (cell == 0) {
grid[rowIndex][colIndex] = '.'
} else {
grid[rowIndex][colIndex] = `${cell}`
}
}
})
})
}
showGrid() {
let grid = this.drawGrid()
if (this.mines && this.mines.length > 0) {
this.drawMine(grid)
this.calculateProximity(grid)
}
this.changeZeroToDot(grid)
return grid
}
}
module.exports = Minesweeper
const { expect } = require( 'chai' );
const Minesweeper = require( '../src/minesweeper' );
it('returns empty response', function () {
const minesweeper = new Minesweeper(0, 0)
expect( minesweeper.showGrid() ).to.eql( [] );
});
it('Empty mines return just dots', function () {
const minesweeper = new Minesweeper(3, 2)
expect(minesweeper.showGrid()).to.eql([
['.', '.', '.'],
['.', '.', '.']
] );
});
it('Has 1 mine for 3x2 grid', function () {
const mines = [
[1, 0]
]
const minesweeper = new Minesweeper(3, 2, mines)
expect(minesweeper.showGrid()).to.eql([
['1', '*', '1'],
['1', '1', '1']
] );
});
it('Has 1 mine for 5x3 grid', function () {
const mines = [
[0, 0],
[1, 0],
[1, 2]
]
const minesweeper = new Minesweeper(5, 3, mines)
expect(minesweeper.showGrid()).to.eql([
['*', '*', '1', '.', '.'],
['3', '3', '2', '.', '.'],
['1', '*', '1', '.', '.']
] );
});
it('Has 1 mine for 12x6 grid', function () {
const mines = [
[4, 3]
]
const minesweeper = new Minesweeper(12, 6, mines)
expect(minesweeper.showGrid()).to.eql([
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '1', '1', '1', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '1', '*', '1', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '1', '1', '1', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
] );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment