Skip to content

Instantly share code, notes, and snippets.

@LetsGetItDone
Created July 6, 2018 03:36
Show Gist options
  • Save LetsGetItDone/aae948aa5afff3a8625328cda5510c30 to your computer and use it in GitHub Desktop.
Save LetsGetItDone/aae948aa5afff3a8625328cda5510c30 to your computer and use it in GitHub Desktop.
board.map not a function line 77
const generatePlayerBoard = (numberOfRows, numberOfColumns) => {
const board = [];
for (let i = 0; i < numberOfRows; i++) {
const row = [];
for (let j = 0; j < numberOfColumns; j++) {
row.push(' ');
}
board.push(row);
}
return board;
};
const generateBombBoard = (numberOfRows, numberOfColumns, numberOfBombs) => {
const board = [];
for (let i = 0; i < numberOfRows; i++) {
const row = [];
for (let j = 0; j < numberOfColumns; j++) {
row.push(null);
}
board.push(row);
}
let numberOfBombsPlaced = 0;
while (numberOfBombsPlaced < numberOfBombs) {
const randomRowIndex = Math.floor(Math.random() * numberOfRows);
const randomColumnIndex = Math.floor(Math.random() * numberOfColumns);
if (board[randomRowIndex][randomColumnIndex] !== 'B') {
board[randomRowIndex][randomColumnIndex] = 'B';
numberOfBombsPlaced++;
}
}
return board;
};
const getNumberOfNeighborBombs = (bombBoard, rowIndex, columnIndex) => {
const neighborOffsets = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1],
[1, -1],
[1, 0],
[1, 1]
];
const numberOfRows = bombBoard.length;
const numberOfColumns = bombBoard[0].length;
let numberOfBombs = 0;
neighborOffsets.forEach(offset => {
const neighborRowIndex = rowIndex + offset[0];
const neighborColumnIndex = columnIndex + offset[1];
if (neighborRowIndex >= 0 && neighborRowIndex < numberOfRows && neighborColumnIndex >= 0 && neighborColumnIndex < numberOfColumns) {
if (bombBoard[neighborRowIndex][neighborColumnIndex] === 'B') {
numberOfBombs++;
}
}
});
return numberOfBombs;
};
const flipTile = (playerBoard, bombBoard, rowIndex, columnIndex) => {
if (playerBoard[rowIndex][columnIndex] !== ' ') {
console.log('This tile has already been flipped!');
return;
} else if (bombBoard[rowIndex][columnIndex] === 'B') {
playerBoard[rowIndex][columnIndex] = 'B';
} else {
playerBoard[rowIndex][columnIndex] = getNumberOfNeighborBombs(bombBoard, rowIndex, columnIndex);
}
};
const printBoard = board => {
console.log(board.map(row => row.join(' | ')).join('\n'));
};
let playerBoard = generatePlayerBoard(3, 3);
let bombBoard = generateBombBoard(3, 3, 3);
console.log('Player Board: ');
printBoard(playerBoard);
console.log('Bomb Board: ');
printBoard(bombBoard);
console.log('Number of bombs touch tile');
printBoard(getNumberOfNeighborBombs(bombBoard, 0, 0))
// console.log('Player Board: ')
// printBoard(playerBoard);
//
// console.log('Bomb Board: ');
// printBoard(bombBoard);
//
// flipTile(playerBoard, bombBoard, 0, 0);
// console.log('Updated Player Board: ');
// printBoard(playerBoard)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment