如何处理这个功能?

How to handle this function?

我想写一个街头霸王交替攻击他们的功能。每次攻击都会拉动一些人的心。当其中一名战士的心数为 0 时,战斗结束。但我不知道如何一次又一次地做这个过程。这就是我的代码现在的样子:

public struct StreetFighter {
    var name: String
    var power: Int
    var defence: Int
    var hearts: Int
    func Attack() {
        print("\(name) is going into the offence...")
    }
    func Blocking() {
        print("\(name) is blocking the attack")
    }
}
var connorMcGregor = StreetFighter(name: "Connor McGregor", power: 8, defence: 7, hearts: 100)
var mikeTyson = StreetFighter(name: "Connor McGregor", power: 9, defence: 6, hearts: 100)
var alisterOverreem = StreetFighter(name: "Alister Overreem", power: 7, defence: 10, hearts: 100)
var khabib = StreetFighter(name: "Khabib", power: 9, defence: 8, hearts: 100)


struct Fight {
    var location: String
    mutating func Round1(firstFighter: StreetFighter,secondFighter: StreetFighter) -> Int {
        firstFighter.Attack()
        if firstFighter.power < secondFighter.defence  {
            secondFighter.Blocking()
            secondFighter.Attack()
        } else {
        secondFighter.hearts - firstFighter.power
            }
            
            if secondFighter.power < firstFighter.defence  {
                firstFighter.Blocking()
            } else {
        firstFighter.hearts - secondFighter.power
            }
        if firstFighter.hearts == 0 || secondFighter.hearts == 0 {
        print("The fight is over!")
        
        }
        return firstFighter.hearts
        return secondFighter.hearts
    }
}

var mma = Fight(location: "New York")
mma.Round1(firstFighter: connorMcGregor, secondFighter: khabib)

问题是进程在第一轮后停止。我尝试使用 for-in 或 repeat-while 循环但没有成功。 有人可以帮助我吗?

首先,您可以尝试将一些功能移动到 StreetFighter 本身:

public struct StreetFighter {
    let name: String
    let power: Int
    let defence: Int
    var hearts = 100 // you can provide a default value

    var isAlive: Bool {
        hearts > 0
    }

    func attack() {
        print("\(name) is going into the offence...")
    }

    mutating func defend(attackingPower: Int) {
        if power > defence {
            hurt(damage: attackingPower)
        } else {
            block()
        }
    }

    private func block() {
        print("\(name) is blocking the attack")
    }

    private mutating func hurt(damage: Int) {
        print("\(name) is hurt")
        hearts -= damage
    }
}

然后,在 Fight 结构中添加一个带有 while 循环的函数(您也可以在那里限制轮数):

struct Fight {
    let location: String
    var fighter1: StreetFighter
    var fighter2: StreetFighter

    mutating func start() {
        var roundNumber = 0
        while !isFinished && roundNumber < 12 {
            roundNumber += 1
            round()
            print("Round \(roundNumber): \(fighter1.name) - \(fighter1.hearts), \(fighter2.name) - \(fighter2.hearts)")
        }

        print("The fight is over!")
    }

    private mutating func round() {
        fighter1.attack()
        fighter2.defend(attackingPower: fighter1.power)
        fighter2.attack()
        fighter1.defend(attackingPower: fighter2.power)
    }

    private var isFinished: Bool {
        !fighter1.isAlive || !fighter2.isAlive
    }
}

现在你可以开始战斗了:

let connorMcGregor = StreetFighter(name: "Connor McGregor", power: 8, defence: 7, hearts: 100)
let mikeTyson = StreetFighter(name: "Connor McGregor", power: 9, defence: 6, hearts: 100)
let alisterOverreem = StreetFighter(name: "Alister Overreem", power: 7, defence: 10, hearts: 100)
let khabib = StreetFighter(name: "Khabib", power: 9, defence: 8) // you can use the default `hearts` count

var mma = Fight(location: "New York", fighter1: connorMcGregor, fighter2: khabib)
mma.start()

根据您的目标,您可能希望将 Fight 的某些部分提取到外部。例如 start 函数可以 return 结果(谁赢了)然后你可以对其进行操作。