[iOS study] 撲克牌poker二十一點遊戲開發
By: Yolanda H.
Tags: Arrays, Swift

要做撲克遊戲先要把52張牌做出來：

這裡想用array跟迴圈來做，並把圖片名稱統一改為花色加數字

自創型別

import Foundation struct PokerCards { var Level:Int var Number:Int var PokerName:String var Suit:String }

建立出52張牌

var numbers = ["1","2","3","4","5","6","7","8","9","10","11","12","13"] var suits = ["clubs","diamonds","hearts","spades"] var pokerImageName = "" var card:PokerCards? var cardArray = [PokerCards(Level: 1, Number: 1, PokerName: "clubs1", Suit: "clubs")] func pokerFunc()-> [PokerCards] { for i in 0 ..< suits.count { for j in 0 ..< numbers.count { pokerImageName = suits[i] + numbers[j] card = PokerCards(Level: i+1 , Number: j+1 , PokerName: pokerImageName, Suit: suits[i] ) if let card = card { cardArray += [card] } } } cardArray.removeFirst() return cardArray }

起始洗牌

override func viewDidLoad() { super.viewDidLoad() cardArray = pokerFunc() cardArray.shuffle() }

接著要發牌分配，先發玩家牌，再發莊家牌 ，並且加上分數計算

（以下都接近土法煉鋼，而且很混亂）

var z = 0 var y = 5 var k = 0 var player = 0 var playerA = 0 var bank = 0 var bankA = 0 func dealOneFunc(){

var playerPoint = cardArray[k].Number if cardArray[k].Number > 10 { playerPoint = 10 }else if cardArray[k].Number == 1 { playerPoint = 11 playerA += 1 } player += playerPoint pokers[z].alpha = 1 pokers[z].image = UIImage(named:cardArray[k].PokerName) k += 1 if bank < 17 || bank < player, player <= 21 { var bankPoint = cardArray[k].Number if cardArray[k].Number > 10 { bankPoint = 10 }else if cardArray[k].Number == 1 { bankPoint = 11 bankA += 1 } pokers[y].alpha = 1 pokers[y].image = UIImage(named: cardArray[k].PokerName) bank += bankPoint k += 1 y += 1 } z += 1

}

結算結果輸贏（A記為11點，若會因A而爆牌則A可算為1點。)

func resultFunc(){ if player > 21, playerA != 0 { player -= 10 playerA -= 1 } if bank > 21, bankA != 0 { bank -= 10 bankA -= 1 } if player > 21{ resultLabel.text = "Bust! you lost" resultLabel.textColor = UIColor.red dealButton.isHidden = true passButton.isHidden = true }else if player == 21 { if bank == player { resultLabel.text = "Push" resultLabel.textColor = UIColor.white }else { resultLabel.text = "you win!" resultLabel.textColor = UIColor.yellow dealButton.isHidden = true passButton.isHidden = true } }else if player < 21 { if player > bank { resultLabel.text = "you win!" resultLabel.textColor = UIColor.yellow dealButton.isHidden = true passButton.isHidden = true }else if player <= bank, bank <= 21 { resultLabel.text = "you lost" resultLabel.textColor = UIColor.white dealButton.isHidden = true passButton.isHidden = true }else if player <= bank, bank > 21 { resultLabel.text = "you win!" resultLabel.textColor = UIColor.yellow dealButton.isHidden = true passButton.isHidden = true } } }

func bustFunc(){ if player > 21, playerA != 0 { player -= 10 playerA -= 1 } if bank > 21, bankA != 0 { bank -= 10 bankA -= 1 } if bank <= 21, player > 21{ resultLabel.text = "Bust! you lost" resultLabel.textColor = UIColor.red dealButton.isHidden = true passButton.isHidden = true }else if bank > 21, player <= 21 { resultLabel.text = "you win!" resultLabel.textColor = UIColor.yellow dealButton.isHidden = true passButton.isHidden = true }else if bank > 21, player > 21{ resultLabel.text = "Bust! you lost" resultLabel.textColor = UIColor.red dealButton.isHidden = true passButton.isHidden = true }else if player == 21, bank < 21 { resultLabel.text = "you win!" resultLabel.textColor = UIColor.yellow dealButton.isHidden = true passButton.isHidden = true } }

判斷發完52張牌後的語法

func emptyFunc(){ if k >= 51 { dealButton.isHidden = true passButton.isHidden = true k = 0 pokerBack.alpha = 0 resultLabel.text = "No cards!" cardArray.shuffle() } }

發牌的func

emptyFunc() if z < 4 { dealOneFunc() bustFunc() }else if z == 4 { dealOneFunc() resultFunc() } else { sender.isHidden = true }

與發牌不同的passFunc

emptyFunc() while bank < player { if player <= 21, z < 4, bank < 21, k < 51 { var bankPoint = cardArray[k].Number if cardArray[k].Number > 10 { bankPoint = 10 }else if cardArray[k].Number == 1 { bankPoint = 11 bankA += 1 } pokers[y].alpha = 1 pokers[y].image = UIImage(named: cardArray[k].PokerName) y += 1 k += 1 bank += bankPoint } } resultFunc()

重新開始

@IBAction func restarFunc(_ sender: UIButton) { for i in 0...9 { pokers[i].alpha = 0 } dealButton.isHidden = false passButton.isHidden = false resultLabel.textColor = UIColor.black resultLabel.text = "Black Jack" z = 0 y = 5 player = 0 bank = 0 playerA = 0 bankA = 0 print( "已發張數" + String(k) ) pokerBack.alpha = 1 }

github:

這次重點放在撲克花色的呈現以及分數的比較, 就沒有研究放入動畫了,但模擬過程中發生了幾次畫面當掉（不是死掉）,突然按任何地方都沒反應,不確定是xcode或是語法有問題導致的狀況.