In: Computer Science
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