Challenge: Prototype Pattern
In this challenge, you have to implement the prototype pattern to solve the given problem.
We'll cover the following
Problem statement
In this challenge, you have to implement a Ninja fighting game using the prototype pattern.
You have already been given the Ninja
constructor function, which will be used to create a ninja object. A ninja should have the following properties:
-
name
-
points
The points
are set to 100
by default upon the instantiation of a Ninja
object.
A ninja can pull the following moves in a fight:
-
punch
: ninjaspoints
reduce by20
if they get a punch -
kick
: ninjaspoints
reduce by50
if they get a kick
A ninja can only kick
or punch
the other ninja if the following conditions are met:
-
The other ninja’s
points
are greater than0
. -
The
points
of the ninja itself are also greater than0
.
If the above two conditions are met, you should return the points
of the other ninja as follows.
`{other ninja's "name"} points are {other ninja's "points"}`
If the conditions are not met, you should return the following if a punch was attempted:
`Can't punch {other ninja's name}`
And the following if a kick was attempted:
`Can't kick {other ninja's name}`
Input
Two ninjas fighting
Output
The points of the ninja after being hit or the “Can’t kick/punch” message
Sample input
var ninja1 = new Ninja('Ninja1')
var ninja2 = new Ninja('Ninja2')
ninja1.kick(ninja2)
ninja2.punch(ninja1)
ninja1.kick(ninja2)
ninja1.punch(ninja2)
ninja2.kick(ninja1)
Sample output
"Ninja2's points are 50"
"Ninja1's points are 80"
"Ninja2's points are 0"
"Can't punch Ninja2"
"Can't kick Ninja1"
Challenge #
Take a close look and design a step-by-step solution before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided. Good Luck!
//Define the Ninja class and write rest of the code hereconst Ninja = function() {}
Let’s discuss the solution in the next lesson.