I'm very new to programming and I'm trying to learn the basics of Swift.
I have coded a very simple quiz game for iOS whereby the app moves through the questions in a sequential order. I am wanting to move onto a simple text-adventure game. To start off with I changed the quiz questions to scenarios which you can play through. However, I would now like to have it where you can click either of the two buttons displayed, one button would move you on to one scenario, and the other button on to a different scenario.
Below is the code I have so far, whereby it follows the simple sequential order of scenarios presented to the user where one answer allows you to move on, and one answer doesn't. What would be the best way to implement a structure whereby all the scenarios and answers are stored and the user can navigate through the story by selecting different answers to different scenarios.
import UIKit
struct Scenario {
var Scenario : String!
var Answers : [String]!
var Answer : Int!
}
class ViewController: UIViewController {
@IBOutlet var actionButton: [UIButton]!
@IBOutlet var scenarioLabel: UILabel!
var Scenarios = [Scenario]()
var sNumber = Int()
var answerNumber = Int()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
listScenarios()
pickScenario()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func listScenarios() {
Scenarios = [Scenario(Scenario: "HELLO? Someone please answer me, I NEED HELP!", Answers: ["Who are you? I can help you.","*** Ignore ***"], Answer: 0),
Scenario(Scenario: "That is something I cannot share with you", Answers: ["Why?","I am unwilling to help if you don't share this with me."], Answer: 1),
Scenario(Scenario: "I... I... I just can't. You have to understand", Answers: ["Okay, I'll help.","What has happened to you?"], Answer: 0),]
}
func pickScenario() {
if Scenarios.count > 0 {
sNumber = 0
scenarioLabel.text = Scenarios[sNumber].Scenario
answerNumber = Scenarios[sNumber].Answer
for i in 0..<actionButton.count {
actionButton[i].setTitle(Scenarios[sNumber].Answers[i], forState: UIControlState.Normal)
}
Scenarios.removeAtIndex(sNumber)
}
else {
NSLog("Done!")
}
}
@IBAction func button1(sender: AnyObject) {
if answerNumber == 0 {
pickScenario()
}
else{
NSLog("Wrong!")
}
}
@IBAction func button2(sender: AnyObject) {
if answerNumber == 1 {
pickScenario()
}
else{
NSLog("Wrong!")
}
}
}