-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
380 lines (353 loc) · 10.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
// ship creation
let ship = {
name: "USS Assembly",
hull: 20,
firepower: 5,
accuracy: 0.7,
missiles: 3,
// shield functionality
shield: Math.floor(Math.random() * 5) + 6,
};
// alienFleet creation
const alienFleet = {
aliens: [],
createAlien: function () {
const newAlien = new Alien();
this.aliens.push(newAlien);
},
createMega: function () {
const newMega = new Mega();
newMega.createPod();
newMega.createPod();
this.aliens.push(newMega);
},
};
// Alien class
class Alien {
constructor() {
this.name = `Alien${Alien.num++}`;
this.hull = Math.floor(Math.random() * 4) + 3;
this.firepower = Math.floor(Math.random() * 3) + 2;
this.accuracy = Math.random() * 0.2 + 0.6;
this.points = 50;
}
static num = 1;
}
// Mega class
class Mega {
constructor() {
this.name = "Mega-ship";
this.pods = [];
this.hull = Math.floor(Math.random() * 3) + 8;
this.firepower = Math.floor(Math.random() * 6) + 4;
this.accuracy = Math.random() * 0.2 + 0.6;
this.points = 200;
}
createPod() {
const newPod = new Pod();
this.pods.push(newPod);
}
}
// Pod class
class Pod {
constructor() {
this.name = `Pod${Pod.num++}`;
this.hull = 4;
// this.points = 25;
}
static num = 1;
}
// random array generator
function getRandom(arr, n) {
let result = new Array(n),
len = arr.length,
taken = new Array(len);
while (n--) {
let x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
}
return result;
}
let score = 0;
let round = 1;
let powerupRank = 0;
playGame();
// round functionality
function playRound() {
// random # of attackers generator
alienFleet.aliens = [];
let numOfAttackers = Math.floor(Math.random() * 4) + 3;
for (let i = 1; i <= numOfAttackers; i++) {
alienFleet.createAlien();
}
// add Mega-ship to fleet
alienFleet.createMega();
enemiesStronger();
console.log(
`There are ${
numOfAttackers + 1
} alien ships attacking Earth! Take them down!`
);
while (ship.hull > 0) {
// targeting functionality
let str = `There are ${alienFleet.aliens.length} alien ships left. Pick a ship number to attack:
`;
for (let i = 0; i < alienFleet.aliens.length; i++) {
str = str + (i + 1) + `. ` + alienFleet.aliens[i].name + ` `;
if (i == 6) {
str =
`${str}` +
`
`;
}
}
let targetNum = window.prompt(str);
let target;
if (targetNum < 1 || targetNum > alienFleet.aliens.length) {
console.log(`Invalid target. Shutting ship down.`);
break;
} else {
target = alienFleet.aliens[targetNum - 1];
}
// missile functionality
if (ship.missiles > 0) {
const fireMiss = window.prompt(
`You have ${ship.missiles} missiles. Would you like to fire one? Y/N?`
);
if (fireMiss.toLowerCase() == "yes" || fireMiss.toLowerCase() == "y") {
ship.missiles--;
console.log(
`Missile fired. ${target.name} destroyed. You have ${ship.missiles} missiles left.`
);
score += target.points;
alienFleet.aliens.splice(targetNum - 1, 1);
shipPowerup();
if (!alienFleet.aliens[0]) {
shipStronger();
recharge();
break;
}
continue;
} else if (
fireMiss.toLowerCase() == "no" ||
fireMiss.toLowerCase() == "n"
) {
undefined;
} else {
console.log(`Invalid response. Shutting ship down.`);
break;
}
}
// ship attack turn
console.log(`You fire your lasers with ${ship.firepower} firepower.`);
if (ship.accuracy >= Math.random()) {
if (target instanceof Mega && target.pods[0] != undefined) {
target = target.pods[0];
}
target.hull -= ship.firepower;
if (target.hull <= 0) {
console.log(`${target.name} was hit and has been destroyed.`);
score += target.points;
shipPowerup();
if (target instanceof Alien || target instanceof Mega) {
alienFleet.aliens.splice(targetNum - 1, 1);
} else if (target instanceof Pod) {
alienFleet.aliens[targetNum - 1].pods.shift();
}
if (!alienFleet.aliens[0]) {
shipStronger();
recharge();
break;
}
const prompt = window.prompt(
"Enter retreat or r to retreat or enter attack or a to attack another ship."
);
if (prompt.toLowerCase() == "retreat" || prompt.toLowerCase() == "r") {
console.log("You retreat to fight another day.");
console.log(`You scored ${score} points. Great job!`);
medal();
break;
} else if (
prompt.toLowerCase() == "attack" ||
prompt.toLowerCase() == "a"
) {
continue;
} else {
console.log("Invalid response. Shutting down ship.");
break;
}
} else if (target.hull > 0) {
console.log(
`${target.name} has been hit and has ${target.hull} hull remaining!`
);
}
} else {
console.log(`You missed!`);
}
// multiple attackers funtionality
let attackers = [];
const attackerNum = Math.random();
if (attackerNum >= 0.95) {
attackers = alienFleet.aliens;
console.log(`You're attacked by all alien ships at once.`);
} else if (attackerNum >= 0.8 && attackerNum < 0.95) {
let n = 3;
if (n > alienFleet.aliens.length) {
n = alienFleet.aliens.length;
}
attackers = getRandom(alienFleet.aliens, n);
let attackersNamesArr = [];
for (let attacker of attackers) {
// if (Object.prototype.hasOwnProperty.call(attacker, "name")) {
attackersNamesArr.push(attacker.name);
// }
}
console.log(`You're attacked by: ${attackersNamesArr.join(", ")}`);
} else if (attackerNum >= 0.5 && attackerNum < 0.8) {
let n = 2;
if (n > alienFleet.aliens.length) {
n = alienFleet.aliens.length;
}
attackers = getRandom(alienFleet.aliens, n);
let attackersNamesArr = [];
for (let attacker of attackers) {
// if (Object.prototype.hasOwnProperty.call(attacker, "name")) {
attackersNamesArr.push(attacker.name);
// }
}
console.log(`You're attacked by: ${attackersNamesArr.join(", ")}`);
} else {
attackers = [
alienFleet.aliens[Math.floor(Math.random() * alienFleet.aliens.length)],
];
console.log(`You're attacked by: ${attackers[0].name}`);
}
for (let attacker of attackers) {
if (ship.shield > 0) {
console.log(
`Your shields are holding and provide ${
ship.shield
} hull points. You now have ${ship.shield + ship.hull} hull points.`
);
} else {
ship.shield = 0;
console.log(
`Your shields have failed. You now have ${ship.hull} hull points.`
);
}
// alien attack turn
console.log(
`${attacker.name} fires its lasers with ${attacker.firepower} firepower.`
);
if (attacker.accuracy >= Math.random()) {
ship.hull -=
attacker.firepower - ship.shield > 0
? attacker.firepower - ship.shield
: 0;
if (ship.hull <= 0) {
console.log(`Your ship has been destroyed. Earth is doomed!`);
console.log(`You scored ${score} points. Great job!`);
medal();
break;
} else if (ship.hull > 0) {
console.log(
`Your hull was hit by ${
attacker.firepower - ship.shield > 0
? attacker.firepower - ship.shield
: 0
} damage and has ${ship.hull} hull remaining!`
);
}
ship.shield -= attacker.firepower;
ship.shield = ship.shield > 0 ? ship.shield : 0;
} else {
console.log(`${attacker.name} missed!`);
}
}
}
}
// start game functionality
function playGame() {
// reset score
score = 0;
// reset round
round = 1;
// reset ship stats
ship = {
name: "USS Assembly",
hull: 20,
firepower: 5,
accuracy: 0.7,
missiles: 3,
shield: Math.floor(Math.random() * 6) + 6,
};
playRound();
// replay functionality
let replay = window.prompt("Game over! Would you like to play again? Y/N?");
if (replay.toLowerCase() == "yes" || replay.toLowerCase() == "y") {
console.clear();
playGame();
} else {
return;
}
}
// recharge shields functionality
function recharge() {
let recharge = window.prompt(
`You currently have ${ship.shield} shield. Do you want to return to base to recharge them? Y/N?`
);
if (recharge.toLowerCase() == "y" || recharge.toLowerCase() == "yes") {
ship.shield = Math.floor(Math.random() * 5 + 6);
console.log(
`You return to base and recharge your shields to ${ship.shield}.`
);
} else if (recharge.toLowerCase() == "n" || recharge.toLowerCase() == "no") {
console.log("You bravely continue on without recharging shields.");
} else {
console.log("Invalid response. Shutting down ship.");
return;
}
console.log(
"All the alien ships have been destroyed but they called in reinforcements."
);
playRound();
}
// medal functionality
function medal() {
if (score >= 5000) {
console.log("You got a gold medal!");
} else if (score >= 2500) {
console.log("You got a silver medal!");
} else if (score >= 1000) {
console.log("You got a bronze medal!");
} else {
console.log("You didn't score high enough to get a medal.");
}
}
// stronger ship functionality
function shipStronger() {
ship.hull = Math.floor(ship.hull * 1.25);
ship.firepower = Math.floor(ship.firepower * 1.25);
}
// ship powerup functionality
function shipPowerup() {
while (score - (powerupRank + 1) * 1000 >= 1000) {
ship.hull = Math.floor(ship.hull * 2);
ship.firepower = Math.floor(ship.firepower * 2);
console.log(
`You scored enough points for a powerup! Your ship now has ${ship.hull} hull points and ${ship.firepower} firepower.`
);
powerupRank++;
}
}
// stronger enemies functionality
function enemiesStronger() {
for (let i = 1; i < round; i++) {
for (let alien of alienFleet.aliens) {
alien.hull = Math.floor(alien.hull * 1.5);
alien.firepower = Math.floor(alien.firepower * 1.5);
alien.points *= 2;
}
}
}