Go ahead and open up the “Fighter” class. It should have the code as shown here. Remember, there will be a few errors when we start. They should all disappear once we finish adding our code.
Right now the “Fighter” class has two existing functions, along with a few imports. Remember, don’t modify this code, but you are welcome to take a look at it! There is a “tryRandomAttack” function that is used to pick and use a random attack. There is also a “showAttackList” function used to print out a list of the Fighter’s attacks.
Next, we’re going to create some class variables for our “Fighter.” However, before we do that, try and think about what these variables might be. What data do you think we need to know/save about our “Fighter?” Think of the game itself, a turn-based RPG combat game, what information would be needed for characters in this type of game?
Next, add the code as shown to create four class variables for our “Fighter.”
The “health” variable will be used to keep track of how much health a Fighter has. The “name” variable will be used to store the name of the Fighter. The “attackList” variable will be used to save a list of all of the Attacks that the Fighter can use. The “lastDamageTaken” variable will be used to keep track of the last amount of damage that the Fighter took.
Next, complete the code as shown to create the constructor for the “Fighter” class.
Just like with the “Attack” class, we can see the constructor being used for the initial setup of the class variables. However, unlike the “Attack” class, we don’t use the keyword this. This is because the name of our parameter variables are different from the names of the class variables; so we don’t really need it.
Notice how our “Fighter” class variables are protected. Because of this, we need to create some getter functions to provide access to viewing some of our Fighter’s data.
Add the code as shown to complete all the getter functions needed for the “Fighter” class. These all just return the value of one of the variables, except for “getNumAttacks”. The “getNumAttacks” function returns how many Attacks are currently inside the Fighter’s “attackList” variable.
Complete the code as shown to create the “takeDamage” function, this will allow our Fighter to take damage.
First, notice that we have one parameter called “damage.” This will be how much damage we want the Fighter to take.
Also notice that the return type of the function is a boolean. A boolean can only have two values, true or false. For this function, we’ll be returning true if the Fighter still has health after taking damage, false if they don’t.
Inside the function, the first thing we do is we subtract the damage from our Fighter’s health. We then save the damage into the “lastDamageTaken” variable.
Then, we check if our health is now less or equal to 0. If it is, we set it to zero (so we don’t ever have a negative health) and return false to signify that the Fighter is out of health.
If our health is greater than 0, then our Fighter still has health and we return true.
Complete the code as shown to create the “tryAttack” function. This will allow our Fighter to attack another Fighter!
First let’s take a quick look at the return type of this function. It is an “Attack,” meaning we will return data about an Attack once we are done.
Next let’s look at the parameters for this function. We have two, a Fighter and an int. The Fighter is the Fighter that we are trying to attack, and the int is the number for the attack we are trying to use (for example, if 1, we are trying to use our Fighters 1st attack in its list).
We then have an if statement to check if a valid attackNum was used. When using lists, they start counting at 0. In the game, the attacks start at 1. So our first check “attackNum – 1 < attackList.size()" is making sure we have picked a number from 0 to however many attacks we have. The second check "attackNum > 0″ is making sure we weren’t given a negative number.
Once we’ve made sure our number is valid we get the attack from our list on line 58. We then tell the Fighter we are attacking to “takeDamage” based on how much damage to the attack has (using the Attack’s “getDamage” function).
After attacking the Fighter, we return information on the attack that was used.
However, if we weren’t able to attack because an invalid number was given, we return null. Null is a special type that means “nothing” and a common way for programmers to show that an error has occurred.
With this completed, our “Fighter” class is all done! Next, let’s work on creating some battles.