C++ - 为文本RPG创建基本战斗功能
C++ - Creating Basic Combat Function for Text RPG
我目前正在为一个职业开发一个基本的文本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
时发生。
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- C++ - 为文本RPG创建基本战斗功能
- 我在 C++ 中创建了一个函数来递归反转字符串,但是之后如何使功能打印一个 endl?
- 如果在创建对象时创建了 VPTR,那么为什么具有虚拟功能的类的大小在 32 位系统上为 4,在 64 位机器上为 8
- 创建类成员功能
- 插入功能保持重新创建根节点
- 如何在 python 交易模型中创建功能时间条件
- 如何为带有计时功能的程序创建计时器?
- Protobuf的反序列化功能之一可以使用Cereal重新创建吗?
- 如何使用Sfinae为容器创建模板功能,并根据操作员推断返回类型
- 类自动创建基于成员名称的功能
- 在功能中创建一个数组,然后将其返回到main()
- 从没有预处理器的HANA元组中创建功能签名
- OpenCV Houghline功能创建无限线
- C++的新增功能:创建 2D 数组并具有正确的赋值运算符:分割错误
- 从成员类功能创建线程
- 用于存储由第一个功能创建的指针的设计模式将在以后使用
- 使用netUserAdd功能创建Windows用户配置文件
- 使用创建文件功能创建文件会导致写保护文件
- c++;使用多种功能创建年度日历并打印到输出文件