Published in
3 min readDec 17, 2022
HW9: 猜拳遊戲 App
- 以某種猜拳遊戲為主題: 棒打老虎雞吃蟲
- 顯示幾勝幾敗
- 電腦出拳時發出聲音
學習point(Coding)
*Enum & Enum to Array(CaseIterable)
*property observer: didset
*optional
*Array 的 random function(randomElement)
Storyboard design
RPSViewController(UIViewController)
Swift function
[Code 流程]:IBAction func playerChoice
1. 點擊想出的圖片(棒子 雞 老虎 蟲)並顯示:Enum RPSElement
2. CPU 念出“棒打老出雞吃蟲”: AVSpeechSynthesizer
3. 做延遲判斷給 CPU 一點時間唸完: DispatchQueue.main.asyncAfter
4. CPU random 出一張圖片(棒子 雞 老虎 蟲)並顯示:Enum RPSElement CaseIntrtable(Array) & random
5. 顯示結果並更新比分:Enum RPSStatus & score++ didset
Enum RPSElement & RPSStatus
RPSViewController
import UIKit
import AVFAudio
class RPSViewController: UIViewController {
@IBOutlet weak var cpuImageView: UIImageView!
@IBOutlet weak var playerImageView: UIImageView!
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var cpuScore: UILabel!
@IBOutlet weak var playerScore: UILabel!
@IBOutlet weak var stickButton: UIButton!
@IBOutlet weak var wormButton: UIButton!
@IBOutlet weak var chickenButton: UIButton!
@IBOutlet weak var tigerButton: UIButton!
var playerResult: RPSElement = .worm
var cpuResult: RPSElement = .worm
var totalWins: Int = 0 {//property observer: 贏一分時更新比分和顯示贏
didSet{
resultLabel.text = RPSStatus.win.result
playerScore.text = String(totalWins)
}
}
var totalLoses: Int = 0 {//property observer: 輸一分時更新比分和顯示輸
didSet{
resultLabel.text = RPSStatus.lose.result
cpuScore.text = String(totalLoses)
}
}
let synthesizer = AVSpeechSynthesizer()//說話
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// segue UIButton: stickButton wormButton chickenButton tigerButton
@IBAction func playerChoice(_ sender: UIButton) {
//CPU 說話
let utterance = AVSpeechUtterance(string: "棒打老虎雞吃蟲")
utterance.voice = AVSpeechSynthesisVoice(language: "zh-TW")
utterance.rate = 0.6
synthesizer.speak(utterance)
//給 CPU 說話時間
DispatchQueue.main.asyncAfter(deadline: .now()+0.8) {
//依據點擊 Button 變更我方圖片
switch sender{
case self.stickButton:
if let stick = RPSElement.stick.displayImage{// RPSElement UIImage is Optional
self.playerResult = RPSElement.stick
self.playerImageView.image = stick
}
case self.wormButton:
if let worm = RPSElement.worm.displayImage{
self.playerResult = RPSElement.worm
self.playerImageView.image = worm
}
case self.chickenButton:
if let chicken = RPSElement.chicken.displayImage{
self.playerResult = RPSElement.chicken
self.playerImageView.image = chicken
}
case self.tigerButton:
if let tiger = RPSElement.tiger.displayImage{
self.playerResult = RPSElement.tiger
self.playerImageView.image = tiger
}
default:
self.playerImageView.image = UIImage(named: "rule")
}
//電腦隨機出圖 RPSElement CaseIterable to array
let cpu = RPSElement.allCases.randomElement()!
if let cpuImage = cpu.displayImage{// RPSElement UIImage is Optional
self.cpuResult = cpu
self.cpuImageView.image = cpuImage
}
//結果 & 更新計分表
self.checkResult()
}
}
//檢查雙方結果
func checkResult(){
if(playerResult == RPSElement.stick && cpuResult == RPSElement.tiger){
totalWins += 1
}else if(playerResult == RPSElement.tiger && cpuResult == RPSElement.chicken){
totalWins += 1
}else if(playerResult == RPSElement.chicken && cpuResult == RPSElement.worm){
totalWins += 1
}else if(playerResult == RPSElement.worm && cpuResult == RPSElement.stick){
totalWins += 1
}else if(cpuResult == RPSElement.stick && playerResult == RPSElement.tiger){
totalLoses += 1
}else if(cpuResult == RPSElement.tiger && playerResult == RPSElement.chicken){
totalLoses += 1
}else if(cpuResult == RPSElement.chicken && playerResult == RPSElement.worm){
totalLoses += 1
}else if(cpuResult == RPSElement.worm && playerResult == RPSElement.stick){
totalLoses += 1
}else{
resultLabel.text = RPSStatus.draw.result
}
}
}
Demo
Git
教程參考