#17 猜拳遊戲 App & 研究 Apple 的剪刀石頭布(RPS)範例

簡單實作黑白配小遊戲

功能

  1. 可以調整獲勝的場次 (最少1場)
  2. 顯示目前的跟電腦玩的比數
  3. 依序判斷剪刀石頭布,再判斷方向性決定勝負

步驟

  1. 準備畫面

2. 設置 IBOutlet 及 參數

let RSP = ["✌🏼", "✊🏻", "🖐🏻"]
let directions = ["👆", "👇", "👈", "👉"]
@IBOutlet weak var winGameLabel: UILabel!
@IBOutlet var winGameStepper: UIStepper!
var currentWinGame: Int = 1 {
didSet {
winGameLabel.text = "\(currentWinGame)"
}
}
@IBOutlet weak var myWinLabel: UILabel!
@IBOutlet weak var computerWinLabel: UILabel!
@IBOutlet weak var computerShowLabel: UILabel!
@IBOutlet weak var myShowLabel: UILabel!
var myWin: Int = 0 {
didSet {
myWinLabel.text = "\(myWin)"
}
}
var computerWin: Int = 0 {
didSet {
computerWinLabel.text = "\(computerWin)"
}
}
var myRSP: String?
var myDirection: String?
var compRSP: String?
var compDirection: String?

3. 設置 IBAction 及 各函式

@IBAction func reset(_ sender: Any) {
print(#function)
newGame()
}
@IBAction func play(_ sender: UIButton) {
if (myWin >= currentWinGame) ||
(computerWin >= currentWinGame) {

resetAlert()
return
}

switch sender.tag {
case 0,1,2:
// RPS
myRSP = RSP[sender.tag]
compRSP = RSP.shuffled().first!
myShowLabel.text = myRSP
computerShowLabel.text = compRSP

if myRSP == compRSP {
let alert = UIAlertController(title: "Draw", message: "Play Again", preferredStyle: .alert)
let closeAction = UIAlertAction(title: "Close", style: .default)
alert.addAction(closeAction)
present(alert, animated: true, completion: nil)
}
break
case 3,4,5,6:
// up, down, left, right
myDirection = directions[sender.tag - 3]
compDirection = directions.shuffled().first!

myShowLabel.text = "\(myRSP!) \(myDirection!)"
computerShowLabel.text = "\(compRSP!) \(compDirection!)"

let (title, msg) = checkGame()

let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
let closeAction = UIAlertAction(title: "Close", style: .default)
alert.addAction(closeAction)
if (myWin >= currentWinGame) ||
(computerWin >= currentWinGame) {
let resetAction = UIAlertAction(title: "New Game", style: .default) {_ in
self.newGame()
}
alert.addAction(resetAction)
}
present(alert, animated: true, completion: nil)
break
default:
break
}
}
func resetAlert() {
let alert = UIAlertController(title: "You Need to Reset", message: "Over WinGame Number : \(currentWinGame) !", preferredStyle: .alert)
let closeAction = UIAlertAction(title: "Close", style: .default) {_ in
self.newGame()
}
alert.addAction(closeAction)
present(alert, animated: true, completion: nil)
}
@IBAction func changeWin(_ sender: UIStepper) {
print(#function, sender.value)
currentWinGame = Int(winGameStepper.value)
}
func checkGame() -> (String, String) {
if myRSP == compRSP {
return ("Draw", "Play Again")
} else if (myRSP == "✌🏼" && compRSP == "🖐🏻") ||
(myRSP == "✊🏻" && compRSP == "✌🏼") ||
(myRSP == "🖐🏻" && compRSP == "✊🏻") {

if myDirection == compDirection {
myWin += 1
return ("YOU Win", "Keep going...")
} else {
return ("Draw", "Play Again")
}

} else {
if myDirection == compDirection {
computerWin += 1
return ("You Lose", "Good Luck...")
} else {
return ("Draw", "Play Again")
}
}
}
func newGame() {
myWin = 0
computerWin = 0
}

成果

GitHub

--

--