C ++变量不在While循环中更新

时间:2017-09-17 05:04:08

标签: c++ variables updating

我已经编程了大约3个星期,而且我正在制作这个文明游戏。唯一的问题是在每一轮,你的civ统计数据每轮更新,但在第二轮之后,他们不会更新。基本上我希望程序要做的是在每轮之后加上每个资源并计算人口和黄金,但是在第一轮之后它没有发生。我从来没有上过课,所以不要指望我第一次就把它弄好。

以下是函数内每一轮应该发生的更新代码:

int RoundTotal(int yg, int yk, int yf, int ys, int yr, int yfi,
    int co, int rtp, int gtp, int ap, double tr, int yp, int dp,
    int int yd, double fp) {

    int YourGold = yg, YourStrength = ys, YourKnow = yk, YourFood = yf, 
    YourResource = yr, YourFields = yfi, YourPopulation = yp, YourDefense = yd;

    int ResourceTradeProfit = rtp, GoldTradeProfit = gtp, DroughtProduction = dp;

    int totals, count = co, ArcherPay = ap;
    double taxrate = tr,  FoodProduction = fp;

    if (YourStrength<0) {
        YourStrength = 0;
    }
    FoodProduction = (0.5*YourFields + 0.5*YourKnow - 0.02*YourPopulation)*DroughtProduction;

    YourFood = YourFood + FoodProduction;

    YourGold = YourGold + (taxrate/100)*YourPopulation; 

    YourGold -= (YourStrength / 2);
    YourGold -= YourKnow;
    YourGold -= YourFood;
    YourGold -= ArcherPay;
    YourResource += ResourceTradeProfit;
    YourGold += GoldTradeProfit;

    YourPopulation = YourPopulation + YourFood*FoodProduction;

return totals, YourGold, YourKnow, YourFood, YourStrength,
        YourResource, YourFields, count, ResourceTradeProfit,
        GoldTradeProfit, ArcherPay, taxrate, YourPopulation,
        DroughtProduction, FoodProduction;

忽略变量的所有缩写,除非它们是问题。

2 个答案:

答案 0 :(得分:3)

您的对象没有更新,因为您只从方法中传出一个整数,并且由于您要复制所有数据,因此您在更新功能中执行的所有操作仅在副本上运行而不是来自操作主叫方的原始值。

正如我在评论中提到的,您应该重新考虑您的设计,并考虑使用包含您要更新的值的类。这个答案并不是为了展示最好的&#34;设计,但它应该指向正确的方向。一般来说,拥有一个超过3或4个参数的方法签名很难使用,这使得阅读代码变得更加困难(我强烈推荐阅读Robert Martin的书“清洁代码”)。这是一个如何使用类传递必要数据的示例。您可能希望将更新功能作为此类的一部分。您可能还想考虑将数据对象作为参考传递并直接更新,但这完全取决于您的整体设计。

注意我没有对此进行测试,可能在更新方法中错过了您的某项操作,但希望这可以指明您正确的方向。

class YourData
{
    public:
        int Gold;
        int Strength;
        int Know;
        int Food;
        int Resource;
        int Fields;
        int Population;
        int Defense;

        int ResourceTradeProfit;
        int GoldTradeProfit;
        int DroughtProtection;
        double FoodProduction;

        // Normally you would split out the function definitions between
        // a header file and a .cpp file, but for the example I am just
        // putting the code here.
        YourData() {} // Default constructor
        YourData(const YourData& data) // Copy constructor
        {
            Gold = data.Gold;
            Strength = data.Strength;
            // Left out other data members for brevity
        }

        void updateFoodProduction()
        {
            FoodProduction = (0.5 * Fields + 0.5 * Know - 0.02 * Population) * DroughtProduction;
        }
}

YourData roundTotals(const YourData& data, double taxRate, int archerPay)
{
    YourData updated(data);
    if (updated.Strength < 0) updated.Strength=0;

    updated.updateFoodProduction();
    updated.Food += updated.FoodProduction;
    updated.Gold += (taxrate/100) * updated.Population; 
    updated.Gold -= (updated.Strength / 2);
    updated.Gold -= updated.Know;
    updated.Gold -= updated.Food;
    updated.Gold -= archerPay;
    updated.Resource += updated.ResourceTradeProfit;
    updated.Gold += GoldTradeProfit;

    updated.Population += updated.Food * FoodProduction;

    return updated;
}

答案 1 :(得分:0)

您可以通过引用传递函数的参数,而不是尝试返回所有值(这不是C ++中的选项),以便即使函数结束也会更新它们。为此,您只需将&运算符放在函数原型和定义中的变量名称之前。例如,函数square,它将整数与自身相乘并更新该整数的值:

#include <iostream>
using namespace std;

void square(int &n);

int main()
{
    int i = 4;
    cout << "Before: " << i << endl;
    square(i);
    cout << "After: " << i << endl;

    return 0;
}

void square(int &n) 
{
    n = n * n;
}

输出:

Before: 4
After: 16