Skip to content

Commit 9ab9e21

Browse files
authored
Merge pull request #1 from PascalUlor/pascal-ulor
initial commit
2 parents 33d1564 + 8d53bef commit 9ab9e21

3 files changed

Lines changed: 356 additions & 73 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Pascal Ulor
2+
13
# JavaScript - III
24

35
This challenge focuses on using the `this` keyword as well as getting comfortable with prototypes by building out a fantasy themed video game.

‎assignments/prototypes.js‎

Lines changed: 268 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,69 @@
11
/*
2-
Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.
2+
Object oriented design is commonly used in video games.
3+
For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.
34
45
In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.
56
6-
At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions.
7+
At the bottom of this file are 3 objects that all end up inheriting from Humanoid.
8+
Use the objects at the bottom of the page to test your constructor functions.
79
810
Each constructor function has unique properties and methods that are defined in their block comments below:
911
*/
10-
12+
1113
/*
1214
=== GameObject ===
1315
* createdAt
1416
* name
1517
* dimensions (These represent the character's size in the video game)
1618
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
1719
*/
20+
function GameObject(attributes) {
21+
this.createdAt = attributes.createdAt;
22+
this.name = attributes.name;
23+
this.dimensions = attributes.dimensions;
24+
}
25+
26+
GameObject.prototype.destroy = function () {
27+
return `${this.name} was removed from the game.`;
28+
}
29+
30+
let newGame = new GameObject({
31+
createdAt: '8th May',
32+
name: 'pascal',
33+
dimensions: {
34+
length: 50,
35+
widht: 30,
36+
}
37+
38+
});
1839

1940
/*
2041
=== CharacterStats ===
2142
* healthPoints
2243
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
2344
* should inherit destroy() from GameObject's prototype
2445
*/
46+
function CharacterStats(ChAttr) {
47+
GameObject.call(this, ChAttr);
48+
this.healthPoints = ChAttr.healthPoints;
49+
}
50+
51+
CharacterStats.prototype = Object.create(GameObject.prototype);
52+
53+
CharacterStats.prototype.takeDamage = function () {
54+
return `${this.name} took damage.`;
55+
}
56+
57+
let newCha = new CharacterStats({
58+
createdAt: '8th May',
59+
name: 'pascal',
60+
dimensions: {
61+
length: 50,
62+
widht: 30,
63+
},
64+
healthPoints: '99'
65+
66+
});
2567

