C ++“死亡之战”游戏无法正确追踪武器的使用次数

时间:2016-04-09 02:54:44

标签: c++

所以我是C ++的新手,我目前正在为一项作业进行“死亡之战”游戏,而且我被卡住了。

基本上,你作为玩家将获得使用每种武器一定次数:佳能3次,手榴弹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 ++约定,请原谅我,我还是新手!任何关于它的指针都会很好。

1 个答案:

答案 0 :(得分:1)

问题出在这里。

                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;
            }

在交换机内进行检查会更简单。