-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
708 lines (651 loc) · 19.4 KB
/
app.js
File metadata and controls
708 lines (651 loc) · 19.4 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
var context;
var shape = new Object();
var board = [[4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4],
[4,0,0,0,0,0,0,0,0,0,0,0,0,4,4,0,0,0,0,4],
[4,0,4,4,4,4,0,4,0,4,4,4,0,4,4,0,4,4,0,4],
[4,0,0,0,0,4,0,4,0,0,0,4,0,4,4,0,4,0,0,4],
[4,0,4,4,0,4,0,4,4,4,4,4,0,4,4,0,4,4,0,4],
[4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4],
[4,0,4,4,4,4,0,4,4,0,4,4,4,4,4,4,4,4,0,4],
[4,0,4,4,4,4,0,4,4,0,4,4,4,4,4,4,4,4,0,4],
[4,0,0,0,0,0,0,4,4,0,0,0,0,4,4,0,0,0,0,4],
[4,0,0,0,0,0,0,4,4,4,4,4,0,4,4,0,4,4,4,4],
[4,4,4,0,4,4,0,4,4,4,4,4,0,4,4,0,4,4,4,4],
[4,0,0,0,0,4,0,4,4,0,0,0,0,0,0,0,0,0,0,4],
[4,0,0,0,0,4,0,4,4,0,4,4,4,4,4,4,4,4,0,4],
[4,0,0,0,0,4,0,4,4,0,4,4,0,0,0,0,4,4,0,4],
[4,0,0,0,0,4,0,0,0,0,4,4,0,0,0,0,4,4,0,4],
[4,0,0,0,0,4,0,4,4,0,0,0,0,0,0,0,4,4,0,4],
[4,0,0,0,0,4,0,4,4,0,4,4,4,4,4,4,4,4,0,4],
[4,0,0,0,0,4,0,4,4,0,0,0,0,0,0,0,0,0,0,4],
[4,0,0,0,0,0,0,4,4,0,4,4,4,4,4,4,4,4,0,4],
[4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4]];
var dotsBoard;
var score;
var remain_lives;
var pac_color;
var sixtyPercentColor;
var thirtyPercentColor;
var tenPercentColor;
//var start_time;
//var time_elapsed;
var interval;
var ghostInterval;
var checkInterval;
var timeInterval;
var packBody = new Object();
var eye = new Object();
var numOfGhosts;
var ghostPosition;
var monsterLocation = new Object();
var remain_monster;
var keyRight= 39;
var keyLeft = 37;
var keyUp = 38;
var keyDown = 40;
var time;
var numOfBalls;
var pause;
var seconds;
var currentFood;
// $(document).ready(function() {
// context = canvas.getContext("2d");
// Start();
// });
function stopInterval(){
clearInterval(interval);
clearInterval(ghostInterval);
clearInterval(checkInterval)
clearInterval(timeInterval);
}
function startGame() {
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
canvas.width = canvas.width;
clearCanvas(context,canvas);
//canvas.style.width = window.innerWidth;
//canvas.style.height = window.innerHeight;
Start();
}
function Start() {
pause = false;
packBody.x = 0.15;
packBody.y = 1.85;
eye.x = 5;
eye.y = -10;
clearInterval(interval);
clearInterval(ghostInterval);
clearInterval(checkInterval);
clearInterval(timeInterval);
//clearInterval(labelInterval);
//board = new Array();
dotsBoard = new Array();
score = 0;
remain_lives = 5;
pac_color = "yellow";
var food_remain= numOfBalls;
currentFood = numOfBalls;
ghostPosition = new Array(numOfGhosts);
initializeGhostPos();
placeGhosts();
monsterLocation.i = 1;
monsterLocation.j = 3;
var cnt = 400;
var numSixtyPercent = food_remain*0.6;
var numThirtyPercent = food_remain*0.3;
var numTenPercent = food_remain*0.1;
var pacman_remain = 1;
remain_monster = 1;
seconds = 0;
//tempTime = new Date();
start_time = new Date();
for (var i = 0; i < 20; i++) {
//board[i] = new Array();
dotsBoard[i] = new Array();
for (var j = 0; j < 20; j++) {
if (board[i][j] != 4){
board[i][j] = 0;
}
dotsBoard[i][j] = board[i][j];
var randomNum = Math.random();
if (randomNum <= (1.0 * food_remain) / cnt && board[i][j] != 4 && board[i][j] != 5 && board[i][j] != 7) {
//food_remain--;
var num = Math.random();
if(num < 0.6 && numSixtyPercent > 0 ){
numSixtyPercent--;
food_remain--;
dotsBoard[i][j] = 1;
}
else if(num < 0.9 && numThirtyPercent > 0){
numThirtyPercent--;
food_remain--;
dotsBoard[i][j] = 2;
}
else if(numTenPercent > 0){
numTenPercent--;
food_remain--;
dotsBoard[i][j] = 3;
}
} else if (randomNum < (1.0 * (pacman_remain + food_remain)) / cnt && board[i][j] != 4) {
if(i > 5 && i < 17 && j > 5 && j < 17){
shape.i = i;
shape.j = j;
pacman_remain--;
board[i][j] = 2;
}
} else if (board[i][j] != 4) {
board[i][j] = 0;
dotsBoard[i][j] = 0;
}
cnt--;
//}
}
}
if(pacman_remain > 0){
var emptyCell = findRandomEmptyCell();
shape.i = emptyCell[0];
shape.j = emptyCell[1];
while(i > 5 && i < 17 && j > 5 && j < 17){
emptyCell = findRandomEmptyCell();
shape.i = emptyCell[0];
shape.j = emptyCell[1];
}
}
while (food_remain > 0) {
var emptyCell = findRandomEmptyCell();
if(numSixtyPercent > 0){
numSixtyPercent--;
dotsBoard[emptyCell[0]][emptyCell[1]] = 1;
}
else if(numThirtyPercent > 0){
numThirtyPercent--;
dotsBoard[emptyCell[0]][emptyCell[1]] = 2;
}
else{
numTenPercent--;
dotsBoard[emptyCell[0]][emptyCell[1]] = 3;
}
food_remain--;
}
// for (var i = 0; i < 20; i++) {
// for (var j = 0; j < 20; j++) {
// if(board[i][j] == 4 && (dotsBoard[i][j] == 1 || dotsBoard[i][j] == 2 || dotsBoard[i][j] == 3)){
// window.alert("collpas!!");
// }
// }
// }
// var emptyCell = findRandomEmptyCell(board);
// board[emptyCell[0]][emptyCell[1]] = 6; //Medicine
var emptyCell = findRandomEmptyCell();
dotsBoard[emptyCell[0]][emptyCell[1]] = 6; // Eatable monster
emptyCell = findRandomEmptyCell();
dotsBoard[emptyCell[0]][emptyCell[1]] = 8;// Clock
keysDown = {};
addEventListener(
"keydown",
function(e) {
keysDown[e.keyCode] = true;
if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
},
false
);
addEventListener(
"keyup",
function(e) {
keysDown[e.keyCode] = false;
},
false
);
interval = setInterval(UpdatePosition, 100);
ghostInterval = setInterval(updateGhostPosition, 400);
checkInterval = setInterval(isEatPackman, 50);
timeInterval = setInterval(function () {
if(!pause){
seconds++;
}}, 1000);
}
function findRandomEmptyCell() {
var i = Math.floor(Math.random() * 19 + 1);
var j = Math.floor(Math.random() * 19 + 1);
while (board[i][j] != 0 || dotsBoard[i][j] != 0) {
i = Math.floor(Math.random() * 19 + 1);
j = Math.floor(Math.random() * 19 + 1);
}
return [i, j];
}
function GetKeyPressed() {
//up
if (keysDown[keyUp]) {
return 1;
}
//down
if (keysDown[keyDown]) {
return 2;
}
//left
if (keysDown[keyLeft]) {
return 3;
}
//right
if (keysDown[keyRight]) {
return 4;
}
}
function Draw() {
canvas.width = canvas.width; //clean board
for (var i = 0; i < 20; i++) {
for (var j = 0; j < 20; j++) {
var center = new Object();
center.x = i * 30 + 15;
center.y = j * 30 + 15;
if (board[i][j] == 2) {
if (GetKeyPressed() == 4) { //Right
packBody.x = 0.15;
packBody.y = 1.85;
context.beginPath();
context.arc(center.x, center.y, 15, 0.15 * Math.PI, 1.85 * Math.PI); // half circle
} else if (GetKeyPressed() == 3) { //Left
packBody.x = 1.15;
packBody.y = 0.85;
context.beginPath();
context.arc(center.x, center.y, 15, 1.15 * Math.PI, 0.85 * Math.PI); // half circle
} else if (GetKeyPressed() == 2) { //Down
packBody.x = 0.65;
packBody.y = 0.35;
context.beginPath();
context.arc(center.x, center.y, 15, 0.65 * Math.PI, 0.35 * Math.PI); // half circle
} else if (GetKeyPressed() == 1) { // Up
packBody.x = 1.65;
packBody.y = 1.35;
context.beginPath();
context.arc(center.x, center.y, 15, 1.65 * Math.PI, 1.35 * Math.PI); // half circle
} else {
context.beginPath();
context.arc(center.x, center.y, 15, packBody.x * Math.PI, packBody.y * Math.PI); // half circle
}
context.lineTo(center.x, center.y);
context.fillStyle = pac_color; //color
context.fill();
context.beginPath();
if (GetKeyPressed() == 2 || GetKeyPressed() == 1) {
eye.x = 10;
eye.y = -5;
context.arc(center.x + 10, center.y - 5, 3, 0, 2 * Math.PI); // circle
} else if (GetKeyPressed() == 4 || GetKeyPressed() == 3) {
eye.x = 5;
eye.y = -10;
context.arc(center.x + 5, center.y - 10, 3, 0, 2 * Math.PI); // circle
} else {
context.arc(center.x + eye.x, center.y + eye.y, 3, 0, 2 * Math.PI); // circle
}
context.fillStyle = "black"; //color
context.fill();
} else if ((dotsBoard[i][j] == 1 || dotsBoard[i][j] == 2 || dotsBoard[i][j] == 3) && board[i][j] != 5 && board[i][j] != 7) { //Points
context.beginPath();
context.arc(center.x, center.y, 10, 0, 2 * Math.PI); // circle
if(dotsBoard[i][j] == 1){
context.fillStyle = sixtyPercentColor; //color
}
else if (dotsBoard[i][j] == 2){
context.fillStyle = thirtyPercentColor; //color
}
else if(dotsBoard[i][j] == 3){
context.fillStyle = tenPercentColor; //color
}
context.fill();
} else if (board[i][j] == 4) { //Wall
var img;
//context.beginPath();
//context.rect(center.x - 30, center.y - 30, 60, 60);
//context.fillStyle = "grey"; //color
//context.fill();
img = document.getElementById("wall");
context.drawImage(img, center.x - 15, center.y - 15, 30, 30);
} else if (board[i][j] == 5) { //Ghost
var img;
img = document.getElementById("ghost");
context.drawImage(img, center.x - 15, center.y - 15, 30, 30);
}
else if (dotsBoard[i][j] == 6) { //Medicine
var img;
img = document.getElementById("medicine");
context.drawImage(img, center.x - 15, center.y - 15, 30, 30);
}
else if (board[i][j] == 7) { //Cute monster
var img;
img = document.getElementById("mon");
context.drawImage(img, center.x - 15, center.y - 15, 30, 30);
}
else if (dotsBoard[i][j] == 8) { //Clock
var img;
img = document.getElementById("clock");
context.drawImage(img, center.x - 15, center.y - 15, 30, 30);
}
}
}
}
function UpdatePosition() {
//start_time = tempTime;
if(!pause) {
board[shape.i][shape.j] = 0;
var x = GetKeyPressed();
if (x == 1) {
if (shape.j > 0 && board[shape.i][shape.j - 1] != 4) {
shape.j--;
}
}
if (x == 2) {
if (shape.j < 19 && board[shape.i][shape.j + 1] != 4) {
shape.j++;
}
}
if (x == 3) {
if (shape.i > 0 && board[shape.i - 1][shape.j] != 4) {
shape.i--;
}
}
if (x == 4) {
if (shape.i < 19 && board[shape.i + 1][shape.j] != 4) {
shape.i++;
}
}
if (dotsBoard[shape.i][shape.j] == 1) {
score += 5;
dotsBoard[shape.i][shape.j] = 0;
currentFood--;
} else if (dotsBoard[shape.i][shape.j] == 2) {
score += 15;
dotsBoard[shape.i][shape.j] = 0;
currentFood--;
} else if (dotsBoard[shape.i][shape.j] == 3) {
score += 25;
dotsBoard[shape.i][shape.j] = 0;
currentFood--;
}
if (dotsBoard[shape.i][shape.j] == 6) { // Medicine
remain_lives += 1;
dotsBoard[shape.i][shape.j] = 0;
}
if (dotsBoard[shape.i][shape.j] == 8) { // Clock
time = parseInt(time) + 10;
showSettings();
dotsBoard[shape.i][shape.j] = 0;
}
board[shape.i][shape.j] = 2;
//var currentTime = new Date();
//time_elapsed = (currentTime - start_time) / 1000;
//time_elapsed += tempTime;
if(remain_monster > 0) {
if (monsterLocation.i == shape.i && monsterLocation.j == shape.j) { // Monster
score = score + 50;
remain_monster--;
board[shape.i][shape.j] = 0;
//board[monsterLocation.i][monsterLocation.j] = 0;
}
}
lblScore.value = score;
lblTime.value = seconds;
lblLifes.value = remain_lives;
if (seconds >= time) { //Maximum time game
stopInterval();
pauseMusic();
if (score < 100) {
playDieSound();
setTimeout(function(){
alert("You are better than " + score + " points!", function(){
console.log("Callback executed");
});
}, 300);
} else {
playWinSound();
setTimeout(function(){
alert("Winner!!!", function(){
console.log("Callback executed");
});
}, 300);
}
}
if(currentFood == 0){
stopInterval();
pauseMusic();
playWinSound();
setTimeout(function(){
alert("Winner!!!", function(){
console.log("Callback executed");
});
}, 300);
}
// else if (score >= 100) {
// window.clearInterval(interval);
// setTimeout(function () {window.alert("Winner!!!");},50);
//
// } else
if (remain_lives == 0) {
stopInterval();
pauseMusic();
playDieSound();
setTimeout(function(){
alert("Loser!", function(){
console.log("Callback executed");
});
}, 300);
} else {
Draw();
}
}
}
function isEatPackman() {
// if(remain_monster > 0) {
// if (monsterLocation.i == shape.i && monsterLocation.j == shape.j) { // Monster
// score = score + 50;
// remain_monster--;
// board[shape.i][shape.j] = 0;
// //board[monsterLocation.i][monsterLocation.j] = 0;
// }
// }
if (eatMeGhost()) {
playGhostSound();
remain_lives--;
score = score - 10;
board[shape.i][shape.j] = 0;
var emptyCell = findRandomEmptyCell();
shape.i = emptyCell[0];
shape.j = emptyCell[1];
deleteGhosts();
placeGhosts();
}
Draw();
}
function placeGhosts() {
board[1][1] = 5; // First ghost
ghostPosition[0][0] = 1;
ghostPosition[0][1] = 1;
if(numOfGhosts == 2){
board[1][18] = 5;
ghostPosition[1][0] = 1;
ghostPosition[1][1] = 18;
}
else if(numOfGhosts == 3){
board[1][18] = 5;
board[18][18] = 5;
ghostPosition[1][0] = 1;
ghostPosition[1][1] = 18;
ghostPosition[2][0] = 18;
ghostPosition[2][1] = 18;
}
else if(numOfGhosts == 4){
board[1][18] = 5;
board[18][18] = 5;
board[18][1] = 5;
ghostPosition[1][0] = 1;
ghostPosition[1][1] = 18;
ghostPosition[2][0] = 18;
ghostPosition[2][1] = 18;
ghostPosition[3][0] = 18;
ghostPosition[3][1] = 1;
}
}
function updateGhostPosition(){
if(!pause) {
var num;
var direction;
for (var i = 0; i < numOfGhosts; i++) {
num = Math.random();
board[ghostPosition[i][0]][ghostPosition[i][1]] = 0;
if (num < 0.3) {
// direction = getRandomDirection();
//
// if (direction == 1) { //Up
// if (ghostPosition[i][1] - 1 >= 0 && board[ghostPosition[i][0]][ghostPosition[i][1] - 1] != 4 && board[ghostPosition[i][0]][ghostPosition[i][1] - 1] != 5) {
// ghostPosition[i][1]--;
// }
// } else if (direction == 2) { //Down
// if (ghostPosition[i][1] + 1 < 20 && board[ghostPosition[i][0]][ghostPosition[i][1] + 1] != 4 && board[ghostPosition[i][0]][ghostPosition[i][1] + 1] != 5) {
// ghostPosition[i][1]++;
// }
// } else if (direction == 3) { //Left
// if (ghostPosition[i][0] - 1 >= 0 && board[ghostPosition[i][0] - 1][ghostPosition[i][1]] != 4 && board[ghostPosition[i][0] - 1][ghostPosition[i][1]] != 5) {
// ghostPosition[i][0]--;
// }
// } else if (direction == 4) { //Right
// if (ghostPosition[i][0] + 1 < 20 && board[ghostPosition[i][0] + 1][ghostPosition[i][1]] != 4 && board[ghostPosition[i][0] + 1][ghostPosition[i][1]] != 5) {
// ghostPosition[i][0]++;
// }
// }
var newLication = randomDirection(ghostPosition[i][0], ghostPosition[i][1]);
ghostPosition[i][0] = newLication[0];
ghostPosition[i][1] = newLication[1];
} else {
let currentDistance = distanceSum(shape.i, ghostPosition[i][0], shape.j, ghostPosition[i][1]);
if (currentDistance >= distanceSum(shape.i, ghostPosition[i][0] + 1, shape.j, ghostPosition[i][1]) &&
board[ghostPosition[i][0] + 1][ghostPosition[i][1]] != 4 && board[ghostPosition[i][0] + 1][ghostPosition[i][1]] != 5 &&
ghostPosition[i][0] + 1 < 20 && board[ghostPosition[i][0] + 1][ghostPosition[i][1]] != 7) {
ghostPosition[i][0]++;
} else if (currentDistance >= distanceSum(shape.i, ghostPosition[i][0], shape.j, ghostPosition[i][1] + 1) &&
board[ghostPosition[i][0]][ghostPosition[i][1] + 1] != 4 && board[ghostPosition[i][0]][ghostPosition[i][1] + 1] != 5 &&
ghostPosition[i][1] + 1 < 20 && board[ghostPosition[i][0]][ghostPosition[i][1] + 1] != 7) {
ghostPosition[i][1]++;
} else if (currentDistance >= distanceSum(shape.i, ghostPosition[i][0] - 1, shape.j, ghostPosition[i][1]) &&
board[ghostPosition[i][0] - 1][ghostPosition[i][1]] != 4 && board[ghostPosition[i][0] - 1][ghostPosition[i][1]] != 5 &&
ghostPosition[i][0] - 1 >= 0 && board[ghostPosition[i][0] - 1][ghostPosition[i][1]] != 7) {
ghostPosition[i][0]--;
} else if (currentDistance >= distanceSum(shape.i, ghostPosition[i][0], shape.j, ghostPosition[i][1] - 1) &&
board[ghostPosition[i][0]][ghostPosition[i][1] - 1] != 4 && board[ghostPosition[i][0]][ghostPosition[i][1] - 1] != 5 &&
ghostPosition[i][1] - 1 >= 0 && board[ghostPosition[i][0]][ghostPosition[i][1] - 1] != 7) {
ghostPosition[i][1]--;
}
else{
var newLication = randomDirection(ghostPosition[i][0], ghostPosition[i][1]);
ghostPosition[i][0] = newLication[0];
ghostPosition[i][1] = newLication[1];
}
}
board[ghostPosition[i][0]][ghostPosition[i][1]] = 5;
}
//Update monster move
if (remain_monster > 0) {
// direction = getRandomDirection();
board[monsterLocation.i][monsterLocation.j] = 0;
var newLication = randomDirection(monsterLocation.i, monsterLocation.j);
monsterLocation.i = newLication[0];
monsterLocation.j = newLication[1];
// if (direction == 1) { //Up
//
// if (monsterLocation.j - 1 >= 0 && board[monsterLocation.i][monsterLocation.j - 1] != 4 && board[monsterLocation.i][monsterLocation.j - 1] != 5) {
// monsterLocation.j--;
// }
// } else if (direction == 2) { //Down
// if (monsterLocation.j + 1 < 20 && board[monsterLocation.i][monsterLocation.j + 1] != 4 && board[monsterLocation.i][monsterLocation.j + 1] != 4) {
// monsterLocation.j++;
// }
// } else if (direction == 3) { //Left
// if (monsterLocation.i - 1 >= 0 && board[monsterLocation.i - 1][monsterLocation.j] != 4 && board[monsterLocation.i - 1][monsterLocation.j] != 4) {
// monsterLocation.i--;
// }
// } else if (direction == 4) { //Right
// if (monsterLocation.i + 1 < 20 && board[monsterLocation.i + 1][monsterLocation.j] != 4 && board[monsterLocation.i + 1][monsterLocation.j] != 4) {
// monsterLocation.i++;
// }
// }
board[monsterLocation.i][monsterLocation.j] = 7;
}
if (board[shape.i][shape.j] == 7) { // Monster
score = score + 50;
remain_monster--;
board[shape.i][shape.j] = 0;
board[monsterLocation.i][monsterLocation.j] = 0;
}
}
}
function distanceSum(x1,x2,y1,y2){
return (Math.abs(x1-x2) + Math.abs(y1-y2));
}
function initializeGhostPos(){
for (var i =0; i < numOfGhosts; i++) {
ghostPosition[i] = new Array(2);
}
}
function eatMeGhost(){
for (var i =0; i < numOfGhosts; i++) {
if(shape.i == ghostPosition[i][0] && shape.j == ghostPosition[i][1]){
return true;
}
}
return false;
}
function deleteGhosts(){
for (var i =0; i < numOfGhosts; i++) {
board[ghostPosition[i][0]][ghostPosition[i][1]] = 0;
}
board[monsterLocation.i][monsterLocation.j] = 0;
}
function getRandomDirection () {
var probabilities = [0.25, 0.25, 0.25, 0.25];
var results = [1, 2, 3, 4];
var num = Math.random();
sum = 0;
for (var i = 0; i < probabilities.length -1; i++) {
sum += probabilities[i];
if (num < sum) {
return results[i];
}
}
return results[3];
}
function updateLabels() {
lblScore.value = score;
lblTime.value = time_elapsed;
lblLifes.value = remain_lives;
}
function randomDirection(i,j){
var tempX = i;
var tempY = j;
//while(tempX == i && tempY == j) {
var direction = getRandomDirection();
if (direction == 1) { //Up
if (j - 1 >= 0 && board[i][j - 1] != 4 && board[i][j - 1] != 5 && board[i][j - 1] != 7) {
j--;
//break;
}
} else if (direction == 2) { //Down
if (j + 1 < 20 && board[i][j + 1] != 4 && board[i][j + 1] != 5 && board[i][j - 1] != 7) {
j++;
//break;
}
} else if (direction == 3) { //Left
if (i - 1 >= 0 && board[i - 1][j] != 4 && board[i - 1][j] != 5 && board[i][j - 1] != 7) {
i--;
//break;
}
} else if (direction == 4) { //Right
if (i + 1 < 20 && board[i + 1][j] != 4 && board[i + 1][j] != 5 && board[i][j - 1] != 7) {
i++;
//break;
}
}
// setTimeout(function(){ break; }, 3000);
// }
return [i,j];
}