LoginSignup
0
0

More than 5 years have passed since last update.

Fizz Buzz (iOS版)

Last updated at Posted at 2017-08-20

SwiftでFizz Buzzを作った時の話

SwiftでFizz Buzzを作りました。
普段Swiftを使う機会が少ないので、間違った記述をしているかもしれません。
またQiita初投稿となるので、その点も含めてご指摘いただければと思います。

ソースコード

ViewController.swift
/**********************************************************
  project FizzBuzzApp
  file    ViewController.swift
  date    2017.08.20
**********************************************************/

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var lblCom: UILabel!
    @IBOutlet weak var lblYou: UILabel!
    @IBOutlet weak var btnOther: UIButton!
    @IBOutlet weak var btnFizz: UIButton!
    @IBOutlet weak var btnBuzz: UIButton!
    @IBOutlet weak var btnFizzBuzz: UIButton!
    @IBOutlet weak var btnRestart: UIButton!

    var num: Int = 0


    override func viewDidLoad() {
        super.viewDidLoad()
        num = 0;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    @IBAction func OtherTouch(_ sender: Any) {
        //otherボタンタッチ時の処理
        //ボタンのキャプションを変更する処理を実装する予定
        num = num + 1
        lblYou.text = num.description
        var hoge = decisionYou(input: num)
        if hoge == false {
            dispAlert()
        }
        var dispStr = computer()
        lblCom.text = dispStr
    }


    @IBAction func FizzTouch(_ sender: Any) {
        //fizzボタンタッチ時の処理
        num = num + 1
        lblYou.text = "Fizz"
        var hoge = decisionYou(input: -1)
        if hoge == false {
            dispAlert()
        }
        var dispStr = computer()
        lblCom.text = dispStr
    }

    @IBAction func BuzzTouch(_ sender: Any) {
        //buzzボタンタッチ時の処理
        num = num + 1
        lblYou.text = "Buzz"
        var hoge = decisionYou(input: -2)
        if hoge == false {
            dispAlert()
        }
        var dispStr = computer()
        lblCom.text = dispStr
    }


    @IBAction func FizzBuzzTouch(_ sender: Any) {
        //fizzbuzzボタンタッチ時の処理
        num = num + 1
        lblYou.text = "Fizz Buzz"
        var hoge = decisionYou(input: -3)
        if hoge == false {
            dispAlert()
        }
        var dispStr = computer()
        lblCom.text = dispStr
    }

    @IBAction func RestartTouch(_ sender: Any) {
        num = 0
        lblCom.text = ""
        lblYou.text = ""
    }


    /**********************************************************
     function  decisionYou
     brief     Decisioned user input
     param     (input)     User input by button
     return    (decision)  Decisioned result

     **********************************************************/

    func decisionYou(input:Int) -> Bool {
        //判定処理
        var decision: Bool?   //decisionがfalseでアウト
        if input > 0 {
            if (input % 15) == 0 {
                decision = false
            }else if(input % 5) == 0 {
                decision = false
            }else if(input % 3) == 0 {
                decision = false
            }else{
                decision = true
            }
        }else{
            switch input {
            case -1:
                if (num % 3) == 0 {
                    decision = true
                }else{
                    decision = false
                }
                break

            case -2:
                if (num % 5) == 0 {
                    decision = true
                }else{
                    decision = false
                }
                break

            case -3:
                if (num % 15) == 0 {
                    decision = true
                }else{
                    decision = false
                }

                break

            default:
                break
            }

        }
        return decision!
    }

    /**********************************************************
     function  dispAlert
     brief     Displayed AlertView
     param     none
     return    none
    **********************************************************/
    func dispAlert() {
        let alert: UIAlertController = UIAlertController(title: "Message", message: "Miss", preferredStyle: UIAlertControllerStyle.alert)
        let defaultAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(action: UIAlertAction!) -> Void in
            print("OK")
        })
        alert.addAction(defaultAction)
        present(alert, animated: true, completion: nil)
    }

    /**********************************************************
     function  computer
     brief     computer logic
     param     none
     return    (dispStr)   Display strings
     **********************************************************/
    func computer() -> String {
        var dispStr: String
        num = num + 1
        if num % 15 == 0{
            dispStr = "Fizz Buzz"
        }else if num % 5 == 0 {
            dispStr = "Buzz"
        }else if num % 3 == 0{
            dispStr = "Fizz"
        }else{
            dispStr = num.description
        }
        return dispStr
    }

}


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0