Created
September 27, 2017 17:25
-
-
Save bcuz/d85f6422e6875f6d06b88b9cec90060f 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
export 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 (this._bombBoard[rowIndex][columnIndex] === 'B'){ | |
console.log('Place your bomb here') | |
} | |
else { | |
this._playerBoard [rowIndex][columnIndex] = this.getNumberOfNeighborBombs(rowIndex, columnIndex); | |
} | |
this._numberOfTiles--; | |
}; | |
getNumberOfNeighborBombs(rowIndex, columnIndex) { | |
const neighborOffsets = [ | |
[-1, -1], | |
[-1, 0], | |
[-1, 1], | |
[0, -1], | |
[0, 1], | |
[1, -1], | |
[1, 0], | |
[1, 1] | |
]; | |
const numberOfRows = this._bombBoard.length; | |
const numberOfColumns = this._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 (this._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; | |
} | |
}; |
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
// To play Minesweeper, we will create instances of MineSweeperGame in command line. | |
// For example: | |
// In the command line, navigate to the lib directory and run `node` | |
// Run `.load game.js` to load the contents of this file. | |
// Then create a Game instance and run commands like so: | |
// let game = new Game(3, 3, 3); | |
// game.playMove(0, 1); | |
// game.playMove(1, 2); | |
// When done run `.exit` | |
import {Board} from './board'; | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment