954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

My simple game: what would i use

EDIT: I've got a while loop in my code, just now when player 1 goes to have his/her second attack the hp is reset to 99,

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int p1attack, p2attack, p1hp, p2hp;
    char attack;
    
    p1hp = 99;
    p2hp = 99;
    
    system("TITLE Game.");
    
    while(p1hp > 0 && p2hp > 0)
    {
   
    cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p1attack = rand() % 99 + 1; 
         cout << "You hit " << p1attack << "!";
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    p2hp -= p1attack;
    
    cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
    cin >> attack;
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p2attack = rand() % 99 + 1; 
         cout << "You hit " << p2attack << "!" << endl;
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    
    p1hp -= attack;
    }
    

}
getch();
}
Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 

> p1hp -= attack;
This isn't the value you calculated.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

I've changed it to: p1hp -= p2attack;
But p1hp still stays 99

Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 

Hey Black Magic i have solved your problem.

Well just examine That you have typed this code in

p1hp -= p2attack;


Line 54. Which probarbly doesnt get executed because there are break; statements above it.

So You actually need to paste the code in between Line 46 and line 47 so that the value gets changed.

So here is the total program with the adjustment.

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int p1attack, p2attack, p1hp, p2hp;
    char attack;
    
    p1hp = 99;
    p2hp = 99;
    
    system("TITLE Game.");
    
    while(p1hp > 0 && p2hp > 0)
    {
   
    cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p1attack = rand() % 99 + 1; 
         cout << "You hit " << p1attack << "!";
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    p2hp -= p1attack;
    
    cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
    cin >> attack;
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p2attack = rand() % 99 + 1; 
         p1hp -= p2attack;
         cout << "You hit " << p2attack << "!" << endl;
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    
    
    }
    

}
getch();
}
Sky Diploma
Practically a Posting Shark
864 posts since Mar 2008
Reputation Points: 673
Solved Threads: 130
 

Or, if you'd like to keep in consistent with the code for player 1's attack, you could simply put it outside of the switch statement but inside the while loop (like line 57 or 58 in Sky Diploma's program). If you do do that though, remember to take away that same code from line 47.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

I actually put the code in 47 Keeping in mind that there is an option for an attacker not to attack also. right

For example if the user enters something other than "A".

Sky Diploma
Practically a Posting Shark
864 posts since Mar 2008
Reputation Points: 673
Solved Threads: 130
 

I actually put the code in 47 Keeping in mind that there is an option for an attacker not to attack also. right

For example if the user enters something other than "A".


Hmm.... yes you're very correct. In fact, the same thing should be done to the player1 code, because if p2hp -= p1attack; is called and p1attack hasn't been initialized, you'll get not so optimal results.

So either change it, or in the beginning of the loop set p1attack and p2attack to 0.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

Yes . That is also a good solution ;) Cool Gamer 48

I dint think of that.

Sky Diploma
Practically a Posting Shark
864 posts since Mar 2008
Reputation Points: 673
Solved Threads: 130
 

Thanks guys, now i was wondering now if i should make it so the other player cannot make your hp a negative number, how could this be done? Please reply.

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    int p1attack, p2attack, p1hp, p2hp;
    char attack;
    
    p1hp = 99;
    p2hp = 99;
    
    system("TITLE Game.");
    
    while(p1hp > 0 && p2hp > 0)
    {
   
    cout << "Player 1 (HP:" << p1hp << ") Press 'A' to attack : ";
    cin >> attack;
    
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p1attack = rand() % 50 + 1; 
         cout << "You hit " << p1attack << "!";
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    }
    p2hp -= p1attack;
    
    cout << endl << endl << "Player 2 (HP:" << p2hp << ") Press 'A' to attack : ";
    cin >> attack;
    switch(attack)
    {
                      
    case 'a':
    case 'A': 
         srand ( time(NULL) );
         p2attack = rand() % 50 + 1; 
         p1hp -= p2attack;
         cout << "You hit " << p2attack << "!" << endl << endl;
    break;
    
    default:
         cout << endl << "INVALID SELECTION!";
    break;
    
    
    }
    

}
getch();
}
Black Magic
Junior Poster
178 posts since Apr 2008
Reputation Points: 25
Solved Threads: 4
 

Right after p2hp -= p1attack; , add:

if(p2hp < 0)
    p2hp = 0;


You may also try making the health values unsigned, though the above method should work well enough

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

Hey Black Magic i think you will need to end the game once a person's hp goes into negatives .So try using the || Operator instead of && in Line 16

Sky Diploma
Practically a Posting Shark
864 posts since Mar 2008
Reputation Points: 673
Solved Threads: 130
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You