Search code examples
swiftfunctionadditionsubtraction

Why are these functions not working? Subtractions and additions


yesterday I wrote a code about a fight of different fantasy creatures. They have hearts (like in a computer game), an attacking power and a defence. If the attacking power of the attacker is bigger than the defence of the defender. Than the attacking power will be subtract from the hearts. The fight should be finish when one of the creatures have no Hearts anymore or there more than 11 rounds. But the thing is that the round-addition doesn't work and the heart subtraction doesn't too. Where is the problem? thanks in advance!

protocol Character {
    var name: String {get set}
    var creature: String {get set}
    var specialAttack: String {get set}
    var power: Int {get set}
    var defence: Int {get set}
    var hearts: Int {get set}
}

struct Creature: Character {
    var name: String
    var creature: String
    var specialAttack: String
    var power: Int
    var defence: Int
    var hearts: Int = 30
    
    func attack() {
        print("\(name) is attacking...")
    }
    
    mutating func defend(attackingPower: Int) {
        if defence > attackingPower {
            print("\(name) is blocking the attack...")
} else {
            hearts -= attackingPower
        }
    }
    var isAlive: Bool {
        hearts > 0
    }
    
}
var orc = Creature(name: "Arzog", creature: "orc", specialAttack: "dark knife", power: 6, defence: 7, hearts: 30)
var minotaurus = Creature(name: "Horus", creature: "minotaurus", specialAttack: "hornbump", power: 8, defence: 9, hearts: 30)
var troll = Creature(name: "Ortenstone", creature: "troll", specialAttack: "poison spit", power: 5, defence: 6)
var dragon = Creature(name: "Eragon", creature: "dragon", specialAttack: "fireball", power: 10, defence: 8)
var wizard = Creature(name: "Gandalf", creature: "wizard", specialAttack: "magicspell", power: 7, defence: 9)
var human = Creature(name: "Aragon", creature: "human", specialAttack: "swoard", power: 6, defence: 8)
var HorrorClown = Creature(name: "Pennywise", creature: "horror clown", specialAttack: "bite", power: 9, defence: 7)

struct Battle {
    var world: String
    var firstCreature: Creature
    var secondCreature: Creature
    
    mutating func start() -> Int {
        var roundNumber = 0
        
        func rounds() {
            firstCreature.attack()
            secondCreature.defend(attackingPower: firstCreature.power)
            secondCreature.attack()
            firstCreature.defend(attackingPower: secondCreature.power)
        }
        
        repeat {
            print(" Round: \(roundNumber)")
            rounds()
            roundNumber + 1
        } while  !firstCreature.isAlive || !secondCreature.isAlive || roundNumber < 12
        return roundNumber
        print("Battle is over! \(firstCreature.name) with \(firstCreature.hearts) hearts and \(secondCreature.name) with \(secondCreature) hearts")
        }
    }

var BattleArena = Battle(world: "Middleearth", firstCreature: dragon, secondCreature: wizard)
BattleArena.start()

Solution

  • There are 3 things that I could spot:

    1. As Roman suggests, you should rephrase the condition to be: while firstCreature.isAlive && secondCreature.isAlive && roundNumber < 12

      because you want the battle to continue ONLY IF both of the creatures are alive AND it's less than 12 rounds.

    2. Inside your repeat block, you are incrementing your roundNumber variable by 1 but you're not assigning the incremented value back to the variable. As a result, your roundNumber variable remains unaltered with initial value of 0. To get it right, you have to replace the line with roundNumber = roundNumber + 1 or shorten it further to roundNumber += 1

    3. Your print statement never gets executed as it's after the return key. Using a return key just outright exits the block leaving the lines that follow unexecuted.

    After doing these, I could see that your code worked as intended.