HELP!

I am trying to get this code to run in the output panel and nothing is showing up. I need help figuring out what is wrong with my code and why it will not run.

var firstName = "Bella" var lastName = "Achi"

let fullName = firstName + " " + lastName

print(fullName)

struct Player { var characterName: String var strength: Int var intelligence: Int var wisdom: Int var dexterity: Int var consitution: Int var charisma: Int var itemsCharacterHas: String var characterCatchPhrase: String var currentHP: Int var maximumHP: Int

var Player = (characterName: "Rumble McSkirmish",
                              strength: 18,
                              intelligence: 5,
                              wisdom: 3,
                              dexterity: 15,
                              constitution: 18,
                              charisma: 10,
                              itemsCharacterHas: "Red headband",
                              characterCatchPhrase: "Fireball! Upper cut! Downer cut! Bowl of punch!",
                              currentHP: 138,
                              maximumHP: 145)

func printCurrentHP() {
    print(currentHP)
}

func printCharacterCatchPhrase() {
    print(characterCatchPhrase)
}


mutating func majorDamage() {
    currentHP = -25
}

mutating func healthPack() {
    currentHP = +15
    let currentHP = 138
    if self.currentHP == maximumHP{
        self.currentHP = 145
    }
}

mutating func healthRestore() {
    currentHP == maximumHP
    let currentHP = currentHP
    if currentHP < 0{
        print("Player died")}
}

}

Hi! I am not entirely sure what you were trying to do with the var Player = (characterName: ... part, but I assumed you wanted to create a new object of your struct. You should do this after you declare your struct.

I've fixed a few things and this is a working code:

var firstName = "Bella"
var lastName = "Achi"
let fullName = firstName + " " + lastName
print(fullName)

struct Player {
    var characterName: String
    var strength: Int
    var intelligence: Int
    var wisdom: Int
    var dexterity: Int
    var constitution: Int // I fixed a typo here
    var charisma: Int
    var itemsCharacterHas: String
    var characterCatchPhrase: String
    var currentHP: Int
    var maximumHP: Int
    
    func printCurrentHP() {
        print(currentHP)
    }

    func printCharacterCatchPhrase() {
        print(characterCatchPhrase)
    }


    mutating func majorDamage() {
        currentHP = -25
    }

    mutating func healthPack() {
        currentHP = +15
        let currentHP = 138
        if self.currentHP == maximumHP{
            self.currentHP = 145
        }
    }

    mutating func healthRestore() {
        currentHP == maximumHP
        let currentHP = currentHP
        if currentHP < 0{
            print("Player died")}
    }
}

// creating a new var of type Player
var player  =  Player(characterName: "Rumble McSkirmish",
                               strength: 18,
                               intelligence: 5,
                               wisdom: 3,
                               dexterity: 15,
                               constitution: 18,
                               charisma: 10,
                               itemsCharacterHas: "Red headband",
                               characterCatchPhrase: "Fireball! Upper cut! Downer cut! Bowl of punch!",
                               currentHP: 138,
                               maximumHP: 145)

player.printCharacterCatchPhrase()

Welcome to the forum.

What @CMDdev wrote plus a few comments:

  • when you post code, you should use code formatter to make it more readable.
  • the names of var should start with lowerCase (player and not Player) and not be the same as a class or a struct name
  • player cannot be declared inside its own struct
  • you had misspelled a property
  • you do not need to create all the print func if they are a simple print statement, you may call
print(player.currentHP)

Here is corrected code that works in playground:

var firstName = "Bella"
var lastName = "Achi"
let fullName = firstName + " " + lastName
print(fullName)

struct Player {
    var characterName: String
    var strength: Int
    var intelligence: Int
    var wisdom: Int
    var dexterity: Int
    var constitution: Int        // consitution Misspelled
    var charisma: Int
    var itemsCharacterHas: String
    var characterCatchPhrase: String
    var currentHP: Int
    var maximumHP: Int
    
    func printCurrentHP() {
        print(currentHP)
    }
    
    func printCharacterCatchPhrase() {
        print(characterCatchPhrase)
    }
    
    mutating func majorDamage() {
        currentHP = -25
    }
    
    mutating func healthPack() {
        currentHP = +15
        let currentHP = 138
        if self.currentHP == maximumHP{
            self.currentHP = 145
        }
    }
    
    mutating func healthRestore() {
        currentHP == maximumHP
        let currentHP = currentHP
        if currentHP < 0{
            print("Player died")}
    }
}

// Change to player and move out of Player struct
// create player using Player(…) struct constructor
var player = Player(characterName: "Rumble McSkirmish",
                              strength: 18,
                              intelligence: 5,
                              wisdom: 3,
                              dexterity: 15,
                              constitution: 18,
                              charisma: 10,
                              itemsCharacterHas: "Red headband",
                              characterCatchPhrase: "Fireball! Upper cut! Downer cut! Bowl of punch!",
                              currentHP: 138,
                              maximumHP: 145)

// print whatever you want, such as:
player.printCurrentHP()

A few things I've noticed here. I might have misunderstood what you're trying to do, but:

mutating func majorDamage() {
    currentHP = -25
}
  • Assuming you're going to trigger this majorDamage() function to remove 25 HP (health points?), what this currently does is change it to -25, not reduce it by 25. For that, use currentHP -= 25, and you should then check whether the player has died if their currentHP <= 0.
mutating func healthPack() {
    currentHP = +15
    let currentHP = 138
    if self.currentHP == maximumHP{
        self.currentHP = 145
    }
}
  • currentHP = +15 is a bit weird. You don't need to put the plus sign for positive integers.
  • Adding 15 to currentHP is achieved by writing currentHP += 15.
  • If this function is called when you find a health pack, then what you're doing is setting currentHP to 15, then making a let with the same variable name, and setting it to 138. Then you're checking whether this new currentHP equals their maximum, and setting it to 145, but that's not going to happen unless their maximum is 138. It's the wrong way to do it. You need to check if their currentHP is now over their maximum, and reset it. I have no idea why you're using a second variable here?
mutating func healthRestore() {
    currentHP == maximumHP
    let currentHP = currentHP
    if currentHP < 0{
        print("Player died")}
}
  • This currentHP == maximumHP checks whether the two values match, but does nothing with the result. Did you mean to set currentHP to maximumHP instead? If so, remove one of the equals signs.
  • When will the currentHP ever be below zero when you're running a healthRestore() function? You've just restored their health to the maximum, so how have they died?!
  • Also, the player would surely die if they have zero HP, not only less than zero? Your check should be if currentHP <= 0 (but not in this function; put it in those functions where the HP is reduced).
HELP!
 
 
Q