Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ PROGRAMMING ADD THE FOLLOWING ELEMENTS TO THE CODE PROVIDED: 1. Functions to do the following: Display the rules. void DisplayRules(); Display the main menu,

C++ PROGRAMMING

ADD THE FOLLOWING ELEMENTS TO THE CODE PROVIDED:

1. Functions to do the following: Display the rules. void DisplayRules(); Display the main menu, prompt for and return the users choice. int MainMenu(); or char MainMenu();

For each submenu: Display the menu, prompt for the users choice, determine the point value earned and return that value. int SubMenu1(); int SubMenu2(); int SubMenu3();

2. Include the top 3 names and scores, in order, in the high score list at all times. Create parallel arrays in main() to store the data. Create a text file called highscores.txt. Example Data: Joe 5 Jen 3 Michael 2

3. Functions to do the following:

void ReadInScores(string[], int[]); This function should read in and store the high names and scores in the parallel arrays. void CompareScores(string[], int[], string, int); This function should compare the users score to the high scores that have been stored and should replace the parallel arrays as necessary to keep the highest three scores with the corresponding names in the parallel arrays. Note: Make sure that you move the names with the scores in the file. void ReplaceScores(string[], int[]); This function should open the high score list and replace the file with the values that have been stored in the parallel arrays. Call the above functions in main().

ADD THOSE ELEMENTS TO THE FOLLOWING CODE:

#include #include #include #include #include #include using namespace std;

//Define the main function int main() { //Declare and initialize variables int menuChoice = 0, doorChoice = 0, weaponChoice = 0, score1 = 0, score2 = 0, totalScore = 0, myRandom = 0; string player, date;

//Introduction cout << "WELCOME TO MY RPG GAME !!! "; //seed random number generator srand(time(NULL));

