-1

So I tried to make a little text-based battle system, but it kind of broke... I was wondering if someone could help me figure out why? I tried to use variables and simple math algorithms to make it as easy to run as possible, but it just keeps resetting after player 2's turn. It also broadcasts the "You Can't Heal!" (player 2's turn) message even if the player doesn't heal. I checked the brackets a couple times, as well as my de-bugger, however it says that everything is fine. I'm not sure what the problem is, so if someone could point it out that would be great. Thanks in advance!

Code Below V


using namespace std


int varset()
{
    string ChoiceTeamA;
    string ChoiceTeamB;
    bool Atk1 = false;
    bool Atk2 = false;
    int health = health;
    int health1 = health1;
}
int main() {    
    cout << "player 1 turn - choose what to do" << endl;
    cout << "Player 1's health is " << health << " and Player 2's health is " << health1 << endl;
    cout << "A. Attack" << endl;
    cout << "B. Heal Magic" << endl;
    getline(cin, ChoiceTeamA);
    if (ChoiceTeamA == "A")
        {
                string ChoiceTeamA = "c";
                   health1--;
                   bool Atk1 = false;
                   cout << "Player 1 attacked! Player 2's health is now " << health1 << endl;
        }
    if (ChoiceTeamA == "B")
        {
            string ChoiceTeamA = "0";
            if (health < 5)
                {
                 health++;
                 cout << "Player 1 healed! Player 1's health is now " << health;
                }
            else
                {
                    cout << "You can't heal!" << endl;
                }
        }

    cout << "." << endl;
     cout << "player 2 turn - choose what to do" << endl;
     cout << "Player 1's health is " << health << " and Player 2's health is " << health1 << endl;
    cout << "A. Attack" << endl;
    cout << "B. Heal Magic" << endl;
    getline(cin, ChoiceTeamB);
    if (ChoiceTeamB == "A")
        string ChoiceTeamB = "c";
        {
                string ChoiceTeamA = "c";
                   health--;
                   bool Atk2 = false;
                   cout << "Player 2 attacked! Player 1's health is now " << health << endl;
        }

    if (ChoiceTeamB == "B")
        {
            string ChoiceTeamA = "c";
            if (health < 5)
                {
                 health1++;
                  cout << "Player 2 healed! Player 2's health is now " << health1;
                }
            if (health = 5)
                {
                 cout << "You can't heal!" << endl;
                 cout << "." << endl;
                }
        }

if (health = 0)
    {
        cout << "Player 2 wins!!!";
        return 0;
    }
if (health1 = 0)
    {
        cout << "Player 1 wins!!!";
        return 0;
    }

    return main();
}
  • 2
    `return main();` is a bug. The `c++` standard states that you may not call main(). – drescherjm May 10 '21 at 22:25
  • `if (health1 = 0)` is a bug, you are assigning 0 to health. In `c++` you need to use `==` for a comparison. – drescherjm May 10 '21 at 22:26
  • @drescherjm So what should I do to fix it? – CarSeatHeadrest_Lover May 10 '21 at 22:27
  • And for your "not sure why" any time you get in that situation you should use a debugger. – drescherjm May 10 '21 at 22:27
  • For the first one use a loop instead of calling main(). Or create a second function that is not named main that recursively calls itself. Call that second function from `main()` – drescherjm May 10 '21 at 22:28
  • Be aware that assignments in an "if" conditional are allowed. Whatever is used to assign to the variable will evaluated: 0 as false, true otherwise. – Thomas Matthews May 10 '21 at 22:28
  • If you read in the choice as a `char`, you could use a `switch` statement. – Thomas Matthews May 10 '21 at 22:29
  • Here is a question that talks about calling main(): [https://stackoverflow.com/questions/11349515](https://stackoverflow.com/questions/11349515) – drescherjm May 10 '21 at 22:31
  • You should have an outer loop that loops until a player is dead. This avoids the dilemma of recursively calling `main`. – Thomas Matthews May 10 '21 at 22:31
  • This doesn't compile for me at all because a bunch of variables you use (health, health1, ChoiceTeamA, etc) aren't actually defined in a place where they're accessible to `main`. So whatever code you have that's actually compiling but producing unexpected behavior is different from what you've shown here. Getting rid of the recursive invocation of `main` is a good place to start, though. – Nathan Pierson May 10 '21 at 22:42

1 Answers1

0

I fixed your code, but remember a few things next time:

  • Remember to use good formatting
  • main() cannot be called in your code
  • == is for comparisons and = is for assignments.

Note the use of reference parameters in the turn function.

Don't stop learning. :)

#include <iostream>

using namespace std;

void turn(int playerNum, string choice, int& healthATK, int& healthDEF) {
  int othPlayerNum = 0;
  if(playerNum == 1) 
    othPlayerNum = 2;
  else
    othPlayerNum = 1;
  cout << "Player " << playerNum << " turn - choose what to do" << endl;
  cout << "Player " << playerNum << "'s health is " << healthATK << " and Player " << othPlayerNum <<"'s health is " << healthDEF << endl;
  cout << "A. Attack" << endl;
  cout << "B. Heal Magic" << endl;
  getline(cin, choice);
  if (choice == "A") {
    healthDEF--;
    bool Atk1 = false;
    cout << "Player " << playerNum << " attacked! Player " << othPlayerNum << "'s health is now " << healthDEF << endl;
  }
  if (choice == "B") {
    if (healthATK < 5) {
      healthATK++;
      cout << "Player " << playerNum << " healed! Player " << playerNum << "'s health is now " << healthATK << endl;
    }
    else
      cout << "You can't heal!" << endl;
  }
}

void play() {
  string ChoiceTeamA;
  string ChoiceTeamB;
  bool Atk1 = false;
  bool Atk2 = false;
  int health = 10;
  int health1 = 10;
  while(true) {
    turn(1, ChoiceTeamA, health, health1);
    turn(2, ChoiceTeamB, health1, health);

    if (health <= 0) {
      cout << "Player 2 wins!!!";
      return;
    }
    if (health1 <= 0) {
      cout << "Player 1 wins!!!";
      return;
    }
  }
}

int main() {
  play();
  return 0;
}
TheHawkSG
  • 41
  • 7