Silver Challenge


#1

Here’s my code for the Silver Challenge. I’m getting an error on the thrall array ("Contextual type ‘Int’ cannot be used with array literal), and I’m not sure where to go from here to add to the thrall array.

class Vampire: Monster { override func terrorizeTown() { town?.changePopulation(-1) thrall += 1 } var thrall: Int = [0] }


#2

I found the Silver Challenge to be a bit confusing in that the book hasn’t yet covered static (Class) members/properties. At least that’s how I understood it when I read:

In any case, the way I solved it was by creating a static var in the Vampire class that looks like this:

    static var vampires = [Vampire]()

The only issue I can see with your code is that the Array is being declared as a type Int whereas it should be of type Vampire:

var thrall: [Vampire] = []

In the end, my class looks like this:

class Vampire: Monster {
    static var vampires = [Vampire]()
    
    override func terrorizeTown() {
        Vampire.vampires.append(Vampire())
        
        if town?.population > 0 {
            town?.changePopulation(-1)
            
            if town?.population < 0 {
                town?.population = 0
            }
        }
        
        super.terrorizeTown()
    }
}

I hope that helps explain things a bit more. Again, I may have misunderstood the instructions, but I took it to mean a class (static) property on Vampire.


#3

Here is how I solved it. I hope this helps.

[code]class Vampire: Monster {
var vampires = Vampire

override func terrorizeTown() {
if town?.population > 0 {
let vampire = Vampire()
vampires.append(vampire)
town?.changePopulation(-1)
} else {
print(“This town has no one to turn!”)
}
super.terrorizeTown()
print(“There are (vampires.count + 1) vampires in this town!”)
}
}[/code]


#4

Any issue with just using self below like I did?

[code]class Vampire:Monster{

var vampires = [Vampire]()

override func terrorizeTown() {
    town?.changePopulation(-1)
    vampires.append(self)
}

}[/code]

[code]class Monster {

var town: Town?
var name = "Monster"

func terrorizeTown() {
    
    if town != nil && town?.population > 0 {
        print("\(name) is terrorizing a town!)")
        
    } else {
        print("\(name) hasn't found a town to terrorize yet...")
    }
    
}

}[/code]


#5

Here’s what I got:

[code]class Vampire: Monster {
var vampires = Vampire

override func terrorizeTown() {
    vampires += [self]
    if town?.population > 0 {town?.population--}
    else {town?.population = 0}
    super.terrorizeTown()
}

}[/code]

I guess the idea is that every time you call frankTheVampire.terrorizeTown() (or whatever instance you created), it adds a vampire thrall just to that instance’s array. That explains why we don’t need to use a static variable for this struct (which we haven’t learned about yet).


#6
class Vampire: Monster {
    var vampires = [Vampire]()
    
    override func terrorizeTown() {
        vampires += [Vampire()]
        
        if town?.population > 0 {
            town!.changePopulation(-1)
        }
        
        super.terrorizeTown()
    }
}

#7

I presumed that you’d only get a new Vampire Thrall if there was a person in the town :slight_smile:, so I went with:

class Vampire: Monster {
    var vampireThralls = [Vampire]()

    override func terrorizeTown() {
        if town?.population > 0 {
	    town?.changePopulation(-1)
	    vampireThralls.append(Vampire())
        }
        super.terrorizeTown()
    }

    func printThralls() {
        print("Number of vampire thralls: \(vampireThralls.count)")
    }
}

#8

I wound up doing something similar to @curlypaws:

class Vampire : Monster {
    var thralls: [Vampire] = []

    final override func terrorizeTown() {
        if town?.population > 0 {
            town?.changePopulation(-1)
            thralls.append(Vampire())
        }
        
        super.terrorizeTown()
    }
}

Combined with the solution @myrhillion provided in the Bronze Challenge for the Town class, I think code is pretty clean:

mutating func changePopulation(amount: Int) {
    if (population + amount > 0) {
        population += amount
    } else {
        population = 0
    }
}

#9

Here is my solution, I used the function after changing the population (first a person will be reduced and then a vampire will be created). Also since there are only one tap of Vampires only I have just increased t he count of vampires by one.

class Vampire: Monster {
var vampirePopulation = 0
    
     override func terrorizeTown() {
        town?.changePopulation(amount: -1)
        if town?.population <= 0 {
            town?.population = 0
        } else  {
            town?.population = (town?.population)!
        }
        super.terrorizeTown()
        
    }
    func changeVampirePopulation() -> Int {
       let newVampirePopulation = vampirePopulation + 1
        return newVampirePopulation
    }
}

#10

I totally agree with you here. The wording of this challenge is a bit confusing. From the description of the challenge, it seems that there needs to be one array that keeps adding Vampire() instances every time the function terrorizeTown() is called, on ANY of these instances. In other words, this array will be for the ENTIRE class and not on any of its INSTANCES.

Using static worked for me.