In: Computer Science
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:
Example: You are 14 mile(s) from campus! How do you wish to travel? (1 bus, 2 subway, 3 jetpack)
The player chooses one.
After each turn, the player is informed how much farther she must travel before reaching campus.
Winning/Losing: After the last turn, if the player has reached campus exactly ("You have won!")
Otherwise, explain the problem: "You have over-shot your target!" or "You haven't reached your target!"
And then write “You lose!”
The game will operate as follows:
o Ask user to select transport method (Bus, Subway, or Jetpack)
o Report the user’s new distance from campus
o If the player has reached campus or passed campus and it is not the fourth turn, end the game early – This is a more challenging step! Make sure the rest of your game works before working on this step.
ALSO:
Check that the user input is valid (1-3).
If the user fails to pick a valid number, the program must keep asking the user for a new selection until a valid number is entered. ALSO- you do not lose turns by making an invalid selection.
Use this wording: Invalid choice, try again!
Requirements:
Example Output:
You are 14 mile(s) from campus!
How do you wish to travel? (1 bus, 2 subway, 3 jetpack) 2
You are 9 mile(s) from campus!
How do you wish to travel? (1 bus, 2 subway, 3 jetpack) 1
You are 7 mile(s) from campus!
How do you wish to travel? (1 bus, 2 subway, 3 jetpack) 1
You are 5 mile(s) from campus!
How do you wish to travel? (1 bus, 2 subway, 3 jetpack) 2
You are 0 mile(s) from campus!
You have won!
Please up vote ,comment if any query . Thanks for Question .
Note : check attached image for output ,code compiled and tested in GCC code blocks ide.
Program :
#include<iostream>
using namespace std;
int main()
{
int distance=14,i=0;//initial distance and loop
count
while(i<4) //loop till i<4
{
int choice; //user
chocie
cout<<"You are
"<<distance<<" mile(s) from campus!"<<endl;
//print current distance
cout<<"How do you
wish to travel? (1 bus, 2 subway, 3 jetpack) "; //prompt for
input
cin>>choice;
switch(choice) //switch
case
{
case 1: //if its bus choice
distance-=2; //subtract 2 from distance
i++; //increment loop count
break;
case 2: //its sub way choice
distance-=5;//subtract 5 from distance
i++;
break;
case 3: //its jetpack choice
distance-=10; //subtract 10 from distance
i++;
break;
default: //Invalid input
cout<<" Invalid choice, try again!"<<endl;
break;
}
if(i<=4 &&
distance==0) //if i==4 or i<4 but distance ==0 end game
early
{
cout<<"You are 0 mile(s) from campus!"<<endl; //print 0
distance and won message and break from loop
cout<<"You won!"<<endl;
break;
}
else if(i<=4
&& distance<0) //if we over shot target still did not
came 4th target or 4th target
{
cout<<"You have over-shot your target!"<<endl; //print
over shoot message
cout<<"You lose!"<<endl;
break;
}
else if(i==4 &&
distance>0) //if all four turn completed still far from
target
{
cout<<"You are "<<distance<<" mile(s) from
campus!"<<endl; //print loose message
cout<<"You haven't reached your target!"<<endl;
cout<<"You lose!"<<endl;
break;
}
}
return 0;
}
Output :
Please comment if any query . Upvote ,be safe .