Background
I'm making a turn-based dueling game against a computer. Right now, I'm trying to get the barebones of the game to work without player input. After each turn (one action by the player or computer) one of four possibilities can happen:
- Player lives and computer lives
- Player lives and computer dies
- Player dies and computer lives
- Player dies and computer dies (ex: player's attack damages the player too. Haven't done anything with that yet)
Problem
I can't figure out how to make the program stop when possibilities 2,3, or 4 happen. Right now, the entire thing runs and the player and computer die, even when the player should live with my current code.
Code
Here's a minimum working example of my current efforts. I know I'm doing something dumb, but I just can't pinpoint it. Any suggestions/pointers would be greatly appreciated!
#include <iostream>
class Player
{
private:
int m_hp {};
int m_action {};
public:
Player(int hp = 5): m_hp{hp}
{
}
int attack(const int& attackDamage)
{
return attackDamage;
}
int getHP() const // const because getHP shouldn't alter hp
{
return m_hp;
}
void takeHP(int damage)
{
m_hp = m_hp - damage;
}
void giveHP(int healing)
{
m_hp = m_hp healing;
}
};
class Computer
{
private:
int m_hp {};
public:
Computer(int hp = 5): m_hp{hp}
{
}
int attack(const int& attackDamage)
{
return attackDamage;
}
int getHP() const // const because getHP shouldn't alter hp
{
return m_hp;
}
void takeHP(int damage)
{
m_hp = m_hp - damage;
}
void giveHP(int healing)
{
m_hp = m_hp healing;
}
};
int healthCheck(Player& player, Computer& computer)
{
if (player.getHP() > 0 && computer.getHP() > 0)
{
return 1;
}
else if (player.getHP() > 0 && computer.getHP() <= 0)
{
return 2;
}
else if (player.getHP() <= 0 && computer.getHP() > 0)
{
return 3;
}
else if (player.getHP() <= 0 && computer.getHP() <= 0)
{
return 4;
}
else
{
return 0;
}
}
int gameStatus(int healthCheck, Player& player, Computer& computer)
{
switch(healthCheck)
{
case 1:
std::cout << "The battle continues." << "\n";
std::cout << "Player HP: " << player.getHP() << "\n";
std::cout << "Computer HP: " << computer.getHP() << "\n";
return 1;
break;
case 2:
std::cout << "The monster has been slain";
// add looting function here in future
// Will add money to player object here with addmoney()
return 2;
break;
case 3:
std::cout << "The player has been slain";
return 3;
break;
case 4:
std::cout << "Both duelers have been slain";
return 4;
break;
default:
std::cout << "error: received an int not 1,2,3,4";
return 5;
break;
}
}
void playerTurn(Player& player, Computer& computer)
{
computer.takeHP(1);
}
void computerTurn(Player& player, Computer& computer)
{
player.takeHP(1);
}
int main()
{
// initialize player and computer
Player player {};
Computer computer {};
int gameCondition{1};
// game should continue while the player is still alive
while(gameStatus(gameCondition, player, computer) == 1)
{
playerTurn(player, computer);
// attempt at checking the game status after each turn
gameCondition = healthCheck(player,computer);
gameStatus(gameCondition, player, computer);
computerTurn(player, computer);
gameCondition = healthCheck(player,computer);
gameStatus(gameCondition, player, computer);
}
std::cout << "The battle is over" << "\n";
return 0;
}
CodePudding user response:
you only test the status at the top of the loop,
do this if you want to exit immediately
// game should continue while the player is still alive
while(gameStatus(gameCondition, player, computer) == 1)
{
playerTurn(player, computer);
// attempt at checking the game status after each turn
gameCondition = healthCheck(player,computer);
if(gameStatus(gameCondition, player, computer) != 1) <<<<=====
break;
computerTurn(player, computer);
gameCondition = healthCheck(player,computer);
// test not needed here since its tested at the start of the loop
gameStatus(gameCondition, player, computer);
}