2017-09-17 242 views
1

我一直在编程约3星期,我在做这个游戏CIV。唯一的问题是在每轮比赛中,每轮比赛的统计数据都会更新,但第二轮之后他们不会更新。基本上我希望程序要做的是在每轮之后加入每个资源并计算人口和黄金,但这并不是在第一轮之后发生的。我从来没有上过课,所以不要指望我第一次就做对。C++变量没有更新在While循环

这里是应该发生在函数内部每一轮的更新代码:

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

你期望回报呢? – tkausl

+1

你的函数只返回一个'int'所以对这个函数的调用方面,你只得到一个值。从设计角度来看,您应该创建一个包含所有这些数据的类,以便您可以简单地返回该类或向该类添加更新方法或类似的东西。 – pstrjds

+1

你或许应该阅读有关[逗号操作符(http://en.cppreference.com/w/cpp/language/operator_other),这SO发布[这里](https://stackoverflow.com/q/54142/ 416574) – pstrjds

回答

0

,而不是试图返回所有的价值观,这是不是在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 
3

你的对象不被更新,因为你只是从你的方法传递出一个整数,因为要复制所有的数据,所有正在执行的更新功能的操作都只能在副本操作和而不是来自操作主叫方的原始值。

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

注意我没有测试这个,并且可能错过了更新方法中的一个操作,但希望这可以指出您的方向是正确的。

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

到目前为止的所有作品,除了我不断收到“解析外部符号”的错误,而且不知道如何解决它。 –

+0

@GenghisKhan - 你在哪里得到这个错误,是否与这个代码或其他地方? – pstrjds

+0

在visual studio中,我没有为错误指出行号。编辑:我刚刚得到它的工作,但我得到非常大的数字,并不知道发生了什么,因为它贯穿方程式,但应该得到方式更小的数字。 –