C 变量未更新循环

C++ Variable not updating in While Loop

本文关键字:循环 更新 变量      更新时间:2023-10-16

我已经编程了大约3个星期,我正在制作CIV游戏。唯一的问题是在每回合中,您的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;

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

您的对象没有被更新仅在副本上运行,而不是操作调用侧的原始值。

正如我在评论中提到的那样,您可能应该重新考虑您的设计,并考虑使用包含要更新值的类。这个答案并非旨在显示"最佳"设计,而应该指向您正确的方向。通常,拥有需要超过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;
}

,而不是试图返回所有值,这不是C 中的选项,而是可以通过参考传递函数的参数,以便即使函数结束时也会更新它们。为此,您只需将&运算符放在函数原型和定义中的变量名称之前。例如,功能平方本身将整数乘以乘以整数并更新整数的值:

#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