1A2B小遊戲
Published in
9 min readNov 8, 2020
Class
func
random
isHidden
if else
append
for in
removeAll
removeLast
Array( ).count
Timer( )
判斷過的數字即不能再使用,否則出現重複數字時,B的狀況會重複計算
要先判斷有無A,再判斷有A的B跟無A的B,否則A是在最後數字的時候,B會出問題
陣列在新增或者刪除時都要注意是否Out Of Range
@IBOutlet weak var recordTextView: UITextView!@IBOutlet var questionLabel: [UILabel]!@IBOutlet var answerLabel: [UILabel]!var questions = [Question]() //自建屬性有content字串 與 isUsed布林值var answers = [Answer]() //自建屬性有content字串 與 isUsed布林值var recordContent = "" //紀錄輸入的數字內容var count = 0 //紀錄keyIn數字的次數var guessCount = 0 //記錄猜的次數
func makeQuestions() {for index in 0...3 {let random = Int.random(in: 0...9)questionLabel[index].text = "\(random)"let question = Question()question.content = "\(random)"question.isUsed = falsequestions.append(question)questionLabel[index].isHidden = trueanswerLabel[index].text = ""}}
@IBAction func numberKeyIn(_ sender: UIButton) {if count <= 3 {answerLabel[count].text = sender.currentTitle!let answer = Answer()answer.content = sender.currentTitle!answer.isUsed = falseanswers.append(answer)recordContent.append(sender.currentTitle!)count += 1}print("recordContent",recordContent)print("count",count)}
@IBAction func judgment(_ sender: Any) {guessCount += 1var A = 0var B = 0if answers.count == 4 { //輸入完整才進行判斷,否則會陣列會out of rangefor i in 0...3 {for n in 0...3 {if questions[i].content == answers[n].content && i == n { //先判斷有無A,即內容相同,位置也相同,判斷過的數字為trueA += 1print("i",i)print("n",n)print("questions",questions[i].content)print("answers",answers[n].content)questions[i].isUsed = trueanswers[n].isUsed = truefor o in 0...3 {for k in 0...3 {if questions[o].content == answers[k].content && o != k && questions[o].isUsed == false && answers[k].isUsed == false{ //判斷完A後,判斷剩下有無B,即內容相同,位置不同,判斷過的數字為true,且不可重複判斷B += 1print("o",o)print("k",k)print("questions",questions[o].content)print("answers",answers[k].content)questions[o].isUsed = trueanswers[k].isUsed = true}}}}}}if A == 0 { //全都沒有A的情況執行for o in 0...3 {for k in 0...3 {if questions[o].content == answers[k].content && o != k && questions[o].isUsed == false && answers[k].isUsed == false{ //判斷有無B,即內容相同,位置不同,判斷過的數字為true,且不可重複判斷B += 1print("o",o)print("k",k)print("questions",questions[o].content)print("answers",answers[k].content)questions[o].isUsed = trueanswers[k].isUsed = true}}}}for u in 0...3 { //以利重新輸入判斷questions[u].isUsed = falseanswers[u].isUsed = false}print("A",A,"B",B)recordTextView.textAlignment = .centerrecordTextView.font = UIFont.boldSystemFont(ofSize: 20)if A == 4 {recordTextView.text.append("恭喜答對了!!"+"共猜了\(guessCount)次\n")for i in 0...3 {questionLabel[i].isHidden = false}}else{recordTextView.text.append("\(recordContent)"+" \(A)A\(B)B\n")answers.removeAll()for i in 0...3 {answerLabel[i].text = ""}count = 0recordContent = ""}}else{recordTextView.textAlignment = .centerrecordTextView.font = UIFont.boldSystemFont(ofSize: 20)recordTextView.text.append("請輸入完整唷~\n")answers.removeAll()for i in 0...3 {answerLabel[i].text = ""}count = 0recordContent.removeAll()}}
@IBAction func replay(_ sender: Any) {questions.removeAll()answers.removeAll()recordContent = ""recordTextView.text.removeAll()count = 0guessCount = 0makeQuestions()}
@IBAction func LastNumberDelete(_ sender: Any) {count -= 1if recordContent != "" {recordContent.removeLast()answerLabel[count].text = ""}if answers.count > 0 {answers.removeLast()print(answers)}if count < 0 {count = 0}print("count",count)print("recordContent",recordContent)}
@IBAction func giveTips(_ sender: Any) {let random = Int.random(in: 0...3)var timer = Timer()timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (Timer) inself.questionLabel[random].isHidden = truevar sec = 0sec += 1if sec > 0 {timer.invalidate()}print("sec",sec)})questionLabel[random].isHidden = false}