Created
September 5, 2016 11:02
-
-
Save Darmaal/bb241faa820b20e0ec5361a4a42425d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//the core of the application | |
//notice every func in this class is private except for the initialiser | |
import Foundation | |
class SpaceAdventureModified { | |
let planetarySystem:PlanetarySystem | |
init (planetarySystem:PlanetarySystem){ | |
self.planetarySystem = planetarySystem | |
} | |
//doesnt reutrn anything | |
private func displayIntroduction() { | |
print("Welcome to the \(planetarySystem.name)!") | |
print("There are \(planetarySystem.planets.count) planets to explore. ") | |
} | |
//func which awaits the user input | |
private func responseToPrompt (prompt:String) -> String { | |
print(prompt) | |
return getUserInput() | |
} | |
private func greetAdventurer(){ | |
let name = responseToPrompt("What's is your name?") | |
print("Nice to meet you, \(name). My name is Eirene. I am an old friend of Zuldaf. ") | |
} | |
private func visit(planetName:String){ | |
print("Traveling to \(planetName) ...") | |
for planet in planetarySystem.planets { | |
if planetName == planet.name { | |
//accessing the following variables from our main class | |
print("Arrived at \(planet.name).\(planet.description)") | |
} | |
} | |
} | |
//most complex func | |
private func determineDestination(){ | |
var decision = "" | |
//creating a special loop, using an Exclimation mark before the loop -> inverting it. | |
while !( decision == "Yes" || decision == "No"){ | |
decision = responseToPrompt("Shall I randomly choose a planet to visit? (Yes or No)") | |
if decision == "Yes" { | |
//the following code will access our planetarySystem class and activate the random method. | |
if let planet = planetarySystem.randomPlanet { | |
visit(planet.name) | |
} else { | |
print("Sorry, but there are no planets in this system.") | |
} | |
}else if decision == "No" { | |
var planetName = responseToPrompt("Ok, name the planet you would like to visit") | |
//check if the planet exists in our solarsystem | |
if OurPlanetsNames.contains(planetName){ | |
visit(planetName) | |
} | |
else{ | |
//print("makes no sense to me, again") | |
//the following random func will generate one random number each time it's been called, numbers are | |
// 0,1,2,3,4 and not 5! | |
let MyRandomInt = Int(arc4random_uniform(5)) | |
//personal line of code | |
if MyRandomInt == 0{print("No planet named \(planetName)")} | |
if MyRandomInt == 1{print("Again")} | |
if MyRandomInt == 2{print("Try again")} | |
if MyRandomInt == 3{print("Hmm, is that a Planet? I wonder. I wonder a lot...;( ")} | |
if MyRandomInt == 4{print("The one who developed me is so inrobotic, he stressed that I be \"case sesnitive\"")} | |
determineDestination() | |
} | |
}else{ | |
//print("makes no sense to me, again") | |
//the following random func will generate one random number each time it's been called, numbers are | |
// 0,1,2,3,4 and not 5! | |
let MyRandomInt = Int(arc4random_uniform(5)) | |
//personal line of code | |
if MyRandomInt == 0{print("Sorry, I didn't get that. Was it Yes?")} | |
if MyRandomInt == 1{print("Gibberish to me!, you can just type in Yes for a random selection!")} | |
if MyRandomInt == 2{print("Nope, doesn't ring a bell sweetie. It's a Yes or No answer")} | |
if MyRandomInt == 3{print("I really should see it to update my database ;( ")} | |
if MyRandomInt == 4{print("The one who developed me is so inrobotic, he stressed that I be \"case sesnitive\"")} | |
} | |
} | |
} | |
//this func will start our game -> here calling all of our three functions | |
func start(){ | |
displayIntroduction() | |
greetAdventurer() | |
if !planetarySystem.planets.isEmpty { | |
print("Let's go on an Adventure!") | |
determineDestination() | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment