Created
September 2, 2017 19:14
-
-
Save jfrites/620e579d824cec6f4f4145deaf4bdfc0 to your computer and use it in GitHub Desktop.
This file contains 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.generateBombBoard(numberOfRows, numberOfColumns, numberOfBombs); | |
} | |
get playerBoard() { | |
return this._playerBoard | |
} | |
flipTile(rowIndex, colIndex) { | |
if (this._playerBoard[rowIndex][colIndex] !== ' ') { // added space inside of '' | |
console.log('This tile has already been flipped!') | |
return; | |
} else if (this._bombBoard[rowIndex][colIndex] === 'B') { | |
this._playerBoard[rowIndex][colIndex] = 'B'; | |
} else { | |
this._playerBoard[rowIndex][colIndex] = this.getNumberOfNeighborBombs(rowIndex, colIndex); | |
} | |
this._numberOfTiles--; | |
} | |
getNumberOfNeighborBombs(rowIndex, colIndex) { | |
const neighborOffsets = [ | |
[-1, -1], | |
[0, -1], | |
[1, -1], | |
[1, 0], | |
[1, 1], | |
[-1, 0], | |
[-1, 1], | |
[0, 1] | |
]; | |
const numberOfRows = this._bombBoard.length; | |
const numberOfColumns = this._bombBoard[0].length; | |
let numberOfBombs = 0; // added let | |
neighborOffsets.forEach(offset => { | |
const neighborRowIndex = rowIndex + offset[0]; //removed parantheses | |
const neighborColIndex = colIndex + offset[1]; // removed parantheses | |
if (neighborRowIndex >= 0 && neighborRowIndex < numberOfRows && neighborColIndex >= 0 && neighborColIndex < numberOfColumns) { | |
if (this._bombBoard[neighborRowIndex][neighborColIndex] === 'B') { | |
numberOfBombs++; | |
} | |
} | |
}); | |
return numberOfBombs; | |
} | |
hasSafeTiles() { | |
return this._numberOfTiles !== this._numberOfBombs | |
} | |
print() { | |
console.log(this._playerBoard.map(row => row.join(' | ')).join('\n')) | |
} | |
static generatePlayerBoard(numberOfRows, numberOfColumns) { | |
let board = [] | |
for (let rowIndex = 0; rowIndex < numberOfRows; rowIndex++) { | |
let rows = []; | |
for (let colIndex = 0; colIndex < numberOfColumns; colIndex++) { | |
rows.push(' '); | |
} | |
board.push(rows); | |
} | |
return (board); | |
} | |
static generateBombBoard(numberOfRows, numberOfColumns, numberOfBombs) { | |
let board = [] | |
for (let rowIndex = 0; rowIndex < numberOfRows; rowIndex++) { | |
let rows = []; | |
for (let colIndex = 0; colIndex < numberOfColumns; colIndex++) { | |
rows.push(null); | |
} | |
board.push(rows); | |
} | |
let numberOfBombsPlaced = 0; | |
while (numberOfBombsPlaced < numberOfBombs) { | |
let randomRowIndex = (Math.floor(Math.random() * numberOfRows)) | |
let randomColIndex = (Math.floor(Math.random() * numberOfColumns)) | |
if (board[randomRowIndex][randomColIndex] !== 'B') { | |
board[randomRowIndex][randomColIndex] = 'B'; | |
numberOfBombsPlaced++; | |
} | |
//board[randomRowIndex][randomColIndex] = '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) | |
} | |
} |
This file contains 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.js' | |
class Game { | |
constructor (numberOfRows, numberOfColumns, numberOfBombs) { | |
this._board = new Board(numberOfRows, numberOfColumns, numberOfBombs) | |
} | |
playMove (rowIndex, colIndex) { | |
this._board.flipTile(rowIndex, colIndex) | |
if (this._board.playerBoard[rowIndex][colIndex] === 'B') { | |
console.log('Game Over') | |
this._board.print() | |
} else if (this._board.hasSafeTiles()) { | |
console.log('You Win') | |
} 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