Drill

ProblemsGo › games

Calculate combat damage

easygamesMathGo

A simple combat system subtracts the defender's rating from the attacker's, but damage never drops below zero.

combatRound(attack: int, defense: int) → int

Go 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.

Solve it in Python →

Where you start

func combatRound(attack int, defense int) int {
	
}

Worked examples

CallResult
combatRound(10, 5)5
combatRound(3, 7)0
combatRound(0, 0)0
combatRound(100, 100)0

Hint

Subtract, then clamp.

Reference solution in Go
func combatRound(attack int, defense int) int {
	diff := attack - defense
	if diff < 0 {
		return 0
	}
	return diff
}

The same problem in another language

More games problems in Go