Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help! I'm not sure exactly how to get started on this assignment You have been contracted to write a C++ program for a simple text-based

Help! I'm not sure exactly how to get started on this assignment

You have been contracted to write a C++ program for a simple text-based game's battle system. The input to your program will be a file named characterInfo.txt. The file contains five lines in the following format:

Hero_Name Hero_Health Spell/Ability_Name Monster_Name Monster_Health Spell/Ability_Name Min_Attack_Damage Max_Attack_Damage Min_Spell/Ability_Damage Max_Spell/Ability_Damage Min_Potion Max_Potion 

The first line represents the hero's name (string), starting health (int), and spell/ability name (string). The second line represents the enemy's name (string), starting health (int), and spell/ability name (string). If any name has two or more words, the words will be connected using underscores. Here are two examples

Valid File Invalid File
Some_Hero 157 Fireball Bad_Guy 250 Acid_Bomb 5 25 15 35 20 30 
Some Hero 157 Fireball Bad_Guy 250 Acid_Bomb 5 25 15 35 20 30 

The second file is invalid because there's a space between "Some" and "Hero". The remaining three lines indicate the minimum and maximum possible damage for the actions the hero and enemy can take. You can assume that the file will always be in the correct format.

After reading the file, the program will open a file for output called battleResults.txt. A message stating the characters' names and starting health will be printed to both the terminal window and the file.

For example,

Some_Hero (157 HP) will battle Bad_Guy (250 HP) 

Next, the program will print out which character's turn it is and their health. Then the program will display a menu with three options representing the actions each character can take. The hero always goes first. For example,

It's Some_Hero's turn. Current HP = 157 1: Attack 2: Use Fireball 3: Use a Potion Enter action: 

Options 1 and 3 are the same for both characters. Option 2 must include the name of the character's spell or ability. You can use integers (as shown above) or chars (such as 'a' for attack, 's' for spell, and 'p' for potion) for your menu options.

The user will select one of the three options. Use a switch statement to determine which option was selected and take the following action depending on what the user chose:

Attack: Generate a random value between Min_Attack_Damageand Max_Attack_Damage and subtract that value from the other character's health.

Spell / Ability: Generate a random value between Min_Spell/Ability_Damage and Max_Spell/Ability_Damage and subtract that value from the other character's health.

Potion: Generate a random value between Min_Potion and Max_Potion and add that amount to the current character's health. However, a character's current health should never exceed the starting health listed in the file. Also, a character can use a potion a maximum of 3 times. After that, using the potion option should have no effect other than a loss of turn for the current character. Make sure to print a message saying that the character is out of potions in this case.

Regardless of the action selected, you must print out a message describing what happened to both the terminal window and the file. After printing, display the menu again for the other character's turn. See the sample output below.

Continue processing turns until one of the characters has 0 or negative health. Print out an appropriate victory message and exit the program.

Assumptions, Other Requirements and Tips

If the file characterInfo.txt does not exist, print an appropriate error message and exit the program. Hint: the ifstream function is_open can help here.

You can assume the file characterInfo.txt is properly formatted and the min values are strictly less than the max values.

All terminal input must be validated. Print out an appropriate message and prompt the user to try again if they enter an invalid value. Hint: use the functions provided in the header: http://www.cplusplus.com/reference/cctype/ (Links to an external site.)

Don't forget to close your files when you are done with them!

You must use a switch structure to determine which option was selected.

All messages that detail the results of an action must include the name of the character(s) involved.

If a character's health changes as the result of an action, the new health value must also be displayed.

All names of characters and spells will have underscores between words if they contain two or more words.

Feel free to make the messages as humorous or silly as you want! Beware messages in poor taste might be frowned upon by the grader.

Test your program with multiple files to make sure it can handle a variety of inputs.

Make good use of white space in your program: vertical spacing, indentation, horizontal spacing, etc.

You may find it useful to write functions that help you do certain tasks for this program. Make sure these functions have a clear purpose and do the tasks for a single idea. For example, don't have one function that prints a main menu, but also does something for a potion, when those things should each be in their own function.

Formatting, Comments, and Submission Guidelines

See Programming Assignment Guidelines. Failure to follow the guidelines will result in a loss of credit as detailed in said guidelines.

Sample Output

Note: Your output does not have to be formatted exactly like this. As long as all the required information is displayed, you will get credit for having the correct output. Values in bold were input by the user.

Some_Hero (157 HP) will battle Bad_Guy (250 HP) It's Some_Hero's turn. Current HP = 157 1: Attack 2: Use Fireball //note how the hero's spell/ability is listed here 3: Use a Potion Enter action: 2 Some_Hero casts Fireball! Bad_Guy takes 22 damage. New HP = 228 It's Bad_Guy's turn. Current HP = 228 1: Attack 2: Use Acid_Bomb //note how the enemy's spell/ability is listed here 3: Use a Potion Enter action: a =INVALID INPUT= Please enter a value between 1 and 3 It's Bad_Guy's turn. Current HP = 228 1: Attack 2: Use Acid_Bomb 3: Use a Potion Enter action: 1 Bad_Guy attacks Some_Hero for 13 damage! New HP = 144 It's Some_Hero's turn. Current HP = 144 1: Attack 2: Use Fireball 3: Use a Potion Enter action: 3 Some_Hero chugs a potion and recovers 25 HP! New HP = 157 // Note how the hero's health did not exceed his starting health It's Bad_Guy's turn. Current HP = 228 1: Attack 2: Use Acid_Bomb 3: Use a Potion Enter action: . . . 

The corresponding output file battleResults.txt would have the following lines:

Some_Hero (157 HP) will battle Bad_Guy (250 HP) Some_Hero casts Fireball! Bad_Guy takes 22 damage. New HP = 228 Bad_Guy attacks Some_Hero for 13 damage! New HP = 144 Some_Hero chugs a potion and recovers 25 HP! New HP = 157 . . .

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

Minicase 4 Thomson Corporation Thomson Corporation Stockholder

Answered: 1 week ago