-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
343 lines (259 loc) · 10.1 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
1. Score Board on top
2. Game board : size = 9x9
3. how many kind of candies will there be? = 6
4. candies to come in randomly
*****5. make sure we do not have 3 or more candies together on initial load*********
6. add functionality to swap candies : left right top bottom
prevent diagonal swap from happening
take care of edge cases
****show not-allowed cursor when dragging over not allowed boxes
7. after swap: scan the board to check if we have 3 or more candies in row/column
8. If we do have 3 or more candies:
.0 mark them for removal
.1 remove them
.2 add new random candies
.3 increment the score board
9. add animations so its visually appealing to user,
10. add candy images
11. make sure once marked candies are removed, we dont get the same candy again in those places
*/
/*
__0_1_2__
#0 | 1 2 3 |
#1 | 4 5 6 |
#2 | 7 8 9 |
*/
var gameBoard = document.getElementsByClassName('game-board')[0];
var score = document.getElementsByClassName('score')[0];
const candies = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5'];
const NUM_OF_CANDIES = 6;
const GRID_SIZE = 9
var swapBox1, swapBox2;
function updateScoreBoard(incrementScoreBy){
var currentScore = parseInt(score.innerHTML);
score.innerHTML = currentScore + incrementScoreBy;
}
function removeMarkedBoxesAndGenerateNew(markedBoxes){
for(var i=0; i<markedBoxes.length; i++){
var box = markedBoxes[i];
delete box.dataset.toberemoved;
//box.className = 'box '+getRandomCandy();
var candyNotWanted = '';
candies.forEach(function(value){
//console.log('box : ', box.dataset.id, ' class ', box.className, ' value: ', value);
if(box.classList.contains(value)){
//console.log('box: ', box.dataset.id, ' has ', value);
candyNotWanted = value;
box.classList.remove(value);
}
});
//console.log('candyNotWanted for : ', box.dataset.id, ' is ', candyNotWanted);
var newCandy = getRandomCandyExcept(candyNotWanted);
//console.log('new candy for : ', box.dataset.id, ' is ', newCandy);
//console.log('****************************************************************** ');
box.classList.add(newCandy);
}
}
function markForRemovalInRow(rowNum, numOfBoxes, startPositionFromEnd){
var boxesToRemove = document.querySelectorAll('.box[data-row="'+rowNum+'"]');
for(var i=startPositionFromEnd-1; i >= startPositionFromEnd-numOfBoxes; i--){
boxesToRemove[i].dataset.toberemoved = true;
}
}
function markForRemovalInCol(colNum, numOfBoxes, startPositionFromEnd){
var boxesToRemove = document.querySelectorAll('.box[data-col="'+colNum+'"]');
for(var i=startPositionFromEnd-1; i >= startPositionFromEnd-numOfBoxes; i--){
boxesToRemove[i].dataset.toberemoved = true;
}
}
function scanCols(){
for(var colNum=0; colNum<GRID_SIZE; colNum++){
/** CODE IN THIS SCOPE RUNS FOR EACH COLUMN */
var arrOfBoxClassesInaCol = [];
var boxesInACol = document.querySelectorAll('.box[data-col="'+colNum+'"]');
for(var rowNum=0; rowNum<boxesInACol.length; rowNum++){
/** CODE IN THIS SCOPE RUNS FOR EACH BOX IN A COLUMN */
var className = boxesInACol[rowNum].className;
className = className.replace('box', '').trim();
arrOfBoxClassesInaCol.push(className);
}
var countClasses = 1;
for(var i=1; i<arrOfBoxClassesInaCol.length; i++){
if(arrOfBoxClassesInaCol[i] == arrOfBoxClassesInaCol[i-1]){
countClasses++
}else{
if(countClasses >= 3){
console.log(countClasses, ' boxes with class ', arrOfBoxClassesInaCol[i], ' in col:',colNum , ' at: ',i);
markForRemovalInCol(colNum, countClasses, i);
}
countClasses = 1;
}
}
/** SCANNING FINISHED FOR 1 COLUMN */
console.log('AFTER SCAN finished for col: ', colNum);
if(countClasses >= 3){
console.log(countClasses, ' boxes with class ', arrOfBoxClassesInaCol[i], ' in col:',colNum , ' at: ',i);
markForRemovalInCol(colNum, countClasses, i);
}
}
}
function scanRows(){
for(var rowNum=0; rowNum<GRID_SIZE; rowNum++){
/** CODE IN THIS SCOPE RUNS FOR EACH ROW */
var arrOfBoxClassesInaRow = [];
var boxesInARow = document.querySelectorAll('.box[data-row="'+rowNum+'"]');
for(var colNum=0; colNum<boxesInARow.length; colNum++){
/** CODE IN THIS SCOPE RUNS FOR EACH BOX IN A ROW */
var className = boxesInARow[colNum].className;
className = className.replace('box', '').trim();
arrOfBoxClassesInaRow.push(className);
}
var countClasses = 1;
for(var i=1; i<arrOfBoxClassesInaRow.length; i++){
if(arrOfBoxClassesInaRow[i] == arrOfBoxClassesInaRow[i-1]){
countClasses++
}else{
if(countClasses >= 3){
console.log(countClasses, ' boxes with class ', arrOfBoxClassesInaRow[i], ' in row:',rowNum , ' at: ',i);
markForRemovalInRow(rowNum, countClasses, i)
}
countClasses = 1;
}
}
/** SCANNING FINISHED FOR 1 ROW */
console.log('AFTER SCAN finished for row: ', rowNum);
if(countClasses >= 3){
console.log(countClasses, ' boxes with class ', arrOfBoxClassesInaRow[i], ' in row:',rowNum , ' at: ',i);
markForRemovalInRow(rowNum, countClasses, i)
}
}
/*console.log('AFTER ALL ROWS ARE SCANNED');
if(countClasses >= 3){
console.log(countClasses, ' boxes with class ', arrOfBoxClassesInaRow[i], ' in row:',rowNum-1 , ' at: ',i);
markForRemovalInRow(rowNum-1, countClasses, i)
}*/
}
function scanBoard(){
scanRows();
scanCols();
setTimeout(function(){
var markedBoxes = document.querySelectorAll('[data-toberemoved]');
updateScoreBoard(markedBoxes.length);
removeMarkedBoxesAndGenerateNew(markedBoxes);
}, 500)
}
function touchmove_handler(ev) {
ev.preventDefault();
console.log('touchmove ', ev);
//ev.dataTransfer.dropEffect = "move";
}
function touchend_handler(ev) {
ev.preventDefault();
console.log('touchend ', ev);
swapBox2 = ev.target;
var proceed = isSwapAllowed(swapBox1, swapBox2);
if(proceed == true){
swapBoxes(swapBox1, swapBox2);
scanBoard();
}
}
function touchstart_handler(event){
// store a ref. on the dragged elem
console.log('touchstart ', event);
swapBox1 = event.target;
}
function dragover_handler(ev) {
ev.preventDefault();
//ev.dataTransfer.dropEffect = "move";
}
function drop_handler(ev) {
ev.preventDefault();
swapBox2 = ev.target;
var proceed = isSwapAllowed(swapBox1, swapBox2);
if(proceed == true){
swapBoxes(swapBox1, swapBox2);
scanBoard();
}
}
function dragstart_handler(event){
// store a ref. on the dragged elem
swapBox1 = event.target;
}
function swapBoxes(box1, box2){
var classofBox1 = box1.className;
var classofBox2 = box2.className;
box1.className = classofBox2;
box2.className = classofBox1;
}
function isSwapAllowed(box1, box2){
if(box1.className === box2.className){
console.log('same class, cannot swap');
return false;
}
var id1 = parseInt(box1.dataset.id);
var id2 = parseInt(box2.dataset.id);
console.log('isSwapAllowed 1 ', id1, id2, GRID_SIZE%id1, (id1+1)%GRID_SIZE)
/** if the box being dragged is on left edge */
if(id1%GRID_SIZE===0 && id2 == id1-1){
return false;
}
/** if the box being dragged is on right edge */
if((id1+1)%GRID_SIZE===0 && id2 == id1+1){
return false;
}
console.log('isSwapAllowed 2 ', id1, id2, id1+1, id1-1, id1+GRID_SIZE, id1-GRID_SIZE)
/** +1 -1 +GRID_SIZE -GRID_SIZE */
if(id2 == id1+1 || id2 == id1-1 || id2 == id1+GRID_SIZE || id2 == id1-GRID_SIZE){
return true;
}
return false;
}
function createBox(count, c, row, col){
var div = document.createElement('div');
div.classList.add('box');
div.dataset.id = count;
div.dataset.row = row;
div.dataset.col = col;
//div.innerHTML = count
div.classList.add(c);
div.draggable = true;
div.addEventListener('dragover', dragover_handler);
div.addEventListener('drop', drop_handler);
div.addEventListener("dragstart", dragstart_handler);
div.addEventListener('touchmove', touchmove_handler);
div.addEventListener('touchend', touchend_handler);
div.addEventListener("touchstart", touchstart_handler);
return div;
}
function getRandomCandyExcept(candyNotWanted){
var candiesArr = Object.assign([], candies);
var indexToRemove = candiesArr.indexOf(candyNotWanted);
candiesArr.splice(indexToRemove, 1);
var randomIndex = parseInt(Math.random()*candiesArr.length);
return candiesArr[randomIndex];
}
function getRandomCandy(){
var randomIndex = parseInt(Math.random()*candies.length);
return candies[randomIndex];
}
function generateRandomCandies(){
/*for(var i=0; i<GRID_SIZE*GRID_SIZE; i++){
var c = 'c'+parseInt(Math.random()*NUM_OF_CANDIES);
var newBox = createBox(i, c);
gameBoard.append(newBox);
}*/
var count = 0;
for(var rowNum=0; rowNum<GRID_SIZE; rowNum++){
for(var colNum=0; colNum<GRID_SIZE; colNum++){
var c = getRandomCandy();
var newBox = createBox(count, c, rowNum, colNum);
gameBoard.append(newBox);
count++;
}
}
}
function main(){
generateRandomCandies();
}
main();