Skip to content

Instantly share code, notes, and snippets.

@austriker27
Created January 22, 2018 21:36
Show Gist options
  • Save austriker27/ee534fcce698178e362c199a65f8a811 to your computer and use it in GitHub Desktop.
Save austriker27/ee534fcce698178e362c199a65f8a811 to your computer and use it in GitHub Desktop.
2D array practice with minesweeper
let exampleWeird2DArray = [
[0, 0],
[99, 42, 92, 53],
[],
[2, 6, 87, 43, 82],
[2],
]
let MINEFIELD = [
[null, null, null, null, 'bomb'],
[null, null, null, null, 'bomb'],
['bomb', null, null, null, null],
[null, null, null, 'bomb', null],
[null, 'bomb', null, null, null]
]
let row = 0
let col = 4
MINEFIELD[row][col]
let x = 1
let y = 4
MINEFIELD[y][x]
// bounds-checking helper function
// returns an "empty" value if the coordinate doesn't exist.
function getCell(grid, row, col) {
let empty = null;
if (row < 0 || col < 0) {
return empty;
} else if (row >= grid.length) {
return empty;
} else if (col >= grid[row].length) {
return empty;
} else {
return grid[row][col]
}
}
let irow = 1
let icol = 1
// accessing the array directory may lead to index
// access errors on sides and corners.
MINEFIELD[irow - 1][icol] // north
MINEFIELD[irow + 1][icol] // south
MINEFIELD[irow][icol - 1] // west
MINEFIELD[irow][icol + 1] // east
MINEFIELD[irow - 1][icol - 1] // north west
MINEFIELD[irow - 1][icol + 1] // north east
MINEFIELD[irow + 1][icol - 1] // south west
MINEFIELD[irow + 1][icol + 1] // south east
// using the getCell helper function
// prevents index-access errors.
getCell(MINEFIELD, irow - 1, icol) // north
getCell(MINEFIELD, irow + 1, icol) // south
getCell(MINEFIELD, irow, icol - 1) // west
getCell(MINEFIELD, irow, icol + 1) // east
getCell(MINEFIELD, irow - 1, icol - 1) // north west
getCell(MINEFIELD, irow - 1, icol + 1) // north east
getCell(MINEFIELD, irow + 1, icol - 1) // south west
getCell(MINEFIELD, irow + 1, icol + 1) // south east
// this function accepts a 2D array representing
// a minesweeper minefield. It returns a new 2D
// array where each cell has a number representing
// how many bombs that cell touches,
// let MINEFIELD = [
// [null, null, null, null, 'bomb']
// [null, null, null, null, 'bomb']
// ['bomb', null, null, null, null]
// [null, null, null, 'bomb', null]
// [null, 'bomb', null, null, null]
// ]
// [
// [0, 0, 0, 2, *]
// [1, 1, 0, 2, *]
// [*, 1, 1, 2, 2]
// [2, 2, 2, *, 1]
// [1, *, 2, 1, 1]
// ]
function markMines(field) {
for(let row = 0; row < field.length, row++) {
for(let col = 0; col < field[row].length; col++) {
if (getCell[i] === 'bomb')
field = '*';
// loop over all the neighbor spots and count bombs
else if(
let counter = 0;
getCell(MINEFIELD, irow - 1, icol) // north
counter ++;
getCell(MINEFIELD, irow + 1, icol) // south
counter ++;
getCell(MINEFIELD, irow, icol - 1) // west
counter ++;
getCell(MINEFIELD, irow, icol + 1) // east
counter ++
getCell(MINEFIELD, irow - 1, icol - 1) // north west
counter ++
getCell(MINEFIELD, irow - 1, icol + 1) // north east
counter ++;
getCell(MINEFIELD, irow + 1, icol - 1) // south west
counter ++;
getCell(MINEFIELD, irow + 1, icol + 1) // south east
counter ++;
)
field = counter;
}
}
return field
}
let result = markMines(MINEFIELD)
assert(result[0][0], 0)
assert(result[1][0], 1)
assert(result[1][1], 1)
assert(result[3][0], 2)
assert(result[4][4], 1)
function assert(actual, expected) {
if (expected !== actual) {
console.log("expected:", expected, "actual:", actual)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment