Silver Challenge


#1

Vampire.swift

class Vampire: Monster {
    
    var vampireThralls = [Vampire]()
    
    override func terrorizeTown() {
        
        town?.changePopulation(-1)
        vampireThralls.append(Vampire())
    }
}

main.swift

var myTown = Town()

let dracula = Vampire()
dracula.town = myTown
dracula.name = "Dracula"

for var i = 0; i < 20; i++ {
    
    dracula.terrorizeTown()
    dracula.town?.printTownDescription()
    dracula.vampireThralls[i].name = "Dracula's Thrall \(i)"
    print(dracula.vampireThralls[i].name)
}
print("Number of \(dracula.name)'s thralls \(dracula.vampireThralls.count)")

#2

I did the following:import Foundation

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

final override func terrorizeTown() {
    vampireThralls.append(Vampire())
    town?.changePopulation(by: -vampireThralls.count)
    super.terrorizeTown()
}

}


#3

My approach:

class Vampire: Monster {

var vampireThralls: [Vampire] = []

override func terrorizeTown() {
    
    let thrall = Vampire()
    vampireThralls.append(thrall)
    
    if town?.population > 0 {
        (town?.population >= 1) ? (town?.changePopulation(-1)) : (town?.population = 0)
    }
    super.terrorizeTown()
}
/*
 static var vampireThralls: [Vampire] = []
 
 override func terrorizeTown() {
 
 let thrall = Vampire()
 Vampire.vampireThralls.append(thrall)
 
 if town?.population > 0 {
 (town?.population >= 1) ? (town?.changePopulation(-1)) : (town?.population = 0)
 
 }
 super.terrorizeTown()
 }
 */

}

In main.swift:

import Foundation

var myTown = Town()
myTown.changePopulation(500)

let vampire1 = Vampire()
let vampire2 = Vampire()
let vampire3 = Vampire()

vampire1.name = "Vampire 1"
vampire2.name = "Vampire 2"
vampire3.name = “Vampire 3”

vampire1.town = myTown
vampire2.town = myTown
vampire3.town = myTown

vampire1.terrorizeTown()
vampire1.terrorizeTown()
vampire1.terrorizeTown()
vampire1.terrorizeTown()
vampire2.terrorizeTown()

print (“there are (vampire1.vampireThralls.count) thralls”)
print (“there are (vampire2.vampireThralls.count) thralls”)
print (“there are (vampire3.vampireThralls.count) thralls”)
/*
print (“there are (Vampire.vampireThralls.count) thralls”)
print (“there are (Vampire.vampireThralls.count) thralls”)
print (“there are (Vampire.vampireThralls.count) thralls”)
*/


#4

@rolfino I tried your approach and it worked for the most part. However, when printing the number of thralls, it does so for each individual vampire instance only (for example, it prints 3 thralls for vampire1, but only 1 for vampire2), and it does not add up all the thralls from the other vampires, which I think is what the exercise intends.

I have not yet found how to do so. Any improvements on your side? Ideas?


#5

Hi Husain instead of this:

import Foundation

class Vampire: Monster {

var vampireThralls: [Vampire] = []

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

}

try instead this other way:

import Foundation

class Vampire: Monster {

static var vampireThralls: [Vampire] = []

override func terrorizeTown() {                                                          // my add for the Gold Challenge
    
    let thrall = Vampire()                                                               // my add for the Gold Challenge
    Vampire.vampireThralls.append(thrall)                                                // my add for the Gold Challenge
    
    if town?.population > 0 {                                                          // my add for the Gold/B Challenge
        (town?.population >= 1) ? (town?.changePopulation(-1)) : (town?.population = 0)// my add for the Gold/B Challenge
        // or: (town?.population > 1) ? (town?.changePopulation(-1)) : (town?.population = 0)
    }
    super.terrorizeTown()
}

}

And then in main.swift:

import Foundation

var myTown = Town()
myTown.changePopulation(500)

let fredTheZombie = Zombie()
fredTheZombie.town = myTown
fredTheZombie.name = "Zombie"
fredTheZombie.terrorizeTown()
fredTheZombie.town?.printTownDescription()

fredTheZombie.changeName(“Fred the Zombie”, walksWithLimp: false)
print(fredTheZombie.walksWithLimp)

let vampire1 = Vampire()
let vampire2 = Vampire()
let vampire3 = Vampire()

vampire1.name = "Vampire 1"
vampire2.name = "Vampire 2"
vampire3.name = “Vampire 3”

vampire1.town = myTown
vampire2.town = myTown
vampire3.town = myTown

vampire1.terrorizeTown()
vampire1.terrorizeTown()
vampire1.terrorizeTown()
vampire1.terrorizeTown()
vampire2.terrorizeTown()

print (“there are (Vampire.vampireThralls.count) thralls”)


#6

That’s great, I actually did try this method (forgot to update you guys here, apologies). It worked.

This has been discussed in another topic in the forum (also titled Silver Challenge). One person suggested the use of static so that the array becomes a class array rather than an instance array.

This, in turn, can collect all the Vampire instances into one, let’s say ‘global’ array (for the class).

Thanks for your contribution!