@IBAction func getChoki(_ sender: Any) { let results2 = ["Image1","Image2","Image3"] let random2 = arc4random_uniform(UInt32(results2.count)) let image2 = UIImage(named: results2[Int(random2)]) imageView.image = image2 let image5 = UIImage(named: "Image5") imageView2.image = image5 let even = "あいこ" let win = "あなたの勝ち" let lose = "あなたの負け" if random2 == 0 { self.gameResult.text = lose if self.userDefaults.object(forKey: "load") != nil { if self.userDefaults.integer(forKey:"load") < 0 { self.load = self.userDefaults.integer(forKey:"load") self.load -= 1 } else { self.load = -1 } } else { self.load = -1 } } else if(random2 == 1){ self.gameResult.text = even if self.userDefaults.object(forKey: "load") != nil { self.load = self.userDefaults.integer(forKey: "load") } else { self.load = 0 } } else { self.gameResult.text = win if self.userDefaults.object(forKey: "load") != nil { if self.userDefaults.integer(forKey:"load") > 0 { self.load = self.userDefaults.integer(forKey:"load") self.load += 1 } else { self.load = 1 } } else { self.load = 1 } } self.gameResult.text = String(self.load) + str UserDefaults.standard.set(self.load, forKey: "load") }
7連勝中でも、一度負けると-1連勝に成りました。
あとはviewの分岐ですね。