Created
August 24, 2017 17:32
-
-
Save JavierPons/830e1076c03847baf32904ebb615514f to your computer and use it in GitHub Desktop.
Minesweet
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
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('GAME OVER!'); | |
this._board.print(); | |
}else if ( ){ | |
console.log('WINNER!!'); | |
}else{ | |
console.log('Current Board'); | |
this._board.print(); | |
} | |
} | |
} | |
class Board{ | |
constructor(numberOfRows, numberOfColumns, numberOfBombs){ | |
this._numberOfRows = numberOfRows; | |
this._numberOfColumns = numberOfColumns; | |
this._numberOfBombs = numberOfBombs; | |
this._numberOfTiles = (numberOfRows*numberOfBombs); | |
this._playerBoard = Board.generatePlayerBoard(numberOfRows, numberOfColumns); | |
this._bombBoard = Board.generateBombBoard(numberOfRows, numberOfColumns, numberOfBombs); | |
this._numberOfEmptySpaces = numberOfRows * numberOfColumns; | |
} | |
get playerBoard(){ | |
return this._playerBoard; | |
} | |
flipTile(){ | |
this._flipTile = (rowIndex, columnIndex) => { | |
if (playerBoard[rowIndex][columnIndex] !== ' ') { | |
console.log('This tile has already been flipped!'); | |
return; | |
} else if ('B' === bombBoard[rowIndex][columnIndex]) { | |
playerBoard[rowIndex][columnIndex] = 'B'; | |
} else { | |
playerBoard[rowIndex][columnIndex] = this.getNumberOfNeighborBombs(rowIndex, columnIndex); | |
}; | |
}; | |
this._numberOfTiles--; | |
} | |
getNumberOfNeighborBombs(){ | |
this._getNumberOfNeighborBombs = function(rowIndex, columnIndex){ | |
const neighborOffsets = [ | |
[-1, -1], | |
[-1, 0], | |
[-1, 1], | |
[0, -1], | |
[0, 1], | |
[0, -1], | |
[1, 0], | |
[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]; | |
if (neighborRowIndex >= 0 && neighborRowIndex < numberOfRows && neighborColumnIndex >= 0 && neighborColumnIndex < numberOfColumns) { | |
if (bombBoard[neighborRowIndex][neighborColumnIndex] == 'B') { | |
numberOfBombs++; | |
}; | |
}; | |
return numberOfBombs; | |
}); | |
}; | |
} | |
hasSafeTiles(){ | |
return this._numberOfTiles !== this._numberOfBombs; | |
} | |
/* maybe here there is an error */ | |
print(board) { | |
return console.log(board.map(row => row.join(' | ')).join('\n')); | |
} | |
static generatePlayerBoard(numberOfRows, numberOfColumns) { | |
const board = []; | |
for (let rowIndex = 0; rowIndex < numberOfRows; rowIndex++) { | |
let row = []; | |
for (let columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) { | |
row.push(' '); | |
} | |
board.push(row); | |
} | |
return (board); | |
} | |
static generateBombBoard(numberOfRows, numberOfColums, numberOfBombs){ | |
let board = []; | |
for (let rowIndex = 0; rowIndex < numberOfRows; rowIndex++) { | |
let row = []; | |
for (let columnIndex = 0; columnIndex < numberOfColums; columnIndex++) { | |
row.push(null); | |
} | |
board.push(row); | |
} | |
let numberOfBombsPlaced = 0; | |
while (numberOfBombsPlaced < numberOfBombs) { | |
/* 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. */ | |
let randomRowIndex = Math.floor(Math.random() * numberOfRows); | |
let randomColumIndex = Math.floor(Math.random() * numberOfColums); | |
if (board[randomRowIndex][randomColumIndex] !== 'B') { | |
board[randomRowIndex][randomColumIndex] = 'B'; | |
numberOfBombsPlaced++; | |
} | |
board[randomRowIndex][randomColumIndex] = 'B'; | |
numberOfBombsPlaced++; | |
} | |
return board; | |
} | |
} | |
/*let test = generatePlayerBoard(5,4); | |
console.log(test); */ | |
/*const playerBoard = generatePlayerBoard(3, 4); | |
const bombBoard = 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); | |
console.log(g.playMove(1,2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the solution after consulting.
class Game{
constructor(numberOfRows, numberOfColumns, numberOfBombs){
this._board = new Board(numberOfRows, numberOfColumns, numberOfBombs);
}
}
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);
}
}
];
const numberOfRows = this._bombBoard.length;
const numberOfColumns = this._bombBoard[0].length;
let 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;
}
/* maybe here there is an error */
print() {
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, numberOfColums, numberOfBombs){
let board = [];
for (let i = 0; i < numberOfRows; i++) {
let row = [];
for (let j = 0; j < numberOfColums; j++) {
row.push(null);
}
board.push(row);
}
let numberOfBombsPlaced = 0;
while (numberOfBombsPlaced < numberOfBombs) {
/* 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. */
let randomRowIndex = Math.floor(Math.random() * numberOfRows);
let randomColumIndex = Math.floor(Math.random() * numberOfColums);
if (board[randomRowIndex][randomColumIndex] !== 'B') {
board[randomRowIndex][randomColumIndex] = 'B';
numberOfBombsPlaced++;
}
}
return board;
}
}
/*let test = generatePlayerBoard(5,4);
console.log(test); */
/const playerBoard = generatePlayerBoard(3, 4);
const bombBoard = 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(2,1);