Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LetsGetItDone/254fbdd9b2a0926c1bc8f2a31a38a0e4 to your computer and use it in GitHub Desktop.
Save LetsGetItDone/254fbdd9b2a0926c1bc8f2a31a38a0e4 to your computer and use it in GitHub Desktop.
board is not adding " | " <pipes> to array
const generatePlayerBoard = (numberOfRows, numberOfColumns) => {
let board = [];
for (var i = 0; i < numberOfRows; i++) {
let row = [];
for (var j = 0; j < numberOfColumns; j++) {
row.push(' ');
} board.push(row);
}
return board;
};
const generateBombBoard = (numberOfRows, numberOfColumns, numberOfBombs) => {
let board = [];
for (var i = 0; i < numberOfRows; i++) {
let row = [];
for (var j = 0; j < numberOfColumns; j++) {
row.push(null);
} board.push(row);
}
let numberOfBombsPlaced = 0;
while (numberOfBombsPlaced < numberOfBombs) {
let randomRowIndex = Math.floor(Math.random() * numberOfRows);
let randomColumnIndex = Math.floor(Math.random() * numberOfColumns);
board[randomRowIndex, randomColumnIndex] = 'B'
numberOfBombsPlaced++;
// The code in your while loop has the potential to place bombs on top of already existing bombs. This will be fixed when you learn about control flow.
}
return board;
};
const printBoard = board => {
console.log(board.map(row => row.join(' | ')).join('\n'));
}
console.log(generateBombBoard(3,3,2)); //BDD a bomb board is create with "null" values;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment