我用c ++做了一个非常简单的游戏。大部分工作都很好,但每当我尝试重放游戏时,价值都不会重置并继续我最后一次离开的地方,这不是我想要它发生的事情。就像我们说的那样,在我的第一次尝试中,我击败了敌人并且它的生命值为零,当我重试它时,该值不会从100开始,但它从零开始。我真的想要解决这个问题。无论如何,这是我的代码(“它很长):
#include <iostream>
#include <string>
using namespace std;
class Health{
private:
int health;
Health(const Health& cpy){
health = cpy.health;
}
public:
Health(){
health = 100;
}
void display_health(){
cout << "Health: " << health << endl;
}
Health& operator -=(int atk){
health -= atk;
return *this;
}
bool operator <(int compare){
return (health < compare);
}
bool operator >(int compare){
return !(this->operator<(compare));
}
bool operator &&(const int compare){
return (health && compare);
}
};
class Hero : public Health{
public:
void kick(){
cout << "Your kick damaged 45 health points" << endl;
}
void Punch(){
cout << "Your punch damaged 25 health points" << endl;
}
void Knife(){
cout << "Your kinfe damaged 60 health points" << endl;
}
void Tackle(){
cout << "Your tackle damaged 10 health points" << endl;
}
};
class Enemy : public Health{
public:
void Basic_hit(){
cout << "Enemy damaged you by 25 point" << endl;
}
};
int main(){
//local vaiables
string name;
int choice;
string ply_agn = "y";
//instance
Hero hero;
Enemy enmy;
//user interface
do{ //entire game repeats
cout << "Enter you character name: ";
getline(cin, name);
cout << "Your default health is : ";
hero.display_health();
cout << endl;
do{
//user interface
cout << "0.Kick - 45" << endl;
cout << "1.Punch - 25 " << endl;
cout << "2.Kinfe - 60" << endl;
cout << "3.Tackle - 10 " << endl;
//players choice
cout << "Which move you like to chose: ";
cin >> choice;
cout << endl;
//switch function
switch (choice)
{
case 0:
hero.kick();
cout << "Enemy ";
enmy -= 45;
enmy.display_health();
break;
case 1:
hero.Punch();
cout << "Enemy ";
enmy -= 25;
enmy.display_health();
break;
case 2:
hero.Knife();
cout << "Enemy ";
enmy -= 60;
enmy.display_health();
break;
case 3:
hero.Tackle();
cout << "Enemy ";
enmy -= 10;
enmy.display_health();
break;
default:
cout << "Invalid move! Try again" << endl;
break;
}
cout << endl;
//Enemy move
enmy.Basic_hit();
hero -= 25;
cout << "The enemy hit you with a basic hit. You recieved 25 damage" << endl;
cout << "Your ";
hero.display_health();
cout << endl;
} while (enmy > 0);
//winning condition
if (enmy < 0 && hero > 0) {
cout << "You won" << endl;
}
else if (hero < 0 && enmy > 0){
cout << "You lose" << endl;
}
else if (hero < 0 && enmy < 0){
cout << "This game is a draw" << endl;
}
} while (ply_agn == "y"); //This is where I think is messing up the game
//repeating condition
cout << "Would you like to play again: ";
cin >> ply_agn;
system("pause");
return 0;
}
谢谢你的时间
答案 0 :(得分:4)
点击
Hero hero;
Enemy enmy;
之后的do{
...
string ply_agn = "y";
//user interface
do{ //entire game repeats
//instance
Hero hero;
Enemy enmy;
...
答案 1 :(得分:4)
正如其他人所说,你需要将你的Hero
和Enemy
对象放在你的循环中,这样它们就会在游戏的每个循环之后重新创建。
你的程序中的另一个错误是你mix getline
with cin
,所以在你的下一个循环中,你将无法给你的角色一个新名字。为避免这种情况,您需要在cin.ignore
函数调用之前放置getline()
,如下所示
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, name);
此处,std::numeric_limits在标头<limits>
中定义并被使用,因为它是流的最大大小,因此它会忽略许多换行符\n
留在缓冲区cin
。
作为最后的想法,你需要重复问题之前循环测试,就像这样
cout << "Would you like to play again: ";
cin >> ply_agn;
} while (ply_agn == "y");
否则ply_agn
始终为y
,用户将无法退出游戏: - )
答案 2 :(得分:2)
您从未在游戏开始时明确重置健康状况。如果您要通过这样的循环重置,则游戏的第一步应该是重置Hero
和Enemy
。只需将您声明它们作为do while
循环中第一行的位置移动即可。