Calculate combat damage
A simple combat system subtracts the defender's rating from the attacker's, but damage never drops below zero.
- Damage equals attack minus defense.
- A negative result is clamped to zero.
CombatRound(attack: int, defense: int) → int
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int CombatRound(int attack, int defense) {
}
Worked examples
| Call | Result |
|---|---|
CombatRound(10, 5) | 5 |
CombatRound(3, 7) | 0 |
CombatRound(0, 0) | 0 |
CombatRound(100, 100) | 0 |
Hint
Subtract, then clamp.
Reference solution in C#
public int CombatRound(int attack, int defense) {
return Math.Max(0, attack - defense);
}