SRL Archive - C/C++ Help and Tutorials

Help with my mini text based game, values reset


Rs-Gp-4U

2008-04-27 12:55:01

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,

[PHP]#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 -= p2attack;
}


}
getch();
}[/PHP]

Please help a.s.a.p

Jackrawl

2008-04-29 19:43:22

What is getch?
The error was that you put the new value of p1hp right after a break [in a switch loop].
(//<<<<—- indicates error, //>>>>—- indicates fix)
[php]#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;

//<<<<—- indicates error
}
p1hp -= p2attack;//>>>>—- indicates fix

}
getch();
}
[/php]
Also, you could compact the code a little bit, storing attack to one variable, then subtracting it based on a boolean.

Rs-Gp-4U

2008-05-01 13:12:02

Thanks, i've got the program working now, and btw getch(); is a function that keeps your program running when finsihed, outputted text etc until another key is pressed

boberman

2008-05-06 12:21:34

humm, this looks very procedural, you may want to give OO a try (MUCH better for games).

Here is an example.
class Player
{
private:
int hp;
public:
Player() // Ran when a new object is created
{
srand(time(NULL)); // Seed at startup, not every time
hp = 100;
}
int attacked()
{
int hit;
hit = rand() % 99 + 1;
hp -= hit;
return hit;
}
int getHP()
{
return hp;
}
};
int main()
{
Player p1;
Player p2;

char attack;
while(p1.getHP() < 0 && p2.getHP() < 0)
{
cout << "Player 1 HP " << p1.getHP() << " (Press 'A' to attack): ";
cin >> attack;
toUpper(attack);

switch (attack)
{
case 'A':
cout << "You hit " << p2.attacked() << endl;
break;
default:
cout << "Invalid Command\n";
}

cout << "Player 2 HP " << p2.getHP() << " (Press 'A' to attack): ";
cin >> attack;
toUpper(attack);

switch (attack)
{
case 'A':
cout << "You hit " << p1.attacked() << endl;
break;
default:
cout << "Invalid Command\n";
}
}
return 0;
}
Actually that could be made much cleaner with some operator overloads (the ostream << would do nicely) as well you can require attacked to take in a player and base damage on the HP of the attacker. Lots of fun stuff that you could do. But OO is really the way to go :)