gpt4 book ai didi

ios - 在 IOS 应用程序中比较 2 次掷骰子

转载 作者:行者123 更新时间:2023-12-01 18:35:49 26 4
gpt4 key购买 nike

我有一个带有 2 个 ImageView 的应用程序。每个人随机显示一张骰子面的图像(从 1 到 6),最终结果看起来像是为玩家和计算机模拟掷骰子。

它看起来像这样:

enter image description here

我有一个数组,其中包含显示的每个图像的名称:let des: Array = ["one", "two", "three", "four", "five", "six"]
我还有一个功能可以随机化为 2 个玩家显示的图像

        UIView.animate(withDuration: 0.5) {
self.dicePlayer.image = UIImage(named: self.des.randomElement() ?? "one")
self.diceComputer.image = UIImage(named: self.des.randomElement() ?? "two")

}
// on appelle la fonction qui gere les scores
gestionDesScores()
}

我想在这 2 次掷骰之间进行比较(即:如果玩家 1 掷骰子优于玩家 2 掷骰子,则玩家获胜,反之亦然)。

最佳答案

将你的骰子值放在一个枚举中:

enum Die: String,CaseIterable{
case One = "one"
case Two = "two"
case Three = "three"
case Four = "four"
case Five = "five"
case Six = "six"
}

添加扩展名:
extension CaseIterable where Self: Equatable {
var index: Self.AllCases.Index? {
return Self.allCases.firstIndex { self == $0 }
}
}

你的 throwDie 方法看起来像:
    func throwDice(){
let userResult = Die.allCases.randomElement()
let computerResult = Die.allCases.randomElement()

UIView.animate(withDuration: 0.2, animations: {
self.dicePlayer.image = UIImage(named: userResult?.rawValue ?? "one")
self.diceComputer.image = UIImage(named: computerResult?.rawValue ?? "one")
}) { (_) in
print("Index Value : \(computerResult?.index) : \(computerResult?.rawValue)")
//You may use the indexValue for comparison and the rawValue to assign your image
}
}

不言自明,我相信。

关于ios - 在 IOS 应用程序中比较 2 次掷骰子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59599549/

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