Created
October 1, 2017 03:58
-
-
Save khayyamsaleem/f8bb95a13f1baf95a5d911a68511ecf9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const generatePlayerBoard = (numberOfRows, numberOfColumns) => { | |
let board = []; | |
for (let i = 0; i < numberOfRows; i++){ | |
let row = []; | |
for (let j = 0; j < numberOfColumns; j++){ | |
row.push(' '); | |
} board.push(row); | |
} return board }; | |
// console.log(generatePlayerBoard(3,2)); | |
const generateBombBoard = (numberOfRows, numberOfColumns, numberOfBombs) => { | |
let board = []; | |
for (let i = 0; i < numberOfRows; i++){ | |
let row = []; | |
for (let 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); | |
if (board[randomRowIndex][randomColumnIndex] !== 'B'){ | |
board[randomRowIndex][randomColumnIndex] = 'B'; | |
numberOfBombsPlaced++ | |
}; | |
//An important note: 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 getNumberOfNeighborBombs = (bombBoard, rowIndex, columnIndex) => { | |
const neighborOffsets = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,0],[1,-1],[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]; | |
console.log("NRI: " + neighborRowIndex, "NCI: " + neighborColumnIndex); | |
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 => { | |
board.map(row => row.join(' | ')).join('\n'); | |
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: '); | |
console.log(bombBoard); | |
printBoard(bombBoard); | |
flipTile(playerBoard, bombBoard, 2, 2); | |
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