// 12 小時後的1A2B遊戲

Pan Pan
Pan Pan
Sep 1, 2018 · 7 min read

今天寫了個1A2B的遊戲

demo如下

ps. 因為我全部寫在同個view,所以程式會有一堆isHidden語法QQ & 畫面我真的不太會設計所以很醜

以下是我的程式碼

////  ViewController.swift//  random////  Created by 潘柏全 on 2018/9/1.//  Copyright © 2018年 Pan. All rights reserved.//import UIKitclass ViewController: UIViewController {@IBOutlet weak var inputTextField: UITextField!@IBOutlet weak var resultLabel: UILabel!@IBOutlet weak var inputTextView: UITextView!@IBOutlet weak var ruleTextView: UITextView!@IBOutlet weak var startButton: UIButton!@IBOutlet weak var restartButton: UIButton!@IBOutlet weak var submitButton: UIButton!var count : Int = 0;var numArray = [String]()var ansArray = [String]()override func viewDidLoad() {super.viewDidLoad()inputTextField.isHidden = trueresultLabel.isHidden = trueinputTextView.isHidden = truerestartButton.isHidden = truesubmitButton.isHidden = true// Do any additional setup after loading the view, typically from a nib.}// 開始遊戲@IBAction func start(_ sender: Any) {// 參數設定(產生4個不重複數字)initParm()ruleTextView.isHidden = truestartButton.isHidden = trueinputTextField.isHidden = falseresultLabel.isHidden = falseinputTextView.isHidden = false}// 重新一局@IBAction func restart(_ sender: Any) {// 參數設定(產生4個不重複數字)initParm()restartButton.isHidden = true}// 送出@IBAction func submit(_ sender: Any) {let input = inputTextField.text!resultLabel.text = "";inputTextField.text = ""// 防呆if verify(input : input){// 運算結果count+=1var resultA : Int = 0;var resultB : Int = 0;for inputIndex in 0...input.count - 1 {var char = input[input.index(input.startIndex, offsetBy: inputIndex)]for ansIndex in 0...3{// 數字正確且位置正確if String(char) == ansArray[ansIndex], inputIndex == ansIndex{resultA+=1}// 數字正確但位置不正確else if String(char) == ansArray[ansIndex]{resultB+=1}}}// 運算結果印在歷史紀錄inputTextView.text.append("\(input)  \(resultA)A\(resultB)B\n")// 成功猜出數字if resultA == 4 {resultLabel.text?.append("恭喜您成功!共使用\(count)次猜對數字")restartButton.isHidden = falsesubmitButton.isHidden = trueinputTextField.isHidden = true}}}// initfunc initParm(){count = 0;numArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]ansArray = []inputTextField.isHidden = falsesubmitButton.isHidden = falseinputTextView.text = ""inputTextField.text = ""resultLabel.text = ""// 產生4個不重複之數字(0~9)for i in 0...3{let randomNum = Int.random(in: 0...numArray.count-1)ansArray.append(numArray[randomNum])numArray.remove(at: randomNum)}}// 防呆驗證func verify(input : String) -> Bool{var result = false// 正規表示式let regex =  "^[0-9]{4}$"let predicate = NSPredicate(format:"SELF MATCHES %@",regex)if input.count == 0 {resultLabel.text?.append("答案不得為空")}else if !predicate.evaluate(with: input){resultLabel.text?.append("答案必須為4個數字")}else if checkRepeat(input : input){resultLabel.text?.append("答案不得有重複之數字")}else {result = true;}return result;}// 重複數字檢查func checkRepeat(input : String) -> Bool{var array = [String]()for inputIndex in 0...input.count - 1 {var char = input[input.index(input.startIndex, offsetBy: inputIndex)]if inputIndex == 0 {array.append(String(char))}else{for arrayIndex in 0...array.count-1{if String(char) == array[arrayIndex]{return true;}}array.append(String(char))}}return false;}}

其中可以討論的部分

  1. 如何產生4個不重複的數字
  2. 如何防呆使用者輸入,是否為數字這裡使用正規表示式(regex)
  3. 如何拆解使用者輸入之數字

哇嗚嗚嗚嗚

彼得潘的 Swift iOS App 開發教室

學習 Swift iOS App 開發的學生作品集

    Pan Pan

    Written by

    Pan Pan

    彼得潘的 Swift iOS App 開發教室

    學習 Swift iOS App 開發的學生作品集

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade