I have two simple tests initially triggered by a title of a game setting: "Highest score Wins" or rule.hasSuffix("wins."). The code in both cases looks for the highest score and in the latter case checks to see if a target score has been achieved. This work fine. But when I display the final results, the first works fine and the latter fails to display the name; defaulting to the No One Wins - which is incorrect. The relevant pieces of the code are:
case rule == "Highest score Wins":
// Find the highest score
let maxScore = scores.max(by: { $0.1 < $1.1 })?.1 ?? 0
let topScorers = scores.filter { $0.1 == maxScore }
gameIsDraw = topScorers.count > 1
winner = topScorers.count == 1 ? topScorers.first?.0 : nil
// This work and will display the winner name correctly - see below
case rule.hasSuffix("wins."):
// "Player to reach X wins."
let topScorers = scores.filter { $0.1 >= target }
if !topScorers.isEmpty {
let maxScore = scores.max(by: { $0.1 < $1.1 })?.1 ?? 0
let topScorers = scores.filter { $0.1 == maxScore }
gameIsDraw = topScorers.count > 1
winner = topScorers.count == 1 ? topScorers.first?.0 : nil
// This works but does not display the name
//This is the section that displays the names (winner)
if gameIsDraw {
Text("The Game is a Draw")
.font(.largeTitle)
.bold()
.foregroundColor(.orange)
} else {
Text("Game Over!")
.font(.largeTitle)
.bold()
Text("\(winner?.playername ?? "No One") Wins!")
.font(.title)
.foregroundColor(.green)
}
I have tried everything, but am losing the will to live