-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeesweeper.js
More file actions
177 lines (153 loc) · 3.54 KB
/
beesweeper.js
File metadata and controls
177 lines (153 loc) · 3.54 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Icons made by Freepik from www.flaticon.com
const COLS = 12;
const ROWS = 12;
let grid;
let bee;
function preload() {
bee = loadImage('bee24x24.png');
}
function setup() {
createCanvas(460, 460);
grid = new Grid(COLS, ROWS);
}
function draw() {
background(220);
grid.render();
}
function gameOver() {
grid.cells.forEach(cell => {
cell.revealed = true;
cell.getBeeCount();
});
noLoop();
}
function mousePressed() {
// Using traditional `for` loop for performance as return
// statement ends function execution, whereas a `forEach`
// loop calls a function for every element.
for (let i = 0; i < grid.cells.length; i++) {
if (grid.cells[i].contains(mouseX, mouseY)) {
return grid.cells[i].reveal();
}
}
}
class Grid {
constructor(cols, rows) {
this.cells = this.createGrid(cols, rows);
}
createGrid(cols, rows) {
const size = width / cols;
const grid = [];
for (let j = 0; j < rows; j++) {
for (let i = 0; i < cols; i++) {
grid.push(new Cell(i, j, size));
}
}
return grid;
}
getCell(x, y) {
if (x < 0 || x >= COLS || y < 0 || y > ROWS) {
return false;
}
return this.cells[x + y * COLS];
}
render() {
this.cells.forEach(cell => {
fill(255);
cell.render();
});
}
}
class Cell {
constructor(x, y, s) {
this.x = x * s;
this.y = y * s;
this.s = s;
this.i = x;
this.j = y;
this.bee = random(1) > 0.8;
this.revealed = false;
this.neighbours = undefined;
this.textColor = color(0, 0, 0);
}
getNeighbours() {
return [
grid.getCell(this.i - 1, this.j - 1), // top left
grid.getCell(this.i, this.j - 1), // top mid
grid.getCell(this.i + 1, this.j - 1), // top right
grid.getCell(this.i - 1, this.j), // left
grid.getCell(this.i + 1, this.j), // right
grid.getCell(this.i - 1, this.j + 1), // bot left
grid.getCell(this.i, this.j + 1), // bot mid
grid.getCell(this.i + 1, this.j + 1) // bot right
].filter(Boolean);
}
countBees() {
const neighbours = this.getNeighbours();
return neighbours.reduce((bees, cell) => {
return cell.bee ? bees + 1 : bees;
}, 0);
}
getBeeCount() {
this.neighbours = this.countBees();
switch (this.neighbours) {
case 0:
this.revealNeighbours();
break;
case 1:
this.textColor = color(50, 50, 225);
break;
case 2:
this.textColor = color(60, 165, 60);
break;
case 3:
this.textColor = color(225, 50, 0);
break;
case 4:
this.textColor = color(125, 50, 125);
}
}
contains(x, y) {
// Checks if the cell contains a point, used to know if it has been clicked
return (
x > this.x && x < this.x + this.s &&
y > this.y && y < this.y + this.s
);
}
reveal() {
if (this.bee) {
gameOver();
} else {
this.revealed = true;
this.getBeeCount();
}
}
revealNeighbours() {
const neighbours = this.getNeighbours();
neighbours.forEach(n => {
if (!n.bee && !n.revealed) {
n.reveal();
}
});
}
render() {
if (!this.revealed) {
fill(255);
rect(this.x, this.y, this.s);
return;
}
fill(200);
rect(this.x, this.y, this.s);
if (this.bee) {
image(bee, this.x + 6, this.y + 6);
} else {
if (this.neighbours > 0) {
push();
stroke(this.textColor);
fill(this.textColor)
text(this.neighbours, this.x + 16, this.y + 22);
pop();
}
}
}
}