gpt4 book ai didi

swift - 有没有更好的方法来处理 Sprite Kit 中的结果逻辑? ( swift )

转载 作者:行者123 更新时间:2023-11-28 11:58:57 25 4
gpt4 key购买 nike

我正在尝试处理游戏的结果。但我不确定以下方法是否正确。该方法有效,我正在寻找更好的解决方案来计算结果。

func result(a: Int, b: Int, z: Int){
// a: User/Opponent'answer (Int), b: User/Opponent'answer (Int), z: Correct Answer
let az = abs(a - z) //User/Opponent based on GameData.shared.userDetected
let bz = abs(b - z) //Opponent/User based on GameData.shared.userDetected

if GameData.shared.userDedected == true {


if az == bz {
print("Draw")

} else if az < bz {
print("User won az < az")

} else if bz < az {
print("User lost bz < az")

}
} else if GameData.shared.userDedected == false {
if az == bz {
print("Draw")

} else if az < bz {
print("User Lost az < bz")

} else if bz < az {
print("User won bz < az")

}
}
}

最佳答案

你的代码很好,但如果你想清理它,你可以这样做:

func result(a: Int, b: Int, z: Int){
// a: User/Opponent'answer (Int), b: User/Opponent'answer (Int), z: Correct Answer
let az = abs(a - z) * GameData.shared.userDedected ? 1 : -1 //User/Opponent based on GameData.shared.userDetected
let bz = abs(b - z) * GameData.shared.userDedected ? 1 : -1 //Opponent/User based on GameData.shared.userDetected

if az == bz {
print("Draw")
} else if az < bz {
print("User won az < bz")
} else if bz < az {
print("User lost bz < az")
}
}

基本上,根据 userDedected 的值确定是否应该翻转标志

你还可以这样做:

func result(a: Int, b: Int, z: Int){
// a: User/Opponent'answer (Int), b: User/Opponent'answer (Int), z: Correct Answer
let value = (abs(a - z) - abs(b - z) ) * GameData.shared.userDedected ? 1 : -1 //User/Opponent based on GameData.shared.userDetected

if value == 0 {
print("Draw")
} else if value < 0 {
print("User won az < bz")
} else if value > 0 {
print("User lost bz < az")
}
}

关于swift - 有没有更好的方法来处理 Sprite Kit 中的结果逻辑? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50406762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com