Question

In: Computer Science

please do this in C++! I want to understand it, it must be done before the...

please do this in C++! I want to understand it, it must be done before the evening or nightime. Follow instructions exactly as it says. Please send a screenshot also with your code so I can see how it is supposed to be formatted. Since typing it a chegg answer, makes it look a bit messy.


Your program will read in a file of commands. There are three types of commands:

Warrior creates a new warrior with the specified name and strength. Battle causes a battle to occur between two warriors. Status lists all warriors, alive or dead, and their strengths.

A sample input file looks like:

“Warrior Jim 42
Warrior Lancelot 15
Warrior Arthur 15
Warrior Torvalds 20
Warrior Gates 8
Status
Battle Arthur Lancelot
Battle Jim Lancelot
Battle Torvalds Gates
Battle Gates Lancelot
Status”

The name of the input file will be "warriors.txt". Note that the commands do not have to appear in that order. The only requirement is that a Battle command cannot appear until the specified warriors have been seen.

The Status command displays how many warriors there, then displays each one with his strength. The Warrior command does not display anything. The Battle command displays one line to says who is fighting whom and a second line to report the results, as shown below.

The output (which would display on the screen) for this file should look like:

“There are: 5 warriors
Warrior: Jim, strength: 42
Warrior: Lancelot, strength: 15
Warrior: Arthur, strength: 15
Warrior: Torvalds, strength: 20
Warrior: Gates, strength: 8
Arthur battles Lancelot
Mutual Annihilation: Arthur and Lancelot die at each other's hands
Jim battles Lancelot
He's dead, Jim
Torvalds battles Gates
Torvalds defeats Gates
Gates battles Lancelot
Oh, NO! They're both dead! Yuck!
There are: 5 warriors
Warrior: Jim, strength: 42
Warrior: Lancelot, strength: 0
Warrior: Arthur, strength: 0
Warrior: Torvalds, strength: 12
Warrior: Gates, strength: 0”

For the sequence of commands, your output should looke exactly as shown above. Please don't exercise you creativity here.

Use a struct. That means we do not want you to define classes or methods, or to use data hiding.

Make good use of functions. In this program, for example, when you determine that you are looking at a Warrior command then you should call a function to handle that command. Similarly with the other commands. The program should have a "reasonable" amount of useful comments explaining what each part of your code does. The code must be well formatted. Never turn in code with bad indentation.
All structs, functions and variables should have good names. Function names should clearly identify what the function does. Follow the convention that structs begin with an uppercase letter, constants are all uppercase and functions and variables begin with a lowercase letter.

Solutions

Expert Solution

Screenshot

--------------------------------------------------------------------------------------------------

Program

//Header files
#include <iostream>
#include<vector>
#include<fstream>
#include<string>
using namespace std;
//Create a struct for warrior
struct Warrior {
   string name;
   int strength;
};
//Function protottypes
void createWarrior(vector<Warrior>& warriors,ifstream& in);
void displayStatus(vector<Warrior> warriors);
void takeBattle(vector<Warrior>& warriors, ifstream& in);
int main()
{
   //Create a vector for warrirs storage
   vector<Warrior> warriors;
   //Variable to read command from file
   string cmd;
   //File open
   ifstream in("warriors.txt");
   //If file not found
   if (!in) {
       cout << "File not found!!!" << endl;
       exit(0);
   }
   //Read each lines command first
   while (in >> cmd) {
       //Execute each commands
       if (cmd == "Warrior") {
           createWarrior(warriors, in);
       }
       else if (cmd == "Status") {
           displayStatus(warriors);
       }
       else if (cmd == "Battle") {
           takeBattle(warriors, in);
       }
   }
   //Close the file
   in.close();
}
//Function to insert a warrior
void createWarrior(vector<Warrior>& warriors,ifstream& in) {
   //Create a warroir
   Warrior warrior;
   //Read warrior details
   in >> warrior.name >> warrior.strength;
   //Push into warriors vector
   warriors.push_back(warrior);
}
//Function to display status of each warriors
void displayStatus(vector<Warrior> warriors) {
   cout << "There are " << warriors.size() << " warriors\n";
   for (int i = 0; i < warriors.size(); i++) {
       cout << "Warrior: " << warriors[i].name << ", strength: " << warriors[i].strength << endl;
   }
}
//Function to get battle details
void takeBattle(vector<Warrior>& warriors, ifstream& in) {
   string warrior1, warrior2;
   int index1, index2;
   bool flag1 = false, flag2 = false;
   //Read battling warriors names
   in >> warrior1 >> warrior2;
   //Check if both warrirs present in the warriors list
   for (int i = 0; i < warriors.size(); i++) {
       if (warrior1 == warriors[i].name) {
           flag1 = true;
           index1 = i;
       }
       if (warrior2 == warriors[i].name) {
           flag2 = true;
           index2 = i;
       }
   }
   //If present
   if (flag1 == flag2) {
       //Display battling warriors
       cout << warriors[index1].name << " battles " << warriors[index2].name << endl;
       //Check each conditions and display battle cresult and update strength accordingly
         if (warriors[index1].strength ==0 && warriors[index2].strength == 0) {
            cout << "Oh, NO! They're both dead! Yuck!" << endl;
         }
       else if (warriors[index1].strength == warriors[index2].strength) {
           cout << "Mutual Annihilation: " << warriors[index1].name << " and " << warriors[index2].name << " die at each other's hands" << endl;
           warriors[index1].strength = warriors[index2].strength = 0;
       }
       else if (warriors[index1].strength==0 && warriors[index2].strength!=0) {
           cout << "He's dead, "<< warriors[index2].name<< endl;
       }
       else if (warriors[index1].strength != 0 && warriors[index2].strength == 0) {
           cout << "He's dead, " << warriors[index1].name << endl;
       }
       else if (warriors[index1].strength > warriors[index2].strength) {
           cout << warriors[index1].name<<" defeats "<< warriors[index2].name << endl;
           warriors[index1].strength -= warriors[index2].strength;
           warriors[index2].strength = 0;
       }
       else if (warriors[index1].strength < warriors[index2].strength) {
           cout << warriors[index2].name << " defeats " << warriors[index1].name << endl;
           warriors[index2].strength -= warriors[index1].strength;
           warriors[index1].strength = 0;
       }
   }
}

