You are on the wrong path. If your game has many difficulties and game types, then you should get rid of these variables:
var hiScoreEasyA = 0
var hiScoreHardA = 0
var hiScoreEasyB = 0
var hiScoreHardB = 0
and replace them with a dictionary that maps each high score category to the high score. A "high score category" is simply a difficulty and a game type combined, e.g. "easy A", "hard B":
struct HighscoreCategory: Hashable {
let difficulty: String
let gameType: String
}
The dictionary looks like this:
var highScores = [
HighscoreCategory(difficulty: "Easy", gameType: "A"): 0,
HighscoreCategory(difficulty: "Hard", gameType: "A"): 0,
HighscoreCategory(difficulty: "Easy", gameType: "B"): 0,
HighscoreCategory(difficulty: "Hard", gameType: "B"): 0,
]
Now to set the high score of the current game type and difficulty, you don't need any loops at all! You just do
func setHiScore() -> Void {
let highScoreCategory = HighscoreCategory(difficulty: currentDifficulty, gameType: currentGame)
let previousHighscore = highScores[highScoreCategory] ?? 0
if previousHighscore < currentScore {
newHiScore = true
highScores[highScoreCategory] = currentScore // this is the line where you set it
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…