How to limit my numbers in Swift

Hello everyone, quick question, in Swift, how to limit numbers? I'm practicing on the creation of a game between 2 players, and I want the limit of health points of my characters to be exactly between 0 and 100. If my character loses health points, I don't want it to fall to -10 (negative) and on the contrary if he gains health points, I don't want it to exceed 100 health points.
I'm an beginner, I'm starting with Swift

There is my code :

Code Block // MARK: - Characters
class Character {
    var name: String
    var lives: Int // health points
    var power : Int
    init(name: String, lives: Int, power: Int) {
        self.name = name
        self.lives = lives
        self.power = power
    }
    convenience init(){
        self.init(name:"", lives: 100, power: 0)
    }
// get heal //
    func heal(anotherCharacter: Character){
        if anotherCharacter.lives < 100{
            anotherCharacter.lives += 20
        }
    }
// attack another character //
    func attack(otherCharacter: Character){
        otherCharacter.lives -= power
    }
}
// MARK: - Soldier A
class SoldierA: Character{
    override init(name: String, lives: Int, power: Int) {
        super.init(name: "Daniel", lives: 100, power: 50)
    }
}
// MARK: - Soldier B
class SoldierB: Character{
    override init(name: String, lives: Int, power: Int) {
        super.init(name: "Mark", lives: 100, power: 50)
    }
}
// MARK: - Fight
let player1 = SoldierA()
let player2 = SoldierB()
player1.attack(otherCharacter: player2)
player1.attack(otherCharacter: player2)
player1.attack(otherCharacter: player2)
player2.lives
player1.heal(anotherCharacter: player1)
player1.lives


Answered by OOPer in 658548022

it's not the same request !

At least, you should mark the right solution as SOLVED.
If your solution was really SOLVED, you had no need to start this new thread.

I want the limit of health points of my characters to be exactly between 0 and 100. 

What in your code represents health points?

If my character loses health points,

Where is the code that your character loses health points?

if he gains health points,

Where is the code that your character gains health points?


Better if you continued your old thread with adding this code.
| What in your code represents health points?
lives represents health points

| Where is the code that your character loses health points?
attack function is the code that my characters can loses health points

| Where is the code that your character gains health points?
heal function is the code that my characters can gains health points


it's not the same request ! 😕
Accepted Answer

it's not the same request !

At least, you should mark the right solution as SOLVED.
If your solution was really SOLVED, you had no need to start this new thread.

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:
Code Block
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:
Code Block
func heal(anotherCharacter: Character){
anotherCharacter.lives = min(100, anotherCharacter.lives + 20)
}
func attack(otherCharacter: Character){
otherCharacter.lives = min(100, max(0, otherCharacter.lives + power))
}


Oh, please use the SOLVED mark as it should be.

When you ask a question, and one of the replies really solved your issue, then you mark that reply as SOLVED.
Not on an arbitrary one.
This is the first time I've come to ask for help on the forum and I still don't know how it works here !
What I have to do now ?

What I have to do now ? 

Have in mind what would be the right usage of this site.

Have you read this article?
Apple Developer Forums

How this site works and how you should use this site are described there.
Please read it carefully if not yet.

Undo is not supported in this site, so you should better consider taking enough time when doing things on this site.

Especially, please think for future readers. Marking SOLVED on the right solution would benefit for the future readers suffering the same issue.
That is close to the other question you asked.

You could have a func:

Code Block
func trimHealthPoints(healthPts: Int) -> Int {
var newHealthPts : Int
newHealthPts = mint(healthPts, 100)
newHealthPts = max(healthPts, 0)
return newHealthPts
}


Then in the place where you change the health points, you will have something like:

player.hp += 10
// or player.hp -= 10

Then call
Code Block
player.hp += 10
player.hp = trimHealthPoints(player.hp)


Another way is to create an extension for Int
Code Block
extension Int {
var trimmed: Int {
var val : Int = self
val = Swift.min(val, 100) // Swift prefix needed in extension
val = Swift.max(val, 0)
return val
}
}

then use as follows
Code Block
player.hp += 10
player.hp = player.hp.trimmed

Don't forget to close the thread if solved.
Sorry for the stupidity, next time I'll know how to do it. Thank you for your help. 🙂

next time I'll know how to do it. 

Yes, please. There is no closing in this site. If my answer does not solve your issue, you can continue discussion on this thread.
How to limit my numbers in Swift
 
 
Q