Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The sheriff’s responsibility is to protect the town. He and the bad guy are head

ID: 3771447 • Letter: T

Question

The sheriff’s responsibility is to protect the town. He and the bad guy are headed for a showdown. This is not a fair battle. Randomly select when each player shoots at the other guy. And assign a probability for each shot to hit its target. When hit the health is decremented, until it is depleted. The next hit would decrement the life attribute.


'GunSlinger' Class Definition
                private
                    bool lives
                    int shield
                    int health
                public
                    Name, string
                    Constructor
                        Gunslinger(string)
                        takes a string argument and sets the name of the object
                        initialize the private variables
                    void Shoot(GunSlinger* gunman)
                        When using a class object in the same Classes method you need to pass the address of the instanced object
                        call the ImHit() method of the object being shot
                        don't use 'this' but the local name of the object passed in
                            for example, gunman->ImHit()
                    void ImHit()
                        Decrements the private variables
                        for example
                            when shields give out, start to decrement health
                            when health gives out ... well, you know.
                    bool getLife()
                        return the status of lives
            The main() function should
                Create two Gunslingers
                seed the random number function
                set up a while loop, testing if both gunslingers are alive
                    randomly choose who shoots
            Everything will fly by unless you slow it down.
                #include <Windows.h>
                Sleep(int) where int is in milliseconds

Here's what I got:

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

class GunSlinger
{

private:
   bool lives;
   int shield;
   int health;

public:
   string Name;
   GunSlinger(string);
   void Shoot(GunSlinger*);
   void ImHit();
   bool getLife();
};


GunSlinger::GunSlinger(string alias)
{
   Name = alias;
   lives = true;
   shield = 5;
   health = 5;
}

void GunSlinger::Shoot(GunSlinger* gunman)
{
   gunman->ImHit();
   Name = alias;
}

void GunSlinger::ImHit()
{
   if (rand() % 10 > 5)
   {
       if (shield > 0)
       {
           shield--;
           cout << "My protection is giving out!";
       else if (health--);
       cout << "Na Na! You missed me";
       else
       {

           lives = false;
       }
       }
   }
}

bool GunSlinger::getLife()
{
   return lives;
}

int main()
{
  

   GunSlinger sherriff("John");
   GunSlinger badguy("Joe");
   while (sherriff.getLife() && badguy.getLife())
   {
       if (rand() % 2 == 1)
           sherriff.Shoot(badguy);
      
       else
           badguy.Shoot(sherriff);
   }
}

Explanation / Answer

Here is the updated code for you. Hope this will help you. If you need any further refinement, just get back to me.

#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class GunSlinger
{
private:
bool lives;
int shield;
int health;
public:
string Name;
GunSlinger(string);
void Shoot(GunSlinger*);
void ImHit();
bool getLife();
};

GunSlinger::GunSlinger(string alias)
{
Name = alias;
lives = true;
shield = 5;
health = 5;
}
void GunSlinger::Shoot(GunSlinger* gunman)
{
gunman->ImHit();
//Name = alias;
}
void GunSlinger::ImHit()
{
if (rand() % 10 > 5)
{
if (shield > 0)           //If you have shields.
{
shield--;           //Decrement shield by 1.
cout << Name<<": My protection is giving out!"<<endl;
}
else if (health > 0)   //If you ran out of shields, and you have health.
{
health--;               //Decrement health.
shield = 5;           //Reset the shield to 5, as in the new health, you'll again have 5 shields.
cout << Name<<": Na Na! You missed me"<<endl;   
}
else                   //If you have neither shields, nor health.
{
lives = false;       //You are no more alive. DEAD.....
}
}
}
bool GunSlinger::getLife()
{
return lives;
}
int main()
{
  
GunSlinger sherriff("John");
GunSlinger badguy("Joe");
while (sherriff.getLife() && badguy.getLife())
{
if (rand() % 2 == 1)
sherriff.Shoot(&badguy);
  
else
badguy.Shoot(&sherriff);
}
if(sherriff.getLife())
cout<<badguy.Name<<" lost."<<endl;
else
cout<<sherriff.Name<<" lost."<<endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote