Swiftで行こう…第40回「ババ抜き11」(もちゃち)

2015-03-28

目次ページへ

ーーーYosemite Xcode6.1.1ーーー
btnHikuTouch関数とhiku関数をまとめます。
以下、全体のソースコードです。

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        put53()
    }

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

    var eachCard: [[UILabel]] = [[],[],[],[]]
    var eachButton: [UIButton] = []
    var cardObj:Card = Card(cdCnt: Card.CardSum.joker)
    
    func put53() {
        for i in 0 ..< 4 {
            for j in 0 ..< 14 {
                var lbl = UILabel(frame: CGRectMake(0, 0, 50, 21))
                lbl.center = CGPointMake(50 + 40 * CGFloat(j % 7), 50 + 120 * CGFloat(i) + 50 * CGFloat(j / 7))
                lbl.textAlignment = NSTextAlignment.Center
                eachCard[i] += [lbl]
                self.view.addSubview(lbl)
            }
            let btn = UIButton(frame: CGRectMake(0, 0, 50, 21)) as UIButton
            btn.center = CGPointMake(20, 50 + 120 * CGFloat(i) + 25)
            btn.setTitle("⚪️", forState: .Normal)
            btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
            btn.addTarget(self, action: "btnTapped:", forControlEvents: .TouchUpInside)
            btn.tag = i
            eachButton += [btn]
            self.view.addSubview(btn)
        }
        share()
        show()
    }
    
    var btnEnable:Bool = true
    
    func btnTapped(sender: UIButton) {
        if btnEnable == false {
            return
        }
        for i in 0 ..< 4 {
            if eachButton[i] == sender {
                if sender.currentTitle == "⚪️" {
                    sender.setTitle("⚫️", forState: .Normal)
                } else {
                    sender.setTitle("⚪️", forState: .Normal)
                }
            } else {
                eachButton[i].setTitle("⚪️", forState: .Normal)
            }
        }
    }
    
    func share() {
        btnEnable = true
        cardObj.reset()
        for i in 0 ..< 4 {
            for j in 0 ..< 14 {
                if i != 0 && j == 13 {
                    eachCard[i][j].tag = -1
                } else {
                    var ii: Int
                    ii = cardObj.put()
                    eachCard[i][j].text = cardObj.disp(ii)
                    eachCard[i][j].tag = ii
                }
            }
        }
    }

    func show() {
        for i in 0 ..< 4 {
            for j in 0 ..< 14 {
                if eachCard[i][j].tag >= 0 {
                    eachCard[i][j].text = cardObj.disp(eachCard[i][j].tag)
                } else {
                    eachCard[i][j].text = " "
                }
            }
        }
    }
    
    @IBAction func btnGoTouch(sender: AnyObject) {
        share()
        show()
    }
    
    @IBAction func btnPairTouch(sender: AnyObject) {
        var selected:Bool = false
        for i in 0 ..< 4 {
            if eachButton[i].currentTitle == "⚫️" {
                selected = true
                break
            }
        }
        if !selected {
            var alert = UIAlertView()
            alert.title = "選択"
            alert.message = "4組のうちのどれかを選択して下さい。"
            alert.addButtonWithTitle("OK")
            alert.show()
            return
        }
        btnEnable = false
        for i in 0 ..< 4 {
            for j in 0 ..< 13 {
                if (eachCard[i][j].tag == 52) || (eachCard[i][j].tag == -1) {
                    continue
                }
                for k in j + 1 ..< 14 {
                    if (eachCard[i][k].tag == 52) || (eachCard[i][k].tag == -1) {
                        continue
                    }
                    if (eachCard[i][j].tag % 13) == (eachCard[i][k].tag % 13) {
                        eachCard[i][j].tag = -1
                        eachCard[i][k].tag = -1
                        break
                    }
                }
            }
            var find: Bool = false
            var k: Int = 0
            for j in 0 ..< 14 {
                if eachCard[i][j].tag == -1 {
                    find = true
                } else {
                    if find {
                        eachCard[i][k].tag = eachCard[i][j].tag
                        eachCard[i][j].tag = -1
                    }
                    k++
                }
            }
        }
        show()
    }
    
    func take(i: Int, randm: Bool) {
        var kl = 0
        for j in 0 ..< 14 {
            if eachCard[i][j].tag == -1 {
                kl = j
                break
            }
        }
        var k = 0
        if randm {
            k = Int(arc4random_uniform(UInt32(kl)))
        } else {
            for j in 0 ..< kl {
                if Int(eachCardButton[j].tag) == 1 {
                    k = j
                    break
                }
            }
        }
        
        var ii = 0
        if i < 3 {
            ii = i + 1
        } else {
            ii = 0
        }
        var stop: Bool = false
        if eachCard[i][k].tag != 52 {
            for jj in 0 ..< 14 {
                if stop {
                    eachCard[ii][jj - 1].tag = eachCard[ii][jj].tag
                }
                if eachCard[ii][jj].tag == -1 {
                    break
                }
                if eachCard[ii][jj].tag == 52 {
                    continue
                }
                if (eachCard[ii][jj].tag % 13) == (eachCard[i][k].tag % 13) {
                    stop = true
                }
            }
        }
        if stop == false {
            for jj in 0 ..< 14 {
                if eachCard[ii][jj].tag == -1 {
                    eachCard[ii][jj].tag = eachCard[i][k].tag
                    break
                }
            }
        }
        for j in k + 1 ..< 14 {
            eachCard[i][j - 1].tag = eachCard[i][j].tag
        }
        eachCard[i][13].tag = -1
        show()
    }
    
    @IBAction func hiku(sender: AnyObject) {
        take(0, randm:true)
    }
    
    var eachCardButton: [UIButton] = []
    
    @IBAction func shuffle(sender: AnyObject) {
        var i = 1
        var kl: UInt32 = 0
        var cardList: [Int] = []
        for j in 0 ..< 14 {
            if eachCard[i][j].tag == -1 {
                kl = UInt32(j)
                break
            }
            cardList += [eachCard[i][j].tag]
        }
        
        for j in 0 ..< Int(kl) {
            var jj = Int(arc4random_uniform(kl - j))
            eachCard[i][j].tag = cardList[jj]
            for k in jj ..< Int(kl - j) - 1 {
                cardList[k] = cardList[k + 1]
            }
        }
        show()

        for j in 0 ..< Int(kl) {
            let btn = UIButton(frame: CGRectMake(0, 0, 50, 21))
            btn.center = CGPointMake(50 + 40 * CGFloat(j % 7), 50 + 120 * CGFloat(i) + 50 * CGFloat(j / 7))
            btn.setTitle("◼︎", forState: .Normal)
            btn.setTitleColor(UIColor.blueColor(), forState: .Normal)
            btn.addTarget(self, action: "cardBtnTapped:", forControlEvents: .TouchUpInside)
            btn.tag = 0
            eachCardButton += [btn]
            self.view.addSubview(btn)
        }
    }

    func cardBtnTapped(sender: UIButton) {
        for cardBtn in eachCardButton {
            cardBtn.setTitleColor(UIColor.blueColor(), forState: .Normal)
            cardBtn.tag = 0
        }
        sender.setTitleColor(UIColor.redColor(), forState: .Normal)
        sender.tag = 1
    }
    
    @IBAction func btnHikuTouch(sender: AnyObject) {
        take(1, randm:false)
        
        for cardBtn in eachCardButton {
            cardBtn.removeFromSuperview()
        }
        eachCardButton.removeAll()
    }
    
}

次回は、自分のカードを並べ替える処理を追加します。

目次ページへ

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