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

2014-11-23

目次ページへ

ーーーMavericks Xcode6.1ーーー
前回の問題の答の一つです。
014-01

014-02
以下ソースコードです。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var lblCard1: UILabel!
    @IBOutlet weak var lblCard2: UILabel!
    @IBOutlet weak var lblCard3: UILabel!
    @IBOutlet weak var lblCard4: UILabel!
    @IBOutlet weak var lblCard5: 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
    }

    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

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

            switch i {
            case 0:
                lblCard1.text = strCard
            case 1:
                lblCard2.text = strCard
            case 2:
                lblCard3.text = strCard
            case 3:
                lblCard4.text = strCard
            case 4:
                lblCard5.text = strCard
            default:
                lblCard1.text = strCard
            }
        }
    }
}

5枚程度でもラベルをプログラムで生成する方が良いかも知れません。

014-03

014-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 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] = []

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

次回は、捨てるカードを選択出来るようにします。これは結構難しいです。

目次ページへ

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