-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.js
294 lines (226 loc) · 8.41 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
class Game {
constructor(username, difficulty) {
this._makeAvailableValues = (from, to) => {
const valuesTab = [];
const toCodeValue = to.charCodeAt();
let currentCodeValue = from.charCodeAt();
for (currentCodeValue; currentCodeValue <= toCodeValue; currentCodeValue++) {
valuesTab.push(currentCodeValue);
}
return [...String.fromCharCode(...valuesTab)];
};
const _availableValues = this._makeAvailableValues("A", "Z");
this.getAvailableValues = () => _availableValues;
this._stats = new Stats(username, difficulty);
this._numberOfSquares = this._generateNumberOfSquares(difficulty);
this._numberOfMatchedSqueres = 0;
this._squares = [];
this._gameState = "inactive";
}
get stats() {
return this._stats;
}
set stats(stats) {
return (this._stats = stats);
}
get numberOfSquares() {
return this._numberOfSquares;
}
set numberOfSquares(numberOfSquares) {
return (this._numberOfSquares = numberOfSquares);
}
get numberOfMatchedSqueres() {
return this._numberOfMatchedSqueres;
}
get squares() {
return this._squares;
}
get gameState() {
return this._gameState;
}
set gameState(state) {
if (
typeof state !== "string" ||
(state !== "ongoing" &&
state !== "stopped" &&
state !== "init" &&
state !== "inactive" &&
state !== "failed" &&
state !== "win")
)
throw new Error("incorect value of parameter");
return (this._gameState = state);
}
_generateNumberOfSquares(difficulty) {
if (difficulty === "easy") return (this.numberOfSquares = 24);
if (difficulty === "normal") return (this.numberOfSquares = 36);
if (difficulty === "hard") return (this.numberOfSquares = 48);
this.stats.difficulty = "normal";
return (this.numberOfSquares = 36);
}
_getRandomValue(values) {
if (!values.length || !(this.getAvailableValues() instanceof Array))
throw new Error("wrong type of available values");
return values[Math.floor(Math.random() * values.length)];
}
_removeAscribedValue(values, index) {
values.splice(index, 1);
}
_makeAvailablePositions() {
const availablePositions = [];
let currentIndex = 0;
while (currentIndex < this.numberOfSquares) {
availablePositions.push(currentIndex);
currentIndex++;
}
if (currentIndex !== availablePositions.length ||
availablePositions.length !== this.numberOfSquares)
throw new Error("Incorrect initialization of Available Positions");
return availablePositions;
}
_getRandom(available) {
if (!available.length || !(available instanceof Array))
throw new Error("wrong type of available");
return available[Math.floor(Math.random() * available.length)];
}
_removeAscribed(tab, index) {
tab.splice(index, 1);
}
_sortSquaresByOrder(tab) {
tab.sort((a, b) => {
if (!(a instanceof Square && b instanceof Square))
throw new Error("Element of Array has not require type");
return a.order - b.order;
});
}
changeDifficulty(difficulty) {
this._numberOfSquares = this._generateNumberOfSquares(difficulty);
this.stats.difficulty = difficulty;
}
getSquare(index) {
return this.squares[index];
}
setSquare(index, newSquare) {
if (!(newSquare instanceof Square))
throw new Error("new square must be instance of Square class!");
return (this.squares[index] = newSquare);
}
addSquare(newSquare) {
if (!(newSquare instanceof Square))
throw new Error("new square must be instance of Square class!");
this.squares.push(newSquare);
return newSquare;
}
removeSquare(index) {
this.squares.splice(index, 1);
}
makeMatch() {
this._numberOfMatchedSqueres += 2;
if (this.checkIfWin()) this.gameState = "win";
}
resetMatches() {
return (this._numberOfMatchedSqueres = 0);
}
initRandomizedSquares() {
if (!this.getAvailableValues() || !(this.getAvailableValues() instanceof Array))
throw new Error("wrong type of available values");
if (this.squares.length) this.squares.length = 0;
const values = [...this.getAvailableValues()];
const availablePositions = this._makeAvailablePositions();
while (this.squares.length < this.numberOfSquares) {
const randomValue = this._getRandom(values);
let randomPosition = this._getRandom(availablePositions);
const sqr = this.addSquare(new Square(randomValue, randomPosition));
this._removeAscribed(
availablePositions,
availablePositions.indexOf(randomPosition)
);
randomPosition = this._getRandom(availablePositions);
this.addSquare(new Square(randomValue, randomPosition, sqr));
this._removeAscribed(values, values.indexOf(randomValue));
this._removeAscribed(
availablePositions,
availablePositions.indexOf(randomPosition)
);
}
this._sortSquaresByOrder(this.squares);
return this.squares;
}
_mainGameAction() {
const prev = {
active: null,
activeSquare: null,
state: false
};
return e => {
if (!(this.getSquare(e.target.dataset.order).isMatched || this.gameState === 'failed')) {
this.stats.updateActionsCounter();
if (prev.active === null || prev.state === false) {
prev.active = e.target;
prev.activeSquare = this.getSquare(prev.active.dataset.order);
prev.activeSquare.changeActivity();
prev.active.textContent = prev.activeSquare.value;
prev.active.style.backgroundColor = prev.activeSquare.color;
prev.state = true;
return;
}
const active = e.target;
const activeSquare = this.getSquare(active.dataset.order);
active.textContent = activeSquare.value;
if (activeSquare.makeMatching(prev.activeSquare)) {
this.makeMatch()
active.style.backgroundColor = activeSquare.getMatchedColor();
prev.active.style.backgroundColor = prev.activeSquare.getMatchedColor();
} else {
this.gameState = 'failed';
active.style.backgroundColor = activeSquare.getFailedColor();
prev.active.style.backgroundColor = prev.activeSquare.getFailedColor();
}
if (this.gameState === 'failed') {
setTimeout(() => {
active.style.backgroundColor = activeSquare.getPassiveColor();
prev.active.style.backgroundColor = activeSquare.getPassiveColor();
prev.active.textContent = "";
active.textContent = "";
this.gameState = 'ongoing';
}, 500)
}
if (this.gameState === 'ongoing') {
prev.active = null;
prev.activeSquare = null;
}
prev.state = false;
}
};
}
_gameEventHandler = this._mainGameAction();
initGame(grid, timer, counter = null) {
if (this.gameState === "ongoing" || this.gameState === "stopped")
this.resetGame(grid, timer, counter);
this.gameState = "init";
grid.root.className = `game-grid ${this.stats.difficulty.toString()}`;
grid.render(this.initRandomizedSquares());
}
startGame(grid, timer) {
if (!(this.gameState === "init" || this.gameState === "stopped")) return;
this.gameState = "ongoing";
grid.root.addEventListener("click", this._gameEventHandler);
this.stats.startTimer(timer);
}
resetGame(grid, timer, counter) {
this.gameState = "inactive";
grid.root.removeEventListener("click", this._gameEventHandler);
this.stats.resetTimer(timer);
this.stats.resetActionsCounter(counter);
this.resetMatches();
}
stopGame(grid) {
this.gameState = "stopped";
grid.root.removeEventListener("click", this._gameEventHandler);
this.stats.stopTimer();
}
checkIfWin() {
// return this.numberOfMatchedSqueres === 2 ? true : false;
return this.numberOfMatchedSqueres === this.numberOfSquares ? true : false;
}
}