【问题标题】:C++ - Creating Basic Combat Function for Text RPGC++ - 为文本 RPG 创建基本战斗功能
【发布时间】:2018-09-20 16:45:11
【问题描述】:

我目前正在为一个班级制作一个基本的文本角色扮演游戏,但我在战斗系统方面遇到了一些奇怪的问题。基本上,我可以选择在每个战斗阶段攻击或不攻击敌人。当我攻击敌人时,健康会以预期的速度下降。但是,当我选择不攻击时(即敌人仍在攻击),我对敌人造成的伤害并没有在该回合直接说明,但是当我选择下次攻击时,它对敌人造成了预期的伤害敌人以及我应该没有攻击时造成的伤害。

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

我的代码如下:

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) Yes\n(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++ 工具箱现在非常有限。我只有大约一个月左右的时间来研究它……我的教授甚至还没有教我们数组,这是我自己学习的)。非常感谢您的帮助。

【问题讨论】:

  • 在这两种情况下您都需要拨打showEnemyHp(),这样会降低马力。这是一个名称非常糟糕的函数,因为它没有显示任何内容,而且它有你意想不到的副作用。在这里使用调试器会很有帮助,因为您可以准确地看到代码的作用。我建议尽早学习使用其中一种。
  • 感谢您的意见。因此会发生什么样的意外副作用?我一定会听取您对调试器的建议,感谢您的提示。
  • 这次是通缉的副作用,意味着健康状况发生了变化。但是,如果有人在阅读您的代码并看到“显示敌人 hp”,他们会认为它只会显示它而不是更改它。所以它有一个意想不到的副作用,并且没有实际效果,因为它没有显示任何东西。这就是命名函数等很重要的原因。例如,attackEnemy 会传达更清晰的意图。所以这不是在你不知情的情况下发生的任何事情,只是一个清晰度问题。
  • 这很有意义。再次感谢!
  • 嗨 Cody,您如何在 Visual Studio 等 IDE 中编写 C++?如果您能够在代码中设置断点并在执行过程中逐行逐行执行,那么这将有助于您理解代码中的逻辑和行为问题。否则,我倾向于将代码提取到一个简单的测试函数中,这样我就可以使用函数的输入和输出,而不必再次运行整个代码。但继续前进,科迪,你的动力给我留下了深刻的印象。

标签: c++ function


【解决方案1】:

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

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

还有,把

cout &lt;&lt; "The enemy now has " &lt;&lt; enemyhp &lt;&lt; "HP left." &lt;&lt; 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的打印就可以解决这个问题

} 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 &gt; 0 &amp;&amp; enemyhp &gt; 0);,这似乎是多余的,因为该语句仅在控件离开while(true){...} 循环时才会执行,而这仅在enemyhp &lt;= 0 时发生.

【讨论】:

  • 我必须感谢您对所有内容的深入解释。这为我清除了很多东西。谢谢:)
猜你喜欢
  • 2013-07-07
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
相关资源
最近更新 更多