-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragonslayer.js
152 lines (134 loc) · 4.33 KB
/
dragonslayer.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
/*heroes and dragons
Fight a dragon; you will mostly lose
*/
var hero = {
firstName: "Hero",
hp: 10,
wins: 0,
deaths: 0
};
// invoke to create a dragon
function dragon(name, colour, hp) {
this.firstName = name;
this.colour = colour;
this.hp = hp;
this.defending = false;
}
// invoke each time the hero dies, until they win or quit
function heroDeath() {
hero.deaths++;
console.log("YOU DIED");
var keepPlaying = window.confirm("Resurrect?");
if (keepPlaying) {
hero.hp = 10;
gameInPlay = true;
}
else {
combat = false;
}
}
function randomDamage(modifier, base) {
return Math.floor(Math.random()*modifier + base);
}
// higher modifiers increase the chance of success
function gameOfChance(modifier) {
return Math.floor(Math.random()*modifier);
}
// fill an array with dragons, Faerun seems to be full of them...
var dragonList = [
new dragon("Gnawbone", "black", 15),
new dragon("Iceclaws", "white", 20),
new dragon("Venomfang", "green", 25),
new dragon("Stormwing", "blue", 30),
new dragon("Inferno", "red", 35)
];
// the game begins!
confirm("Heroes and Dragons!");
hero.firstName = prompt("What is your name?");
// randomly select a dragon from the dragonList, and notify the player
var dragon = dragonList[Math.floor(Math.random() * dragonList.length)];
console.log(hero.firstName + " is now fighting a " + dragon.colour + " dragon!");
// combat begins
var combat = true;
var totalDamage = 0;
while (combat) {
// hero action
var heroAction = prompt("attack, defend or flee?");
switch (heroAction) {
case 'attack':
console.log("You attack the dragon!");
var checkHit = gameOfChance(4); // approx 1/4 chance of missing
if (checkHit) {
var damageThisRound = randomDamage(10, 1);
console.log("The attack does " + damageThisRound + " damage!");
totalDamage += damageThisRound;
if (totalDamage >= dragon.hp) {
console.log("You killed the dragon!");
hero.wins++;
combat = false;
}
} else {
console.log("The attack missed...");
}
break;
case 'defend':
console.log("You raise your shield and ready yourself...");
hero.defending = true;
break;
case 'flee':
console.log("You attempt to run away!");
var runSuccess = gameOfChance(2); // approx 1/2 chance of failure
if (runSuccess) {
console.log("You escape safely!");
combat = false;
} else {
console.log("You are too slow; the dragon eats you.");
heroDeath();
}
break;
default:
console.log("You hesitate; the dragon eats you.");
heroDeath();
} // end of heroAction
// add dragon turn
var dragonAction = gameOfChance(10);
if (dragonAction >7) {
console.log("The dragon spits " + dragon.colour + " flames at you...");
if (hero.defending) {
console.log("You evade the attack!");
hero.defending = false;
}
else {
var damageThisRound = randomDamage(10, 10);
console.log("The attack does " + damageThisRound + " damage!");
hero.hp -= damageThisRound;
if (hero.hp < 1) {
heroDeath();
}
}
}
else if (dragonAction <5) {
console.log("The dragon strikes with it's claws!");
var checkHit = gameOfChance(4); // approx 1/4 chance of missing
if (checkHit && hero.defending) {
console.log("You evade the attack!");
hero.defending = false;
}
else if (checkHit) {
var damageThisRound = randomDamage(8, 2);
console.log("The attack does " + damageThisRound + " damage!");
hero.hp -= damageThisRound;
if (hero.hp < 1) {
heroDeath();
}
}
else {
console.log("The attack missed...");
}
}
else {
console.log("The dragon hisses and moves to flank you...");
}
// end of dragonAction
}
// add a switch for win lose draw?