-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
79 lines (67 loc) · 1.69 KB
/
bot.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
class MinesweeperBot {
constructor(rows, cols) {
this.rows = rows;
this.cols = cols;
this.board = [];
this.visited = [];
}
initializeBoard() {
for (let i = 0; i < this.rows; i++) {
this.board[i] = [];
this.visited[i] = [];
for (let j = 0; j < this.cols; j++) {
this.board[i][j] = '-';
this.visited[i][j] = false;
}
}
}
placeMines(numMines) {
// Not needed for the modified code
}
printBoard() {
for (let i = 0; i < this.rows; i++) {
let rowString = '';
for (let j = 0; j < this.cols; j++) {
rowString += this.board[i][j] + ' ';
}
console.log(rowString);
}
}
playGame() {
this.initializeBoard();
this.expandAllSafeCells();
console.log('Welcome to Minesweeper!');
this.printBoard();
console.log('Congratulations! You won the game.');
}
expandAllSafeCells() {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
if (this.board[i][j] !== '-') continue;
let numAdjacentMines = this.countAdjacentMines(i, j);
this.board[i][j] = numAdjacentMines.toString();
}
}
}
countAdjacentMines(row, col) {
let count = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
let newRow = row + i;
let newCol = col + j;
if (
newRow >= 0 &&
newRow < this.rows &&
newCol >= 0 &&
newCol < this.cols &&
this.board[newRow][newCol] === 'M'
) {
count++;
}
}
}
return count;
}
}
let minesweeperBot = new MinesweeperBot(8, 8); // Set the desired board size
minesweeperBot.playGame();