有没有一种方法可以减少IF语句的数量?

时间:2020-04-25 20:47:48

标签: c++

我的代码充满了If语句,如果不消除它们,我想减少数量。我怎么做?任何其他提示和改进,非常感谢! 我刚刚开始学习编码,并遵循一门教程来编写此代码,但是正在努力了解如何最小化IF的数量。

void Combat() {
    //combat simiulator
    CombatHUD();
    int userAttack;
    int userDamage = 1000; //8 * level / 2;
    int monsterAttack = 6 * monsterlevel / 2;

    if (character.totalHealth >= 1 && monsterHealth >= 1) {
        std::cout << "\n";
        std::cout << "1. Attack\n";
        std::cout << "2. Block\n";
        std::cout << "3. Run\n";
        std::cout << "\n";
        std::cin >> userAttack;

        if (userAttack == 1) {
            //User Attack
            std::cout << "Attacking... you did " << userDamage << " to the " << currentMonster << std::endl;
            monsterHealth = monsterHealth - userDamage;
            Sleep(1000);
            CombatHUD();
            if (monsterHealth >= 1) {
                std::cout << "Monster is attacking... \n";
                character.totalHealth = character.totalHealth - monsterAttack;
                std::cout << "You recieved " << monsterAttack << " damage " << std::endl;

                if (character.totalHealth <= 0) {
                    character.totalHealth = 0;
                    system("cls");
                    std::cout << "You died! Game over!";
                    Sleep(2000);
                    exit(0);
                }
            }
            else if (monsterHealth <= 0) {
                monsterHealth = 0;
                if (character.level != character.maxlevel) {
                    character.current_xp += monsterXp;
                    LevelUp();
                }
                std::cout << "\n";
                std::cout << "You defeated " << currentMonster << " you get " << monsterXp << "XP.\n";
                Sleep(2000);
                HUD();
            }
            Sleep(1000);
            Combat();
        }
        else if (userAttack == 2) {
            //User Block. broken?
            std::cout << "Blocking\n";
            int i = rand() % 100 + 1;
            if (i >= 50) {
                std::cout << "You blocked the incoming attack\n";
                character.heal = character.level * 10 / 2;
                std::cout << "you have been healed for " << character.heal << std::endl;
                character.totalHealth += character.heal;
                Sleep(1000);
                Combat();
            }
        }

        else if (userAttack == 3) {
            //User escape
            std::cout << "You try to run\n";
            int x = rand() % 100 + 1;
            if (x >= 50) {
                std::cout << "You run away\n";
                HUD();
            }
            else {
                std::cout << "You failed to run away \n";
                std::cout << "Monster does a critical hit! \n";
                character.totalHealth -= monsterAttack + 10;
                std::cout << "You suffered " << monsterAttack + 10 << "Your current health is " << character.totalHealth << std::endl;
                Sleep(2000);
                Combat();

            }

        }
        else {
            std::cout << "Invalid Input\n";
            Sleep(500);
            Movement();
        }
    }
}

void Movement() {
    //user movement. enhance?
    int choice;
    std::cout << "\n\n";
    std::cout << "1. Move forward\n";
    std::cout << "2. Chill\n";
    std::cout << "3. Move Backwards\n";
    std::cout << "\n";

    std::cin >> choice;

    if (choice == 1) {
        int temp = rand() % 100 + 1;
        std::cout << "You begin moving forward...\n";
        if (temp >= 50) {
            Monster();
            std::string tempName = monsterName[rand() % currentMonsterNames];
            std::cout << "A " << tempName << "! Get ready to fight it!\n";
            currentMonster = tempName;
            Sleep(1000);
            Combat();
        }
        std::cout << "You find nothing\n";
        Sleep(1000);
        HUD();

    }
    else if (choice == 2) {
        std::cout << "You want to chill for the rest of the day\n";
        if (character.totalHealth <= 99) {
            character.totalHealth += 10 * character.level;
        }
        std::cout << "You healed by chilling Health is now " << character.totalHealth << std::endl;
        Sleep(1000);
        HUD();
    }
    else if (choice == 3) {

        std::cout << "You begin moving backwards...\n";
        std::cout << "You're going no where\n";
        Sleep(2000);
        system("cls");

    }
    else {
        std::cout << "Invalid Input\n";
        Sleep(500);
        Movement();
    }






}

