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

2015-01-21

目次ページへ

ーーーMavericks Xcode6.1ーーー
今回は、トランプの基本部分を別クラスに外出しします。

028-01

028-02

以下ソースコードです。

import UIKit

class Card {
    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
    
    init() {
        for i in 0 ..< cardCount {
            check += [false]
        }
        count = cardCount
    }
    
    func reset() {
        for i in 0 ..< cardCount {
            check[i] = false
        }
        count = cardCount
    }
    
    func put() -> (String?, 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]
        }
        
        return (strCard, card)
    }
    
    func disp(i: Int) -> String {
        var strCard: String
        strCard = mark[i / 13] + number[i % 13]
        return strCard
    }
}

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!

    var cardObj:Card = Card()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        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 = cardObj.disp(card)
            }
            eachCard[sender.tag].text = strCard
        }
    }

    var eachCard: [UILabel] = []
    var eachButton: [UIButton] = []

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

    func yaku() -> (Int, String) {
        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
        var msg:String = ""
        if ck > 0 {
            var alert = UIAlertView()
            switch ck {
            case 1:
                mul = 1
                msg = "ワンペア"
            case 2:
                mul = 2
                msg = "ツーペア"
            case 3:
                mul = 3
                msg = "スリーカード"
            case 4:
                mul = 6
                msg = "フルハウス"
            case 6:
                mul = 8
                msg = "フォーカード"
            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
                        msg = "ロイヤルストレートフラッシュ"
                    } else {
                        mul = 9
                        msg = "ロイヤルストレート"
                    }
                } else {
                    if chk {
                        mul = 7
                        msg = "ストレートフラッシュ"
                    } else {
                        mul = 4
                        msg = "ストレート"
                    }
                }
            } else {
                if chk {
                    mul = 5
                    msg = "フラッシュ"
                } else {
                    mul = 0
                    msg = "ブタ"
                }
            }
        }
        return (mul, msg)
    }
    
    func flush() {
        var mul: Int = 0
        var msg: String
        (mul, msg) = yaku()
        var title: String
        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) {
        cardObj.reset()

        for i in 0 ..< 5 {
            (eachCard[i].text, eachCard[i].tag) = cardObj.put()
        }
        btnRe.enabled = true
        var msg:String
        (beforeMul, msg) = yaku()
        lblBefore.text = "基準" + beforeMul.description
    }

    @IBAction func btnReTouch(sender: AnyObject) {
        btnRe.enabled = false
        for i in 0 ..< 5 {
            if eachButton[i].currentTitle == "on" {
                (eachCard[i].text, eachCard[i].tag) = cardObj.put()
                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()
    }
    
}

さて、トランプの基本部分をCardクラスとして外出ししましたが、これは別ファイルにすると1つのファイルが大きくなりすぎなくて良いです。また他で使い回しも出来ます。ただ一つのファイルの方が見通しが良いというのもあります。

028-03

左側フォルダの所で右クリックします。

028-04

New File…を選択します。

028-05

現れたダイアログで上のiOS側のSourceを選択し、Swift Fileを選びます。

028-06

Save As:にCardClassとタイプしてCreateボタンを押します。

028-07

作られたCardClass.swiftファイルにCardクラスを移動します。
以下ソースコード2つです。
CardClassです。

import Foundation

class Card {
    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
    
    init() {
        for i in 0 ..< cardCount {
            check += [false]
        }
        count = cardCount
    }
    
    func reset() {
        for i in 0 ..< cardCount {
            check[i] = false
        }
        count = cardCount
    }
    
    func put() -> (String?, 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]
        }
        
        return (strCard, card)
    }
    
    func disp(i: Int) -> String {
        var strCard: String
        strCard = mark[i / 13] + number[i % 13]
        return strCard
    }
}

メインクラスです。

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!

    var cardObj:Card = Card()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        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 = cardObj.disp(card)
            }
            eachCard[sender.tag].text = strCard
        }
    }

    var eachCard: [UILabel] = []
    var eachButton: [UIButton] = []

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

    func yaku() -> (Int, String) {
        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
        var msg:String = ""
        if ck > 0 {
            var alert = UIAlertView()
            switch ck {
            case 1:
                mul = 1
                msg = "ワンペア"
            case 2:
                mul = 2
                msg = "ツーペア"
            case 3:
                mul = 3
                msg = "スリーカード"
            case 4:
                mul = 6
                msg = "フルハウス"
            case 6:
                mul = 8
                msg = "フォーカード"
            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
                        msg = "ロイヤルストレートフラッシュ"
                    } else {
                        mul = 9
                        msg = "ロイヤルストレート"
                    }
                } else {
                    if chk {
                        mul = 7
                        msg = "ストレートフラッシュ"
                    } else {
                        mul = 4
                        msg = "ストレート"
                    }
                }
            } else {
                if chk {
                    mul = 5
                    msg = "フラッシュ"
                } else {
                    mul = 0
                    msg = "ブタ"
                }
            }
        }
        return (mul, msg)
    }
    
    func flush() {
        var mul: Int = 0
        var msg: String
        (mul, msg) = yaku()
        var title: String
        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) {
        cardObj.reset()

        for i in 0 ..< 5 {
            (eachCard[i].text, eachCard[i].tag) = cardObj.put()
        }
        btnRe.enabled = true
        var msg:String
        (beforeMul, msg) = yaku()
        lblBefore.text = "基準" + beforeMul.description
    }

    @IBAction func btnReTouch(sender: AnyObject) {
        btnRe.enabled = false
        for i in 0 ..< 5 {
            if eachButton[i].currentTitle == "on" {
                (eachCard[i].text, eachCard[i].tag) = cardObj.put()
                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()
    }
    
}

次回はCardクラスのジョーカー対応をしようと思います。

目次ページへ

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