我在下面发布了一个简单的测验应用程序的代码,该应用程序会询问用户给定状态的资本是什么。我有一个名为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.
}
}
答案 0 :(得分:3)
最简单的解决方案是使用一个方法返回数组的初始版本,然后在完成或再次播放时调用,您可以使用该方法设置数组。
或者,您可以创建第二个数组来保存初始数组数据(当然是副本),然后从第二个数组中删除对象。再次点击播放后,只需将初始数组数据复制回第二个数组,然后重新开始。第二个数组将是显示问题并从中删除数据的数组。
答案 1 :(得分:0)
我只想创建一个返回填充数组的辅助方法。
您在游戏开始时(例如在您的self.startGame()实现中)初始化您正在使用的属性,这样每次游戏开始时阵列都将满。
这看起来像是:
self.placeArray! = self.fullPlacesArray();