2668
/*
2769
=== Humanoid (Having an appearance or character resembling that of a human.) ===
@@ -32,7 +74,33 @@
3274
* should inherit destroy() from GameObject through CharacterStats
3375
* should inherit takeDamage() from CharacterStats
3476
*/
35-
77+
78+
function Humanoid(humanAttr) {
79+
CharacterStats.call(this, humanAttr);
80+
this.team = humanAttr.team;
81+
this.weapons = humanAttr.weapons;
82+
this.language = humanAttr.language;
83+
}
84+
85+
Humanoid.prototype = Object.create(CharacterStats.prototype);
86+
Humanoid.prototype.greet = function() {
87+
return `${this.name} offers a greeting in ${this.language}.`;
88+
}
89+
90+
const kent = new Humanoid({
91+
createdAt: '8th May',
92+
name: 'Superman',
93+
dimensions: {
94+
length: 50,
95+
widht: 30,
96+
},
97+
healthPoints: 1000,
98+
team: 'Justice league',
99+
weapons: 'heat-vision',
100+
language: 'kryptonia'
101+
102+
})
103+
36104
/*
37105
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
38106
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
@@ -41,70 +109,203 @@
41109

42110
// Test you work by un-commenting these 3 objects and the list of console logs below:
43111

44-
/*
45-
const mage = new Humanoid({
46-
createdAt: new Date(),
47-
dimensions: {
48-
length: 2,
49-
width: 1,
50-
height: 1,
51-
},
52-
healthPoints: 5,
53-
name: 'Bruce',
54-
team: 'Mage Guild',
55-
weapons: [
56-
'Staff of Shamalama',
57-
],
58-
language: 'Common Tongue',
59-
});
60-
61-
const swordsman = new Humanoid({
62-
createdAt: new Date(),
63-
dimensions: {
64-
length: 2,
65-
width: 2,
66-
height: 2,
67-
},
68-
healthPoints: 15,
69-
name: 'Sir Mustachio',
70-
team: 'The Round Table',
71-
weapons: [
72-
'Giant Sword',
73-
'Shield',
74-
],
75-
language: 'Common Tongue',
76-
});
77-
78-
const archer = new Humanoid({
79-
createdAt: new Date(),
80-
dimensions: {
81-
length: 1,
82-
width: 2,
83-
height: 4,
84-
},
85-
healthPoints: 10,
86-
name: 'Lilith',
87-
team: 'Forest Kingdom',
88-
weapons: [
89-
'Bow',
90-
'Dagger',
91-
],
92-
language: 'Elvish',
93-
});
94-
95-
console.log(mage.createdAt); // Today's date
96-
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
97-
console.log(swordsman.healthPoints); // 15
98-
console.log(mage.name); // Bruce
99-
console.log(swordsman.team); // The Round Table
100-
console.log(mage.weapons); // Staff of Shamalama
101-
console.log(archer.language); // Elvish
102-
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
103-
console.log(mage.takeDamage()); // Bruce took damage.
104-
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
105-
*/
112+
113+
const mage = new Humanoid({
114+
createdAt: new Date(),
115+
dimensions: {
116+
length: 2,
117+
width: 1,
118+
height: 1,
119+
},
120+
healthPoints: 5,
121+
name: 'Bruce',
122+
team: 'Mage Guild',
123+
weapons: [
124+
'Staff of Shamalama',
125+
],
126+
language: 'Common Tongue',
127+
});
128+
129+
const swordsman = new Humanoid({
130+
createdAt: new Date(),
131+
dimensions: {
132+
length: 2,
133+
width: 2,
134+
height: 2,
135+
},
136+
healthPoints: 15,
137+
name: 'Sir Mustachio',
138+
team: 'The Round Table',
139+
weapons: [
140+
'Giant Sword',
141+
'Shield',
142+
],
143+
language: 'Common Tongue',
144+
});
145+
146+
const archer = new Humanoid({
147+
createdAt: new Date(),
148+
dimensions: {
149+
length: 1,
150+
width: 2,
151+
height: 4,
152+
},
153+
healthPoints: 10,
154+
name: 'Lilith',
155+
team: 'Forest Kingdom',
156+
weapons: [
157+
'Bow',
158+
'Dagger',
159+
],
160+
language: 'Elvish',
161+
});
162+
163+
console.log(mage.createdAt); // Today's date
164+
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
165+
console.log(swordsman.healthPoints); // 15
166+
console.log(mage.name); // Bruce
167+
console.log(swordsman.team); // The Round Table
168+
console.log(mage.weapons); // Staff of Shamalama
169+
console.log(archer.language); // Elvish
170+
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
171+
console.log(mage.takeDamage()); // Bruce took damage.
172+
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
173+
106174

