I think the problem is a matter of variable scope.
Consider the variables myOpponent and pOHealth here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void GameLoopOpponnentBegins()
{
Player myPlayer;
Opponnent myOpponnent;
int *pPHealth = PlayerHealthHeap();
int *pOHealth = OpponnentHealthHeap();
while (*pPHealth != 0 && *pOHealth != 0)
{
myOpponnent.Script();// contains unrelated variables with the same names
myPlayer.Menu();
//-----------------------//still shows the begin value of 150 and 170
cout << "My health is: " << *pPHealth << endl;
cout << "His health is: " << *pOHealth << endl;
}
}
|
Within the function Script, the variables myOpponent and pOHealth are local to the function. They are unrelated to the variables by the same name outside the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void Opponnent::Script()
{
Opponnent myOpponnent;// not the opponent calling the function.
int *pOHealth = OpponnentHealthHeap();// not the pOHealth in the calling function, so that value is unaffected.
int potion = 2;
if (*pOHealth <= 60 && potion >= 0)
{
*pOHealth += 50;
--potion;
}
else
{
OpponnentAttack();
}
}
|
Without examining your code in detail (please develop and test individual features and functions as you go), I suggest modifying Script to take the pOHealth variable from outside the function, so it can be modified. Also don't declare a local opponent in the function. Let the function operate on the calling opponent object.
Eg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void Opponnent::Script(int * pHealth)
{
int potion = 2;
if (*pHealth <= 60 && potion >= 0)
{
*pHealth += 50;
--potion;
}
else
{
OpponnentAttack();
}
}
|
Not sure that will be enough to fix, but should be in the right direction.
Same issue with the Player::Menu() function.
Also, please use code tags, as LB has asked.
@Lorence30. That code is fine. You can give constructor args when allocating just a single instance.
The problem arises when allocating arrays:
int* pi = new int[5];// can't give ctor args here.