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

2014-12-07

目次ページへ

ーーーMavericks Xcode6.1ーーー
前回の問題は、ペアの詳細を判別出来ないかという事でした。
019-01

019-02

019-03

019-04

019-05

019-06
前回の問題の答の一つです。
以下ソースコードです。

import UIKit

class ViewController: UIViewController {
    
    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

        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 flush() {
        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
            }
        }
        if chk {
            var alert = UIAlertView()
            alert.title = "フラッシュ"
            alert.message = "フラッシュです"
            alert.addButtonWithTitle("OK")
            alert.show()
        } else {
            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 msg: String
            switch ck {
            case 1:
                msg = "ワンペア"
            case 2:
                msg = "ツーペア"
            case 3:
                msg = "スリーカード"
            case 4:
                msg = "フルハウス"
            case 6:
                msg = "フォーカード"
            default:
                msg = ""
            }
            if ck > 0 {
                var alert = UIAlertView()
                alert.title = "ペア"
                alert.message = msg + "です"
                alert.addButtonWithTitle("OK")
                alert.show()
            }
        }
    }
    
    @IBAction func btnGoTouch(sender: AnyObject) {
        for i in 0 ..< cardCount {
            check[i] = false
        }
        count = cardCount

        for i in 0 ..< 5 {
            put(i)
        }
        flush()
    }

    @IBAction func btnReTouch(sender: AnyObject) {
        for i in 0 ..< 5 {
            if eachButton[i].currentTitle == "on" {
                put(i)
                eachButton[i].setTitle("off", forState: .Normal)
            }
        }
        flush()
    }
}

このように役の判定はとてもシンプルに出来ます。もちろん他の方法でも良いです。

ここで、ペアがあったら、フラッシュ、ストレート(番号が5つ並ぶ)はなく、ペアが無い場合にフラッシュとストレートがある可能性があります。フラッシュとストレートは同時に起こる可能性もあります。作って来たシステムでは、フラッシュかどうか調べ、フラッシュでなければペアを調べるという形になっています。ここは、まずペアかどうか調べ、ペアでない場合に、フラッシュ、ストレートを調べるという形にします。

019-07

019-08

019-09

019-10

019-11

以下ソースコードです。

import UIKit

class ViewController: UIViewController {
    
    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

        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 flush() {
        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 msg: String
        switch ck {
        case 1:
            msg = "ワンペア"
        case 2:
            msg = "ツーペア"
        case 3:
            msg = "スリーカード"
        case 4:
            msg = "フルハウス"
        case 6:
            msg = "フォーカード"
        default:
            msg = ""
        }
        if ck > 0 {
            var alert = UIAlertView()
            alert.title = "ペア"
            alert.message = msg + "です"
            alert.addButtonWithTitle("OK")
            alert.show()
        } 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
                }
            }
            if chk {
                var alert = UIAlertView()
                alert.title = "フラッシュ"
                alert.message = "フラッシュです"
                alert.addButtonWithTitle("OK")
                alert.show()
            }
        }
    }
    
    @IBAction func btnGoTouch(sender: AnyObject) {
        for i in 0 ..< cardCount {
            check[i] = false
        }
        count = cardCount

        for i in 0 ..< 5 {
            put(i)
        }
        flush()
    }

    @IBAction func btnReTouch(sender: AnyObject) {
        for i in 0 ..< 5 {
            if eachButton[i].currentTitle == "on" {
                put(i)
                eachButton[i].setTitle("off", forState: .Normal)
            }
        }
        flush()
    }
}

さて問題です。ストレートを判定するにはどうすれば良いでしょう。ストレートとは5枚が連番になる組み合わせです。本来ストレートはK(13)の次にA(1)が続くのですが、今回は、Kの次にAが続かないとして良いです。AからKまでの中で続いていたらストレートとします。

目次ページへ

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