Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Game {
- constructor(numberOfRows, numberOfColumns, numberOfBombs) {
- this._board = new Board(numberOfRows,numberOfColumns,numberOfBombs);
- }
- playMove(rowIndex, columnIndex) {
- this._board.flipTile(rowIndex, columnIndex);
- if (this._board.playerBoard[rowIndex]
- [columnIndex] === 'B'){
- console.log('The game is over!');
- this._board.print();
- }
- else if (!this._board.hasSafeTiles()) {
- console.log('congratulates the user on winning');
- } else {
- console.log('Current Board:');
- this._board.print();
- }
- }
- }
- class Board {
- constructor(numberOfRows, numberOfColumns, numberOfBombs) {
- this._numberOfBombs = numberOfBombs;
- this._numberOfTiles = (numberOfRows * numberOfColumns);
- this._playerBoard = Board.generatePlayerBoard(numberOfRows, numberOfColumns);
- this._bombBoard = Board.generatePlayerBoard(numberOfRows, numberOfColumns, numberOfBombs);
- }
- get playerBoard () {
- return this._playerBoard;
- }
- flipTile(rowIndex, columnIndex) {
- if (this._playerBoard[rowIndex][columnIndex] !== '') {
- console.log('This tile has already been flipped!');
- return;
- }
- else if (bombBoard[rowIndex][columnIndex] === 'B'){
- console.log('Place your bomb here')
- }
- else {
- playerBoard [rowIndex][columnIndex] = this.getNumberOfNeighborBombs(rowIndex, columnIndex);
- }
- this._numberOfTiles--;
- };
- this_getNumberOfNeighborBombs(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;
- const 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;
- };
- hasSafeTiles(){
- return this._numberOfTiles !== this._numberOfBombs;
- }
- print(board){
- console.log(this._playerBoard.map(row => row.join(' | ')).join('\n'));
- }
- static 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;
- };
- static 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++;
- };
- /*The code in your while loop has the potential to place
- bombs on top of already existing bombs.*/
- };
- return board;
- }
- };
- //console.log(playerBoard);
- /* let playerBoard = Board.generatePlayerBoard(3, 3);
- let bombBoard = Board.generateBombBoard(3, 4, 5);
- console.log('Player Board: ');
- printBoard(playerBoard);
- console.log('Bomb Board: ');
- printBoard(bombBoard);
- flipTile(playerBoard, bombBoard, 0, 0);
- console.log('Updated Player Board:');
- printBoard(playerBoard); */
- const g = new Game(3, 3, 3);
- g.playMove(1,0);
- //printBoard(board);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement