2017-09-17 243 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中,我沒有爲錯誤指出行號。編輯:我剛剛得到它的工作,但我得到非常大的數字,並不知道發生了什麼,因爲它貫穿方程式,但應該得到方式更小的數字。 –