我在下面发布了一个简单的问答应用程序代码,该应用程序询问用户给定州的首府是什么。我有一个名为 placeArray
的数组,每次从该数组中提出问题时,我都会调用 removeAtIndex()
以避免重复问题。游戏结束时,placeArray
部分或完全为空。
那么当用户再次选择播放时,如何将数组恢复到原来的状态呢?
import UIKit
import AudioToolbox
class ViewController: UIViewController {
var userInput:String = ""
var processedAnswerString = ""
var finalAnswerString = ""
var questionString:String = ""
var correctAnswerString = ""
var gameInPlay : Bool = false
var correctAnswers : Int = 0
var wrongAnswers : Int = 0
var numberOfRounds : Int = 0
var indexNumber : Int = 0
var placeArray = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"]
var capitalArray = ["MONTGOMERY", "JUNEAU", "PHOENIX", "LITTLE ROCK", "SACRAMENTO", "DENVER" ,"HARTFORD", "DOVER", "TALLAHASSEE", "ATLANTA", "HONOLULU", "BOISE", "SPRINGFIELD", "INDIANAPOLIS", "DES MOINES", "TOPEKA", "FRANKFORT", "BATON ROUGE", "AUGUSTA", "ANNAPOLIS", "BOSTON", "LANSING", "SAINT PAUL", "JACKSON", "JEFFERSON CITY", "HELENA", "LINCOLN", "CARSON CITY", "CONCORD", "TRENTON", "SANTA FE", "ALBANY", "RALEIGH", "BISMARCK", "COLUMBUS", "OKLAHOMA CITY", "SALEM", "HARRISBURG", "PROVIDENCE", "COLUMBIA", "PIERRE", "NASHVILLE", "AUSTIN", "SALT LAKE CITY", "MONTPELIER", "RICHMOND", "OLYMPIA", "CHARLESTON", "MADISON", "CHEYENNE"]
@IBOutlet weak var theTimer: UILabel!
@IBOutlet weak var thePlace: UILabel!
@IBOutlet weak var theGuess: UITextField!
@IBOutlet weak var theCorrectAnswerScore: UILabel!
@IBOutlet weak var theWrongAnswerScore: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
startGame()
indexNumber = Int(arc4random_uniform(50))
thePlace.text = "What is the capital of \(placeArray[indexNumber])?"
}
var startTime = NSTimeInterval()
var timer = NSTimer()
var gameTime:Double = 100
func startGame() {
let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
}
func updateTime() {
var currentTime = NSDate.timeIntervalSinceReferenceDate()
var elapsedTime = currentTime - startTime
var seconds = gameTime-elapsedTime
if seconds > 0 {
elapsedTime -= NSTimeInterval(seconds)
theTimer.text = "\(Int(seconds))"
} else {
timer.invalidate()
let alertController = UIAlertController(title: "Time's Up!", message: "You got \((correctAnswers/50)*100)% correct", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Play Again", style: .Default) { (action) in
println("OK Pressed")
self.wrongAnswers = 0
self.correctAnswers = 0
self.theGuess.text = ""
self.gameTime = 100
self.updateTime()
self.startGame()
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
}
}
@IBAction func returnKeyPressed(sender: AnyObject) {
userInput = theGuess.text
questionString = capitalArray[indexNumber]
processedAnswerString = userInput.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
//strip white space from answer
//allow either with or without capitalization
if processedAnswerString.capitalizedString == questionString.capitalizedString {
correctAnswers++
numberOfRounds++
gameTime = gameTime + (gameTime/10)
if placeArray.count > 0 {
placeArray.removeAtIndex(indexNumber)
capitalArray.removeAtIndex(indexNumber)
theCorrectAnswerScore.text = "Yes: \(String(correctAnswers))"
theGuess.text = ""
println("The current placeArray count is \(placeArray.count)")
println("The current capitalArray count is \(capitalArray.count)")
println("The current indexNumber is \(indexNumber)")
}
} else {
wrongAnswers++
numberOfRounds++
if gameTime - (gameTime/10) >= 0 {
gameTime = gameTime - (gameTime/10)
}
if placeArray.count > 0 {
placeArray.removeAtIndex(indexNumber)
capitalArray.removeAtIndex(indexNumber)
theWrongAnswerScore.text = "No: \(String(wrongAnswers))"
theGuess.text = ""
}
println("The current placeArray count is \(placeArray.count)")
println("The current capitalArray count is \(capitalArray.count)")
println("The current indexNumber is \(indexNumber)")
}
indexNumber = Int(arc4random_uniform(UInt32(placeArray.count)))
if placeArray.count != 0 {
thePlace.text = "What is the capital of \(placeArray[indexNumber])?"
} else {
let alertController = UIAlertController(title: "You win!", message: "Nice work!", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Play Again", style: .Cancel) { (action) in
self.wrongAnswers = 0
self.correctAnswers = 0
self.theGuess.text = ""
self.gameTime = 100
self.updateTime()
self.startGame()
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
最简单的解决方案是使用一个方法返回数组的初始版本,然后当您完成或再次播放时调用您可以使用该方法设置数组。
或者,您可以创建第二个数组来保存您的初始数组数据(当然是副本),然后从第二个数组中删除对象。再次点击播放后,只需将初始数组数据复制回第二个数组并重新开始。第二个数组将是显示问题并从中删除数据的数组。
关于ios - 当用户点击 "Play Again"时,如何在 Swift 中重置数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26994829/