C++ - 为文本RPG创建基本战斗功能

C++ - Creating Basic Combat Function for Text RPG

本文关键字:功能 创建 文本 RPG C++      更新时间:2023-10-16

我目前正在为一个职业开发一个基本的文本RPG,我在战斗系统中遇到了一些奇怪的问题。基本上,我可以选择在每个战斗阶段攻击或不攻击敌人。当我攻击敌人时,生命值会以预期的速度下降。但是,当我选择不攻击时(这意味着敌人仍然攻击(,我对敌人造成的伤害在该回合中没有直接说明,但是当我选择下一次攻击时,它会对敌人造成预期的伤害以及我应该没有攻击时的伤害。

(警告:我知道我的代码会写得很差,而且可能没有最好的编程逻辑......我是一名新学生,只是使用我目前所学到的知识。

我的代码如下:

int showEnemyHp(int enemyhp, int attack) {
enemyhp = enemyhp - attack;
return enemyhp;
}
int showHp(int hp, int enemyattack) {
hp = hp - enemyattack;
return hp;
}
void enemyBattle() {
int hp = 30, enemyhp = 20, attack = 10, enemyattack = 5;
int hitEnemy;
while (true) {
cout << "Hit enemy?n(1) Yesn(2) No" << endl;
cin >> hitEnemy;
if (hitEnemy == 1) {
enemyhp = showEnemyHp(enemyhp, attack);
hp = showHp(hp, enemyattack);
cout << "You hit the enemy." << endl;
cout << "The enemy now has " << enemyhp << "HP left." << endl;
if (enemyhp <= 0) {
cout << "You've killed the enemy!" << endl;
break;
} else if (enemyhp > 0) {
cout << "The enemy hits you back." << endl;
cout << "You now have " << hp << "HP left." << endl;
}
} else if (hitEnemy == 2) {
enemyHp = showEnemyHp(enemyhp, attack);
hp = showHp(hp, enemyattack);
cout << "You have chosen not to hit the enemy." << endl;
cout << "The enemy hits you back." << endl;
cout << "You now have " << hp << "HP left." << endl;
} else {
cout << "You can't do that!" << endl;
return enemyBattle();
}
}
while (hp > 0 && enemyhp > 0);
}

谁能帮助我做错了什么,为什么会这样?此外,解决此类问题的任何提示都会很棒。我也希望得到有关如何更好地优化此代码的任何帮助。(就像我说的,我的C++工具箱现在非常有限。我只有大约一个月左右的时间学习它......我的教授甚至还没有教过我们数组,这是我自己学的(。非常感谢您的任何帮助。

你的整体逻辑似乎很好,除了一些你可能没有考虑的细节。就像库莫宁@Sami在他的评论中提到的,showEnemyHp()函数实际上是更新敌人的生命值而不是显示它。

最好将函数重命名为更合适的名称int showEnemyHp(int, int)int showHp(int, int),例如int updateEnemyHp(int, int)int updateUserHp(int, int),如果您打算仅将它们用于更新 HP 值而不用于其他任何目的。

另外,把

cout << "The enemy now has " << enemyhp << "HP left." << endl;

在检查敌人HP是否为负值之前,可能会导致打印可能不需要的HP负值(假设HP的负值不打算显示(。最好在处理极端条件后打印HP值。

cout << "You hit the enemy." << endl;
if (enemyhp <= 0) {
cout << "You've killed the enemy!" << endl;
break;
} else if (enemyhp > 0) {
cout << "The enemy now has " << enemyhp << "HP left." << endl;
cout << "The enemy hits you back." << endl;
cout << "You now have " << hp << "HP left." << endl;
}

还应该注意的是,当用户决定不攻击时,敌人的更新HP不会显示,因此在此回合中对敌人造成的任何伤害都不会显示。因此,只需为敌方HP添加打印即可解决此问题

} else if (hitEnemy == 2) {
enemyHp = showEnemyHp(enemyhp, attack);
// Dammage done when user decides not to attack, probably should be commented?
hp = showHp(hp, enemyattack);
cout << "You have chosen not to hit the enemy." << endl;
cout << "The enemy hits you back." << endl;
cout << "You now have " << hp << "HP left." << endl;
// Handle negative values of enemy HP and print final value
if (enemyhp <= 0) {
cout << "You've killed the enemy!" << endl;
break;
} else if (enemyhp > 0) {
cout << "The enemy now has " << enemyhp << "HP left." << endl;
cout << "The enemy hits you back." << endl;
cout << "You now have " << hp << "HP left." << endl;
}
}

在处理无效输入的最后一个条件分支中,在返回期间调用enemyBattle()可能会导致无限递归,无论在什么情况下,这都是非常非常糟糕的。最好将return enemyBattle();替换为continue;,这将简单地继续循环执行

} else {
cout << "You can't do that!" << endl;
// Continue program
continue;
}

如果您希望程序在用户输入无效输入时退出,则可以return enemyBattle();替换为return;,因此您的代码将如下所示:

} else {
cout << "You can't do that!" << endl;
// Exit program
return;
}

同样重要的是要记住,该程序不会检查用户的HP,以防用户的HP变为零或负数,则程序可能需要通过通知用户来处理它。

最后,我认为在程序结束时不需要while (hp > 0 && enemyhp > 0);,这似乎是多余的,因为该语句仅在控件离开while(true){...}循环时才执行,这只会在enemyhp <= 0时发生。