Skip to content

Instantly share code, notes, and snippets.

@JavierPons
Created August 24, 2017 17:32
Show Gist options
  • Save JavierPons/830e1076c03847baf32904ebb615514f to your computer and use it in GitHub Desktop.
Save JavierPons/830e1076c03847baf32904ebb615514f to your computer and use it in GitHub Desktop.
Minesweet
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));
@JavierPons
Copy link
Author

I have problems to fix line 12. I should do this: If there is a bomb, the game is over! Inside of the if statement, log a message indicating that the game is over.

After that, print the board by calling .print() on this._board.

That completes the first condition. Chain an if / else statement now. This statement chould check if there are no safe tiles on this._board.
Stuck? Get a hint

Remember, if a board doesn't have any safe tiles left on it, then the user has won.

Inside of the statement, log a message that congratulates the user on winning.

Finally, chain an else to the statements.

Remember, unless a player has flipped a bomb or won the game, the user should be allowed to continue playing.

Inside of the else, log a message that says Current Board:. On the next line, print the board.

@JavierPons
Copy link
Author

Here is the solution after consulting.

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('BOMB! Game over!');
        this._board.print();
    }else if (!this._board.hasSafeTiles() ){
        console.log('You win! Final board:');
        this._board.print();
        
    }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.generateBombBoard(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' ) {
  this._playerBoard[rowIndex][columnIndex] = 'B';
} 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;
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;
}

hasSafeTiles(){        
  return this._numberOfTiles !== this._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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment