@seangarvey/minesweeperFourthBuild
JavaScript

Game plays but cannot be played from the terminal and requires guesses using the index/indexes of your move instead of the actual row/column

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Create the Game Class.
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 (!this._board.hasSafeTiles()) {
			console.log('Congratulations! You Won! GAME OVER!');
		}
		else {
			console.log('Current Board: ');
			this._board.print();
		}
	}
}

// Create the Board Class.
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;
	}
	
	get bombBoard() {
		return this._bombBoard;
	}
	
	// Allow User To Flip Tile.
    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--;
	}
	
	// Find Number of Neighboring Bombs.
    getNumberOfNeighborBombs(rowIndex, columnIndex) {
		const neighborOffsets = [];
		for (let neighborOffsetsIndex = 0; neighborOffsetsIndex < 9; neighborOffsetsIndex++) {
			neighborOffsets.push([]);
		}
		neighborOffsets[0] = [-1,-1];
		neighborOffsets[1] = [-1,0];
		neighborOffsets[2] = [-1,1];
		neighborOffsets[3] = [0,-1];
		neighborOffsets[4] = [0,0];
		neighborOffsets[5] = [0,1];
		neighborOffsets[6] = [1,-1];
		neighborOffsets[7] = [1,0];
		neighborOffsets[8] = [1,1];
		const numberOfRows = this._bombBoard.length;
		const numberOfColumns = this._bombBoard[0].length;
		let numberOfNeighborBombs = 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') {
					numberOfNeighborBombs++;
				}
			}
		});
		return numberOfNeighborBombs;
	}
	
	// Check for Safe Tiles.
	hasSafeTiles() {
		return (this._numberOfTiles !== this._numberOfBombs)
	}
	
	// Print the Game Board.
    print() {
		console.log(this._playerBoard.map(row => row.join(' | ')).join('\n'));
    }
	
	// Dynamically Generate a Player Board As a Static Method On the Board Class.
	static generatePlayerBoard(numberOfRows, numberOfColumns) {
		const board = [];
		for (let numberOfRowsIndex = 0; numberOfRowsIndex < numberOfRows; numberOfRowsIndex++) {
			const row = [];
			for (let numberOfColumnsIndex = 0; numberOfColumnsIndex < numberOfColumns; numberOfColumnsIndex++) {
				row.push(' ');
			} 
			board.push(row);
		}
		return board;
	}
	
	// Dynamically Generate a Bomb Board As a Static Method On the Board Class.
	static generateBombBoard(numberOfRows, numberOfColumns, numberOfBombs) {
		const board = [];
		for (let numberOfRowsIndex = 0; numberOfRowsIndex < numberOfRows; numberOfRowsIndex++) {
			const row = [];
			for (let numberOfColumnsIndex = 0; numberOfColumnsIndex < numberOfColumns; numberOfColumnsIndex++) {
				row.push(' ');
			} 
			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++;
			}
		}
		return board;
	}
}

// Instantiate the Game Class
const g = new Game(3, 3, 3);

// Play a Move
g.playMove(0,0);
Native Browser JavaScript