In: Computer Science
IN SWIFT LANGUAGE
In this lab we are going to create a game that uses all the different types of control flow operators that exist in SWIFT LANGUAGE.
The game will be a choose your own adventure game. The user will have to answer a series of questions and this will change the path of a story. This must be done by using if/else statements that are nested within other if/else statements. Your story must have four (4) different possible endings. Beyond this, you must provide some setup for the story to provide customized messages. Ask the user questions that will be used to capture details for use within the story.
Ask the user at least one question that has them choose something from a list of possible values and use a switch to set the appropriate variable.
Ask the user at least one question that results in a numeric range and use an interval switch to set another variable.
Also ask the user a binary (yes/no or true/false) question. Use the answer from this in a ternary to store another variable.
Use these variables within the output of the messages to the user in the story.
Example:
Example Program Output
Here is an example of a how the program might work...
Enter your favorite color? Pick "red", "orange", "yellow", "green", or "blue" > red Do you like apples? "Yes" or "No"? > Yes How fast can you run a mile? Enter as a decimal number. > 8.5 You are walking down the street eating some apples when a red tiger runs up and takes your lunch and then quickly runs away. Do you want to "chase" the tiger or "run away"? > run away You run away from the tiger but it is a bad idea to run from an animal. It turns and decides to try to eat you. You see a bus blocking your path up ahead. Do you "get on" the bus or "go around"? > go around There is a zoo keeper on the other side of the bus only to see a subway entrance. He captures the tiger and you are saved. The End.
But then if you ran it again it might do this...
Enter your favorite color? Pick "red", "orange", "yellow", "green", or "blue" > green Do you like apples? "Yes" or "No"? > No How fast can you run a mile? Enter as a decimal number. > 20 You are walking down the street eating some oranges when a green tiger runs up and takes your lunch and then walks away. Do you want to "chase" the tiger or "run away"? > chase The tiger runs but falls into the river. Do you "help it" or "leave it"? > help it You save the tiger and it is grateful. You have a new pet. The End.
// in this swift code we can conditional statements like if else
and switch and nested if else statement
print("Enter your Favourite Color, \rPick
'red','green','yellow',or'blue'")
var color = readLine()
print(color!)
print("Do you like apples? 'Yes' or 'No'?")
var apple = readLine()
print(apple!)
print("How fast can you run a mile? Enter as a decimal
number.")
var mile = readLine()
print(mile!)
if apple!.lowercased() == "yes"{
if color!.lowercased() == "red"{
print("You are walking down the street eating some apples when a
red tiger runs up and takes your apples and then quickly runs
away.")
print("Do you want to 'chase' the tiger or 'run away'?")
let option = readLine()
if option!.lowercased()=="run away"{
print(option!)
print("You run away from the tiger but it is a bad idea to run from
an animal. It turns and decides to try to eat you. You see a bus
blocking your path up ahead. Do you 'get on' the bus or 'go
around'?")
let option2 = readLine()
if option2!.lowercased() == "go around"{
print(option2!)
print("There is a zoo keeper on the other side of the bus only to
see a subway entrance. He captures the tiger and you are saved.
\rThe End")
}else{
print("You got on the bus and bus started to move and you are
saved. \rThe End")
}
}else{
print(option!)
print("The tiger runs but falls into the river. Do you 'help it' or
'leave it'?")
let option1 = readLine()
if option1!.lowercased() == "help it"{
print(option1!)
print("You save the tiger and it is grateful. You have a new pet.
\rThe End.")
}else{
print("The tiger is dead. \rThe End")
}
}
}
else if color!.lowercased() == "blue"{
print("You are walking down the street eating some apples when a
blue tiger runs up and takes your apples and then quickly runs
away.")
print("Do you want to 'chase' the tiger or 'run away'?")
let option = readLine()
if option!.lowercased()=="run away"{
print(option!)
print("You run away from the tiger but it is a bad idea to run from
an animal. It turns and decides to try to eat you. You see a car
blocking your path up ahead. Do you 'get on' the bus or 'go
around'?")
let option2 = readLine()
if option2!.lowercased() == "go around"{
print(option2!)
print("There is a Animal Hunter on the other side of the bus only
to see a subway entrance. He captures the tiger and you are saved.
\rThe End")
}else{
print("You got on the car and car started to move and you are
saved. \rThe End")
}
}else{
print(option!)
print("The tiger runs but falls into the pit. Do you 'help it' or
'leave it'?")
let option1 = readLine()
if option1!.lowercased() == "help it"{
print(option1!)
print("You save the tiger and it is grateful. You have a new pet.
\rThe End.")
}else{
print("The tiger is traped. \rThe End")
}
}
}
else if color!.lowercased() == "green"{
print("You are walking down the street eating some apples when a
green tiger runs up and takes your apples then quickly runs
away.")
print("Do you want to 'chase' the tiger or 'run away'?")
let option = readLine()
if option!.lowercased()=="run away"{
print(option!)
print("You run away from the tiger but it is a bad idea to run from
an animal. It turns and decides to try to eat you. You see a bus
blocking your path up ahead. Do you 'get on' the bus or 'go
around'?")
let option2 = readLine()
if option2!.lowercased() == "go around"{
print(option2!)
print("There is a zoo keeper on the other side of the bus only to
see a subway entrance. He captures the tiger and you are saved.
\rThe End")
}else{
print("You got on the bus and bus started to move and you are
saved. \rThe End")
}
}else{
print(option!)
print("The tiger runs and went missing. Do you 'search it' or
'leave it'?")
let option1 = readLine()
if option1!.lowercased() == "search it"{
print(option1!)
print("You got the tiger and it is traped. Do you 'help it' or
'leave it'?")
let opt2 = readLine()
if opt2! == "help it"{
print(opt2!)
print("You save the tiger and it is grateful. You have a new pet.
\rThe End.")
}else{
print("The tiger is dead. \rThe End")
}
}else{
print("The tiger is dead. \rThe End")
}
}
}
else{
print("You are walking down the street eating some apples when a
yellow tiger runs up and takes your apples and then quickly runs
away.")
print("Do you want to 'chase' the tiger or 'run away'?")
let option = readLine()
if option!.lowercased()=="run away"{
print(option!)
print("You run away from the tiger but it is a bad idea to run from
an animal. It turns and decides to try to eat you. You see a bus
blocking your path up ahead. Do you 'get on' the bus or 'go
around'?")
let option2 = readLine()
if option2!.lowercased() == "go around"{
print(option2!)
print("There is a zoo keeper on the other side of the bus only to
see a subway entrance. He captures the tiger and you are saved.
\rThe End")
}else{
print(option2!)
print("You got on the bus and bus started to move and you are
saved. \rThe End")
}
}else{
print(option!)
print("The tiger runs but falls into the river. Do you 'help it' or
'leave it'?")
let option1 = readLine()
if option1!.lowercased() == "help it"{
print(option1!)
print("You save the tiger and it is grateful. You have a new pet.
\rThe End.")
}else{
print(option1!)
print("The tiger is dead. \rThe End")
}
}
}
}
else{
switch color!.lowercased(){
case "red":
print("You are walking down the street eating some apples when a
red tiger runs up and takes your lunch and then quickly runs
away.")
print("Do you want to 'chase' the tiger or 'run away'?")
let option = readLine()
if option!.lowercased()=="run away"{
print(option!)
print("You run away from the tiger but it is a bad idea to run from
an animal. It turns and decides to try to eat you. You see a bus
blocking your path up ahead. Do you 'get on' the bus or 'go
around'?")
let option2 = readLine()
if option2!.lowercased() == "go around"{
print(option2!)
print("There is a zoo keeper on the other side of the bus only to
see a subway entrance. He captures the tiger and you are saved.
\rThe End")
}else{
print(option2!)
print("You got on the bus and bus started to move and you are
saved. \rThe End")
}
}else{
print(option!)
print("The tiger runs but falls into the river. Do you 'help it' or
'leave it'?")
let option1 = readLine()
if option1!.lowercased() == "help it"{
print(option1!)
print("You save the tiger and it is grateful. You have a new pet.
\rThe End.")
}else{
print(option1!)
print("The tiger is dead. \rThe End")
}
}
case "green":
print("You are walking down the street eating some apples when a
green tiger runs up and takes your grapes then quickly runs
away.")
print("Do you want to 'chase' the tiger or 'run away'?")
let option = readLine()
if option!.lowercased()=="run away"{
print(option!)
print("You run away from the tiger but it is a bad idea to run from
an animal. It turns and decides to try to eat you. You see a bus
blocking your path up ahead. Do you 'get on' the bus or 'go
around'?")
let option2 = readLine()
if option2!.lowercased() == "go around"{
print(option2!)
print("There is a zoo keeper on the other side of the bus only to
see a subway entrance. He captures the tiger and you are saved.
\rThe End")
}else{
print(option2!)
print("You got on the bus and bus started to move and you are
saved. \rThe End")
}
}else{
print(option!)
print("The tiger runs and went missing. Do you 'search it' or
'leave it'?")
let option1 = readLine()
if option1!.lowercased() == "search it"{
print(option1!)
print("You got the tiger and it is traped. Do you 'help it' or
'leave it'?")
let opt2 = readLine()
if opt2! == "help it"{
print(opt2!)
print("You save the tiger and it is grateful. You have a new pet.
\rThe End.")
}else{
print(opt2!)
print("The tiger is dead. \rThe End")
}
}else{
print("leave it")
print("The tiger is dead. \rThe End")
}
}
case "blue":
print("You are walking down the street eating some apples when a
blue tiger runs up and takes your oranges and then quickly runs
away.")
print("Do you want to 'chase' the tiger or 'run away'?")
let option = readLine()
if option!.lowercased()=="run away"{
print(option!)
print("You run away from the tiger but it is a bad idea to run from
an animal. It turns and decides to try to eat you. You see a car
blocking your path up ahead. Do you 'get on' the bus or 'go
around'?")
let option2 = readLine()
if option2!.lowercased() == "go around"{
print(option2!)
print("There is a Animal Hunter on the other side of the bus only
to see a subway entrance. He captures the tiger and you are saved.
\rThe End")
}else{
print(option2!)
print("You got on the car and car started to move and you are
saved. \rThe End")
}
}else{
print(option!)
print("The tiger runs but falls into the pit. Do you 'help it' or
'leave it'?")
let option1 = readLine()
if option1!.lowercased() == "help it"{
print(option1!)
print("You save the tiger and it is grateful. You have a new pet.
\rThe End.")
}else{
print("leave it")
print("The tiger is traped. \rThe End")
}
}
case "yellow":
print("You are walking down the street eating some apples when a
yellow tiger runs up and takes your mango and then quickly runs
away.")
print("Do you want to 'chase' the tiger or 'run away'?")
let option = readLine()
if option!.lowercased()=="run away"{
print(option!)
print("You run away from the tiger but it is a bad idea to run from
an animal. It turns and decides to try to eat you. You see a bus
blocking your path up ahead. Do you 'get on' the bus or 'go
around'?")
let option2 = readLine()
if option2!.lowercased() == "go around"{
print(option2!)
print("There is a zoo keeper on the other side of the bus only to
see a subway entrance. He captures the tiger and you are saved.
\rThe End")
}else{
print(option2!)
print("You got on the bus and bus started to move and you are
saved. \rThe End")
}
}else{
print(option!)
print("The tiger runs but falls into the river. Do you 'help it' or
'leave it'?")
let option1 = readLine()
if option1!.lowercased() == "help it"{
print(option1!)
print("You save the tiger and it is grateful. You have a new pet.
\rThe End.")
}else{
print("leave it")
print("The tiger is dead. \rThe End")
}
}
default:
print("You are walking down the street eating some apples when a
red tiger runs up and takes your lunch and then quickly runs
away.")
print("Do you want to 'chase' the tiger or 'run away'?")
let option = readLine()
if option!.lowercased()=="run away"{
print(option!)
print("You run away from the tiger but it is a bad idea to run from
an animal. It turns and decides to try to eat you. You see a bus
blocking your path up ahead. Do you 'get on' the bus or 'go
around'?")
let option2 = readLine()
if option2!.lowercased() == "go around"{
print(option2!)
print("There is a zoo keeper on the other side of the bus only to
see a subway entrance. He captures the tiger and you are saved.
\rThe End")
}else{
print(option2!)
print("You got on the bus and bus started to move and you are
saved. \rThe End")
}
}else{
print(option!)
print("The tiger runs but falls into the river. Do you 'help it' or
'leave it'?")
let option1 = readLine()
if option1!.lowercased() == "help it"{
print(option1!)
print("You save the tiger and it is grateful. You have a new pet.
\rThe End.")
}else{
print("leave it")
print("The tiger is dead. \rThe End")
}
}
}
}