Question

In: Computer Science

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 turn.
  • Riding a Subway moves the player forward 5 miles each turn
  • Riding a Jetpack moves the player forward 10 mile each turn.
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:

  • Report how far the user is from campus – the player starts 14 mile away
  • For each turn:

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:

  • You must use a loop (while, do-while, or for) to loop through the 4 turns. (The loop can include a condition to allow ending early, if you so choose.)
  • You must use at least one if or switch statement
  • Require the user enters one of three numbers to specify the transport method

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!

Solutions

Expert Solution

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 .


Related Solutions

C++ In this lab you will be using nested for loops to print out stars in...
C++ In this lab you will be using nested for loops to print out stars in a Diamond pattern such as this: * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** * For example , if number of rows=8 then the above pattern is derived. You are to take the input for the number of lines(rows) from a file named "input_diamond" and output the pattern into both the terminal and an output file named "output_diamond".
In this exercise we will practice using nested loops. You will write s program that prompts...
In this exercise we will practice using nested loops. You will write s program that prompts the user to enter an integer between 1 and 9 and displays a pyramid of numbers, as shown in the example below. The program must validate the input given by the user and make sure that: if the user enters a non-integer the program issues an error message and requests the user provides a valid input. if the user does not enter a number...
Objectives: Practice using files and strings. More practice using loops with files and strings Background Assume...
Objectives: Practice using files and strings. More practice using loops with files and strings Background Assume you're working on a contract where the company is building a e-mailing list. Your task is to write a C++ program that reads through an e-mail stored in the current working directory, called mail.dat, and outputs every email address found as individual lines to a file called email_list.dat. For this assignment, if a whitespace delimited string of characters has an embedded commercial at sign...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
In this lab, you will practice the use of array by building an simple maze game,...
In this lab, you will practice the use of array by building an simple maze game, where your task is to control the prince to defeat the monster and save Snow White. You have to work based on the given skeleton code. Game Description The Snow White is detained in the maze by the monster, who needs the prince's help to rescure her out from the exit. The pre-set maze is shown below, with width 11 and height 8. The...
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company.
The purpose of this lab is to practice using inheritance and polymorphism.    We need a set of classes for an Insurance company.   Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc.    Insurance policies have a lot of data and behavior in common.   But they also have differences.   You want to build each insurance policy class so that you can reuse as much code as possible.   Avoid duplicating code.   You’ll save time, have less code to...
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and use a function named highest() that takes three inputs and returns the highest number. After you have got it working, try calling the function with inputs ‘hat’, ‘cat’, ‘rat’.
Using C Language Write a program segment that computes 1 + 2 + 3 + ......
Using C Language Write a program segment that computes 1 + 2 + 3 + ... + ( n - 1) + n , where n is a data value. Follow the loop body with an if statement that compares this value to (n * (n + 1)) / 2 and displays a message that indicates whether the values are the same or different. Please give me code to just copy and paste
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT