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

C++ Variable not updating in While Loop

我已经编程了大约 3 个星期了,我正在制作这款文明游戏。唯一的问题是在每一轮中,你的文明数据每轮都会更新,但在第二轮之后,它们不会更新。基本上我想让程序做的是在每一轮之后将每一种资源相加并计算人口和黄金,但它不会在第一轮之后发生。我从来没有 类 工作过,所以不要指望我第一次就做对了。

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

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;

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

你的对象没有被更新,因为你只是从你的方法中传递了一个整数,并且因为你正在复制所有数据,所以你在更新函数中执行的所有操作都只对副本进行操作而不是操作调用方的原始值。

正如我在评论中提到的,您可能应该重新考虑您的设计并考虑使用包含您要更新的值的 class。此答案无意展示 "best" 设计,但它应该为您指明正确的方向。一般来说,具有超过 3 或 4 个参数的方法签名很难使用,并且会使阅读代码变得更加困难(我强烈推荐阅读 Robert Martin 的书 Clean Code)。这是一个示例,说明如何使用 class 来传递必要的数据。您可能希望将更新功能作为此 class 的一部分。您可能还想考虑只传入数据对象作为参考并直接更新它,但这完全取决于您的整体设计。

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

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

您可以通过引用传递函数的参数,而不是尝试 return 所有值(这在 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