lives represents health points attack function is the code that my characters can loses health points heal function is the code that my characters can gains health points You can write something like this:
		func heal(anotherCharacter: Character){
				anotherCharacter.lives += 20
				if anotherCharacter.lives > 100 {
						anotherCharacter.lives = 100
				}
		}
		func attack(otherCharacter: Character){
				otherCharacter.lives -= power
				if otherCharacter.lives < 0 {
						otherCharacter.lives = 0
				}
				//If `power` can never be negative, the following check is not needed
				if otherCharacter.lives > 100 {
						otherCharacter.lives = 100
				}
		}
Or like this, if you prefer using min, max as specified in your other thread:
		func heal(anotherCharacter: Character){
				anotherCharacter.lives = min(100, anotherCharacter.lives + 20)
		}
		func attack(otherCharacter: Character){
				otherCharacter.lives = min(100, max(0, otherCharacter.lives + power))
		}
Topic:
Programming Languages
SubTopic:
Swift
Tags: