-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
298 lines (274 loc) Β· 8.2 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
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
295
296
297
298
var _ = require("lodash");
let __puzzle = document.querySelector(".puzzle");
const possLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let __resultState = [];
let __stateFlatten = [];
let __rowSize = 0;
let __columnSize = 0;
// create puzzle array
const randomPuzzle = () => {
__resultState = [];
__stateFlatten = [];
for (i = 0; i < __rowSize; i++) {
__resultState[i] = [];
for (j = 0; j < __columnSize; j++) {
__resultState[i][j] = possLetter.charAt(Math.floor(Math.random() * 36));
}
}
isRandomG();
};
//algorithm π¨βπ»
findPointer = (r, c) => {
let result = {
left: {},
right: {},
top: {},
bottom: {}
};
// π£
r + 1 <= __rowSize - 1
? (result.bottom = r + 1 + "-" + c)
: (result.bottom = null);
r - 1 >= 0 ? (result.top = r - 1 + "-" + c) : (result.top = null);
// π
c + 1 <= __columnSize - 1
? (result.right = r + "-" + (c + 1))
: (result.right = null);
c - 1 >= 0 ? (result.left = r + "-" + (c - 1)) : (result.left = null);
return result;
};
// bg color π
const randomBgColor = () => {
var x = Math.floor(Math.random() * 256);
var y = Math.floor(Math.random() * 256);
var z = Math.floor(Math.random() * 256);
var bgColor = "rgb(" + x + "," + y + "," + z + ")";
return bgColor;
};
// create dom element box
const createDom = () => {
let __count = 1;
let shuffleResult = _.shuffle(__stateFlatten);
__puzzle.style.width = 120 * __columnSize + "px";
__puzzle.style.height = 100 * __rowSize + "px";
_.forEach(__resultState, (arrRow, rowKey) => {
_.forEach(arrRow, (arrCol, colKey) => {
let state = shuffleResult[__count - 1];
let element = document.createElement("div");
element.classList.add("plz");
element.style.backgroundColor = randomBgColor();
element.style.top = 100 * rowKey + "px";
element.style.left = 120 * colKey + "px";
element.setAttribute("rowCol", rowKey + "-" + colKey);
element.setAttribute("initial", state);
element.innerHTML = _.indexOf(__stateFlatten, state) + 1 + " π";
__puzzle.appendChild(element);
__count++;
});
});
__plz = document.querySelectorAll(".plz");
let whereIsPointer = document.querySelector(
`[initial="${_.last(__stateFlatten)}"]`
);
whereIsPointer.classList.add("pointer");
whereIsPointer.style.backgroundColor = "transparent";
whereIsPointer.innerHTML = "";
startPuzzling();
};
const startPuzzling = () => {
__pointer = document.querySelector(".pointer");
let attr = __pointer.getAttribute("rowCol");
let row = Number(attr.split("-")[0]);
let col = Number(attr.split("-")[1]);
setDirection(row, col);
startMoveEvent();
};
const startMoveEvent = () => {
__plz.forEach(_which =>
_which.addEventListener("click", e => {
let direction = e.target.getAttribute("moveDirection");
let attr = e.target.getAttribute("rowCol");
let row = Number(attr.split("-")[0]);
let col = Number(attr.split("-")[1]);
if (!direction) return null;
moveTo(direction, e.target);
setNewIndex(direction, e.target);
removePrevDirection();
setDirection(row, col);
isGameOver();
})
);
};
// TODO
// π if margin or padding then is function calculate not working
// π need boundary calculate properly
// π
const moveTo = (where, who) => {
let el = who;
switch (where) {
case "left":
el.style.left =
el.getBoundingClientRect().left +
el.getBoundingClientRect().width -
__puzzle.getBoundingClientRect().left +
"px";
__pointer.style.left =
__pointer.getBoundingClientRect().left -
__pointer.getBoundingClientRect().width -
__puzzle.getBoundingClientRect().left +
"px";
break;
case "right":
el.style.left =
el.getBoundingClientRect().left -
el.getBoundingClientRect().width -
__puzzle.getBoundingClientRect().left +
"px";
__pointer.style.left =
__pointer.getBoundingClientRect().left +
__pointer.getBoundingClientRect().width -
__puzzle.getBoundingClientRect().left +
"px";
break;
case "top":
el.style.top =
el.getBoundingClientRect().top +
el.getBoundingClientRect().height -
__puzzle.getBoundingClientRect().top +
"px";
__pointer.style.top =
__pointer.getBoundingClientRect().top -
__pointer.getBoundingClientRect().height -
__puzzle.getBoundingClientRect().top +
"px";
break;
case "bottom":
el.style.top =
el.getBoundingClientRect().top -
el.getBoundingClientRect().height -
__puzzle.getBoundingClientRect().top +
"px";
__pointer.style.top =
__pointer.getBoundingClientRect().top +
__pointer.getBoundingClientRect().height -
__puzzle.getBoundingClientRect().top +
"px";
break;
}
};
const setNewIndex = (where, who) => {
let el = who;
let attr = el.getAttribute("rowCol");
var moveRow = Number(attr.split("-")[0]);
var moveCol = Number(attr.split("-")[1]);
let PointerAttr = __pointer.getAttribute("rowCol");
var pointerRow = Number(PointerAttr.split("-")[0]);
var pointerCol = Number(PointerAttr.split("-")[1]);
switch (where) {
case "left":
el.setAttribute("rowCol", moveRow + "-" + (moveCol + 1));
__pointer.setAttribute("rowCol", pointerRow + "-" + (pointerCol - 1));
break;
case "right":
el.setAttribute("rowCol", moveRow + "-" + (moveCol - 1));
__pointer.setAttribute("rowCol", pointerRow + "-" + (pointerCol + 1));
break;
case "top":
el.setAttribute("rowCol", moveRow + 1 + "-" + moveCol);
__pointer.setAttribute("rowCol", pointerRow - 1 + "-" + pointerCol);
break;
case "bottom":
el.setAttribute("rowCol", moveRow - 1 + "-" + moveCol);
__pointer.setAttribute("rowCol", pointerRow + 1 + "-" + pointerCol);
break;
}
};
const removePrevDirection = () => {
__plz.forEach(_which => _which.removeAttribute("moveDirection"));
};
const setDirection = (row, col) => {
let dObj = findPointer(row, col);
// console.log(dObj);
if (dObj.left) {
setDirectionAttr(dObj.left, "left");
}
if (dObj.right) {
setDirectionAttr(dObj.right, "right");
}
if (dObj.top) {
setDirectionAttr(dObj.top, "top");
}
if (dObj.bottom) {
setDirectionAttr(dObj.bottom, "bottom");
}
};
const setDirectionAttr = (attr, moveMark) => {
let el = document.querySelector(`[rowCol="${attr}"]`);
el.setAttribute("moveDirection", moveMark);
};
// check is random array has duplicate key
const isRandomG = () => {
if (_.isEqual(_.flatten(__resultState), _.uniq(_.flatten(__resultState)))) {
__stateFlatten = _.flattenDeep(__resultState);
console.log("π₯ ", __resultState);
console.log("π΅οΈββοΈ ", __stateFlatten);
createDom();
} else {
randomPuzzle();
}
};
// π£
const isGameOver = () => {
let whatIsGameStatus = [];
for (i = 0; i < __rowSize; i++) {
__resultState[i] = [];
for (j = 0; j < __columnSize; j++) {
let el = document.querySelector(`[rowCol="${i}-${j}"]`);
let pointer = el.getAttribute("initial");
whatIsGameStatus.push(pointer);
}
}
if (_.isEqual(whatIsGameStatus, __stateFlatten)) {
setTimeout(() => {
let isPlayAgain = confirm("You Made it !! π . want to play again?");
if (isPlayAgain) {
__puzzle.innerHTML = "";
randomPuzzle();
} else {
return null;
}
}, 300);
}
};
// π²
const __puzzleGame = (row, col) => {
__rowSize = Number(row);
__columnSize = Number(col);
__puzzle.innerHTML = "";
randomPuzzle();
};
const onLoadGameStart = () => {
__puzzleGame(3, 3);
};
onLoadGameStart();
let btnALl = document.querySelectorAll(".control button");
btnALl.forEach(btn => {
btn.addEventListener("click", e => {
btnALl.forEach(btn => btn.classList.remove("active"));
e.target.classList.add("active");
switch (e.target.innerText.toLowerCase()) {
case "easy":
__puzzleGame(3, 3);
break;
case "medium":
__puzzleGame(3, 4);
break;
case "hard":
__puzzleGame(4, 4);
break;
case "harder":
__puzzleGame(4, 5);
break;
}
});
});