-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
681 lines (563 loc) · 20.3 KB
/
script.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
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
"use strict";
let ctx;
let boatXPosition;
let boatYPosition;
let xMediumIcebergValues = [];
let yMediumIcebergValues = [];
let xSmallIcebergValues = [];
let ySmallIcebergValues = [];
let xMediumIceberg;
let yMediumIceberg;
let xSmallIceberg;
let ySmallIceberg;
let holeXPosition;
let holeYPosition;
let flagXPosition;
let flagYPosition;
let xHoleCover;
let yHoleCover;
let movementList = [];
let boatMovement;
let turn;
let score = 0;
let flagsCollected = 0;
let holesCovered = 0;
let flagsLost = 0;
let distanceBoatAndFlag;
let distanceBoatAndHole;
let distanceBoatAndCover;
let gameOver = false;
let movementsMsg = "Movement Program: ";
function setup() {
ctx = document.getElementById("drawingSurface").getContext("2d");
createElementsPositions();
drawBoard(gameOver);
}
function createElementsPositions() {
boatXPosition = 25;
boatYPosition = 32;
while(xMediumIcebergValues.length < 4) {
let samePosition = false;
xMediumIceberg = 60 + (50 * Math.floor(Math.random()*10));
yMediumIceberg = 7 + (50 * Math.floor(Math.random()*7));
for(let i = 0; i < xMediumIcebergValues.length; i++) {
if(xMediumIceberg == xMediumIcebergValues[i] || yMediumIceberg == yMediumIcebergValues[i]) {
samePosition = true;
}
}
if(!samePosition) {
xMediumIcebergValues.push(xMediumIceberg);
yMediumIcebergValues.push(yMediumIceberg);
}
}
while(xSmallIcebergValues.length < 7 ) {
let samePosition = false;
xSmallIceberg = 65 + (50 * Math.floor(Math.random()*10));
ySmallIceberg = 5 + (50 * Math.floor(Math.random()*7));
for(let i = 0; i < xSmallIcebergValues.length; i++) {
if(xSmallIceberg == xSmallIcebergValues[i] || ySmallIceberg == ySmallIcebergValues[i]) {
samePosition = true;
}
}
for(let i = 0; i < xMediumIcebergValues.length; i++) {
if(xSmallIceberg == xMediumIcebergValues[i] + 5 && ySmallIceberg == yMediumIcebergValues[i] + 48) {
samePosition = true;
} else if (xSmallIceberg == xMediumIcebergValues[i] + 5 && ySmallIceberg == yMediumIcebergValues[i] - 2) {
samePosition = true;
}
}
if(!samePosition) {
xSmallIcebergValues.push(xSmallIceberg);
ySmallIcebergValues.push(ySmallIceberg);
}
}
do {
holeXPosition = 75 + (50 * Math.floor(Math.random()*10));
holeYPosition = 25 + (50 * Math.floor(Math.random()*7));
} while(isSamePosition(holeXPosition, holeYPosition, xMediumIcebergValues, yMediumIcebergValues, xSmallIcebergValues, ySmallIcebergValues));
do {
flagXPosition = 80 + (50 * Math.floor(Math.random()*10));
flagYPosition = 40 + (50 * Math.floor(Math.random()*7));
} while(!validFlagPosition(flagXPosition, flagYPosition, holeXPosition, holeYPosition, xMediumIcebergValues, yMediumIcebergValues, xSmallIcebergValues, ySmallIcebergValues));
}
function isSamePosition(x, y, xValues1, yValues1, xValues2, yValues2) {
let samePosition = false;
for(let i = 0; i < xValues1.length; i++) {
let xValue = xValues1[i];
let yValue = yValues1[i];
if((x == xValue + 15) && (y == yValue + 18) || (x == xValue + 15) && (y == yValue + 68)) {
samePosition = true;
}
}
for(let i = 0; i < xValues2.length; i++) {
let xValue = xValues2[i];
let yValue = yValues2[i];
if((x == xValue + 10) && y == yValue + 20) {
samePosition = true;
}
}
return samePosition;
}
function validFlagPosition(xFlag, yFlag, xHole, yHole, xValues1, yValues1, xValues2, yValues2) {
let validPosition = true;
if((xFlag == xHole + 5) && (yFlag == yHole + 15)) {
validPosition = false;
}
for(let i = 0; i < xValues1.length; i++) {
let xIceberg = xValues1[i];
let yIceberg = yValues1[i];
if((xFlag == xIceberg + 20) && (yFlag == yIceberg + 33) || (xFlag == xIceberg + 20) && (yFlag == yIceberg + 83)) {
validPosition = false;
}
}
for(let i = 0; i < xValues2.length; i++) {
let xIceberg = xValues2[i];
let yIceberg = yValues2[i];
if((xFlag == xIceberg + 15) && (yFlag == yIceberg + 35)) {
validPosition = false;
}
}
return validPosition;
}
function drawBoard(gameOver) {
ctx.save();
ctx.clearRect(0,0,600,400);
ctx.beginPath();
ctx.fillStyle = "DeepSkyBlue";
ctx.strokeStyle = "blue";
for(let i = 0; i < 8; i++) {
ctx.save();
for(let i = 0; i < 12; i++) {
ctx.rect(1,1,49,49);
ctx.stroke();
ctx.fill();
ctx.translate(50,0);
}
ctx.restore();
ctx.translate(0,50);
}
ctx.restore();
drawHole(holeXPosition, holeYPosition);
drawSmallIceberg(xSmallIcebergValues, ySmallIcebergValues);
drawMediumIceberg(xMediumIcebergValues, yMediumIcebergValues);
drawHoleCover(xHoleCover, yHoleCover);
drawFlagPoint(flagXPosition, flagYPosition);
if(boatXPosition > 0 && boatYPosition > 0) {
drawBoat(boatXPosition, boatYPosition);
}
if(gameOver) {
displayGameOverInfo();
}
}
function saveMovements(e) {
let movement = "";
let symbol = "";
if(!gameOver && boatMovement == undefined) {
if(e.key == "ArrowUp") {
movement = "Up";
symbol = "↑";
} else if(e.key == "ArrowRight") {
movement = "Right";
symbol = "→";
} else if(e.key == "ArrowDown") {
movement = "Down";
symbol = "↓";
} else if(e.key == "ArrowLeft") {
movement = "Left";
symbol = "←";
} else if(e.key == 'c' || e.key == 'C') {
movement = "Hole Cover";
symbol = "⊝";
}
if(movement.length > 0 && movementList.length < 5) {
let indexOfCover = movementList.indexOf("Hole Cover");
if(indexOfCover == -1 || movement != "Hole Cover") {
movementList.push(movement);
movementsMsg+= symbol + " ";
}
}
document.getElementById("movements").innerHTML = movementsMsg;
}
}
function runProgram() {
if(!gameOver) {
if(movementList.length == 5 && boatMovement == undefined) {
turn = 1;
boatMovement = setInterval(moveBoat, 900);
movementsMsg = "Movement Program: ";
document.getElementById("errorMsg").style.display = "none";
document.getElementById("score").style.display = "block";
document.getElementById("instructions").style.display = "none";
} else {
if(movementList.length < 5) {
document.getElementById("errorMsg").innerHTML = "You need to add 5 movements before Run.";
}
if(boatMovement != undefined) {
document.getElementById("errorMsg").innerHTML = "You need to wait until the current movements finish";
}
document.getElementById("errorMsg").style.display = "inline-block";
}
}
}
function moveBoat() {
let direction = movementList.splice(0,1);
if(turn == 6 || gameOver) {
clearInterval(boatMovement);
boatMovement = undefined;
document.getElementById("movements").innerHTML = "Movement Program: ";
} else {
if(direction == "Up") {
if(hitIceberg(boatXPosition,boatYPosition,direction)) {
boatYPosition = boatYPosition;
} else {
boatYPosition -= 50;
}
if(boatYPosition < 0) {
boatYPosition = 32;
}
} else if (direction == "Down") {
if(hitIceberg(boatXPosition,boatYPosition,direction)) {
boatYPosition = boatYPosition;
} else {
boatYPosition += 50;
}
if(boatYPosition > 400) {
boatYPosition = 382;
}
} else if (direction == "Left") {
if(hitIceberg(boatXPosition,boatYPosition,direction)) {
boatXPosition = boatXPosition;
} else {
boatXPosition -= 50;
}
if(boatXPosition < 0) {
boatXPosition = 25;
}
} else if(direction == "Right") {
if(hitIceberg(boatXPosition,boatYPosition,direction)) {
boatXPosition = boatXPosition;
} else {
boatXPosition += 50;
}
if(boatXPosition > 600) {
boatXPosition = 575;
}
} else if(direction == "Hole Cover") {
xHoleCover = boatXPosition;
yHoleCover = boatYPosition - 7;
}
holeXPosition -= 50;
if(isSamePosition(holeXPosition, holeYPosition, xMediumIcebergValues, yMediumIcebergValues, xSmallIcebergValues, ySmallIcebergValues) || holeXPosition < 0) {
do {
holeXPosition = 75 + (50 * Math.floor(Math.random()*10));
holeYPosition = 25 + (50 * Math.floor(Math.random()*7));
} while(isSamePosition(holeXPosition, holeYPosition, xMediumIcebergValues, yMediumIcebergValues, xSmallIcebergValues, ySmallIcebergValues));
}
if((holeXPosition == flagXPosition - 5) && (holeYPosition == flagYPosition - 15)) {
score-= 50;
flagsLost++;
do {
flagXPosition = 80 + (50 * Math.floor(Math.random()*10));
flagYPosition = 40 + (50 * Math.floor(Math.random()*7));
} while(!validFlagPosition(flagXPosition, flagYPosition, holeXPosition, holeYPosition, xMediumIcebergValues, yMediumIcebergValues, xSmallIcebergValues, ySmallIcebergValues));
}
distanceBoatAndFlag = distanceCalculation(boatXPosition, boatYPosition, flagXPosition, flagYPosition);
if(distanceBoatAndFlag < 15) {
score += 100;
flagsCollected++;
do {
flagXPosition = 80 + (50 * Math.floor(Math.random()*10));
flagYPosition = 40 + (50 * Math.floor(Math.random()*7));
} while(!validFlagPosition(flagXPosition, flagYPosition, holeXPosition, holeYPosition, xMediumIcebergValues, yMediumIcebergValues, xSmallIcebergValues, ySmallIcebergValues));
}
distanceBoatAndHole = distanceCalculation(boatXPosition, boatYPosition, holeXPosition, holeYPosition);
distanceBoatAndCover = distanceCalculation(boatXPosition, boatYPosition, xHoleCover, yHoleCover);
if(distanceBoatAndHole < 15 || (distanceBoatAndHole < 15) && (distanceBoatAndCover < 15)) {
boatXPosition = -10;
boatYPosition = -10;
gameOver = true;
}
if((xHoleCover == holeXPosition) && (yHoleCover == holeYPosition) && (distanceBoatAndHole > 15)) {
score += 150;
xHoleCover = -20;
yHoleCover = -20;
holesCovered++;
do {
holeXPosition = 75 + (50 * Math.floor(Math.random()*10));
holeYPosition = 25 + (50 * Math.floor(Math.random()*7));
} while(isSamePosition(holeXPosition, holeYPosition, xMediumIcebergValues, yMediumIcebergValues, xSmallIcebergValues, ySmallIcebergValues));
}
}
turn++;
document.getElementById("score").innerHTML = "Score: " + score;
drawBoard(gameOver);
}
function distanceCalculation(x1,y1,x2,y2) {
return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
}
function hitIceberg(boatXPosition, boatYPosition, direction) {
let hit = false;
if(direction == "Up") {
for(let i = 0; i < xSmallIcebergValues.length; i++) {
xSmallIceberg = xSmallIcebergValues[i];
ySmallIceberg = ySmallIcebergValues[i];
if((boatXPosition == xSmallIceberg + 10) && (boatYPosition == ySmallIceberg + 77)) {
hit = true;
}
}
for(let i = 0; i < xMediumIcebergValues.length; i++) {
xMediumIceberg = xMediumIcebergValues[i];
yMediumIceberg = yMediumIcebergValues[i];
if((boatXPosition == xMediumIceberg + 15) && (boatYPosition == yMediumIceberg + 125)) {
hit = true;
}
}
} else if(direction == "Down") {
for(let i = 0; i < xSmallIcebergValues.length; i++) {
xSmallIceberg = xSmallIcebergValues[i];
ySmallIceberg = ySmallIcebergValues[i];
if((boatXPosition == xSmallIceberg + 10) && (boatYPosition == ySmallIceberg - 23)) {
hit = true;
}
}
for(let i = 0; i < xMediumIcebergValues.length; i++) {
xMediumIceberg = xMediumIcebergValues[i];
yMediumIceberg = yMediumIcebergValues[i];
if((boatXPosition == xMediumIceberg + 15) && (boatYPosition == yMediumIceberg - 25)) {
hit = true;
}
}
} else if(direction == "Left") {
for(let i = 0; i < xSmallIcebergValues.length; i++) {
xSmallIceberg = xSmallIcebergValues[i];
ySmallIceberg = ySmallIcebergValues[i];
if((boatXPosition == xSmallIceberg + 60) && (boatYPosition == ySmallIceberg + 27)) {
hit = true;
}
}
for(let i = 0; i < xMediumIcebergValues.length; i++) {
xMediumIceberg = xMediumIcebergValues[i];
yMediumIceberg = yMediumIcebergValues[i];
if((boatXPosition == xMediumIceberg + 65) && (boatYPosition == yMediumIceberg + 25) || (boatXPosition == xMediumIceberg + 65) && (boatYPosition == yMediumIceberg + 75)) {
hit = true;
}
}
} else if(direction == "Right") {
for(let i = 0; i < xSmallIcebergValues.length; i++) {
xSmallIceberg = xSmallIcebergValues[i];
ySmallIceberg = ySmallIcebergValues[i];
if((boatXPosition == xSmallIceberg - 40) && (boatYPosition == ySmallIceberg + 27)) {
hit = true;
}
}
for(let i = 0; i < xMediumIcebergValues.length; i++) {
xMediumIceberg = xMediumIcebergValues[i];
yMediumIceberg = yMediumIcebergValues[i];
if((boatXPosition == xMediumIceberg - 35) && (boatYPosition == yMediumIceberg + 25) || (boatXPosition == xMediumIceberg - 35) && (boatYPosition == yMediumIceberg + 75)) {
hit = true;
}
}
}
return hit;
}
function resetGame() {
score = 0;
flagsCollected = 0;
flagsLost = 0;
holesCovered = 0;
gameOver = false;
xSmallIcebergValues = [];
ySmallIcebergValues = [];
xMediumIcebergValues = [];
yMediumIcebergValues = [];
xHoleCover = -20;
yHoleCover = -20;
movementList = [];
clearInterval(boatMovement);
createElementsPositions();
drawBoard(gameOver);
boatMovement = undefined;
document.getElementById("score").innerHTML = "Score: " + score;
document.getElementById("movements").innerHTML = "Movement Program:";
document.getElementById("errorMsg").style.display = "none";
document.getElementById("score").style.display = "none";
document.getElementById("instructions").style.display = "block";
}
function drawBoat(x,y) {
//bottom
ctx.save();
ctx.beginPath();
ctx.fillStyle = "gray";
ctx.moveTo(x, y);
ctx.lineTo(x+22, y);
ctx.lineTo(x+12, y+12);
ctx.lineTo(x-12, y+12);
ctx.lineTo(x-22, y);
ctx.lineTo(x, y);
ctx.stroke();
ctx.fill();
//right flag
ctx.beginPath();
ctx.fillStyle = "white";
ctx.moveTo(x,y-3);
ctx.lineTo(x, y-27);
ctx.lineTo(x+17, y-3);
ctx.lineTo(x,y-3);
ctx.stroke();
ctx.fill();
//left flag
ctx.beginPath();
ctx.moveTo(x-3, y-3);
ctx.lineTo(x-3, y-23);
ctx.lineTo(x-20, y-3);
ctx.lineTo(x-3, y-3);
ctx.stroke();
ctx.fill();
//small left flag
ctx.beginPath();
ctx.fillStyle = "red";
ctx.moveTo(x-3, y-25);
ctx.lineTo(x-3, y-30);
ctx.lineTo(x-12, y-25);
ctx.lineTo(x-3, y-25);
ctx.stroke();
ctx.fill();
//floats
ctx.strokeStyle = "DarkRed";
ctx.lineWidth = 2;
let distance = 0;
for(let i= 1; i<= 3; i++) {
ctx.beginPath();
ctx.arc((x-12) + distance, y+5, 2, 0, 2*Math.PI);
distance+=12;
ctx.stroke();
}
ctx.restore();
}
function drawHole(x,y) {
let grdColor = ctx.createRadialGradient(x,y,20,x,y,2);
grdColor.addColorStop(0, "DarkBlue");
grdColor.addColorStop(1, "black");
ctx.save();
ctx.strokeStyle = "blue";
ctx.fillStyle = grdColor;
ctx.beginPath();
ctx.arc(x,y,20,0,2*Math.PI);
ctx.stroke();
ctx.fill();
ctx.restore();
}
function drawFlagPoint(x,y) {
//bottom
ctx.save();
ctx.beginPath();
ctx.strokeStyle = "black";
ctx.fillStyle = "yellow";
ctx.ellipse(x, y, 15, 10, 0*Math.PI, Math.PI, 2*Math.PI);
ctx.stroke();
ctx.fill();
//flag
ctx.moveTo(x,y-10);
ctx.lineTo(x,y-10);
ctx.lineTo(x,y-30);
ctx.stroke();
ctx.save();
ctx.beginPath();
ctx.fillStyle = "red";
ctx.moveTo(x-2,y-30);
ctx.lineTo(x-2,y-30);
ctx.lineTo(x-23,y-30);
ctx.lineTo(x-15,y-25);
ctx.lineTo(x-23,y-20);
ctx.lineTo(x-2,y-20);
ctx.fill();
ctx.stroke();
ctx.restore();
}
function drawSmallIceberg(xValues,yValues) {
ctx.save();
ctx.beginPath();
ctx.fillStyle = "PaleTurquoise";
ctx.strokeStyle = "DarkBlue";
for(let i = 0; i < xValues.length; i++) {
ctx.moveTo(xValues[i],yValues[i]);
ctx.lineTo(xValues[i],yValues[i]);
ctx.lineTo(xValues[i]+20,yValues[i]+4);
ctx.lineTo(xValues[i]+25,yValues[i]+25);
ctx.lineTo(xValues[i]+15,yValues[i]+40);
ctx.lineTo(xValues[i]-5,yValues[i]+30);
ctx.lineTo(xValues[i],yValues[i]+20);
ctx.lineTo(xValues[i]-5,yValues[i]+10);
ctx.lineTo(xValues[i],yValues[i]);
ctx.fill();
ctx.stroke();
}
ctx.restore();
}
function drawMediumIceberg(xValues,yValues) {
ctx.save();
ctx.beginPath();
ctx.fillStyle = "PaleTurquoise";
ctx.strokeStyle = "DarkBlue";
for(let i = 0; i < xValues.length; i++) {
ctx.moveTo(xValues[i],yValues[i]);
ctx.lineTo(xValues[i],yValues[i]);
ctx.lineTo(xValues[i]-5,yValues[i]+45);
ctx.lineTo(xValues[i]+5,yValues[i]+60);
ctx.lineTo(xValues[i]-5,yValues[i]+80);
ctx.lineTo(xValues[i]+10,yValues[i]+90);
ctx.lineTo(xValues[i]+25,yValues[i]+80);
ctx.lineTo(xValues[i]+35,yValues[i]+45);
ctx.lineTo(xValues[i]+35,yValues[i]+25);
ctx.lineTo(xValues[i]+25,yValues[i]+20);
ctx.lineTo(xValues[i]+30,yValues[i]+5);
ctx.lineTo(xValues[i],yValues[i]);
ctx.fill();
ctx.stroke();
}
ctx.restore();
}
function drawHoleCover(x, y) {
ctx.save();
ctx.strokeStyle = "black";
ctx.fillStyle = "gray"
ctx.lineWidth = 5;
ctx.beginPath();
ctx.arc(x,y,17,0,2*Math.PI);
ctx.stroke();
ctx.fill();
ctx.lineWidth = 3.5;
ctx.beginPath();
ctx.lineTo(x-10,y);
ctx.lineTo(x+10,y);
ctx.stroke();
ctx.restore();
}
function displayGameOverInfo() {
ctx.save();
ctx.beginPath();
ctx.fillStyle = "WhiteSmoke";
ctx.rect(100, 100, 400, 200);
ctx.fill();
ctx.stroke();
//game over
ctx.font = "60px Georgia";
ctx.fillStyle = "red";
ctx.strokeStyle = "black";
ctx.fillText("Game Over", 150,155);
ctx.strokeText("Game Over", 150,155);
//info points
ctx.font = "20px Verdana";
ctx.fillStyle = "black";
ctx.fillText("flags collected............. " + flagsCollected, 165, 187);
ctx.fillText("sea holes covered........ " + holesCovered, 165, 217);
ctx.fillText("flags lost.................... " + flagsLost, 167, 247);
//score
ctx.font = "bold 22px Georgia";
ctx.fillStyle = "DarkBlue";
if (score <= 0) {
ctx.fillStyle = "red";
}
ctx.fillText("Score......................... " + score, 160, 280);
ctx.restore();
}