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

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...
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...
Goals Practice loops and conditional statements Description Create a game for elementary school students to practice...
Goals Practice loops and conditional statements Description Create a game for elementary school students to practice multiplication. The application should display a greeting followed by a set of mathematical questions.  In each set the player is asked to enter a number from 1 to 9 to test multiplication skills or to press ‘E’ to exit the game.  The application checks if the number entered is indeed between 1 and 9 and if not displays a message inviting the...
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...
Language: C++ NEEDS TO WORK IN VISUAL BASIC The code is broken and loops in a...
Language: C++ NEEDS TO WORK IN VISUAL BASIC The code is broken and loops in a few places please fix it #include<iostream> using namespace std; //function declaration void EnterRents(int*, int); void displayRents(int*, int); void selectionSort(int*, int); int sumRents(int* temp, int size) {    int sum = 0;    for (int i = 0; i < size; i++)    {        sum += *(temp + i);    }    return sum; } void Displaymemory(int* temp, int size) {    /*int...
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...
We continue from the two tables in lab 3 part 2, and practice with functions and...
We continue from the two tables in lab 3 part 2, and practice with functions and the GROUP BY statement. The data in each table should be as below Table Product: PROD_ID PROD_NAME PROD_PRICE PROD_VENDOR 1101 Table 100 2 1102 Chair 80 3 1103 Armchair 90 2 1104 Nightstand 110 1 1105 Bed 200 3 1106 Dresser 150 3 1107 Daybed 190 2 1108 Ash Table 120 2 1109 Cherry Table 130 2 1110 Table - High 100 2 1111...
We continue from the two tables in lab 3 part 2, and practice with functions and...
We continue from the two tables in lab 3 part 2, and practice with functions and the GROUP BY statement. The data in each table should be as below Table Product: PROD_ID PROD_NAME PROD_PRICE PROD_VENDOR 1101 Table 100 2 1102 Chair 80 3 1103 Armchair 90 2 1104 Nightstand 110 1 1105 Bed 200 3 1106 Dresser 150 3 1107 Daybed 190 2 1108 Ash Table 120 2 1109 Cherry Table 130 2 1110 Table - High 100 2 1111...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT