Lameのプログラマの日記

LameというグループでiOSアプリを作っている大学生たちです

Swiftでマインスイーパー!

こんにちは!Lameの牧野です! 今日はここ数日で作っているアプリについて紹介します!

それはマインスイーパーアプリです! タイトルでバレバレでした笑

下の画像が開発中のデモ画像です!まだまだ改良が必要ですが、、

f:id:com_Lame:20180416025441j:plainf:id:com_Lame:20180416025447j:plain
タイトルとプレイ画面!

今のところで苦労した所は、ScrollViewにある画像の更新処理でした。 と言っても上級者の人達にはめちゃくちゃ簡単な処理だと言われそうですが、笑

水曜日にはリリース完了してると思うので、その時にこのブログでも宣伝するので是非インストールお願いします!

以下大変だった部分のソースコード抜粋です!参考になる部分が少しでもあったら嬉しいです! 他に良い書き方などあったら教えてください!

それではまた明後日! Lame 牧野

//ScrollViewを作るメソッド
func makeScrollView(){
        tagNumber = 0

        //scrollView設定
        scrollView.backgroundColor = UIColor.gray
        scrollView.frame.size = CGSize(width: 300, height: 400)
        scrollView.center = self.view.center
        scrollView.contentSize = CGSize(width: 100*collectionX, height: 100*collectionY)
        scrollView.delegate = self
        scrollView.contentOffset = offsetPosition

        // ScrollViewの中身
        for i in 0 ..< collectionY {
            for j in 0 ..< collectionX {
                imageView = UIImageView(image: imageArr[dispImageNo[tagNumber]])
                imageView.frame = CGRect(x:CGFloat(100*j),y:CGFloat(100*i),width:scrollView.contentSize.width/CGFloat(collectionX),height:scrollView.contentSize.height/CGFloat(collectionY))
                
                //タイル画像の設定(タグ付け、レコグナイザ設定)
                tagNumber = tagNumber+1
                imageView.tag = tagNumber
                imageView.isUserInteractionEnabled = true
                let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.singleTapping))
                imageView.addGestureRecognizer(singleTap)
                scrollView.addSubview(imageView)
            }
        }
        self.view.addSubview(scrollView)
    }

   //タップ処理
 @objc func singleTapping(_ sender: UITapGestureRecognizer) {
   //各種設定 
        var clearCount: Int = 0
        var buttonTag : Int = 0
        var bombCount : Int = 0
        var LFlag : Bool = false
        var RFlag : Bool = false

   //tagごとの処理
        if let numButton = sender.view as? UIImageView {
            buttonTag = numButton.tag

    //右端左端の判定
            for i in 1...collectionY{
                if buttonTag == i*collectionX{
                    RFlag = true
                }else if buttonTag == (i-1)*collectionX+1{
                    LFlag = true
                }
            }
            
            //クリア判定
            for i in 0 ..< collectionX*collectionY{
                if dispImageNo[i] == 0{
                    clearCount = clearCount + 1
                }
            }
            //ゲームオーバー判定
            for i in 0 ..< bombNumber{
                if buttonTag == randomBomb[i]{
                    dispImageNo[buttonTag-1] = 10
                    makeScrollView()
                    //画面遷移処理中略
                }
            }
            //周りのbombを数える
            for i in 0 ..< bombNumber{
                if RFlag{
                    if buttonTag-1 == randomBomb[i] {bombCount = bombCount+1}
                    if buttonTag-collectionX == randomBomb[i] {bombCount = bombCount+1}
                    if buttonTag+collectionX == randomBomb[i] {bombCount = bombCount+1}
                    if buttonTag-collectionX-1 == randomBomb[i] {bombCount = bombCount+1}
                    if buttonTag+collectionX-1 == randomBomb[i] {bombCount = bombCount+1}
                }else if LFlag{
                   //中略
                }else{
                   //中略
                }
            }
            
     //Bombの数に合わせてScrollViewを再描画
            if bombCount != 0{
                dispImageNo[buttonTag-1] = bombCount
                offsetPosition = CGPoint(x:scrollView.contentOffset.x,y:scrollView.contentOffset.y)
                point = point + bombCount
                makeScrollView()
            }else{
                dispImageNo[buttonTag-1] = 9
                offsetPosition = CGPoint(x:scrollView.contentOffset.x,y:scrollView.contentOffset.y)
                makeScrollView()
            }
            
            //クリア処理
            if clearCount-1 == bombNumber{
                usrD.set(point, forKey: "point")
                //画面遷移処理中略
            }
        }
    }