2017-09-17 58 views
1

私は今、約3週間プログラミングしています。私はこのcivゲームを作っています。唯一の問題は、各ラウンド中に、各ラウンドが毎回更新されたが、2ラウンド後に更新されないということです。基本的に私がプログラムにしたいのは、各ラウンド後に各リソースを集計し、人口と金を計算することですが、最初のラウンド後には起こっていません。私は授業を受けたことがないので、最初に正しいことを期待しないでください。彼らは問題がある場合を除き、変数アップトップのための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

あなたは[comma operator](http://en.cppreference.com/w/cpp/language/operator_other)とこのSO post [here](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つ以上の引数を取るメソッドシグネチャを使用することは難しく、コードの読み込みはずっと難しくなります(ロバートマーティンの本「クリーンコード」を読むことを強くお勧めします)。これは、クラスを使用して必要なデータを渡す方法の例です。更新機能をこのクラスの一部にすることができます。また、参照としてデータオブジェクトを渡して直接更新することを検討することもできますが、それはすべて全体の設計に依存します。

ノート私はこれをテストしなかったし、アップデート方法であなたの操作の1つを見逃しているかもしれないが、うまくいけば正しい方向にあなたを指している。

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

ビジュアルスタジオでは、エラーの行番号は表示されません。編集:私はちょうどそれを働かせましたが、非常に大きな数字を得て、何が起こっているか分かりません。なぜなら、方程式を通っているからです。 –

関連する問題