/
index.js
374 lines (315 loc) · 11.5 KB
/
index.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
function drawField() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
for (var i = 0; i < 5; i++) {
var positionX = 100 * i;
ctx.moveTo(positionX, 0);
ctx.lineTo(positionX, 400);
ctx.strokeStyle = "#c7eb7a";
ctx.lineWidth = "8";
ctx.stroke();
}
for (var j = 0; j < 5; j++) {
var positionY = 100 * j;
ctx.moveTo(0, positionY);
ctx.lineTo(400, positionY);
ctx.strokeStyle = "#c7eb7a";
ctx.lineWidth = "8";
ctx.stroke();
}
}
function pickRandomItem() {
var randomItem = Math.floor(Math.random() * arrayOfEmptyBlocks.length);
return randomItem;
}
// pickRandomItem() goes through the arrayOfEmptyBlocks and picks randomly an item. The return value is stored in the variable randomItem
function callRun2() {
var randomProperty = pickRandomItem();
drawBlock(arrayOfEmptyBlocks[randomProperty]);
deleteNumFromArray(randomProperty);
}
function deleteNumFromArray(randomProperty) {
var blockRemovedArray = arrayOfEmptyBlocks.splice(randomProperty, 1);
if (blockRemovedArray !== null && blockRemovedArray.length != 0) {
arrayOfFullBlocks.push(blockRemovedArray[0]);
}
if (arrayOfEmptyBlocks.length === 0) {
document.getElementById("myBtn").disabled = true;
}
}
function drawBlock(currentBlock) {
var ctx = document.getElementById("myCanvas").getContext("2d");
var img = document.getElementById("img" + currentBlock.img);
ctx.drawImage(img, currentBlock.xPos, currentBlock.yPos, currentBlock.xFill, currentBlock.yFill);
}
function block(name, img, xPos, yPos, xFill, yFill) {
this.name = name;
this.img = img;
this.xPos = xPos;
this.yPos = yPos;
this.xFill = xFill;
this.yFill = yFill;
this.upDated = false;
this.blockLeft = null;
this.blockRight = null;
this.blockUp = null;
this.blockDown = null;
this.setBlockNeighbors = function(left, right, up, down) {
this.blockLeft = left;
this.blockRight = right;
this.blockUp = up;
this.blockDown = down;
};
}
var arrayOfEmptyBlocks = [];
var arrayOfFullBlocks = [];
var currentlyMoving = false;
//console.log("*********",arrayOfFullBlocks);
function createBlocks() {
var block1 = new block("block1", 1, 4, 4, 92, 92);
var block2 = new block("block2", 1, 104, 4, 92, 92);
var block3 = new block("block3", 1, 204, 4, 92, 92);
var block4 = new block("block4", 1, 304, 4, 92, 92);
var block5 = new block("block5", 1, 4, 104, 92, 92);
var block6 = new block("block6", 1, 104, 104, 92, 92);
var block7 = new block("block7", 1, 204, 104, 92, 92);
var block8 = new block("block8", 1, 304, 104, 92, 92);
var block9 = new block("block9", 1, 4, 204, 92, 92);
var block10 = new block("block10", 1, 104, 204, 92, 92);
var block11 = new block("block11", 1, 204, 204, 92, 92);
var block12 = new block("block12", 1, 304, 204, 92, 92);
var block13 = new block("block13", 1, 4, 304, 92, 92);
var block14 = new block("block14", 1, 104, 304, 92, 92);
var block15 = new block("block15", 1, 204, 304, 92, 92);
var block16 = new block("block16", 1, 304, 304, 92, 92);
block1.setBlockNeighbors(null, block2, null, block5);
block2.setBlockNeighbors(block1, block3, null, block6);
block3.setBlockNeighbors(block2, block4, null, block7);
block4.setBlockNeighbors(block3, null, null, block8);
block5.setBlockNeighbors(null, block6, block1, block9);
block6.setBlockNeighbors(block5, block7, block2, block10);
block7.setBlockNeighbors(block6, block8, block3, block11);
block8.setBlockNeighbors(block7, null, block4, block12);
block9.setBlockNeighbors(null, block10, block5, block13);
block10.setBlockNeighbors(block9, block11, block6, block14);
block11.setBlockNeighbors(block10, block12, block7, block15);
block12.setBlockNeighbors(block11, null, block8, block16);
block13.setBlockNeighbors(null, block14, block9, null);
block14.setBlockNeighbors(block13, block15, block10, null);
block15.setBlockNeighbors(block14, block16, block11, null);
block16.setBlockNeighbors(block15, null, block12, null);
arrayOfEmptyBlocks.push(block1, block2, block3, block4, block5, block6,
block7, block8, block9, block10, block11, block12, block13,
block14, block15, block16);
}
createBlocks();
// following is a test functions for the above data
function checkNeighbors() {
var test = "";
for (var i = 0; i < arrayOfEmptyBlocks.length; i++) {
currBlock = arrayOfEmptyBlocks[i];
left = "";
right = "";
up = "";
down = "";
if (currBlock.blockLeft !== null) {
left = currBlock.blockLeft.name;
}
if (currBlock.blockRight !== null) {
right = currBlock.blockRight.name;
}
if (currBlock.blockUp !== null) {
up = currBlock.blockUp.name;
}
if (currBlock.blockDown !== null) {
down = currBlock.blockDown.name;
}
test += currBlock.name + ": (left: " + left + ")" +
", (right: " + right + ")" +
", (up: " + up + ")" +
", (down: " + down + ")" +
"\n";
}
document.getElementById("neighborCheck").innerText = test;
}
// document.onkeydown = function(e) {
document.body.addEventListener('keydown', function(e) {
if (!currentlyMoving) {
e = e || window.event;
switch (e.which || e.keyCode) {
case 37: // left
move("left");
break;
case 38: // up
move("up");
break;
case 39: // right
move("right");
break;
case 40: // down
move("down");
break;
default:
return; // exit this handler for other keys
}
}
})
function move(direction) {
var movement = false;
currentlyMoving = true;
var arrayToMove = [];
for (var i = 0; i < arrayOfFullBlocks.length; i++) {
currentBlock = arrayOfFullBlocks[i];
var currentBlockMove;
switch (direction) {
case "left":
currentBlockMove = currentBlock.blockLeft;
break;
case "right":
currentBlockMove = currentBlock.blockRight;
break;
case "up":
currentBlockMove = currentBlock.blockUp;
break;
case "down":
currentBlockMove = currentBlock.blockDown;
break;
}
// if the block cannot move right, either because there is a NULL or because the space is already full, meaning the block is in the arrayOfFullBlocks
if (currentBlockMove === null || (arrayOfFullBlocks.indexOf(currentBlockMove) !== -1 && currentBlockMove.img !== currentBlock.img)) {
//if it is null, or it is in the arrayOfFullBlocks and the number of currentBlockMove is not the same images
//i.e. no movement should happen
continue;
} else if (arrayOfFullBlocks.indexOf(currentBlockMove) !== -1 && currentBlockMove.img == currentBlock.img) {
if (currentBlockMove.upDated || currentBlock.upDated) {
continue;
//no movement happens
}
currentBlockMove.upDated = true;
currentBlockMove.img = currentBlockMove.img + 1;
var justOneBlockArray = arrayOfFullBlocks.splice(i, 1);
// we splice the item of the arrayOfFullBlocks, but push it to a variable, since otherwise it is stored as an array within the arrayOfEmptyBlocks.
if (justOneBlockArray !== null && justOneBlockArray.length != 0) {
arrayOfEmptyBlocks.push(justOneBlockArray[0]);
}
movement = true;
arrayToMove.push(currentBlock);
break;
}
currentBlockMove.img = currentBlock.img;
arrayOfFullBlocks[i] = currentBlockMove;
arrayOfEmptyBlocks[arrayOfEmptyBlocks.indexOf(currentBlockMove)] = currentBlock;
// we swapped two positions. We put currentBlockRight (it was in the "empty" array) into the arrayOfFullBlocks. We put currentBlock, which was in the arrayOfFullBlocks into the arrayOfEmptyBlocks (where all empty arrays are.
movement = true;
arrayToMove.push(currentBlock);
}
loopAnimation(arrayToMove, direction);
if (movement) {
setTimeout(function() { move(direction) }, 120);
} else {
clearField(true);
redrawField(true);
callRun2();
currentlyMoving = false;
}
}
function clearField(resetBlock) {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
for (var i = 0; i < arrayOfEmptyBlocks.length; i++) {
currentBlock = arrayOfEmptyBlocks[i];
if (resetBlock) {
currentBlock.img = 1;
currentBlock.upDated = false;
}
ctx.clearRect(currentBlock.xPos, currentBlock.yPos, currentBlock.xFill, currentBlock.yFill);
}
}
function redrawField(resetUpdate) {
for (var i = 0; i < arrayOfFullBlocks.length; i++) {
currBlock = arrayOfFullBlocks[i];
drawBlock(currBlock);
if (resetUpdate) {
currBlock.upDated = false;
}
}
}
drawField();
function loopAnimation(arrayToMove, direction) {
for (var j = 0; j < arrayToMove.length; j++) {
var currBlock = arrayToMove[j];
var oldPos;
switch (direction) {
case "left":
oldPos = currBlock.xPos;
break;
case "right":
oldPos = currBlock.xPos;
break;
case "up":
oldPos = currBlock.yPos;
break;
case "down":
oldPos = currBlock.yPos;
break;
}
for (var i = 0; i < 5; i++) {
setTimeout(function() {
animate(currBlock, direction)
}, 20 * i);
}
setTimeout(function() { resetLocation(currBlock, oldPos, direction) }, 120);
}
if (arrayToMove.length > 0) {
for (var k = 0; k < 5; k++) {
setTimeout(function() {
drawField()
}, 20 * k);
}
setTimeout(function() {
clearField(false);
redrawField(false);
}, 120);
}
}
function animate(currBlock, direction) {
eraseBlock(currBlock);
updateLocation(currBlock, direction);
drawBlock(currBlock);
}
function eraseBlock(currBlock) {
var ctx = document.getElementById("myCanvas").getContext("2d");
ctx.clearRect(currBlock.xPos, currBlock.yPos, currBlock.xFill, currBlock.yFill);
}
function updateLocation(currBlock, direction) {
switch (direction) {
case "left":
currBlock.xPos = currBlock.xPos - 20;
break;
case "right":
currBlock.xPos = currBlock.xPos + 20;
break;
case "up":
currBlock.yPos = currBlock.yPos - 20;
break;
case "down":
currBlock.yPos = currBlock.yPos + 20;
break;
}
}
function resetLocation(currBlock, oldPos, direction) {
switch (direction) {
case "left":
currBlock.xPos = oldPos;
break;
case "right":
currBlock.xPos = oldPos;
break;
case "up":
currBlock.yPos = oldPos;
break;
case "down":
currBlock.yPos = oldPos;
break;
}
}