Swiftで行こう…第12回「トランプ4」(もちゃち)

2014-11-16

目次ページへ

ーーーMavericks Xcode6.1ーーー

012-01

012-02

012-03

012-04

前回の答えの一例です。
以下ソースコードです。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var lblCard: UILabel!
    @IBOutlet weak var lblCount: UILabel!

    @IBOutlet weak var lblKA: UILabel!
    @IBOutlet weak var lblK2: UILabel!
    @IBOutlet weak var lblK3: UILabel!
    @IBOutlet weak var lblK4: UILabel!
    @IBOutlet weak var lblK5: UILabel!
    @IBOutlet weak var lblK6: UILabel!
    @IBOutlet weak var lblK7: UILabel!
    @IBOutlet weak var lblK8: UILabel!
    @IBOutlet weak var lblK9: UILabel!
    @IBOutlet weak var lblKT: UILabel!
    @IBOutlet weak var lblKJ: UILabel!
    @IBOutlet weak var lblKQ: UILabel!
    @IBOutlet weak var lblKK: 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
    }

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

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

    @IBAction func btnGoTouch(sender: AnyObject) {
        if count == 0 {
            for i in 0 ..< cardCount {
                check[i] = false
            }
            lblKA.text = ""
            lblK2.text = ""
            lblK3.text = ""
            lblK4.text = ""
            lblK5.text = ""
            lblK6.text = ""
            lblK7.text = ""
            lblK8.text = ""
            lblK9.text = ""
            lblKT.text = ""
            lblKJ.text = ""
            lblKQ.text = ""
            lblKK.text = ""
            var alert = UIAlertView()
            alert.title = "初期化"
            alert.message = "カードを配り直します"
            alert.addButtonWithTitle("OK")
            alert.show()
            count = cardCount
        }

        var card: Int = 0
        var randInt = Int(arc4random_uniform(UInt32(count)))
        var i = 0
        do {
            while check[card] {
                card++
            }
            if i == randInt {
                break
            } else {
                i++
                card++
            }
        } while true

        check[card] = true

        count--
        lblCount.text = count.description

        if card == 52 {
            lblCard.text = "JK"
        } else {
            lblCard.text = mark[card / 13] + number[card % 13]
        }

        switch card {
        case 0:
            lblKA.text = lblCard.text
        case 1:
            lblK2.text = lblCard.text
        case 2:
            lblK3.text = lblCard.text
        case 3:
            lblK4.text = lblCard.text
        case 4:
            lblK5.text = lblCard.text
        case 5:
            lblK6.text = lblCard.text
        case 6:
            lblK7.text = lblCard.text
        case 7:
            lblK8.text = lblCard.text
        case 8:
            lblK9.text = lblCard.text
        case 9:
            lblKT.text = lblCard.text
        case 10:
            lblKJ.text = lblCard.text
        case 11:
            lblKQ.text = lblCard.text
        default:
            lblKK.text = lblCard.text
        }
    }
}

肝の部分が

        var card: Int = 0
        var randInt = Int(arc4random_uniform(UInt32(count)))
        var i = 0
        do {
            while check[card] {
                card++
            }
            if i == randInt {
                break
            } else {
                i++
                card++
            }
        } while true

となります。中々難しい処理だと思います。もっと簡単に出来たら良いのですが。
こうも書けます。

        var card: Int = 0
        var randInt = Int(arc4random_uniform(UInt32(count)))
        while randInt > 0 {
            while check[card] {
                card++
            }
            randInt--
            card++
        }
        while check[card] {
            card++
        }

この様にも書けます。

        var card: Int = 0
        var randInt = Int(arc4random_uniform(UInt32(count)))
        while randInt >= 0 {
            while check[card] {
                card++
            }
            randInt--
            if randInt > 0 {
                card++
            }
        }

この様にも書けます。

        var card: Int = 0
        var randInt = Int(arc4random_uniform(UInt32(count)))
        for i in 0 ... randInt {
            while check[card] {
                card++
            }
            if i < randInt {
                card++
            }
        }

同じ事を実現させるのに書き方は沢山あります。

さて、♣️だけではなくて、他のマークも出したい所ですが、ラベルを並べるのだけでも大変です。そこでラベルをコードから生成させます。⬛️は「しかく」で出て来ます。

012-05

012-06

コードから生成させる例です。とりあえず♣️だけです。
以下ソースコードです。

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var lblCard: UILabel!
    @IBOutlet weak var lblCount: 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]
            var lbl = UILabel(frame: CGRectMake(0, 0, 200, 21))
            lbl.center = CGPointMake(160, 50 + CGFloat(i) * 30)
            lbl.textAlignment = NSTextAlignment.Center
            lbl.text = "⬛️"
            eachCard += [lbl]
            self.view.addSubview(lbl)
        }
        count = cardCount
    }

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

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

    @IBAction func btnGoTouch(sender: AnyObject) {
        if count == 0 {
            for i in 0 ..< cardCount {
                check[i] = false
                eachCard[i].text = "⬛️"
            }
            var alert = UIAlertView()
            alert.title = "初期化"
            alert.message = "カードを配り直します"
            alert.addButtonWithTitle("OK")
            alert.show()
            count = cardCount
        }

        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--
        lblCount.text = count.description

        if card == 52 {
            lblCard.text = "JK"
        } else {
            lblCard.text = mark[card / 13] + number[card % 13]
        }
        eachCard[card].text = lblCard.text
    }
}

さて問題です。♣️だけでなく、他のマークも出すようにするにはどうすれば良いでしょう。とりあえずJKは出さなくて良いです。

目次ページへ

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