I modified main.swift
and added a Vampire.swift
class to my project.
//
// main.swift
// MonsterTown
//
import Foundation
var myTown = Town()
myTown.changePopulation(by: 500)
let fredTheZombie = Zombie()
fredTheZombie.town = myTown
fredTheZombie.terrorizeTown()
fredTheZombie.town?.printDescription()
let vicTheVampire = Vampire()
vicTheVampire.town = myTown
vicTheVampire.terrorizeTown()
vicTheVampire.town?.printDescription()
print("Vic has \(vicTheVampire.thralls.count) thrall.")
vicTheVampire.terrorizeTown()
vicTheVampire.town?.printDescription()
print("Vic has \(vicTheVampire.thralls.count) thralls.")
vicTheVampire.terrorizeTown()
vicTheVampire.town?.printDescription()
print("Vic has \(vicTheVampire.thralls.count) thralls.")
//
// Vampire.swift
// MonsterTown
//
import Foundation
class Vampire: Monster {
var thralls = [Vampire]()
override func terrorizeTown() {
// Try to capture one of the townspeople.
if town?.population ?? 0 > 0 {
town?.changePopulation(by: -1)
thralls.append(Vampire())
}
super.terrorizeTown()
}
}
Iām curious whether there is a more concise way to write the population check.