Strength Reduction in a Combat System
Imagine you're building a simple combat system for a game. Characters attack each other, and the damage dealt is based on their strength. However, a defender's armor reduces the incoming damage. This challenge asks you to implement a function that calculates the final damage dealt after armor reduction.
Problem Description
You need to write a Go function called CalculateDamage that takes the attacker's strength, the defender's armor, and a damage multiplier as input. The function should calculate the damage dealt after applying the armor reduction. The armor reduces damage linearly; for every point of armor, the damage taken is reduced by a fixed percentage (defined by the damage multiplier).
What needs to be achieved:
- Calculate the initial damage by multiplying the attacker's strength by the damage multiplier.
- Reduce the initial damage based on the defender's armor. The reduction is calculated as
armor * damageMultiplier. - Ensure the final damage is never negative. If the armor reduction exceeds the initial damage, the final damage should be 0.
Key Requirements:
- The function must accept three integer arguments:
attackerStrength,defenderArmor, anddamageMultiplier. - The function must return an integer representing the final damage dealt.
- The function must handle cases where the armor reduces the damage to zero or below.
Expected Behavior:
The function should accurately calculate the damage reduction based on the provided inputs and return the final, non-negative damage value.
Edge Cases to Consider:
defenderArmoris 0: No damage reduction should occur.attackerStrengthis 0: No damage should be dealt, regardless of armor.damageMultiplieris 0: No damage should be dealt, regardless of strength or armor.defenderArmoris greater thanattackerStrength * damageMultiplier: The final damage should be 0.- Negative inputs: While not explicitly required, consider how your function behaves with negative inputs (e.g., negative strength or armor). The problem description implies positive values, but robustness is always a plus.
Examples
Example 1:
Input: attackerStrength = 10, defenderArmor = 5, damageMultiplier = 2
Output: 10
Explanation: Initial damage = 10 * 2 = 20. Damage reduction = 5 * 2 = 10. Final damage = 20 - 10 = 10.
Example 2:
Input: attackerStrength = 5, defenderArmor = 10, damageMultiplier = 1
Output: 0
Explanation: Initial damage = 5 * 1 = 5. Damage reduction = 10 * 1 = 10. Final damage = 5 - 10 = -5. Since damage cannot be negative, the final damage is 0.
Example 3:
Input: attackerStrength = 20, defenderArmor = 2, damageMultiplier = 0.5
Output: 9
Explanation: Initial damage = 20 * 0.5 = 10. Damage reduction = 2 * 0.5 = 1. Final damage = 10 - 1 = 9.
Constraints
attackerStrength: 0 <=attackerStrength<= 1000defenderArmor: 0 <=defenderArmor<= 100damageMultiplier: 0.1 <=damageMultiplier<= 2.0- The function must return an integer.
- The function should execute in O(1) time complexity.
Notes
Think about the order of operations. It's important to calculate the initial damage before applying the armor reduction. Consider using math.Max to ensure the final damage is never negative. The damageMultiplier can be a float, but the final result must be an integer. You may need to use math.Floor or math.Trunc to convert the float result to an integer.