猜拳App
Published in
Nov 7, 2020
還沒能做得出來 TextView 可以隨著新增文字Auto Scroll to Bottom
random
button.setTitle
button.setImage
OutletCollections
textView.append
textView.text
textView.textAlignment
textView.font
textView.isEditable
@IBOutlet var chooseButton: [UIButton]!@IBOutlet weak var selfImageView: UIImageView!@IBOutlet weak var enemyImageView: UIImageView!@IBOutlet weak var winLabel: UILabel!@IBOutlet weak var loseLabel: UILabel!@IBOutlet weak var textView: UITextView!let imageView = ["paper","stone","scissors"] //布、石頭、剪刀 的圖片名稱 陣列let buttonTitle = ["0","1","2"] //給布、石頭、剪刀 的數字的字串標題 陣列 對應圖片名稱陣列的 0,1,2var score = 0 //計分數var round = 0 //計局數var winCount = 0 //計勝次var loseCount = 0 //計敗次
@IBAction func buttonChoose(_ sender: UIButton) {let enemyRandom = Int.random(in: 0...2)enemyImageView.image = UIImage(named: imageView[enemyRandom])for i in 0...2 {chooseButton[i].setImage(UIImage(named: imageView[i]), for: UIControl.State.normal) //設置選項button的圖片chooseButton[i].setTitle(buttonTitle[i], for: UIControl.State.normal) //設置選項button的Title}selfImageView.image = sender.currentImage //將點選的button的圖片 存到 要顯示的ImageViewlet senderValue = Int(sender.currentTitle!)round += 1
switch enemyRandom {
case 0:
switch senderValue {
case 0:
textView.text.append("\n"+"哇嗚~平手")
case 1:
textView.text.append("\n"+"很可惜~輸了")
score -= 10
loseCount += 1
default:
textView.text.append("\n"+"運氣不錯~贏了")
score += 10
winCount += 1
}
case 1:
switch senderValue {
case 0:
textView.text.append("\n"+"運氣不錯~贏了")
score += 10
winCount += 1
case 1:
textView.text.append("\n"+"哇嗚~平手")
default:
textView.text.append("\n"+"很可惜~輸了")
score -= 10
loseCount += 1
}
default:
switch senderValue {
case 0:
textView.text.append("\n"+"很可惜~輸了")
score -= 10
loseCount += 1
case 1:
textView.text.append("\n"+"運氣不錯~贏了")
score += 10
winCount += 1
default:
textView.text.append("\n"+"哇嗚~平手"+"")
}
}
//排除首場輸掉導致負分的情況
if score < 0 {
score = 0
}
textView.textAlignment = .center
textView.font = UIFont.boldSystemFont(ofSize: 20)
textView.isEditable = false
textView.text.append("\n"+"第\(round)局,目前得分\(score)"+"\n")
winLabel.text = "勝:\(winCount)"
loseLabel.text = "敗:\(loseCount)"
}