-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
225 lines (163 loc) · 7.33 KB
/
app.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
$(function() {
const GRID_SIZES = [8, 16, 22];
const NUM_MINES_LVLS = [10, 40, 99];
const BG_COLORS = ['#aad751', '#9cc448'];
const BG_COLORS_OPEN = ['#e5c29f', '#d7b899'];
const TEXT_COLORS = ['blue', 'green', 'red', 'purple', 'darkorange', 'magenta', 'brown', 'black'];
const TEXT_COLORS_LIGHT = ['#6666ff', '#66ff66', '#ff6666', '#bc66ff', '#ffd199', '#ffb3ff', '#e69e9e', '#666666'];
var grid_size = GRID_SIZES[0];
var num_mines = NUM_MINES_LVLS[0];
var cells = [];
var game_over = false;
// Define class cell and its methods
class Cell{
constructor(x, y, is_mine=false, hidden=true, marked=false){
this.is_mine = is_mine;
this.hidden = hidden;
this.marked = marked;
this.x = x;
this.y = y;
cells.push(this);
}
create_button(){
var btn = $('<button class="game-cell" oncontextmenu="return false;">');
$(".game-grid").css("grid-template-columns", `repeat(${grid_size}, 1fr)`)
$(".game-grid").css("grid-template-rows", `repeat(${grid_size}, 1fr)`)
$(".game-grid").append(btn);
//Bind the click event
// We use an arrow function to avoid 'this' being lost
$(btn).mousedown( (e) => {
if(e.which == 1){
this.left_click_action();
}else if(e.which == 3){
this.right_click_action();
}
});
// Make the button a property of the object
this.button_object = btn;
// Produce checkerboard pattern
this.button_object.css({"background": BG_COLORS[(this.x+this.y)%2]})
}
// On left click, game is over the cell is a mine
// Else display the number of neighbouring mines
left_click_action(){
if(!game_over){
if(!this.marked){
if(this.is_mine){
Cell.show_mine();
}else{
this.show_neighbour_mine_count();
Cell.update_game_text();
}
}
// check if player won
if(cells.filter(cell => cell.hidden).length == num_mines){
$(".game-title").text("You won!");
game_over = true;
}
}
}
// Right click is for marking suspected mines
right_click_action(){
if(!game_over && this.hidden){
// marked cells become unmarked and vice versa
this.marked = !this.marked;
// change colour
this.marked ? $(this.button_object).css("background", "red") : $(this.button_object).css("background", BG_COLORS[(this.x+this.y)%2]);
Cell.update_game_text();
}
}
show_neighbour_mine_count(){
if(this.hidden){
let neighbour_mine_count = this.calculate_neighbour_mine_count();
if(neighbour_mine_count != 0){
$(this.button_object).text(neighbour_mine_count);
}
$(this.button_object).css("background", BG_COLORS_OPEN[(this.x+this.y)%2]);
$(this.button_object).css("color", TEXT_COLORS[neighbour_mine_count-1]);
this.hidden = false;
// if neighbour_mine_count is 0, we use recursion to open up all surrounding cells
if(neighbour_mine_count == 0){
for(let i=-1; i<=1;i++){
for(let j=-1; j<=1;j++){
this.get_cell_from_coords(this.x+i, this.y+j)?.show_neighbour_mine_count()
}
}
}
}
}
calculate_neighbour_mine_count(){
let neighbours = [];
for(let i=-1; i<=1;i++){
for(let j=-1; j<=1;j++){
neighbours.push(this.get_cell_from_coords(this.x+i, this.y+j));
}
}
neighbours = neighbours.filter(obj => obj != undefined)
return neighbours.filter(cell => cell.is_mine == true).length;
}
// Return the cell object from given x and y
get_cell_from_coords(x, y){
return cells.filter(cell => cell.x == x && cell.y == y)[0]
}
// show all mines and display that the player has lost
static show_mine(){
game_over = true;
$(".game-title").text("You lost!");
// Show all mines, mark x on mines that were wrongly identified
let unmarked_mines = cells.filter(cell => cell.is_mine && !cell.marked);
let wrong_marks = cells.filter(cell => !cell.is_mine && cell.marked);
for(let i=0; i<unmarked_mines.length; i++){
unmarked_mines[i].button_object.css({"color": TEXT_COLORS[i%TEXT_COLORS.length],
"background": TEXT_COLORS_LIGHT[i%TEXT_COLORS.length],
"font-size": ["30px", "25px", "20px"][GRID_SIZES.indexOf(grid_size)]
});
unmarked_mines[i].button_object.text("•");
}
for(let i=0; i<wrong_marks.length; i++){
wrong_marks[i].button_object.css({"background": BG_COLORS[(wrong_marks[i].x+wrong_marks[i].y)%2],
"color": "red",
"font-size": ["30px", "25px", "20px"][GRID_SIZES.indexOf(grid_size)]
});
wrong_marks[i].button_object.text("×");
}
}
static update_game_text(){
$(".game-text").html($('<text style="font-size:30px">'+'■ '+'</text>\n'));
$(".game-text").append(num_mines-cells.filter(cell => cell.marked).length);
}
static create_mines(){
let mines = cells.sort(() => 0.5 - Math.random()).slice(0,num_mines);
for(let i=0; i<mines.length; i++){
mines[i].is_mine = true;
}
}
}
// define cells
function define_cells(size){
for(let i=0; i<size; i++){
for(let j=0; j<size; j++){
let c = new Cell(i, j);
c.create_button();
}
}
}
// Reset button
$(".reset-btn").mousedown( () =>{
grid_size = GRID_SIZES[$(".difficulty-level").val()];
num_mines = NUM_MINES_LVLS[$(".difficulty-level").val()];
$(".game-title").text("Minesweeper");
game_over = false;
for(i=0; i<cells.length;i++){
cells[i].button_object.remove();
}
cells = [];
define_cells(grid_size);
Cell.update_game_text();
Cell.create_mines();
});
define_cells(GRID_SIZES[0]);
// Create game text
Cell.update_game_text();
Cell.create_mines();
});