107175
// Stretch task:
108176
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
109177
// * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0;
110-
// * Create two new objects, one a villain and one a hero and fight it out with methods!
178+
// * Create two new objects, one a villain and one a hero and fight it out with methods!
179+
180+
/**
181+
* Hero and Villian Constructors
182+
*/
183+
function Hero(feat) {
184+
Humanoid.call(this, feat);
185+
this.powerStat = feat.powerStat;
186+
}
187+
188+
Hero.prototype = Object.create(Humanoid.prototype);
189+
190+
function Villain(feat) {
191+
Humanoid.call(this, feat);
192+
this.powerStat = feat.powerStat;
193+
}
194+
195+
Villain.prototype = Object.create(Humanoid.prototype);
196+
Humanoid.prototype.speak = function() {
197+
return `Hi I am ${this.name} the leader of ${this.team}.`;
198+
}
199+
Hero.prototype.attack = function(person) {
200+
if(this.powerStat.durability + this.powerStat.combat +this.powerStat.power + this.powerStat.strength > 30) {
201+
person.healthPoints -= 30;
202+
return `${person.name} health point is now ${person.healthPoints}`
203+
} else {
204+
person.healthPoints -= 10;
205+
return `${person.name} health point is now ${person.healthPoints}`
206+
}
207+
}
208+
209+
Villain.prototype.attack = function(person) {
210+
if(this.powerStat.durability + this.powerStat.combat +this.powerStat.power + this.powerStat.strength > 30) {
211+
person.healthPoints -= 30;
212+
return `${person.name} health point is now ${person.healthPoints}`
213+
} else {
214+
person.healthPoints -= 10;
215+
return `${person.name} health point is now ${person.healthPoints}`
216+
}
217+
}
218+
219+
const superman = new Hero({
220+
createdAt: new Date(),
221+
name: 'Superman',
222+
dimensions: {
223+
length: 50,
224+
widht: 30,
225+
},
226+
healthPoints: 100,
227+
team: 'Justice league',
228+
weapons: 'heat-vision',
229+
language: 'kryptonia',
230+
powerStat: {
231+
intelligence: 8,
232+
durability: 10,
233+
speed: 10,
234+
combat: 7,
235+
power: 10,
236+
strength: 10
237+
}
238+
});
239+
240+
const lex = new Villain({
241+
createdAt: new Date(),
242+
name: 'Lex Luthor',
243+
dimensions: {
244+
length: 50,
245+
widht: 30,
246+
},
247+
healthPoints: 20,
248+
team: 'Legion of doom',
249+
weapons: 'kryptonite',
250+
language: 'multi-lingual',
251+
powerStat: {
252+
intelligence: 10,
253+
durability: 3,
254+
speed: 1,
255+
combat: 2,
256+
power: 1,
257+
strength: 1
258+
}
259+
});
260+
261+
const darkside = new Villain({
262+
createdAt: new Date(),
263+
name: 'Darkside',
264+
dimensions: {
265+
length: 50,
266+
widht: 30,
267+
},
268+
healthPoints: 100,
269+
team: 'Legion of doom',
270+
weapons: 'Omega beam',
271+
language: 'multi-lingual',
272+
powerStat: {
273+
intelligence: 10,
274+
durability: 10,
275+
speed: 9,
276+
combat: 8,
277+
power: 10,
278+
strength: 10
279+
}
280+
});
281+
282+
function randomIndex(min, max) {
283+
return Math.floor(Math.random() * (max - min + 1) + min);
284+
}
285+
286+
// Starts a fight between two fighters
287+
function battle(fighter1, fighter2) {
288+
console.log(`Fight Started between ${fighter1.name} and ${fighter2.name}`);
289+
let winner = '';
290+
while (fighter1.healthPoints > 0 && fighter2.healthPoints > 0) {
291+
const index = randomIndex(1, 2);
292+
if (index == 1) {
293+
console.log(fighter2.takeDamage());
294+
console.log(
295+
`${fighter1.attack(fighter2)}`
296+
);
297+
} else {
298+
console.log(fighter1.takeDamage());
299+
console.log(
300+
`${fighter2.attack(fighter1)}`
301+
);
302+
}
303+
}
304+
305+
winner = fighter1.healthPoints >0? console.log(`Fight Ended, ${fighter1.name} is the new champion! ${fighter2.destroy()}`) :
306+
console.log(`Fight Ended, ${fighter2.name} is the new champion! ${fighter1.destroy()}`);
307+
return winner;
308+
}
309+
310+
// console.log(battle(superman, lex));
311+
console.log(battle(superman, darkside));

0 commit comments

Comments
 (0)