Skip to content

Instantly share code, notes, and snippets.

@YavorK
Last active December 23, 2022 18:56
Show Gist options
  • Save YavorK/2510465f18fe70aa6562cc54efc96618 to your computer and use it in GitHub Desktop.
Save YavorK/2510465f18fe70aa6562cc54efc96618 to your computer and use it in GitHub Desktop.
/*
Solution to https://edabit.com/challenge/voZCqTGMSNjCrRhf9
Minesweeper I — Grid
This challenge is based on the game Minesweeper.
Create a function that takes a grid of # and -, where each hash (#) represents a mine and each dash (-) represents a mine-free spot. Return an array where each dash is replaced by a digit indicating the number of mines immediately adjacent to the spot (horizontally, vertically, and diagonally).
Examples
numGrid([
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"]
]) ➞ [
["0", "0", "0", "0", "0"],
["0", "1", "1", "1", "0"],
["0", "1", "#", "1", "0"],
["0", "1", "1", "1", "0"],
["0", "0", "0", "0", "0"],
]
numGrid([
["-", "-", "-", "-", "#"],
["-", "-", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "-", "-", "-", "-"],
["#", "-", "-", "-", "-"]
]) ➞ [
["0", "0", "0", "1", "#"],
["0", "1", "1", "2", "1"],
["0", "1", "#", "1", "0"],
["1", "2", "1", "1", "0"],
["#", "1", "0", "0", "0"]
]
numGrid([
["-", "-", "-", "#", "#"],
["-", "#", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "#", "#", "-", "-"],
["-", "-", "-", "-", "-"]
]) ➞ [
["1", "1", "2", "#", "#"],
["1", "#", "3", "3", "2"],
["2", "4", "#", "2", "0"],
["1", "#", "#", "2", "0"],
["1", "2", "2", "1", "0"],
]
*/
const mineBoards = [
[
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
],
[
["-", "-", "-", "-", "#"],
["-", "-", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "-", "-", "-", "-"],
["#", "-", "-", "-", "-"],
],
[
["-", "-", "-", "#", "#"],
["-", "#", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "#", "#", "-", "-"],
["-", "-", "-", "-", "-"],
],
];
function newBoards() {
const newBoards = [];
for (let boardIndex = 0; boardIndex < mineBoards.length; boardIndex++) {
const board = mineBoards[boardIndex]
const newBoard = [];
for (let lineIndex = 0; lineIndex < board.length; lineIndex++) {
let line = board[lineIndex];
const newLine = [];
for (let cellIndex = 0; cellIndex < line.length; cellIndex++) {
let cell = line[cellIndex]
if (cell === '-') {
cell = 0 //start counting the '#'
// right/left
const elementOnTheRightPosition = cellIndex + 1;
const elementOnTheLeftPosition = cellIndex - 1;
//top/down
const lineAbove = board[lineIndex - 1]
const lineBelow = board[lineIndex + 1]
// has mine to the right
if(line[elementOnTheRightPosition] === '#'){
cell++
}
// has mine to the down-right
if(lineBelow?.[elementOnTheRightPosition] === '#'){
cell++
}
// has mine to the down
if (lineBelow?.[cellIndex] === '#') {
cell++
}
// has mine to the down left
if(lineBelow?.[elementOnTheLeftPosition] === '#'){
cell++
}
// has mine to the left
if(line[elementOnTheLeftPosition] === '#'){
cell++
}
// has mine top left
if(lineAbove?.[elementOnTheLeftPosition] === '#'){
cell++
}
// has mine top
if(lineAbove?.[cellIndex] === '#'){
cell++
}
// has mine top right
if(lineAbove?.[elementOnTheRightPosition] === '#'){
cell++
}
}
newLine.push(cell +'')
// ^ put a string so '#' and '0' (not 0) are evenly formatted in columns
// [ '1', '1', '2', '#', '#' ],
// [ '1', '#', '3', '3', '2' ],
// vs
// [ 1, 1, 2, '#', '#' ],
// [ 1, '#', 3, 3, 2 ],
}
newBoard.push(newLine);
}
newBoards.push(newBoard);
}
console.log(newBoards)
}
newBoards()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment