-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript3.js
74 lines (65 loc) · 1.75 KB
/
script3.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
class Ship {
constructor(name, hull, firepower, accuracy) {
this.name = name;
this.hull = hull;
this.firepower = firepower;
this.accuracy = accuracy;
this.isAlive = true;
}
attack(target) {
target.hull = target.hull - this.firepower;
if (target.hull <= 0) {
target.isAlive = false;
console.log(`You have destroyed ${target.name}!`);
} else {
target.isAlive = true;
console.log(
`${target.name} has survived with a remaining hull of ${target.hull}. Prepare for revenge attack!`
);
target.attack(this); //If the ship survives, it attacks you
}
}
}
const ussHelloWorld = new Ship("USS HelloWorld", 20, 5, 0.7);
//console.log(ussHelloWorld);
const names = [
"InterGalactic",
"AlieNaut",
"PlanetHopper",
"SpaceCase",
"CosmicJungle",
"EarthShatterer",
];
const alienShipsArray = [];
for (let i = 0; i < names.length; i++) {
alienShipsArray.push(
new Ship(
names[i],
Math.floor(Math.random() * 4) + 3,
Math.floor(Math.random() * 3) + 2,
Math.floor(Math.random() * 3 + 6) / 10
)
);
}
//console.log(alienShipsArray);
ussHelloWorld.attack(alienShipsArray[i]);
const
let i = 0;
alienShipsArray.forEach(Ship);
if (ussHelloWorld.hull <= 0) {
console.log(
`Sorry Earthlings, but your hull has reached ${ussHelloWorld.hull} and your Game is Over.`
);
}
if (alienShipsArray[i].hull <= 0) {
console.log(
`Sorry Aliens, but your hull has reached ${alienShipsArray[i].hull} and your Game is Over.`
);
}
const battle = function (humans, aliens) {
aliens.attack(humans);
if (humans.hull > 0) {
humans.attack(aliens);
}
};
battle(ussHelloWorld, alienShipsArray[i]);