使用 While 循环实现游戏结果:回合制决斗游戏
Implementing Game Outcomes with While Loop: Turn-based Dueling Game
背景
我正在针对计算机制作一款回合制决斗游戏。现在,我正在尝试让游戏的准系统在没有玩家输入的情况下运行。在每一轮之后(玩家或计算机的一个动作)可能会发生四种可能性之一:
- 玩家生活和电脑生活
- 玩家存活,电脑死亡
- 玩家死亡,电脑存活
- 玩家死亡和计算机死亡(例如:玩家的攻击也会伤害玩家。还没有做任何事情)
问题
当可能性 2、3 或 4 发生时,我不知道如何让程序停止。现在,整个事情都在运行,播放器和计算机死机了,即使播放器应该使用我当前的代码也是如此。
代码
这是我当前工作的一个最低限度的工作示例。我知道我在做一些愚蠢的事情,但我无法确定它。任何 suggestions/pointers 将不胜感激!
#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;
}
你只测试循环顶部的状态,
如果您想立即退出,请执行此操作
// 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);
}
背景
我正在针对计算机制作一款回合制决斗游戏。现在,我正在尝试让游戏的准系统在没有玩家输入的情况下运行。在每一轮之后(玩家或计算机的一个动作)可能会发生四种可能性之一:
- 玩家生活和电脑生活
- 玩家存活,电脑死亡
- 玩家死亡,电脑存活
- 玩家死亡和计算机死亡(例如:玩家的攻击也会伤害玩家。还没有做任何事情)
问题
当可能性 2、3 或 4 发生时,我不知道如何让程序停止。现在,整个事情都在运行,播放器和计算机死机了,即使播放器应该使用我当前的代码也是如此。
代码
这是我当前工作的一个最低限度的工作示例。我知道我在做一些愚蠢的事情,但我无法确定它。任何 suggestions/pointers 将不胜感激!
#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;
}
你只测试循环顶部的状态,
如果您想立即退出,请执行此操作
// 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);
}