在IOS应用程序中比较两次掷骰子
我有一个具有2个图像视图的应用程序。每一个都随机显示一张从1到6的骰子面图像,最终结果看起来就像是玩家和计算机掷骰子的模拟 看起来是这样的: 我有一个数组,其中包含显示的每个图像的名称: 设des:Array=[1,2,3,4,5,6] 我也有一个功能,随机图像显示的2名球员在IOS应用程序中比较两次掷骰子,ios,arrays,swift,xcode,uiimageview,Ios,Arrays,Swift,Xcode,Uiimageview,我有一个具有2个图像视图的应用程序。每一个都随机显示一张从1到6的骰子面图像,最终结果看起来就像是玩家和计算机掷骰子的模拟 看起来是这样的: 我有一个数组,其中包含显示的每个图像的名称: 设des:Array=[1,2,3,4,5,6] 我也有一个功能,随机图像显示的2名球员 UIView.animate(withDuration: 0.5) { self.dicePlayer.image = UIImage(named: self.des.randomE
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()
}
我想对这两次掷骰进行比较,即:如果玩家一次掷骰优于玩家二次掷骰,则玩家获胜,反之亦然。您可以尝试
let fImg = self.des.randomElement() ?? "one"
let sImg = self.des.randomElement() ?? "two"
UIView.animate(withDuration: 0.5) {
self.dicePlayer.image = UIImage(named:fImg)
self.diceComputer.image = UIImage(named:sImg)
}
然后
你可以试试
let fImg = self.des.randomElement() ?? "one"
let sImg = self.des.randomElement() ?? "two"
UIView.animate(withDuration: 0.5) {
self.dicePlayer.image = UIImage(named:fImg)
self.diceComputer.image = UIImage(named:sImg)
}
然后
我建议将这两个抛出存储为人类可读的Int值。将这些属性添加到类中:
var playerToss = 1
var computerToss = 1
然后修改您的函数,如下所示:
func lancerDeDes() {
self.playerToss = Int.random(in: 1...6)
self.computerToss = Int.random(in: 1...6)
UIView.animate(withDuration: 0.5) {
// subtract 1 because des array is indexed 0...5 not 1...6
self.dicePlayer.image = UIImage(named: self.des[self.playerToss - 1])
self.diceComputer.image = UIImage(named: self.des[self.computerToss - 1])
}
// on appelle la fonction qui gere les scores
gestionDesScores()
}
然后您可以直接比较playerToss和computerToss:
if self.playerToss > self.computerToss {
// the player wins!
}
将这些值存储为Int除了使比较更容易之外,还可以帮助您进行调试
幻数
在你们的程序中,我认为6代表什么是清楚的。一般来说,避免在代码中使用魔法数字和未注释的数字是明智的。因此,您可以将卷写为:
self.playerToss = Int.random(in: des.indices) + 1
self.computerToss = Int.random(in: des.indices) + 1
通过使用des.index从des数组中获取范围0…5,可以确保即使des数组中的值的数量发生变化,代码也能正常工作
出于调试目的,我个人仍然会将转鼓存储在1…6范围内,但您可以通过将转鼓存储在0…5范围内来避免+1和-1。我建议将这两个转鼓存储为人类可读的Int值。将这些属性添加到类中:
var playerToss = 1
var computerToss = 1
然后修改您的函数,如下所示:
func lancerDeDes() {
self.playerToss = Int.random(in: 1...6)
self.computerToss = Int.random(in: 1...6)
UIView.animate(withDuration: 0.5) {
// subtract 1 because des array is indexed 0...5 not 1...6
self.dicePlayer.image = UIImage(named: self.des[self.playerToss - 1])
self.diceComputer.image = UIImage(named: self.des[self.computerToss - 1])
}
// on appelle la fonction qui gere les scores
gestionDesScores()
}
然后您可以直接比较playerToss和computerToss:
if self.playerToss > self.computerToss {
// the player wins!
}
将这些值存储为Int除了使比较更容易之外,还可以帮助您进行调试
幻数
在你们的程序中,我认为6代表什么是清楚的。一般来说,避免在代码中使用魔法数字和未注释的数字是明智的。因此,您可以将卷写为:
self.playerToss = Int.random(in: des.indices) + 1
self.computerToss = Int.random(in: des.indices) + 1
通过使用des.index从des数组中获取范围0…5,可以确保即使des数组中的值的数量发生变化,代码也能正常工作
出于调试目的,我个人仍然会将掷骰存储在1…6范围内,但您可以通过将掷骰存储在0…5范围内来避免+1和-1。将骰子值存储在枚举中:
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
}
}
我相信这是不言自明的。在枚举中包含您的骰子值:
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
}
}
我相信这是不言自明的