Swiftで行こう…第26回「ポーカー13」(もちゃち)

2015-01-10

目次ページへ

ーーーMavericks Xcode6.1ーーー
さて、最初に出来ていた役分マイナスするという改造ですが、一つの例を示します。

026-01

026-02

026-03

026-04

026-05

026-06

026-07

026-08

ラベルを一つ追加しました。ここで基準となる値を出すようにしました。最初からワンペアが出来ていたら1と出ます。
以下ソースコードです。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var btnRe: UIButton!
    @IBOutlet weak var lblPoint: UILabel!
    @IBOutlet weak var lblBet: UILabel!
    @IBOutlet weak var btnPlus: UIButton!
    @IBOutlet weak var btnMinus: UIButton!
    @IBOutlet weak var lblBefore: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        for i in 0 ..< cardCount {
            check += [false]
        }
        count = cardCount
        point = 10
        betPoint = 0
        display()

        for i in 0 ..< 5 {
            var lbl = UILabel(frame: CGRectMake(0, 0, 50, 21))
            lbl.center = CGPointMake(50 + 50 * CGFloat(i), 50)
            lbl.textAlignment = NSTextAlignment.Center
            lbl.text = "⬛️"
            eachCard += [lbl]
            self.view.addSubview(lbl)
            
            let btn = UIButton(frame: CGRectMake(0, 0, 50, 21)) as UIButton
            btn.center = CGPointMake(50 + 50 * CGFloat(i), 100)
            btn.setTitle("off", forState: .Normal)
            btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
            btn.addTarget(self, action: "btnTapped:", forControlEvents: .TouchUpInside)
            btn.tag = i
            eachButton += [btn]
            self.view.addSubview(btn)
        }
    }

    func btnTapped(sender: UIButton) {
        if sender.currentTitle == "off" {
            sender.setTitle("on", forState: .Normal)
            eachCard[sender.tag].text = "⬛️"
        } else {
            sender.setTitle("off", forState: .Normal)
            var card = eachCard[sender.tag].tag
            var strCard: String
            if card == 52 {
                strCard = "JK"
            } else {
                strCard = mark[card / 13] + number[card % 13]
            }
            eachCard[sender.tag].text = strCard
        }
    }

    let mark: [String] = ["♣️","♦️","♥️","♠️"]
    let number: [String] = ["A","2","3","4","5","6","7","8","9","T","J","Q","K"]
    let cardCount = 52
    var check: [Bool] = []
    var count = 0
    var eachCard: [UILabel] = []
    var eachButton: [UIButton] = []

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func put(i: Int) {
        var card: Int = 0
        var randInt = Int(arc4random_uniform(UInt32(count)))
        for i in 0 ... randInt {
            while check[card] {
                card++
            }
            if i < randInt {
                card++
            }
        }
        check[card] = true
        
        count--
        
        var strCard: String
        if card == 52 {
            strCard = "JK"
        } else {
            strCard = mark[card / 13] + number[card % 13]
        }
        
        eachCard[i].text = strCard
        eachCard[i].tag = card
    }

    func yaku() -> Int {
        var ck:Int = 0
        for i in 0 ..< 5 {
            for j in i + 1 ..< 5 {
                if eachCard[i].tag % 13 == eachCard[j].tag % 13 {
                    ck += 1
                }
            }
        }
        var mul:Int = 0
        if ck > 0 {
            var alert = UIAlertView()
            switch ck {
            case 1:
                mul = 1
            case 2:
                mul = 2
            case 3:
                mul = 3
            case 4:
                mul = 6
            case 6:
                mul = 8
            default:
                mul = 0
            }
        } else {
            var chk:Bool = true
            var mark: Int = eachCard[0].tag / 13
            for i in 1 ..< 5 {
                if mark != eachCard[i].tag / 13 {
                    chk = false
                    break
                }
            }
            var min:Int = 0
            var max:Int = 0
            min = eachCard[0].tag % 13
            max = eachCard[0].tag % 13
            var isAce:Bool = false
            var isKing:Bool = false
            for i in 0 ..< 5 {
                if eachCard[i].tag % 13 == 0 {
                    isAce = true
                }
                if eachCard[i].tag % 13 == 12 {
                    isKing = true
                }
            }
            var up:Bool = isAce & isKing
            for i in 1 ..< 5 {
                var val = eachCard[i].tag % 13
                if up {
                    if val < 4 {
                        val = val + 13
                    }
                }
                if val < min {
                    min = val
                }
                if max < val {
                    max = val
                }
            }
            if max - min == 4 {
                if max == 13 {
                    if chk {
                        mul = 10
                    } else {
                        mul = 9
                    }
                } else {
                    if chk {
                        mul = 7
                    } else {
                        mul = 4
                    }
                }
            } else {
                if chk {
                    mul = 5
                } else {
                    mul = 0
                }
            }
        }
        return mul
    }
    
    func flush() {
        var mul = yaku()
        var msg: String
        var title: String
        switch mul {
        case 1:
            msg = "ワンペア"
        case 2:
            msg = "ツーペア"
        case 3:
            msg = "スリーカード"
        case 6:
            msg = "フルハウス"
        case 8:
            msg = "フォーカード"
        case 10:
            msg = "ロイヤルストレートフラッシュ"
        case 9:
            msg = "ロイヤルストレート"
        case 7:
            msg = "ストレートフラッシュ"
        case 4:
            msg = "ストレート"
        case 5:
            msg = "フラッシュ"
        case 0:
            msg = "ブタ"
        default:
            msg = ""
        }
        title = msg
        var realMul: Int
        realMul = mul - beforeMul
        if realMul < 0 {
            realMul = 0
        }
        point = point + betPoint * realMul
        betPoint = 0
        if point == 0 {
            title = "ゲームオーバー"
            msg = "ゲームオーバー"
            point = 10
        }
        var alert = UIAlertView()
        alert.addButtonWithTitle("OK")
        alert.title = title
        alert.message = msg + "です"
        alert.show()
        display()
    }
    
    var beforeMul:Int = 0
    
    @IBAction func btnGoTouch(sender: AnyObject) {
        for i in 0 ..< cardCount {
            check[i] = false
        }
        count = cardCount

        for i in 0 ..< 5 {
            put(i)
        }
        btnRe.enabled = true
        beforeMul = yaku()
        lblBefore.text = "基準" + beforeMul.description
    }

    @IBAction func btnReTouch(sender: AnyObject) {
        btnRe.enabled = false
        for i in 0 ..< 5 {
            if eachButton[i].currentTitle == "on" {
                put(i)
                eachButton[i].setTitle("off", forState: .Normal)
            }
        }
        flush()
    }
    
    var point: Int = 0
    var betPoint: Int = 0
    
    func display() {
        lblPoint.text = point.description
        lblBet.text = betPoint.description
    }
    
    @IBAction func btnPlusTouch(sender: AnyObject) {
        if point > 0 {
            point--
            betPoint++
        }
        display()
    }
    
    @IBAction func btnMinusTouch(sender: AnyObject) {
        if betPoint > 0 {
            betPoint--
            point++
        }
        display()
    }
}

ゲームの難易度が高くなり緊張感が出て来ました。さて次回はyaku関数で得点の他に役名を返すようにしたいと思います。

目次ページへ

Since1991 © Shimayugu All Rights Reserved.
アップルおよびアップルのロゴは、米国および他の国々で登録されたApple Inc.の商標です。
湘南マッキントッシュユーザーズグループは、独立したユーザグループで、Apple Japan合同会社が権限を与えた団体、支援する団体、またはその他に承認する団体ではありません。