--

HW11: 骰子比大小之搶紅包遊戲

來和 Peter 搶紅包啦!!

遊戲敘述:
由 Peter 這裡提供紅包 2000 元和一位同學搶紅包
>>兩人各自有三顆骰子,比較雙方點數總合
>>兩人各自初始為 0 元,勝方依據壓注的金額增加紅包總額
>>搶完後若不滿意 Peter 搶回得紅包金額可以按重新開始
>>可決定壓注的金額 100/500/all in
>>用 alert 顯示結果: 紅包還沒搶光時,alert 將顯示 ok & 再玩一次的按鈕,搶光後顯示紅包沒了
>>金額小於賭注時會自動all in

學習point(Coding)
*for 迴圈
*outlet collection
*present alert controller

Storyboard design

DiceViewController(UIViewController)

storyboard

Swift function

peter 的三顆骰子為 outlet collection: cpuDiceImageView: [UIImageView]!
學生的三顆骰子為 outlet collection: playerDiceImageView: [UIImageView]!

[Code 流程]:
1. 參數初始: restart in viewDidLoad()
2. 點擊 Play Button -> IBAction func play: playGame
3. playGame:
當剩餘紅包錢小於賭金時 playGame 會自動 all in
random 骰子(for loop & random)
比較完大小後顯示 alert(present alert)
依據選擇的紅包數更新數值
當 playGame 後的剩餘紅包錢若為 0, play button 變為重新再來
4. 點擊重新再來 -> IBAction func play: restart

DiceViewController

import UIKit

class DiceViewController: UIViewController {

@IBOutlet var cpuDiceImageView: [UIImageView]!
@IBOutlet var playerDiceImageView: [UIImageView]!

@IBOutlet weak var cpuMoney: UILabel!
@IBOutlet weak var playerMoney: UILabel!

@IBOutlet weak var redEnvelope: UILabel!

@IBOutlet weak var typeSegmentedControl: UISegmentedControl!
@IBOutlet weak var betUISegmentedControl: UISegmentedControl!
@IBOutlet weak var startUIButton: UIButton!

var redEnvelopeValue = 2000
var cpuMoneyValue = 0
var playerMoneyValue = 0

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
restart()
}


@IBAction func play(_ sender: Any) {

if startUIButton.currentTitle == "Play" {
playGame()
}else{ //
restart()
}

}

func playGame(){

var betMoney: Int = 0
switch betUISegmentedControl.selectedSegmentIndex {
case 0:
betMoney = 100
case 1:
betMoney = 500
case 2:
betMoney = redEnvelopeValue
default:
betMoney = 0
}
if redEnvelopeValue < betMoney {// 錢不夠就自動all in
betMoney = redEnvelopeValue
}


//random cpu dice
var cpuPoint = 0
for cpuDice in cpuDiceImageView{
let number = Int.random(in: 1...6)
cpuPoint = cpuPoint + number
cpuDice.image = UIImage(systemName: "die.face.\(number).fill")
}
//random player dice
var playerPoint = 0
for playerice in playerDiceImageView{
let number = Int.random(in: 1...6)
playerPoint = playerPoint + number
playerice.image = UIImage(systemName: "die.face.\(number).fill")
}

var alertTitle = ""
var alertMessage = ""
if(playerPoint == cpuPoint){//平局處理
alertTitle = "平手"
alertMessage = "都沒有搶到紅包"

}else if(typeSegmentedControl.selectedSegmentIndex == 0 && playerPoint < cpuPoint) || (typeSegmentedControl.selectedSegmentIndex == 1 && playerPoint > cpuPoint){//勝局處理
playerMoneyValue = playerMoneyValue + betMoney
alertTitle = "YA~~"
alertMessage = "搶到紅包了, 總共搶到 $\(playerMoneyValue)"
playerMoney.text = "$\(playerMoneyValue)"
redEnvelopeValue = redEnvelopeValue - betMoney

}else{//敗局處理
cpuMoneyValue = cpuMoneyValue + betMoney
alertTitle = "OH NO~~"
alertMessage = "沒搶到紅包, Peter 總共搶回 $\(cpuMoneyValue)"
cpuMoney.text = "$\(cpuMoneyValue)"
redEnvelopeValue = redEnvelopeValue - betMoney
}
redEnvelope.text = "$\(redEnvelopeValue)"

//show alert
let controller = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
if(redEnvelopeValue > 0){
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
controller.addAction(okAction)
let playAction = UIAlertAction(title: "再來一次", style: .default){ _ in
self.playGame()
}
controller.addAction(playAction)
}else{
let okAction = UIAlertAction(title: "紅包沒了", style: .default){ _ in
self.startUIButton.setTitle("重新再來", for: .normal)
}
controller.addAction(okAction)
}
present(controller, animated: true)

}

func restart(){
startUIButton.setTitle("Play", for: .normal)
redEnvelopeValue = 2000
cpuMoneyValue = 0
playerMoneyValue = 0
redEnvelope.text = "$\(redEnvelopeValue)"
cpuMoney.text = "$\(cpuMoneyValue)"
playerMoney.text = "$\(playerMoneyValue)"
typeSegmentedControl.selectedSegmentIndex = 0
betUISegmentedControl.selectedSegmentIndex = 0
for cpuDice in cpuDiceImageView{
cpuDice.image = UIImage(systemName: "die.face.1.fill")
}
for playerice in playerDiceImageView{
playerice.image = UIImage(systemName: "die.face.1.fill")
}
}

}

Demo

demo

Git

參考教程

  • 玩家原本有一千元,猜對增加一百元,猜錯減少一百元。

--

--