-1

I want to create a class in Xcode Swift see this code:

class Cards {

    var roundNumber = 10
    var monsters:[
        (Id: Int, Name:String, Description: String, Attack_min: Int, Attack_max: Int, Health: Int, VP: Int, Gold:Int)] = [
        (Id: 1, Name: "Ice dragon", Description: "Attack on Wizards +1", Attack_min: 2, Attack_max: 5, Health: 4, VP: 2, Gold: 4),
        (Id: 2, Name: "Fire dragon", Description: "Attack on Wariors +2", Attack_min: 2, Attack_max: 5, Health: 4, VP: 2, Gold: 4),
        (Id: 3, Name: "Acid witch", Description: "Attack of all heroes -1 \n(Only -1 on heroes)", Attack_min: 3, Attack_max: 6, Health: 5, VP: 4, Gold: 5),
        (Id: 4, Name: "Killer rats", Description: "Health -1 when attacked by supporting cards ", Attack_min: 2, Attack_max: 6, Health: 3, VP: 1, Gold: 3)
    ]
   var monsterDeck = [Any]()

    func randomizer(lowestNumber: Int, highestNumber: Int) -> Int {
        return Int.random(in: lowestNumber ... highestNumber)
    }

    func round() {
        if self.roundNumber == 10 {
            self.monsters.shuffle()
            self.monsterDeck = self.monsters
        } else {
            self.monsterDeck.removeFirst()
        }

        self.roundNumber -= 1
    }
}

var cards = Cards()
cards.round()

print("\(cards.monsterDeck[0].Name)\n")
print(cards.monsterDeck)
print(cards.roundNumber)

var monsters is a array that I going to update myself I want to shuffle the cards and store it in var monsterDeck

what I want is that every time I run the function round() I want a new monster card that is shuffled and the first array of monsterdeck should be removed.

But at the bottom you see print("\(cards.monsterDeck[0].Name)\n") and for that I get an error:

error: MyPlayground.playground:34:31: error: value of type 'Any' has no member 'Name'
print("\(cards.monsterDeck[0].Name)\n")

So how can I fix this? Basicly I want the var monsterDeck to be a array that is a shuffled version of array monsters

I checked this article, but those answers assumed the array would be only string or int.

My example has string and int fields

Ralph Schipper
  • 701
  • 2
  • 12
  • 24

1 Answers1

1

Use structs, not tuples for your Monster data, and then create an array of those structs:

var monsterDeck = [Monster]()

By using Any as the type, you're losing all the detail information about what's in the deck.

Gereon
  • 17,258
  • 4
  • 42
  • 73