[iOS study] 撲克牌poker二十一點遊戲開發
Published in
9 min readApr 6, 2019
要做撲克遊戲先要把52張牌做出來:
這裡想用array跟迴圈來做,並把圖片名稱統一改為花色加數字
自創型別
import Foundationstruct PokerCards {var Level:Intvar Number:Intvar PokerName:Stringvar 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 = 0var y = 5var k = 0var player = 0var playerA = 0var bank = 0var bankA = 0func dealOneFunc(){
var playerPoint = cardArray[k].Numberif cardArray[k].Number > 10 {playerPoint = 10}else if cardArray[k].Number == 1 {playerPoint = 11playerA += 1}player += playerPointpokers[z].alpha = 1pokers[z].image = UIImage(named:cardArray[k].PokerName)k += 1if bank < 17 || bank < player, player <= 21 {var bankPoint = cardArray[k].Numberif cardArray[k].Number > 10 {bankPoint = 10}else if cardArray[k].Number == 1 {bankPoint = 11bankA += 1}pokers[y].alpha = 1pokers[y].image = UIImage(named: cardArray[k].PokerName)bank += bankPointk += 1y += 1}z += 1
}
結算結果輸贏(A記為11點,若會因A而爆牌則A可算為1點。)
func resultFunc(){if player > 21, playerA != 0 {player -= 10playerA -= 1}if bank > 21, bankA != 0 {bank -= 10bankA -= 1}if player > 21{resultLabel.text = "Bust! you lost"resultLabel.textColor = UIColor.reddealButton.isHidden = truepassButton.isHidden = true}else if player == 21 {if bank == player {resultLabel.text = "Push"resultLabel.textColor = UIColor.white}else {resultLabel.text = "you win!"resultLabel.textColor = UIColor.yellowdealButton.isHidden = truepassButton.isHidden = true}}else if player < 21 {if player > bank {resultLabel.text = "you win!"resultLabel.textColor = UIColor.yellowdealButton.isHidden = truepassButton.isHidden = true}else if player <= bank, bank <= 21 {resultLabel.text = "you lost"resultLabel.textColor = UIColor.whitedealButton.isHidden = truepassButton.isHidden = true}else if player <= bank, bank > 21 {resultLabel.text = "you win!"resultLabel.textColor = UIColor.yellowdealButton.isHidden = truepassButton.isHidden = true}}}
func bustFunc(){if player > 21, playerA != 0 {player -= 10playerA -= 1}if bank > 21, bankA != 0 {bank -= 10bankA -= 1}if bank <= 21, player > 21{resultLabel.text = "Bust! you lost"resultLabel.textColor = UIColor.reddealButton.isHidden = truepassButton.isHidden = true}else if bank > 21, player <= 21 {resultLabel.text = "you win!"resultLabel.textColor = UIColor.yellowdealButton.isHidden = truepassButton.isHidden = true}else if bank > 21, player > 21{resultLabel.text = "Bust! you lost"resultLabel.textColor = UIColor.reddealButton.isHidden = truepassButton.isHidden = true}else if player == 21, bank < 21 {resultLabel.text = "you win!"resultLabel.textColor = UIColor.yellowdealButton.isHidden = truepassButton.isHidden = true}}
判斷發完52張牌後的語法
func emptyFunc(){if k >= 51 {dealButton.isHidden = truepassButton.isHidden = truek = 0pokerBack.alpha = 0resultLabel.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].Numberif cardArray[k].Number > 10 {bankPoint = 10}else if cardArray[k].Number == 1 {bankPoint = 11bankA += 1}pokers[y].alpha = 1pokers[y].image = UIImage(named: cardArray[k].PokerName)y += 1k += 1bank += bankPoint}}resultFunc()
重新開始
@IBAction func restarFunc(_ sender: UIButton) {for i in 0...9 {pokers[i].alpha = 0}dealButton.isHidden = falsepassButton.isHidden = falseresultLabel.textColor = UIColor.blackresultLabel.text = "Black Jack"z = 0y = 5player = 0bank = 0playerA = 0bankA = 0print( "已發張數" + String(k) )pokerBack.alpha = 1}
github:
這次重點放在撲克花色的呈現以及分數的比較, 就沒有研究放入動畫了,但模擬過程中發生了幾次畫面當掉(不是死掉),突然按任何地方都沒反應,不確定是xcode或是語法有問題導致的狀況.