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

2014-11-29

目次ページへ

ーーーMavericks Xcode6.1ーーー
前回の問題は、カードを何枚か捨てて、再配布するというものでした。

016-01

016-02

016-03

016-04

さて、前回の問題の答えです。
以下ソースコードです。

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.
    }

    @IBAction func btnGoTouch(sender: AnyObject) {
        for i in 0 ..< cardCount {
            check[i] = false
        }
        count = cardCount

        for i in 0 ..< 5 {
            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
        }
    }

    @IBAction func btnReTouch(sender: AnyObject) {
        for i in 0 ..< 5 {
            if eachButton[i].currentTitle == "on" {
                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
                
                eachButton[i].setTitle("off", forState: .Normal)
            }
        }
    }
}

016-05

016-06

016-07

016-08

最初に配る部分と、追加で配る部分で大分重複があります。これを一つにまとめます。まとめておくと後で修正が楽になります。

以下ソースコードです。

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
    }
    
    @IBAction func btnGoTouch(sender: AnyObject) {
        for i in 0 ..< cardCount {
            check[i] = false
        }
        count = cardCount

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

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

同じ処理をまとめるとすっきりしますね。

さて次回からは、ポーカーの役の判別に入って行きます。まずは簡単な所から、フラッシュを判別する処理を作ってみて下さい。フラッシュとは、5枚とも同じマークになる形です。

目次ページへ

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