void Monster() {
    //monster creator
    monsterHealth = 30; {
    monsterlevel = (rand() % 3) + character.level;
}

    monsterHealth = (rand() % 30) * monsterlevel;

    monsterXp = monsterHealth;

    if (monsterHealth == 0)
        Monster();
    if (monsterlevel == 0)
        Monster();


}

void LevelUp() {
    //level up mechanic
    if (character.current_xp >= character.xp_to_level) {
        character.xp_to_level += floor(character.level + 15 * pow(2, character.level / 7));
        character.totalHealth = floor(character.totalHealth + 10 * pow(2, character.level / 8));

        if (character.level >= character.minLevel && character.level <= character.maxlevel) {
            character.level++;
        }
        else {
            character.level = 5;
        }

        character.maxHealth = character.totalHealth;
        std::cout << "Ba Da Bing! You've leveled up! You're max health has increased!" << std::endl;
        Sleep(2000);
        LevelUp();
    }

    Sleep(2000);
    HUD();

}

2 个答案:

答案 0 :(得分:1)

这是您的“运动”功能,表示为一个开关:

void Movement() {
    //user movement. enhance?
    int choice;
    std::cout << "\n\n";
    std::cout << "1. Move forward\n";
    std::cout << "2. Chill\n";
    std::cout << "3. Move Backwards\n";
    std::cout << "\n";

    std::cin >> choice;

    switch (choice)
    {
        case 1:
            int temp = rand() % 100 + 1;
            std::cout << "You begin moving forward...\n";
            if (temp >= 50) {
                Monster();
                std::string tempName = monsterName[rand() % currentMonsterNames];
                std::cout << "A " << tempName << "! Get ready to fight it!\n";
                currentMonster = tempName;
                Sleep(1000);
                Combat();
            }
            std::cout << "You find nothing\n";
            Sleep(1000);
            HUD();
            break;
        case 2:
            std::cout << "You want to chill for the rest of the day\n";
            if (character.totalHealth <= 99) {
                character.totalHealth += 10 * character.level;
            }
            std::cout << "You healed by chilling Health is now " << character.totalHealth << std::endl;
            Sleep(1000);
            HUD();
            break;
        case 3:
            std::cout << "You begin moving backwards...\n";
            std::cout << "You're going no where\n";
            Sleep(2000);
            system("cls");
            break;
        default:
            std::cout << "Invalid Input\n";
            Sleep(500);
            Movement();
    }
}

学习switch语句并具有更好的功能分离通常可以减少ifs的数量。但是,如果很可能,如果无法避免,那么自己。

答案 1 :(得分:0)

这是软件设计中一个极为广泛的,开放性的问题,而不仅仅是C ++。

在大多数情况下,由于具有条件逻辑,因此您将无法删除if,这很好。但是,您可以遵循一些设计惯例,以使每个函数的if语句更少:

  • 将逻辑分解为较小的功能,以隔离它们的关注点。例如,每个不同的Combat动作可以是单独的功能。这不是 reduce if,而是将每个函数的嵌套量限制在逻辑上需要的地方。

  • 查找重复的代码或模式,并将其提取为函数。同样,这有助于将if限制在需要的地方

  • 使用switch案例代替if / else阶梯

  • 使用较小的函数,可以更改if语句以检查是否有提早终止的情况,并提早返回以避免嵌套。这样可以使嵌套更短,因为提前返回后的主分支隐式为else。例如:

void check_user_death()
{
  // Instead of 'if (character.totalHealth <= 0)'
  if (character.totalHealth > 0) {
    return;
  }
  system("cls");
  std::cout << "You died! Game over!";
  Sleep(2000);
  exit(0);
}

以上所有建议仅是总体软件设计主题,并非严格针对C ++讲


作为不相关的说明,您可能想研究使用循环(例如forwhile),因为您的Combat函数在某些情况下似乎会递归调用自己-可能会导致堆栈溢出。