-
Notifications
You must be signed in to change notification settings - Fork 0
/
mines.js
179 lines (157 loc) · 3.8 KB
/
mines.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
/**
* @author Vojtech Sebo
* @class Mines
*/
class Mines {
/**
* Creates an instance of Mines.
* @param {element} target
* @param {number} [cols=15]
* @param {number} [rows=15]
* @param {number} [mines=35]
* @memberof Mines
*/
constructor(target, cols = 15, rows = 15, mines = 35) {
this.target = target;
this._cols = cols;
this._rows = rows;
this._mines = mines;
this.start();
}
/**
* Start new game
* @memberof Mines
*/
start = () => {
this.target.innerHTML = '';
this._createGame();
};
/**
* Make mines
* @memberof Mines
*/
_makeMines = () => {
let minesPos = [];
while (minesPos.length < this._mines) {
let x = Math.floor(Math.random() * this._rows);
let y = Math.floor(Math.random() * this._cols);
if (!minesPos.filter(a => a.x === x && a.y === y).length > 0) {
minesPos.push({ x, y });
}
}
return minesPos;
};
/**
* Create game
* @memberof Mines
*/
_createGame = () => {
this._fields = [];
let mines = this._makeMines();
for (let r = 0; r < this._rows; r++) {
let row = [];
for (let c = 0; c < this._cols; c++) {
let isMine = false;
let hint = false;
if (mines.filter(mine => mine.x === r && mine.y === c).length > 0) {
isMine = true;
} else {
hint = this._countHints(mines, r, c);
}
row.push(new Field(this, r, c, isMine, hint));
}
this._fields.push(row);
}
this.target.style.gridTemplate = `repeat(${this._rows}, 1fr) / repeat(${this._cols}, 1fr)`;
};
/**
* Calculate hint number
* @memberof Mines
* @param {array} mines
* @param {number} x
* @param {number} y
*/
_countHints = (mines, x, y) => {
return mines.filter(
mine =>
(mine.x === x - 1 && mine.y === y) ||
(mine.x === x + 1 && mine.y === y) ||
(mine.x === x && mine.y === y - 1) ||
(mine.x === x && mine.y === y + 1) ||
(mine.x === x + 1 && mine.y === y + 1) ||
(mine.x === x - 1 && mine.y === y - 1) ||
(mine.x === x + 1 && mine.y === y - 1) ||
(mine.x === x - 1 && mine.y === y + 1)
).length;
};
/**
* Show surround fields
* @memberof Mines
* @param {number} x
* @param {number} y
*/
checkSurround = (x, y) => {
let fields = [
{ x: x - 1, y },
{ x, y: y + 1 },
{ x: x + 1, y },
{ x, y: y - 1 },
{ x: x + 1, y: y - 1 },
{ x: x + 1, y: y + 1 },
{ x: x - 1, y: y - 1 },
{ x: x - 1, y: y + 1 }
];
fields = fields.filter(field => {
if (field.x >= 0 && field.y >= 0 && field.x < this._rows && field.y < this._cols) {
if (!this._fields[field.x][field.y].isShow && !this._fields[field.x][field.y].isMine) {
this._fields[field.x][field.y].show();
if (this._fields[field.x][field.y].hint === 0) {
this.checkSurround(field.x, field.y);
}
}
}
});
};
/**
* Check if the game is over
* @memberof Mines
*/
checkStatus = () => {
if (
this._fields.filter(row => row.filter(field => !field.isMine && !field.isShow).length)
.length === 0
) {
this._win();
}
};
/**
* Show all mines
* @memberof Mines
*/
_showMines = () => {
this._fields.forEach(row =>
row.forEach(field => {
if (field.isMine) field.show();
})
);
};
/**
* User win the game
* @memberof Mines
*/
_win = () => {
setTimeout(() => {
if (confirm('Vyhrál jsi? Chceš hrát znovu?')) this.start();
}, 100);
};
/**
* User lost the game
* @memberof Mines
*/
loss = () => {
setTimeout(() => {
if (confirm('Prohral jsi? Chceš hrát znovu?')) this.start();
}, 100);
this._showMines();
};
}