#9 驚爆21點

21點遊戲規則:

K、Q、J 和 10 牌都算作 10 點。 A 牌既可算作1 點也可算作11 點,由玩家自己決定。其餘所有2 至9 牌均按其原數值計算,最終兩方卡牌比大小,超過21點爆掉輸,剛好21點贏,過五關勝利。

21點術語

  • 拿牌(HIT) : 再拿一張牌
  • 停牌(STAND) : 不再拿牌
  • 下注(BET) : 下籌碼賭金

成品展示:

定義各個物件的 Outlet

    //playerA B Card
@IBOutlet var playerACardImageViews: [UIView]!
@IBOutlet var playerBCardImageViews: [UIView]!
//PlayerA Suit&Rank
@IBOutlet var cardsRankALabels: [UILabel]!
@IBOutlet var cardsSuitA1Labels: [UILabel]!
@IBOutlet var cardsSuitA2Labels: [UILabel]!
//PlayerB Suit&Rank
@IBOutlet var cardsRankBLabels: [UILabel]!
@IBOutlet var cardsSuitB1Labels: [UILabel]!
@IBOutlet var cardsSuitB2Labels: [UILabel]!
//卡片點數
@IBOutlet var cardsTotalLabels: [UILabel]!
//賭金
@IBOutlet weak var totalBetAmountLabel: UILabel!
@IBOutlet weak var betLabel: UILabel!

@IBOutlet weak var betSegment: UISegmentedControl!
//賭金+− Btn

@IBOutlet weak var minusBtn: UIButton!

@IBOutlet weak var addBtn: UIButton!

定義變數

    var cards = [Card]()
var playerACards = [Card]()
var playerBCards = [Card]()

var index = 1
var aCardPoint = 0
var bCardPoint = 0
var aSum = 0
var bSum = 0
var takeCard = Int.random(in: 0...51)

var betAmount = 0
var totalBetAmount = 100

var controller = UIAlertController()
var action = UIAlertAction()

用UIButton來設置Action來判斷輸贏

       //判斷輸贏 A敵方 B我方