while (menuChoice != 3) { //Display Main menu cout << " Please choose from the following Main menu:" << endl; cout << "1) See Rules" << endl; cout << "2) Play Game" << endl; cout << "3) Exit ";

//Prompt the user for a choice from the main menu cout << " Enter your main menu choice here: "; cin >> menuChoice;

//decision structure to determine the program path //multi-way if statement if (menuChoice == 1) { //Display the rules cout << " You have chosen to see the rules. " << endl; cout << "This game awards points to the player based on decisions throughout the game." << endl; cout << "At the end of the game, the points are displayed and saved to a highscore text file to keep track of the best player. " << endl; }

else if (menuChoice == 2) { //Play the game //Prompt the user for player's name cout << " Player, enter your name here: "; cin >> player;

//Prompt the user for the date (define date format) cout << " " << player << ", enter the date here (MM/DD/YYYY): "; cin >> date;

cout << endl; //skip a line

//Intro to Menu Option 2 cout << player << ", You just woke up from a long sleep in the cryogenic tubes... "; cout << "Are you ready to revenge your spouse's death and find your son? ";

cout << "Where do you go now, " << player << "? You must be careful! The Vault-Corp staff may catch you and freeze you back to sleep! "; cout << "You have 3 doors in front of you. Watch out afer openning one of the doors! ";

//Display door selection menu cout << " 1) Left Door 2) Middle Door 3) Rright Door ";

//prompt for doorChoice cout << "Which door do you choose?: "; cin >> doorChoice;

//decision structure to determine the program path based on doorChoice //multi-way if statement if (doorChoice == 1) { //Display winning door choice cout << " You open the Left door and peak inside.It looks like a storage closet with a grate in the back. "; cout << "It appears safe and you continue in... You are on the right track," << " " << player << " " << ", this door brings you to an underground tunnel! "; cout << " You find yourself outside of the Vault in the Wasteland. "; cout << "You are now free," << " " << player << " " << "! It's time to go find your son! "; myRandom = rand() % 1200 + 1; totalScore = myRandom + 1000;

//display formatted chart for the player's name, score, and the date for door 1 //set columns to a width of 15 characters each cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " "; cout << "-------------------------------------------------------------------------- "; cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl;

}

else if (doorChoice == 2) { //Display action door and weaponChoice menu cout << "You Open the door slowly to a dark room. You walk into the room and the door shuts behind you "; cout << "The lights quickly turn on and you find yourself trapped! "; cout << "Watch out, " << player << "! There is a dozen of Vault-Corp staffers in front of you! Look!, There are three weapons on the wall. "; cout << "Grab one quickly: "; cout << " 1.) SILENT PISTOL - this weapon helps you react quickly!" << endl; cout << "2.) SLEDGE HAMMER - a construction tool with a large, flat, metal head which allows you to apply more force over a wide area." << endl; cout << "3.) GERNADES - a handfull of small bomb typically thrown by hand." << endl;

score1 = 500;

//prompt the user for weaponChoice cout << " Grab a weapon quickly: "; cin >> weaponChoice;

//decision structure to determine the program path based on weaponChoice //multi-way if statement if (weaponChoice == 1) { cout << " This pistol helps you react quickly but the Vault-Corp staffers have you outnumbered! "; cout << player << ", Your skills help you liquidize the Vault-Corps staffers, but barely..."; myRandom = rand() % 500 + 1; totalScore = score1 + myRandom;

//display formatted chart for the player's name, score, and the date for door 2/weapon 1 //set columns to a width of 15 characters each cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " "; cout << "-------------------------------------------------------------------------- "; cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl;

}

else if (weaponChoice == 2) { cout << player << ", Are you kidding me? How are you planning to smash a dozen of Vault-Corp staffers with a single hammer?! "; cout << "You swing at one of them only to miss and hit the floor "; cout << "You just got caught by Vault-Corp once again. GAME OVER!"; score2 = 200; totalScore = score1 + score2;

//display formatted chart for the player's name, score, and the date for door 2/weapon 2 //set columns to a width of 15 characters each cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " "; cout << "-------------------------------------------------------------------------- "; cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl;

} else if (weaponChoice == 3) { cout << " Good job, " << player << ", This granate helped you kill all the Vault-Corp staffers at once! "; cout << "You are now are a free man! Go find your son. "; score2 = 600; totalScore = score1 + score2;

//display formatted chart for the player's name, score, and the date for door 2 / weapon 3 //set columns to a width of 15 characters each cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " "; cout << "-------------------------------------------------------------------------- "; cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl;

} else { cout << "Sorry, " << player << ", You are not quick enough! "; score2 = 150; totalScore = score1 + score2;

//display formatted chart for the player's name, score, and the date for wrong door //set columns to a width of 15 characters each cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " "; cout << "-------------------------------------------------------------------------- "; cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl; } } else if (doorChoice == 3) { //Display losing door choice cout << " The right door is not always the right choice, " << player << "You just got shot by one of the Vault-Corp Staffers! " << endl; cout << "GAME OVER!" << endl; totalScore = 100;

//display formatted chart for the player's name, score, and the date for Door 3 //set columns to a width of 15 characters each cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " "; cout << "-------------------------------------------------------------------------- "; cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl; } else { //Display invalid door choice cout << "Sorry," << player << ", the roof is not an option! You don't have a ladder! You must choose 1 of the three doors! "; }

} else if (menuChoice == 3) { //Exit cout << " Are you giving up? Good bye! "; } else { //Display invalid choice. cout << "INVALID. You must choose 1 - 3 from the menu above. "; }

vector data; std::ifstream infile("highscore.txt"); std::string line; while (std::getline(infile, line)) { data.push_back(line); } infile.close();

//declare a file pointer for output ofstream outfile; outfile.open("highscore.txt", ios::out); for (unsigned int i = 0; i < data.size(); i++) { if (data.at(i).find(player) != std::string::npos) { outfile << date << " " << player << " " << totalScore << endl; } else { outfile << data.at(i) << endl; }

} outfile.close();

}

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

From Zero To Data Hero With Chatgpt

Authors: Andrew Wu

1st Edition

B0CQRJPXD9, 979-8989523009

More Books

Students also viewed these Databases questions