gpt4 book ai didi

C++ "Death Battle"游戏无法正确记录武器使用次数

转载 作者:行者123 更新时间:2023-11-30 05:29:11 25 4
gpt4 key购买 nike

所以我是 C++ 的新手,我目前正在为一项任务开发一个“死亡之战”游戏,但我被卡住了。

基本上,作为玩家,您可以使用每种武器一定次数:Canon 3 次,Grenade 4 次。步枪是无限的。

当我使用佳能 3 次时效果很好,然后在我的佳能用完后选择手榴弹时效果很好。如果我再次尝试使用手榴弹,它会给我一条消息,说明我已经用完了。我第二次使用步枪时也会发生同样的情况。

我似乎无法找到这是为什么,或者如何更正它。如果有人能为此指出正确的方向,我将不胜感激!

这是我的代码:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;


int main()
{
int PlayerHealth = 100,
CompHealth = 100,
Turn = 0,
WeaponChoice,
CompWeapon,
Cannon_Damage = 0,
Grenade_Damage = 0,
Rifle_Damage = 0;
int PlayerCanonUse = 3;
int PlayerGrenadeUse = 4;
int CompCanonUse = 3;
int CompGrenadeUse = 4;


srand(static_cast<int>(time(0)));

char yn;
do
{

do
{

if(Turn == 0)// Player Turn
{

cout << "\nWhich weapon will you use? Choose 1, 2, or 3.\n";
cout << "1. Cannon\n";
cout << "2. Grenade\n";
cout << "3. Rifle\n\n";
cin >> WeaponChoice;

//Validate weapon choice

if ((WeaponChoice < 1 || WeaponChoice > 3)){
cout << "\nSilly you. That's not one of the options. Try again.\n";
cin >> WeaponChoice;
}

if (PlayerCanonUse <= 0){
cout << "Sorry, you have used all of your canons. Choose a different weapon.\n";
cin >> WeaponChoice;
}

if (PlayerGrenadeUse <= 0){
cout << "Sorry, you have used all of your grenades. Choose a different weapon.\n";
cin >> WeaponChoice;
}

switch(WeaponChoice)
{

case 1: // player chooses to attack with cannon
Cannon_Damage = (10 + rand() % 6);
cout << "\nYou chose a cannon." << endl;
CompHealth = CompHealth - Cannon_Damage;
cout << setw(10) << Cannon_Damage << " damage!" << endl;
cout << setw(10) << "YOUR HEALTH: " << PlayerHealth << endl;
cout << setw(10) << "OPPONENT HEALTH: " << CompHealth << endl;
PlayerCanonUse = PlayerCanonUse - 1;
break;


case 2:
Grenade_Damage = (7 + rand() % 13);
cout << "\nYou chose a grenade." << endl;
CompHealth = CompHealth - Grenade_Damage;
cout << Grenade_Damage << " damage!" << endl;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;
PlayerGrenadeUse = PlayerGrenadeUse - 1;

break;

case 3:
Rifle_Damage = (3 + rand() % 9);
cout << "\nYou chose a Rifle." << endl;
CompHealth = CompHealth - Rifle_Damage;
cout << Rifle_Damage << " damage!" << endl;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;

break;
}



}



Turn == 1; // Computer Turn

CompWeapon = rand() % 3;


switch(CompWeapon)
{

case 1:

Cannon_Damage = (10 + rand() % 6);
cout<<"\nYour opponent used a cannon, and you lost " << Cannon_Damage << " HP." << endl;
PlayerHealth = PlayerHealth - Cannon_Damage;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;
CompCanonUse = PlayerCanonUse - 1;
break;

case 2:
Grenade_Damage = (7 + rand() % 6);
cout<<"\nYour opponent used a grenade, and you lost " << Grenade_Damage << " HP." << endl;
PlayerHealth = PlayerHealth - Grenade_Damage;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;
CompGrenadeUse = CompGrenadeUse - 1;
break;

case 3:
Rifle_Damage = (3 + rand() % 10);
cout<<"\nYour opponent used a rifle, and you lost " << Rifle_Damage << " HP." << endl;
PlayerHealth = PlayerHealth - Rifle_Damage;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;
break;


}

}
while(PlayerHealth >= 0 && CompHealth >= 0); //loops while both players are alive
if (CompHealth < 0)
cout << "Congratulations! You won!" << endl;

if (PlayerHealth < 0)
cout << "Darn, you lost. Game over." << endl;;

std::cout << "That was fun. Would you like to play again? Enter Y or N:" << std::flush;
} while(std::cin >> yn && (yn == 'Y' || yn == 'y'));
}

此外,如果我没有遵循 C++ 约定,请原谅,我对它还是个新手!对此的任何指示也很好。

最佳答案

问题就出在这里

                if (PlayerCanonUse <= 0){
cout << "Sorry, you have used all of your canons. Choose a different weapon.\n";
cin >> WeaponChoice;
}

if (PlayerGrenadeUse <= 0){
cout << "Sorry, you have used all of your grenades. Choose a different weapon.\n";
cin >> WeaponChoice;
}

即使您没有选择武器,也会检查这两个 if 语句。这可以用类似的东西修复

           if (WeaponChoice == 1 && PlayerCanonUse <= 0){
cout << "Sorry, you have used all of your canons. Choose a different weapon.\n";
cin >> WeaponChoice;
}

if (WeaponChoice == 2 && PlayerGrenadeUse <= 0){
cout << "Sorry, you have used all of your grenades. Choose a different weapon.\n";
cin >> WeaponChoice;
}

在 switch 内部执行此检查会更简单。

关于C++ "Death Battle"游戏无法正确记录武器使用次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36512194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com