Question

In: Computer Science

IN SWIFT LANGUAGE In this lab we are going to create a game that uses all...

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.

Solutions

Expert Solution

// 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")
}
}
}
}


Related Solutions

C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
We are going to create our own implementation of the game “FloodIt”. Several versions of this...
We are going to create our own implementation of the game “FloodIt”. Several versions of this game can be found, for example a version for android, on which this description is based, here. Several Web versions are available In this game, a square board is filled with dots, each having one of six possible colours. The players initially “captures” the top left dot. The player keeps choosing one of the six colours. Each time a colour is chosen, the dots...
Language: C++ 3 Campus Travel Game This lab will practice using loops. We will construct a...
Language: C++ 3 Campus Travel Game This lab will practice using loops. We will construct a short computer game. Please look at the Test Cases for exact wording. For your game, the player’s goal is to reach campus exactly. The player starts 14 miles away and has up to 4 turns to reach campus. At each turn the play can ride either use a Bus, a Subway, or a Jetpack: Riding a Bus moves the player forward 2 miles each...
Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
Use a swift language in xcode software for the following code? 20 pts Create a class...
Use a swift language in xcode software for the following code? 20 pts Create a class called Polygon, this class will have: a. Properties: i. Number of sides ii. Interior angles b. Method i. sides(), this will print to the console how many sides the polygon has ii. interiorAngle(), this will calculate the interior angles and set it to the interior angles property Create another class named Triangle that inherits from Polygon. This class will have to: a. Properties: i....
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
Lab 8 Acid Base Properties of Solutions Lab Report Worksheet In today’s lab we are going...
Lab 8 Acid Base Properties of Solutions Lab Report Worksheet In today’s lab we are going to utilize a simulation to determine the pH of various salt solutions. We will then use those pH values to determine concentrations of hydronium and Hydroxide in solutions. From there we will determine the Ka or Kb Pre-Work: In the table below, use just the salt formula to identify the salt as acidic, basic, or neutral. Table 1: Acidic, Basic, or Neutral Salt Predictions...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game that involves betting on the sum of two dice. The player will start out with some initial total amount of money. During each round, the player can bet some money that the sum of the two dice will be equal to a certain number. If the player wins the bet, that player adds the amount of the bet to his or her current total....
swift language declare a Swift array variable that can hold instances of any class type
swift language declare a Swift array variable that can hold instances of any class type
We started creating a Java class for Car. In this lab we are going to complete...
We started creating a Java class for Car. In this lab we are going to complete it in the following steps: 1- First create the Car class with some required instance variables, such make, model, year, price, speed, maxSpeed, isOn, isMoving, and any other properties you like to add. 2- Provide couple of constructors for initializing different set of important properties, such as make, model, year, and price. Make sure that you do not repeat initialization of instance variables in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT