在C ++中避免使用大量if语句

时间:2017-01-03 21:34:00

标签: c++ c++11 if-statement

我目前正在为编程作业实施游戏 Monopoly 的基于文本的版本。

我已经阅读了很多次,深度嵌套代码对代码的可读性和整个程序的可变性都是有害的。但是在玩大富翁时,需要做很多检查!

例如,这个(不完整的)功能显示标题契约(带有价格,租金和东西的卡片)并允许玩家在房产上建造房屋。

void propertiesSeeBuild(){

    //Check whether the player owns any properties by populating a vector
    vector<Property*> propertiesOwned;
    for(unsigned int i = 0; i < sizeof(board)/sizeof(board[0]); i++){
        if(board[i]->getType()=='p'){
            Property* p = (Property*)board[i];
            if(p->getOwner() == players[currentPlayer])
                propertiesOwned.push_back(p);
        }
    }

    //if he (or she) does
    if(!propertiesOwned.empty()){
        cout << "You own the following properties: " << endl;
        for(unsigned int i = 1; i <= propertiesOwned.size(); i++)
            cout << i << ". " << propertiesOwned[i-1] << endl;
        cout << endl;
        cout << "Choose a building to view/upgrade (1-" << propertiesOwned.size()-1 << "): ";
        int choice = getIntInput(propertiesOwned.size());

        //show the title deeds of the property
        propertiesOwned[choice-1]->titleDeeds();

        int currentColour = propertiesOwned[choice-1]->getColour();

        cout << endl;                   
        cout << "Remember that you can only build houses if you own all the properties in a colour group." << endl;

        //and if he/she owns all the properties of that colour group
        if(players[currentPlayer].getPropertiesOfColour(currentColour) == propertiesOfColour[currentColour]){
            cout << "You currently own all the properties of that colour group, and " 
                 << propertiesOwned[choice-1]-> getName() << " currently has " << propertiesOwned[choice-1]-> getHouses() << " houses." << endl << endl;

            cout << "Do you want to build on this property?" << endl;

            //if he/she does
            if(takeYesNo() == 'Y'){

                //if the number of houses is < 4
                if(propertiesOwned[choice-1]-> getHouses() < 4){
                    cout << "Enter the amount of houses you want to build: ";
                    int noOfHouses = getIntInput(4 - propertiesOwned[choice-1]-> getHouses());  //this guarantees that there is always less than 4 houses

                    //if he/she has enough money
                    if(players[currentPlayer].getBalance() >= noOfHouses * propertiesOwned[choice-1]->getNewHousePrice()){

                    }
                }
            }                   
        }
    }
}

代码是6个深度的点。这是不好的形式?有什么办法可以让代码更具可读性吗?

1 个答案:

答案 0 :(得分:0)

有很多方法可以限制if语句。这是学习过程的一部分,所以在这一点上不要过多。因此,我会从一些简单的事情开始,比如将一些逻辑移到函数中。

例如: 将“if(!propertiesOwned.empty()){”下的所有逻辑移动到某种方法,比如“buildDwelling”或类似的东西(正如Rob K和其他人指出的那样)。然后你可以跟随同样的想法其余的if语句。然后在其中,考虑使用“构建”方法创建其他类,例如“Dwelling”类。

要深入了解如何使用面向对象的编程来限制if语句并整体改进代码,请查看下面的讨论,了解一些想法。当然,这些是更高级的主题,所以开始简单并继续前进。借用SuSE发行版的一句话,“玩得开心!”

http://misko.hevery.com/2008/12/08/clean-code-talks-inheritance-polymorphism-testing/