-------------------------------------------------------------------------------

Output

There are 5 warriors
Warrior: Jim, strength: 42
Warrior: Lancelot, strength: 15
Warrior: Arthur, strength: 15
Warrior: Torvalds, strength: 20
Warrior: Gates, strength: 8
Arthur battles Lancelot
Mutual Annihilation: Arthur and Lancelot die at each other's hands
Jim battles Lancelot
He's dead, Jim
Torvalds battles Gates
Torvalds defeats Gates
Gates battles Lancelot
Oh, NO! They're both dead! Yuck!
There are 5 warriors
Warrior: Jim, strength: 42
Warrior: Lancelot, strength: 0
Warrior: Arthur, strength: 0
Warrior: Torvalds, strength: 12
Warrior: Gates, strength: 0


Related Solutions

Please use c++ and follow the instruction, I really want to fully understand this question and...
Please use c++ and follow the instruction, I really want to fully understand this question and I will use your code to check where I made mistake (Do not skip steps). I have written my own code which has tons of errors, and I am so confused about this lab. I need help. Lab 6.4 – C++ and Functions – Math Test Critical Review A value-returning function is a function that returns a value back to the part of the...
I want to understand how this can be solved in c++. Please send a screenshot also...
I want to understand how this can be solved in c++. Please send a screenshot also with your code so I can see how it is supposed to be formatted or indented. Instructions: Your program will read in a file of commands. There are three types of commands: Warrior creates a new warrior with the specified name and strength. Battle causes a battle to occur between two warriors. Status lists all warriors, alive or dead, and their strengths. A sample...
No sloppy writing, please! I want to be able to see and understand each step :)...
No sloppy writing, please! I want to be able to see and understand each step :) The three polarizing filters are bundled so that the polarizing axes of the second and third filters are 33 ° and 71 °, respectively, relative to the polarizing axis of the first filter. Intensity of unpolarized light hitting the bundle after passing through the bundle is 53 W / cm^2. If the intensity of the incoming light is kept constant, what is the intensity...
TO BE DONE IN C LANGUAGE BEFORE READING THE QUESTION PLEASE NOTE THAT YOUR FUNCTION SHOULD...
TO BE DONE IN C LANGUAGE BEFORE READING THE QUESTION PLEASE NOTE THAT YOUR FUNCTION SHOULD EXACTLY PRODUCE THE GIVEN OUTPUT AND THE TIME COMPLEXITY SHOULD BE O(N^2) YOU NEED TO KNOW ABOUT COCKTAIL SHAKER SORT ALGORITHM: Cocktail-shaker sort is a modification of Bubble sort. Cocktail-shaker sort traverses the data structure in one direction, pushing the largest element ahead towards one end of the data structure, and then in the opposite direction, pushing the smallest element towards the other end,...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind...
C++ Please write a exmaple of class template RedBlackTree.h.(It must be template!). I do not mind about details but please include the basic functions that are necessary for a RedBlackTree.
Please explain your answer thoroughly because I want to understand it well and also please include...
Please explain your answer thoroughly because I want to understand it well and also please include a diagram if possible Two objects which have mass: m1 = 10kg and m2 = 20kg. Both of them are moving at the velocity of: v1 = 20^i ms and v2 = 10^j ms and then the two objects collide completely inelastically. In what direction do the two objects go after the collision? After the collision, how much kinetic energy was lost?
No sloppy writing, please! I want to be able to see and understand each step. Thank...
No sloppy writing, please! I want to be able to see and understand each step. Thank you! The broadcast transmitter transmits a sinusoidal radio signal at a frequency of 95 MHz with a transmission power of 55 kW. The radio signal is received at a distance of 2.5 km with a circular wire antenna with a diameter of 18 cm. What is the maximum possible electromotive force induced in the loop antenna, assuming that the transmitted power is evenly distributed...
MUST BE DONE IN C (NOT C++) In this program we will calculate the average of...
MUST BE DONE IN C (NOT C++) In this program we will calculate the average of x students’ grades (grades will be stored in an array). To do so, please follow these guidelines: - Your program should ask the user for the number of students that are in the class. This number should help you declare your array. - Use the function seen in class to scan the grades of the array. In other words, we will populate the array...
(MUST BE DONE IN C (NOT C++)) In this task, you will create a structure with...
(MUST BE DONE IN C (NOT C++)) In this task, you will create a structure with arrays. You will have to create your own structure. However, make sure to meet these guidelines: - Give the structure whichever name you want. - It must have at least 3 members. - Two of the members must be arrays. - Your members should be of at least two different data-types. In other words, your members cannot be integers only (or floats, or doubles…)....
This is C++ there are intruction and descriptions. Please give me the answer because I understand...
This is C++ there are intruction and descriptions. Please give me the answer because I understand the concept, but don't know how to put in the actual problem yet. Instructions and Assumptions Declare and implement the three functions described below. Your declarations should go in AnyList.h. Your definitions should go in Functions.cpp. For all of these functions, assume the list contains at least three elements. No need to consider the empty list cases. The Functions 1. Overload the insertion operator...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT