-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtextbased-role-playing-game.js
304 lines (300 loc) · 10.8 KB
/
textbased-role-playing-game.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
/**
* Created by esattahaibis on 2015-11-28.
*/
/* Create variables for game */
var flagReplay; /* flag for keeping replay data */
var flagPlayerDead; /* flag of player shows player is alive or dead */
var flagCthulhuDead; /* flag of cthulhu show cthulhu is alive or dead */
var playerHealth; /* variable for player's health */
var cthulhuHealth; /* variable for cthulhu's health */
var playerType; /* variable for player type ( Warrior or Mage ) */
var playerName; /* container that stores player name */
var attackType; /* container that stores which attack type player chose */
var damage; /* container that stores damage */
var logPlayerAttack; /* contains log of the attacks dealt to the cthulhu */
var logCthulhuAttack; /* contains log of the attacks dealt to the player */
/**
* Returns the damage of Warrior attack 1
* @returns {Number} damage dealt by attack1
*/
function WarriorAttackOne() {
var attackChance;
var attackDamage = 0;
attackChance = Math.floor(Math.random() * 100);
if (attackChance < 30) {
/* do lover damage */
attackDamage = 5;
}else if(attackChance >= 30 && attackChance < 70) {
/* do medium damage */
attackDamage = 10;
} else {
/* do high damage */
attackDamage = 20;
}
return attackDamage;
}
/**
* Returns the damage of Warrior attack 2
* @returns {Number} damage dealt by attack2
*/
function WarriorAttackTwo() {
var attackChance;
var attackDamage = 0;
attackChance = Math.floor(Math.random() * 100);
if (attackChance < 30) {
/* do lover damage */
attackDamage = 3;
}else if(attackChance >= 30 && attackChance < 70) {
/* do medium damage */
attackDamage = 20;
} else {
/* do high damage */
attackDamage = 25;
}
return attackDamage;
}
/**
* Returns the damage of Warrior attack 3
* @returns {Number} damage dealt by attack3
*/
function WarriorAttackThree() {
var attackChance;
var attackDamage = 0;
attackChance = Math.floor(Math.random() * 100);
if (attackChance < 30) {
/* do lover damage */
attackDamage = 5;
}else if(attackChance >= 30 && attackChance < 70) {
/* do medium damage */
attackDamage = 10;
} else {
/* do high damage */
attackDamage = 35;
}
return attackDamage;
}
/**
* Returns the damage of Mage attack 1
* @returns {Number} damage dealt by attack1
*/
function MageAttackOne() {
var attackChance;
var attackDamage = 0;
attackChance = Math.floor(Math.random() * 100);
if (attackChance < 30) {
/* do lover damage */
attackDamage = 5;
}else if(attackChance >= 30 && attackChance < 70) {
/* do medium damage */
attackDamage = 10;
} else {
/* do high damage */
attackDamage = 20;
}
return attackDamage;
}
/**
* Returns the damage of Mage attack 2
* @returns {Number} damage dealt by attack2
*/
function MageAttackTwo() {
var attackChance;
var attackDamage = 0;
attackChance = Math.floor(Math.random() * 100);
if (attackChance < 30) {
/* do lover damage */
attackDamage = 3;
}else if(attackChance >= 30 && attackChance < 70) {
/* do medium damage */
attackDamage = 20;
} else {
/* do high damage */
attackDamage = 25;
}
return attackDamage;
}
/**
* Returns the damage of Mage attack 3
* @returns {Number} damage dealt by attack3
*/
function MageAttackThree() {
var attackChance;
var attackDamage = 0;
attackChance = Math.floor(Math.random() * 100);
if (attackChance < 30) {
/* do lover damage */
attackDamage = 5;
}else if(attackChance >= 30 && attackChance < 70) {
/* do medium damage */
attackDamage = 10;
} else {
/* do high damage */
attackDamage = 35;
}
return attackDamage;
}
/**
* Returns the damage of cthulhu
* @returns {Number} damage dealt by Cthulhu
*/
function CthulhuAttack() {
var attackChance;
var attackDamage = 0;
attackChance = Math.floor(Math.random() * 100);
if ( attackChance < 30 ) {
/* do low damage */
attackDamage = 10;
} else if (attackChance >= 30 && attackChance < 70) {
/* do medium damage */
attackDamage = 15;
} else if (attackChance >= 70 && attackChance < 90) {
/* do high damage */
attackDamage = 20;
} else {
/* kill instantly */
attackDamage = 100;
}
return attackDamage;
}
do {
/* Initialize variables with their default values*/
flagReplay = false;
flagPlayerDead = false;
flagCthulhuDead = false;
playerHealth = 100;
cthulhuHealth = 100;
logCthulhuAttack = [];
logPlayerAttack = [];
damage = 0;
/*------------------------------------------------------------------------------*/
alert("Welcome to Clash of Cthulhu.");
playerName = prompt("Before anything else please type your name:","Player One");
playerType = prompt("Please Choose your class (Warrior / Mage):").toLowerCase();
while ( playerType != "warrior" && playerType != "mage") {
playerType = prompt("Unknown Class. Please choose again (Warrior / Mage):").toLowerCase();
}
/* Path for warrior */
if (playerType == "warrior") {
alert("Welcome hero to the world of Mythos where Cthulhu has risen from his deathlike sleep to wreak havoc upon the world. It is up to you to stop him and return this world to peace. He has currently taken up residence in a cave up in the mountains, go there now and confront him to end the worlds suffering! \n After many days you have reached the summit of the mountain. What lies before you is a gargantuan cave entrance littered with cracked scales that appear to have been there for eons. Once inside the cave you notice a shadowy figure near the back of the cave sitting on throne. As you approach it dawns upon you just how far away you were from it as the figure just keeps getting bigger and bigger the closer you get. Now that you are close enough to the beast you realize he is almost 200 meters tall sitting down! As you realize this fear creeps into your thoughts as this is the foe you have come to try and vanquish to save the world. \n READY YOURSELVES FOR YOU ARE ABOUT TO FIGHT THE LEGENDARY CTHULHU!");
do {
attackType = Number(prompt(playerName + " attacks! \nChoose an attack type: \n1-Slice and Dice\n2-Overhead Smash\n3-Horizontal Slash :"));
while (attackType != 1 && attackType != 2 && attackType != 3) {
attackType = Number( prompt("Unknown attack type. Please choose valid attack type \n1-Slice and Dice\n2-Overhead Smash\n3-Horizontal Slash :") );
}
switch ( attackType ) {
case 1:
damage = WarriorAttackOne();
cthulhuHealth -= damage;
/* control Cthulhu is alive if it's dead set the flag to false */
if (cthulhuHealth <= 0) {
flagCthulhuDead = true;
}
break;
case 2:
damage = WarriorAttackTwo();
cthulhuHealth -= damage;
/* control Cthulhu is alive if it's dead set the flag to false */
if (cthulhuHealth <= 0) {
flagCthulhuDead = true;
}
break;
case 3:
damage = WarriorAttackThree();
cthulhuHealth -= damage;
/* control Cthulhu is alive if it's dead set the flag to false */
if (cthulhuHealth <= 0) {
flagCthulhuDead = true;
}
break;
}
logPlayerAttack.push(damage);
if (flagCthulhuDead == true) {
alert("You Defeated Cthulhu. You glorious bastard.");
break;
} else {
alert("You dealt " + damage + " damage to Cthulhu. Cthulhu's remaining health is " + cthulhuHealth + ".");
}
/* Cthulhu's turn to attack */
damage = CthulhuAttack();
logCthulhuAttack.push(damage);
if (damage == 100) {
alert("Cthulhu killed you with one shot.");
flagPlayerDead = true;
} else {
playerHealth -= damage;
if (playerHealth <= 0) {
alert("Cthulhu dealt " + damage + " damage. You are dead");
flagPlayerDead = true;
}
else {
alert("Cthulhu dealt " + damage + " damage. Your remaining health is " + playerHealth + ".");
}
}
}while (flagPlayerDead != true);
}
/* Path for Mage */
else {
alert("Welcome hero to the world of Mythos where Cthulhu has risen from his deathlike sleep to wreak havoc upon the world. It is up to you to stop him and return this world to peace. He has currently taken up residence in a cave up in the mountains, go there now and confront him to end the worlds suffering! \n After many days you have reached the summit of the mountain. What lies before you is a gargantuan cave entrance littered with cracked scales that appear to have been there for eons. Once inside the cave you notice a shadowy figure near the back of the cave sitting on throne. As you approach it dawns upon you just how far away you were from it as the figure just keeps getting bigger and bigger the closer you get. Now that you are close enough to the beast you realize he is almost 200 meters tall sitting down! As you realize this fear creeps into your thoughts as this is the foe you have come to try and vanquish to save the world. \n READY YOURSELVES FOR YOU ARE ABOUT TO FIGHT THE LEGENDARY CTHULHU!");
do {
attackType = Number(prompt(playerName + " attacks! \nChoose an attack type: \n1-Lightning Bolt\n2-Fireball\n3-Blizzard :"));
while (attackType != 1 && attackType != 2 && attackType != 3) {
attackType = Number( prompt("Unknown attack type. Please choose valid attack type \n1-Lightning Bolt\n2-Fireball\n3-Blizzard :") );
}
switch ( attackType ) {
case 1:
damage = MageAttackOne();
cthulhuHealth -= damage;
/* control Cthulhu is alive if it's dead set the flag to true */
if (cthulhuHealth <= 0) {
flagCthulhuDead = true;
}
break;
case 2:
damage = MageAttackTwo();
cthulhuHealth -= damage;
/* control Cthulhu is alive if it's dead set the flag to true */
if (cthulhuHealth <= 0) {
flagCthulhuDead = true;
}
break;
case 3:
damage = MageAttackThree();
cthulhuHealth -= damage;
/* control Cthulhu is alive if it's dead set the flag to true */
if (cthulhuHealth <= 0) {
flagCthulhuDead = true;
}
break;
}
logPlayerAttack.push(damage);
/* Control Cthulhu is dead if it's dead finish the loop*/
if (flagCthulhuDead == true) {
alert("You Defeated Cthulhu. You glorious bastard.");
break;
} else {
alert("You dealt " + damage + " damage to Cthulhu. Cthulhu's remaining health is " + cthulhuHealth + ".");
}
/*-------------------------------------------------------*/
/* Cthulhu's turn to attack */
damage = CthulhuAttack();
logCthulhuAttack.push(damage);
if (damage == 100) {
alert("Cthulhu killed you with one shot.");
flagPlayerDead = true;
} else {
playerHealth -= damage;
if (playerHealth <= 0) {
alert("Cthulhu dealt " + damage + " damage. You are dead");
flagPlayerDead = true;
}
else {
alert("Cthulhu dealt " + damage + " damage. Your remaining health is " + playerHealth + ".");
}
}
}while (flagPlayerDead != true);
}
/* Confirm player wants to play again or not */
console.log(logCthulhuAttack, logPlayerAttack);
flagReplay = window.confirm("Do you wish to play again?");
}while (flagReplay == true);
alert("Thanks for playing Clash of Cthulhu. See you later.");