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

2015-04-05

目次ページへ

ーーーYosemite Xcode6.1.1ーーー
今まで作ってきたものを組み合わせて、ゲームが出来るようにします。とりあえず、きちんと動作しているか確かめる為にカードは表のままにします。また、一つずつ処理を止めます。
とりあえずユーザーは3番目を選ぶとして、1番目だった時(最初にカードを並び替える処理を行う。並び替えの◼︎を出す)、2番目だった時(1番目から引くカードを選ぶ為に選択の◼︎を出す)の例外的な処理を書かなくて良くします。
今回の難易度は結構高いです。

043-01

以下、全体のソースコードです。ボタンを押した時の処理に割り当てられていたものを、押した処理ではなく独立して動くようにしている関数が結構あります。

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
                mine = i
                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()
        step = 0
    }
    
    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()
    }
    
    func hiku() {
        take(step, randm:true)
    }
    
    var eachCardButton: [UIButton] = []
    
    func shuffle() {
        var i = step
        var kl = 0
        var cardList: [Int] = []
        for j in 0 ..< 14 {
            if eachCard[i][j].tag == -1 {
                kl = j
                break
            }
            cardList += [eachCard[i][j].tag]
        }
        
        for j in 0 ..< kl {
            var jj = Int(arc4random_uniform(UInt32(kl) - j))
            eachCard[i][j].tag = cardList[jj]
            for k in jj ..< kl - j - 1 {
                cardList[k] = cardList[k + 1]
            }
        }
        show()
    }
    
    func get() {
        var i = step
        var kl = 0
        var cardList: [Int] = []
        for j in 0 ..< 14 {
            if eachCard[i][j].tag == -1 {
                kl = j
                break
            }
        }

        for j in 0 ..< 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
    }
    
    func btnHikuTouch() {
        take(step, randm:false)
        
        for cardBtn in eachCardButton {
            cardBtn.removeFromSuperview()
        }
        eachCardButton.removeAll()
    }
    
    var eachChangeButton: [UIButton] = []
    
    func btnChangeTouch() {
        var i = mine
        var kl = 0
        for j in 0 ..< 14 {
            if eachCard[i][j].tag == -1 {
                kl = j
                break
            }
        }
        
        for j in 0 ..< 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: "cardChangeTapped:", forControlEvents: .TouchUpInside)
            btn.tag = 0
            eachChangeButton += [btn]
            self.view.addSubview(btn)
        }
    }
    
    func cardChangeTapped(sender: UIButton) {
        var i = mine
        var j = 0
        var k = 0
        for cardBtn in eachChangeButton {
            if cardBtn == sender {
                break;
            }
            k++
        }
        var dis = false
        for cardBtn in eachChangeButton {
            if cardBtn.tag == 1 {
                var change = eachCard[i][j].tag
                eachCard[i][j].tag = eachCard[i][k].tag
                eachCard[i][k].tag = change
                cardBtn.setTitleColor(UIColor.blueColor(), forState: .Normal)
                cardBtn.tag = 0
                dis = true
                show()
                break
            }
            j++
        }
        if dis == false {
            sender.setTitleColor(UIColor.redColor(), forState: .Normal)
            sender.tag = 1
        }
    }
    
    func changeFinishTouch() {
        take(step, randm:true)
        
        for changeBtn in eachChangeButton {
            changeBtn.removeFromSuperview()
        }
        eachChangeButton.removeAll()
    }
    
    var step = 0
    var mine = 0
    
    @IBAction func btnExecTouch(sender: AnyObject) {
        var reMine = mine - 1
        if reMine == -1 {
            reMine = 3
        }
        if step == reMine {
            var i = step
            var kl = 0
            for j in 0 ..< 14 {
                if eachCard[i][j].tag == -1 {
                    kl = j
                    break
                }
            }
            var k = true
            for j in 0 ..< kl {
                if Int(eachCardButton[j].tag) == 1 {
                    k = false
                    break
                }
            }
            if k {
                var alert = UIAlertView()
                alert.title = "選択"
                alert.message = "引くカードを選択して下さい。"
                alert.addButtonWithTitle("OK")
                alert.show()
                return
            }
            btnHikuTouch()
            step++
            if step > 3 {
                step = 0
            }
            btnChangeTouch()
            
        } else if step == mine {
            changeFinishTouch()
            step++
            if step > 3 {
                step = 0
            }
            shuffle()
        } else {
            hiku()
            step++
            if step > 3 {
                step = 0
            }
            shuffle()
        }
        if step == reMine {
            get()
        }
    }
    
}

さて次回は、例外として考慮しなかった部分の作り込みに入ります。1番目と2番目が選ばれた時の処理です。また、配り直しボタンが押された時に青ガイドが出ていたら消す処理も加えます。

目次ページへ

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