I've been trying to do a really easy game here, the thing is that the when a player reaches 0, it just keep going to -5 and such like. WHat's wrong?

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main ()
{
srand(time(0));
int player1 = 100;
int player2 = 100;
char player1Name;
char player2Name;
int PlayerTurn(1);
int Attack;
bool GameOver(false);
bool GameWin(false);
do {
    cout << "it's player" << PlayerTurn << "'s turn" << "do you want to attack with a high risk or a low risk attack? low risk = type1 high risk = type2" << endl;
    cout << "Attck with type >>";
    cin >> Attack;
                  if(Attack == 1) {
                            double hit = rand() % 31 + 10;
                            if(PlayerTurn == 1){
                            player2-hit;
                            cout << "you took " << hit << " points from player2" << endl;
                            cout << "\nplayer2 has " << player2 << " points left\n" << endl;
                            }
                            else {
                                 player1-hit;
                                 cout << "you took " << hit << " points from player1 " << endl;
                                 cout << "\nplayer1 has " << player1 << " points left\n" << endl;
                                 }
                            }
                  if(Attack == 2){
                            double maxhit = rand() % 41 + 18;
                            double iff = rand() % 2 + 1;
                            if(iff == 1){
                                   cout << "you failed" << endl;
                            }
                             if(iff == 2) {
                            if(PlayerTurn == 1){
                            player2-maxhit;
                            cout << "you took " << maxhit << " points from player2" << endl;
                            cout << "\nplayer2 has " << player2 << " points left\n" << endl;
                            }
                            else {
                                 player1-maxhit;
                                 cout << "you took " << maxhit << " points from player1" << endl;
                                 cout << "\nplayer1 has " << player1 << " points left\n" << endl;
                                 }
                            }
                  if(player1 <= 0) {
                             cout << "game over  player1 lost, and player2 won!" << endl;
                                  GameOver = true;   
                                  }
                  if(player2 <= 0) {
                             cout << "game over player2 lost, and player1 won!" << endl;
                                  GameOver = true;
                                  }
                             }
                             
                             if(GameOver){
                                         cout << "it's time to end this, bye!!";
                                         }
                             if(PlayerTurn == 1) {
                                           PlayerTurn = 2;
                             }
                              else {
                                    PlayerTurn = 1;
                                    }
                 } while(!GameOver);
                                    }

Recommended Answers

All 5 Replies

I don't understand what the 0 and -5 thing is with that code, but your player's hit points don't ever get subtracted from. If you do player2-hit; , it doesn't change player2. You need to do player2=player2-hit; to save the changes. Or player2-=hit is a little shorter and does the same thing.

I'd also move the check for player1 and player2 being less than 0 to outside the attack2 if statement. But because you print the hitpoints before checking for a game over, it will still print negative values. You can fix that by only printing positive hitpoints. If the hitpoints are below 0, print 0.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

int positive(int value)
{
    return value >= 0 ? value : 0;
}

int main ()
{
    srand(time(0));
    int player1 = 100;
    int player2 = 100;
    char player1Name;
    char player2Name;
    int PlayerTurn(1);
    int Attack;
    bool GameOver(false);
    bool GameWin(false);
    do {
        cout << "it's player" << PlayerTurn << "'s turn" << "do you want to attack with a high risk or a low risk attack? low risk = type1 high risk = type2" << endl;
        cout << "Attck with type >>";
        cin >> Attack;
        if(Attack == 1) {
            double hit = rand() % 31 + 10;
            if(PlayerTurn == 1){
                player2-=hit;
                cout << "you took " << hit << " points from player2" << endl;
                cout << "\nplayer2 has " << positive(player2) << " points left\n" << endl;
            }
            else {
                player1-=hit;
                cout << "you took " << hit << " points from player1 " << endl;
                cout << "\nplayer1 has " << positive(player1) << " points left\n" << endl;
            }
        }
        if(Attack == 2){
            double maxhit = rand() % 41 + 18;
            double iff = rand() % 2 + 1;
            if(iff == 1){
                cout << "you failed" << endl;
            }
            if(iff == 2) {
                if(PlayerTurn == 1){
                    player2-=maxhit;
                    cout << "you took " << maxhit << " points from player2" << endl;
                    cout << "\nplayer2 has " << positive(player2) << " points left\n" << endl;
                }
                else {
                    player1-=maxhit;
                    cout << "you took " << maxhit << " points from player1" << endl;
                    cout << "\nplayer1 has " << positive(player1) << " points left\n" << endl;
                }
            }
        }

        if(player1 <= 0) {
            cout << "game over  player1 lost, and player2 won!" << endl;
            GameOver = true;   
        }
        if(player2 <= 0) {
            cout << "game over player2 lost, and player1 won!" << endl;
            GameOver = true;
        }

        if(GameOver){
            cout << "it's time to end this, bye!!";
        }
        if(PlayerTurn == 1) {
            PlayerTurn = 2;
        }
        else {
            PlayerTurn = 1;
        }
    } while(!GameOver);
}

hey!! it works! Thank you!!!

I had already fixed the player2=player2-hit;
but the other tip was great!

You could also use player2--

You could also use player2--

You could, but to get the same behavior it has to be in a loop:

for (int x = 0; x < hit; ++x) --player2;

-- only decrements a variable by 1, it is the same as player2 = player2 - 1 or player2 -= 1 .

Ups, haven't seen that value is being decremented by value of variable hit ... pardon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.