// 當 A > B 時, 有可能會遇到 a>21, a=21, a過五關, a=b, a<b, a>b
if aSum > bSum {
cardsTotalLabels[0].textColor = UIColor.red

//a方點數超過21,a方輸 b方持有賭金加上欲出賭金
if aSum > 21 {
totalBetAmount = totalBetAmount+betAmount
print("aSum > 21, total:\(totalBetAmount)")
controller = UIAlertController(title: "YOU WIN!", message: "🤙🏻🤙🏻🤙🏻\nComputer: \(aSum) Busted!\nBetAmount + \(betAmount)", preferredStyle: .alert)
action = UIAlertAction(title: "OK", style: .default) { (_) in
self.gameInit()
}
controller.addAction(action)
present(controller, animated: true, completion: nil)

//a方BlackJack,a方贏
}else if aSum == 21 {
totalBetAmountLabel.text = "$\(totalBetAmount)"
totalBetAmount = totalBetAmount - betAmount
cardsTotalLabels[0].textColor = UIColor.red
//當a方卡牌超過21點:b方持有賭金為零時,遊戲重新開始
if totalBetAmount <= 0 {
print("a方BlackJack,餘額0")
totalBetAmountLabel.text = "$\(totalBetAmount)"
betAmount = 0
betLabel.text = "$\(betAmount)"
controller = UIAlertController(title: "Game Over!", message: "Computer: BlackJack!\nOpps!\nYour bet amount is \(betAmount)!", preferredStyle: .alert)
action = UIAlertAction(title: "Play Again!", style: .default) { (_) in
self.gameInit()
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
totalBetAmount = 100
//當a方卡牌超過21點:b方持有賭金不是零時,扣掉欲出賭金,繼續下一局
}else{
print("a方BlackJack")
controller = UIAlertController(title: "YOU LOSE!", message: "Computer: BlackJack!\nBetAmount - \(betAmount)", preferredStyle: .alert)
action = UIAlertAction(title: "OK", style: .default) { (_) in
self.gameInit()
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
}
//a方過五關,a贏
}else if aSum <= 21, playerACards.count == 5 {
totalBetAmountLabel.text = "$\(totalBetAmount)"
totalBetAmount = totalBetAmount - betAmount
cardsTotalLabels[0].textColor = UIColor.red
//當a方卡牌過五關:b方持有賭金為零時,遊戲重新開始
if totalBetAmount <= 0 {
print("a方過五關,餘額0")
totalBetAmountLabel.text = "$\(totalBetAmount)"
betAmount = 0
betLabel.text = "$\(betAmount)"
controller = UIAlertController(title: "Game Over!", message: "Computer: Five Card Charlie!\nOpps!\nYour bet amount is \(betAmount)!", preferredStyle: .alert)
action = UIAlertAction(title: "Play Again!", style: .default) { (_) in
self.gameInit()
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
totalBetAmount = 100
//當a方卡牌過五關:b方持有賭金不是零時,扣掉欲出賭金,繼續下一局
}else{
print("a方過五關")
controller = UIAlertController(title: "YOU LOSE!", message: "Computer: Five Card Charlie!\nBetAmount - \(betAmount)", preferredStyle: .alert)
action = UIAlertAction(title: "OK", style: .default) { (_) in
self.gameInit()
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
}

//a>b,a贏
}else if aSum > bSum {
totalBetAmountLabel.text = "$\(totalBetAmount)"
totalBetAmount = totalBetAmount-betAmount
cardsTotalLabels[0].textColor = UIColor.red

//當a>b:b方持有賭金為零時,遊戲重新開始
if totalBetAmount <= 0 {
print("aSum > bSum,餘額0")
totalBetAmountLabel.text = "$\(totalBetAmount)"
betAmount = 0
betLabel.text = "$\(betAmount)"
controller = UIAlertController(title: "Game Over!", message: "Computer: \(aSum)>\(bSum)\nOpps!\nYour bet amount is \(betAmount)!", preferredStyle: .alert)
action = UIAlertAction(title: "Play Again!", style: .default) { (_) in
self.gameInit()
}
totalBetAmount = 100
controller.addAction(action)
present(controller, animated: true, completion: nil)

//當a>b:b方持有賭金不是零時,扣掉欲出賭金,繼續下一局
}else{
print("aSum > bSum")
controller = UIAlertController(title: "YOU LOSE!", message: "BetAmount - \(betAmount)", preferredStyle: .alert)
action = UIAlertAction(title: "OK", style: .default) { (_) in
self.gameInit()
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
}
}

//a=b,平手
}else if aSum == bSum {
print("aSum == bSum")
cardsTotalLabels[0].textColor = UIColor.red
cardsTotalLabels[1].textColor = UIColor.red
controller = UIAlertController(title: "TIE GAME!", message: "", preferredStyle: .alert)
action = UIAlertAction(title: "OK", style: .default) { (_) in
self.gameInit()
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
totalBetAmountLabel.text = "$\(totalBetAmount)"

//a<b,a方輸 b方持有賭金加上欲出賭金
}else if aSum < bSum {
print("aSum < bSum")
totalBetAmount = totalBetAmount+betAmount
cardsTotalLabels[1].textColor = UIColor.red

controller = UIAlertController(title: "YOU WIN!", message: "🤙🏻🤙🏻🤙🏻\nComputer: \(aSum) < \(bSum)\nBetAmount + \(betAmount)", preferredStyle: .alert)
action = UIAlertAction(title: "OK", style: .default) { (_) in
self.gameInit()
}
controller.addAction(action)
present(controller, animated: true, completion: nil)
totalBetAmountLabel.text = "$\(totalBetAmount)"

}
}

參考來源:

GITHUB :

--

--

邱奕軒/Charlie
彼得潘的 Swift iOS / Flutter App 開發教室

IOS 初心者 Resolve to perform what you ought. Perform without fail what you resolve