-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
132 lines (111 loc) · 3.71 KB
/
game.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
class Game {
constructor(cols, rows, bombs) {
this.cols = cols
this.rows = rows
this.bombs = bombs
this.grid = []
this.initGrid()
}
initGrid() {
// Create grid
for (let i = 0; i < this.cols; i++) {
let column = []
for (let j = 0; j < rows; j++) {
column.push(new Cell(i, j))
}
this.grid.push(column)
}
// Set bombs
let bombsSet = new Set()
for (let b = 0; b < this.bombs; b++) {
let index_x = floor(random(this.cols))
let index_y = floor(random(this.rows))
while (bombsSet.has(`${index_x}-${index_y}`)) {
index_x = floor(random(this.cols))
index_y = floor(random(this.rows))
}
this.grid[index_x][index_y].bomb = true
bombsSet.add(`${index_x}-${index_y}`)
}
// Set neighbors count
for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
const cell = this.grid[x][y]
if (cell.bomb === true) {
cell.neighbor = -1
}
else {
let total = 0
for (let xoffset = -1; xoffset <= 1; xoffset++) {
let i = cell.i + xoffset
if (i < 0 || i >= this.cols) continue
for (let yoffset = -1; yoffset <= 1; yoffset++) {
let j = cell.j + yoffset
if (j < 0 || j >= this.rows) continue
let neighbor = this.grid[i][j]
if (neighbor.bomb === true) {
total++
}
}
}
cell.neighbor = total
}
}
}
}
show() {
// Display game
this.grid.forEach(col => {
col.forEach(cell => {
// Display each cell
cell.show()
})
})
}
mouseHandler(mouseX, mouseY) {
console.log("click");
for (let i = 0; i < this.cols; i++) {
for (let j = 0; j < this.rows; j++) {
const cell = this.grid[i][j]
if (cell.contains(mouseX, mouseY) === true) {
if(mouseButton === RIGHT) {
cell.mark()
}
else if(mouseButton === LEFT) {
if (cell.bomb === true) {
this.gameOver();
}
else {
this.reveal(cell)
}
}
}
}
}
}
reveal(cell) {
if (cell.state === States.REVEALED) {
return
}
cell.reveal()
if (cell.neighbor === 0) {
// If the neigborhood doesn't contains bomb then we floodfill the surrounding area
for (let xoffset = -1; xoffset <= 1; xoffset++) {
let i = cell.i + xoffset
if (i < 0 || i >= this.cols) continue
for (let yoffset = -1; yoffset <= 1; yoffset++) {
let j = cell.j + yoffset
if (j < 0 || j >= this.rows) continue
this.reveal(this.grid[i][j])
}
}
}
}
gameOver() {
for (var i = 0; i < this.cols; i++) {
for (var j = 0; j < this.rows; j++) {
this.grid[i][j].reveal();
}
}
}
}