Introduce Critical Hit Damage
00:00 The next stretch goal reads to introduce double-damage critical hits that occur when an attack value is a multiple of three. That’s a little throwback to the previous challenge where you had to calculate the factors of a number.
00:16 Maybe this already gives you a hint of how you can solve this. You probably already did solve it, but let’s see how I’m going to go ahead and do that. We want to mess with the damage, right, and give a chance for both the user, the player, as well as the monster to do double damage. Actually, one thing I want to do in here, just to make it a little bit more readable for myself, I’m going to say that here starts the player’s turn, and then down here starts the monster’s turn.
00:47 So both for the player and the monster, I want to introduce this critical hit chance. It’s two places where I have to do something, and that can be okay, but generally it’s a good idea to generalize this into a function.
01:44 So, now I can use this function to figure out whether any number is a factor of three. Is that really enough for me? No, I think I want another function because I’m still calculating the damage twice at the moment. I’m doing it here, and I’m doing it at the monster’s turn, line 58 currently.
So now I’ve added two functions. One I’m going to use to calculate the damage that the player or the monster attack, and in there I use another helper function that I called
is_factor_of_three() that figures out whether a number is divisible by three without a remainder, and if it is, then it’s going to be double damage. Okay, now I just need to replace where I calculate the damage here.
"monster" as an input string here. In that case, this will go to the
else statement. You could use an
elif statement, but it’s nice to catch, maybe you wanna add other attackers in this game, and then you could expand this
calculate_damage_dealt_by(). All right, that looks nice, but does it work? Let’s give it a go.
And let’s try this once more. F5. Attack. All right, there you go. The
Monster scored a critical hit of 36 damage. Ay ay ay, I’ll try again. You attacked the monster. Oh no, they have another critical hit.
I’ve totally got to heal. Heal, heal, and then I’ll try again.
17 … Oh no, the monster scored another critical hit of
30, and then I was defeated by the monster. All right, so this is working.
06:42 I never actually got a critical hit, but that was just bad luck in that case. Both of our damage gets calculated the same way. So if mine was multiple of three, then I would also get double damage.
06:55 Great. Also, I made this a bit more generalized because now I created these two functions that I’m using in the two respective places where I need to calculate any sort of damage, and this makes it easier to extend.
So if I wanted to have another player or another monster, I could just add that in and hook them up with the
calculate_damage_dealt_by() function, which itself uses the
is_factor_of_three() function to figure out whether they should multiply
damage or not. All right, I’m pretty happy with that.
Become a Member to join the conversation.