In Chapter 19’s Challenge C you are asked to create a small game with a battle(_: GameCharacter, vs: GameCharacter) method. The instructions state that the first character strikes first. However, the solution code provided has the second character taking the first strike:
public func battle(_ character1: GameCharacter, vs character2: GameCharacter) {
character1.hitPoints -= character2.attackPoints
if character1.hitPoints < 0 {
print("\(character1.name) defeated!")
return
}
character2.hitPoints -= character1.attackPoints
if character2.hitPoints < 0 {
print("\(character2.name) defeated!")
} else {
print("Both players still active!")
}
}
Shouldn’t the order of evaluating the new hit points be reversed?
public func battle(_ character1: GameCharacter, vs character2: GameCharacter) {
character2.hitPoints -= character1.attackPoints
if character2.hitPoints < 0 {
print("\(character2.name) defeated!")
return
}
character1.hitPoints -= character2.attackPoints
if character1.hitPoints < 0 {
print("\(character1.name) defeated!")
} else {
print("Both players still active!")
}
}
Also, it is stated that a character dies when its hitPoints reaches 0, “If a character reaches 0 hit points, they have lost.” The tests for defeat should be “character_.hitPoints <= 0 { …”