新Swiftで行こう…第20回「ポーカー5」 田部井保

目次 解説編

 前回の問題は、ペアを判別するという事でした。

 前回の問題の答えの一つです。

//
//  ViewController.swift
//  Poker
//
//  Created by 保 Tabei on 2024/09/09.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        for i in 0 ..< 5 {
            let lbl = UILabel(frame: CGRectMake(0, 0, 50, 21))
            lbl.center = CGPointMake(100 + 50 * CGFloat(i), 250)
            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(100 + 50 * CGFloat(i), 300)
            btn.setTitle("off", for: .normal)
            btn.setTitleColor(UIColor.systemGray, for: .normal)
            btn.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)
            btn.tag = i
            eachButton += [btn]
            self.view.addSubview(btn)
        }
    }

    @objc func btnTapped(sender: UIButton) {
        if sender.currentTitle == "off" {
            sender.setTitle("on", for: .normal)
            eachCard[sender.tag].text = "⬛️"
        } else {
            sender.setTitle("off", for: .normal)
            let 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"]

    enum EnumCard {
        static let Count = 52
    }
    
    var check: [Bool] = [Bool](repeating: false, count: EnumCard.Count)
    
    var count = EnumCard.Count

    var eachCard: [UILabel] = []
    
    var eachButton: [UIButton] = []

    func put(i: Int) {
        var card = 0
        let randInt = Int.random(in: 0 ..< count)
        for i in 0 ... randInt {
            while check[card] {
                card += 1
            }
            if i < randInt {
                card += 1
            }
        }
        check[card] = true
        count -= 1
        var strCard: String
        if card == 52 {
            strCard = "JK"
        } else {
            strCard = mark[card / 13] + number[card % 13]
        }
        eachCard[i].text = strCard
        eachCard[i].tag = card
    }

    func flush() {
        var chk = true
        let mark: Int = eachCard[0].tag / 13
        for i in 1 ..< 5 {
            if mark != eachCard[i].tag / 13 { 
                chk = false
                break
            }
        }
        if chk {
            let alert = UIAlertController()
            alert.title = "フラッシュ"
            alert.message = "フラッシュです"
            alert.addAction(UIAlertAction(title: "OK", style: .default))
            present(alert, animated: true, completion: nil)
        } else {
            var ck = false
            for i in 0 ..< 5 {
                for j in i + 1 ..< 5 {
                    if eachCard[i].tag % 13 == eachCard[j].tag % 13 {
                        ck = true
                    }
                }
            }
            if ck {
                let alert = UIAlertController()
                alert.title = "ペア"
                alert.message = "ペアがあります"
                alert.addAction(UIAlertAction(title: "OK", style: .default))
                present(alert, animated: true, completion: nil)
            }
        }
    }

    @IBAction func btnGoTouch(_ sender: Any) {
        for i in 0 ..< EnumCard.Count {
            check[i] = false
        }
        count = EnumCard.Count
        for i in 0 ..< 5 {
            put(i: i)
        }
        flush()
    }
    
    @IBAction func btnReTouch(_ sender: Any) {
        for i in 0 ..< 5 {
            if eachButton[i].currentTitle == "on" {
                put(i: i)
                eachButton[i].setTitle("off", for: .normal)
            }
        }
        flush()
    }
}

 さて、今回追加したペアの判定部分、

            var ck = false
            for i in 0 ..< 5 {
                for j in i + 1 ..< 5 {
                    if eachCard[i].tag % 13 == eachCard[j].tag % 13 {
                        ck = true
                    }
                }
            }
            if ck {
                let alert = UIAlertController()
                alert.title = "ペア"
                alert.message = "ペアがあります"
                alert.addAction(UIAlertAction(title: "OK", style: .default))
                present(alert, animated: true, completion: nil)
            }

 後半部分は、単にメッセージを出す部分なので、前半部分が肝となります。

 カードを全パターン比較して、同じ番号のものがあるか判別しています。このコードを少し変更する事で、ワンペア、ツーペア、スリーカード、フルハウス、フォーカードの区別が出来ないでしょうか。

目次 解説編

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA