-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
430 lines (393 loc) · 11.9 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
// Base on a game name "Dalek" by Heiko Selber
// Written by: Eyal Schachner
const EMPTY = 0;
const TANK = 1;
const MISSILE = 2;
const EXPLOSION = 3;
var grid = document.getElementById("grid");
grid.onclick = function() {
document.getElementById("HelpModal").style.display = "block";
};
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
document.getElementById("HelpModal").style.display = "none";
};
var scoreId = document.getElementById("scoreId");
var levelId = document.getElementById("levelId");
var hiscoreId = document.getElementById("hiscoreId");
var msg = document.getElementById("msg");
var COLS = 14;
var ROWS = 14;
var MAXNUMMISSILES = (COLS - 2) * (ROWS - 2);
var tankPlace = {
x: -1,
y: -1
};
var MissilePlace = {
x: -1,
y: -1
};
var Alive = true;
var Level = 0;
var NumMissileLeft = 0;
var HighestScore;
var BlowActive = true; // blow pressed
var Score = 0;
var Board = Array.from({ length: ROWS }, () => Array(COLS).fill(0));
var PrevBoard = Array.from({ length: ROWS }, () => Array(COLS).fill(0));
var Tcopy = Array.from({ length: ROWS }, () => Array(COLS).fill(0));
StartGame();
function StartGame() {
Alive = true;
Level = 0;
BlowActive = true;
Score = 0;
ClearTable(Board);
ClearTable(PrevBoard);
ClearTable(Tcopy);
HighestScore = ReadHighestScore();
hiscoreId.innerHTML = HighestScore;
scoreId.innerHTML = Score;
StartLevel();
msg.innerHTML = "";
document.getElementById("arrow").style.pointerEvents = "auto";
document.getElementById("control").style.pointerEvents = "auto";
bindKey();
}
function ClearTable(t) {
for (var i = 0; i < ROWS; i++) {
for (var j = 0; j < COLS; j++) {
t[i][j] = EMPTY;
}
}
}
function CopyTable(a, b) { // copy a to b
for (var i = 0; i < ROWS; i++) {
for (var j = 0; j < COLS; j++) {
b[i][j] = a[i][j];
}
}
}
function emptyN(x, y) { // empty Neighborhood when placing missiles
console.log("emptyN function");
if (Board[x][y] != EMPTY) {
return false;
}
for (var i = Math.max(0, x - 1); i <= Math.min(ROWS - 1, x + 1); i++) {
for (var j = Math.max(0, y - 1); j <= Math.min(COLS - 1, y + 1); j++) {
if (Board[i][j] == TANK) {
return false;
}
}
}
return true;
}
function StartLevel() {
console.log("StartLevel function");
document.getElementById("playLevelEnd").play();
Level = Level + 1;
ClearTable(Board);
BlowActive = true;
levelId.innerHTML = Level;
tankPlace.x = Math.floor(Math.random() * ROWS);
tankPlace.y = Math.floor(Math.random() * COLS);
Board[tankPlace.x][tankPlace.y] = TANK;
var i = 0;
do {
MissilePlace.x = Math.floor(Math.random() * ROWS);
MissilePlace.y = Math.floor(Math.random() * COLS);
if (emptyN(MissilePlace.x, MissilePlace.y)) {
Board[MissilePlace.x][MissilePlace.y] = MISSILE;
i = i + 1;
}
} while ((i < 4 * Level) && (i < MAXNUMMISSILES));
NumMissileLeft = i;
DisplayBoard();
TablePrint(Board);
}
function MoveMissiles() {
console.log("MoveMissiles function");
var i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if ((Board[i][j] == TANK) || (Board[i][j] == EXPLOSION)) {
Tcopy[i][j] = Board[i][j];
} else {
Tcopy[i][j] = EMPTY;
}
}
}
// MOVE MISSILE and check for end of game
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
var iNew = i;
var jNew = j;
if (Board[i][j] == MISSILE) { // MOVE MISSILE
if (jNew != tankPlace.y) {
if (jNew > tankPlace.y) {
jNew = jNew - 1;
} else {
jNew = jNew + 1;
}
}
if (iNew != tankPlace.x) {
if (iNew > tankPlace.x) {
iNew = iNew - 1;
} else {
iNew = iNew + 1;
}
}
// check if move on an explosion/missile/tank/empty
// print("New position:",i,j,iNew, jNew, Tcopy[iNew][jNew])
if (Tcopy[iNew][jNew] == EXPLOSION) {
Score = Score + 1;
NumMissileLeft = NumMissileLeft - 1;
}
if (Tcopy[iNew][jNew] == MISSILE) {
Tcopy[iNew][jNew] = EXPLOSION;
Score = Score + 2;
NumMissileLeft = NumMissileLeft - 2;
}
if (Tcopy[iNew][jNew] == TANK) {
Alive = false;
}
if (Tcopy[iNew][jNew] == EMPTY) {
Tcopy[iNew][jNew] = MISSILE;
}
}
}
}
CopyTable(Board, PrevBoard);
CopyTable(Tcopy, Board);
scoreId.innerHTML = Score;
}
function ReadHighestScore() {
console.log("ReadHighestScore function");
return localStorage.getItem("MAHighestScore");
}
function SaveHighestScore() {
console.log("SaveHighestScore function");
localStorage.setItem("MAHighestScore", Score);
}
function DisplayBoard() {
console.log("DisplayBoard function");
grid.innerHTML = "";
for (var i = 0; i < ROWS; i++) {
var row = grid.insertRow(i);
for (var j = 0; j < COLS; j++) {
var cell = row.insertCell(j);
var vvv = document.createAttribute("CellValue");
if (Board[i][j] == TANK) {
console.log("tank: " + i + "," + j);
vvv.value = TANK;
cell.className = "tank";
}
if (Board[i][j] == MISSILE) {
vvv.value = MISSILE;
var angle = -Math.atan2(j - tankPlace.y, i - tankPlace.x) * 180 / Math.PI;
cell.innerHTML = "<img style=\"transform: rotate(" + angle + "deg);\" src=\"image/missile.png\">";
cell.className = "missile";
}
if (Board[i][j] == EXPLOSION) {
vvv.value = EXPLOSION;
cell.className = "explosion";
}
cell.setAttributeNode(vvv);
}
}
}
function EndOfGame() {
console.log("EndOfGame function");
document.getElementById("playEndOfGame").play();
DisplayBoard();
document.getElementById("arrow").style.pointerEvents = "none";
document.getElementById("control").style.pointerEvents = "none";
unbindKey();
if (Score > HighestScore) {
msg.innerHTML = "Game Over<br>Final Score: " + Score + "<br>NEW HIGH SCORE!!!";
SaveHighestScore();
} else {
msg.innerHTML = "Game Over<br>Final Score: " + Score;
}
}
function move(key) {
console.log("move function: " + key);
// Play movement sound
document.getElementById("playButton").play();
// Define direction offsets for movement
const directionOffsets = {
1: [-1, -1], // Up Left
2: [-1, 0], // Up
3: [-1, 1], // Up Right
4: [0, -1], // Left
6: [0, 1], // Right
7: [1, -1], // Down Left
8: [1, 0], // Down
9: [1, 1], // Down Right
};
// Helper function to check boundaries
function isWithinBounds(x, y) {
return x >= 0 && x < ROWS && y >= 0 && y < COLS;
}
// Process movement if the key corresponds to a valid direction
if (directionOffsets[key]) {
const [dx, dy] = directionOffsets[key];
const newX = tankPlace.x + dx;
const newY = tankPlace.y + dy;
// Check if the new position is within bounds
if (isWithinBounds(newX, newY)) {
// Update the board and tank position
Board[tankPlace.x][tankPlace.y] = EMPTY;
tankPlace.x = newX;
tankPlace.y = newY;
// Check for collision
if (Board[tankPlace.x][tankPlace.y] !== EMPTY) {
Alive = false;
}
// Update the board with the new tank position
Board[tankPlace.x][tankPlace.y] = TANK;
// Handle missiles and game state
MoveMissiles();
if (Alive) {
if (NumMissileLeft === 0) {
StartLevel();
} else {
DisplayBoard();
}
} else {
EndOfGame();
}
} else {
console.log("Move out of bounds: " + newX + ", " + newY);
}
} else {
console.log("Invalid key: " + key);
}
}
function stay() {
console.log("stay function");
document.getElementById("playButton").play();
MoveMissiles();
if (Alive) {
if (NumMissileLeft == 0) {
StartLevel();
} else {
DisplayBoard();
}
} else {
EndOfGame();
}
}
function jump() {
console.log("jump function");
document.getElementById("playJump").play();
Board[tankPlace.x][tankPlace.y] = EMPTY;
do {
tankPlace.x = Math.floor(Math.random() * ROWS);
tankPlace.y = Math.floor(Math.random() * COLS);
} while (Board[tankPlace.x][tankPlace.y] != EMPTY);
console.log("jump to: " + tankPlace.x + "," + tankPlace.y);
Board[tankPlace.x][tankPlace.y] = TANK;
MoveMissiles();
if (Alive) {
if (NumMissileLeft == 0) {
StartLevel();
} else {
DisplayBoard();
}
} else {
EndOfGame();
}
}
function blow() {
console.log("blow function");
if (BlowActive) {
document.getElementById("playBlow").play();
BlowActive = false;
for (var i = -1; i <= 1; i++) {
for (var j = -1; j <= 1; j++) {
if ((i != 0) || (j != 0)) { // not center
// console.log(i + "," + j);
if (((tankPlace.x + i) >= 0) && ((tankPlace.x + i) <= ROWS - 1) && ((tankPlace.y + j) >= 0) && ((tankPlace.y + j) <= COLS - 1)) {
if (Board[tankPlace.x + i][tankPlace.y + j] == MISSILE) {
Board[tankPlace.x + i][tankPlace.y + j] = EMPTY;
Score = Score + 1;
NumMissileLeft = NumMissileLeft - 1;
if (NumMissileLeft == 0) {
StartLevel();
}
}
}
}
}
}
scoreId.innerHTML = Score;
DisplayBoard();
}
}
function TablePrint(t) {
var line;
for (var i = 0; i < ROWS; i++) {
line = (i % 10) + " ";
for (var j = 0; j < COLS; j++) {
switch (t[i][j]) {
case EMPTY:
line = line + ".";
break;
case TANK:
line = line + "T";
break;
case MISSILE:
line = line + "M";
break;
case EXPLOSION:
line = line + "E";
break;
}
}
console.log(line);
}
}
function unbindKey() {
document.onkeydown = function() {};
}
function bindKey() {
document.onkeydown = function(event) {
console.log("onkeydown function: " + event.key);
switch (event.key) {
case '7': case 'Home':
move(1);
break;
case '8': case 'ArrowUp':
move(2);
break;
case '9': case 'PageUp':
move(3);
break;
case '4': case 'ArrowLeft':
move(4);
break;
case '5': case 'Clear':
stay();
break;
case '6': case 'ArrowRight':
move(6);
break;
case '1': case 'End':
move(7);
break;
case '2': case 'ArrowDown':
move(8);
break;
case '3': case 'PageDown':
move(9);
break;
case '0': case 'Insert':
blow();
break;
case '.': case 'Delete':
jump();
break;
}
};
}