2017-09-17 45 views
1

약 3 주 동안 프로그래밍 중이며이 civ 게임을 만들고 있습니다. 유일한 문제는 각 라운드 동안이며, 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

[comma operator] (http://en.cppreference.com/w/cpp/language/operator_other)와이 게시물 [여기] (https://stackoverflow.com/q/54142/)에 대해 읽어야합니다. 416574) – pstrjds

답변

0

, 당신은 참조로 함수의 매개 변수를 전달할 수 있습니다. 이렇게하려면 함수 프로토 타입 및 정의에 변수 이름 앞에 & 연산자를 입력하기 만하면됩니다. 예를 들어, 자체 정수를 곱하고 그 정수의 값을 업데이트하는 기능 광장 :

#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 참조). 이것은 클래스를 사용하여 필요한 데이터를 전달하는 방법의 예입니다. 업데이트 기능을이 클래스의 일부로 만들 수 있습니다. 데이터 객체를 참조로 전달하고 직접 업데이트하는 것을 고려해 볼 수도 있지만 전체 디자인에 따라 달라집니다.

참고 참고 :이 방법은 테스트하지 않았으므로 업데이트 방법에서 작업 중 하나를 놓친 것일 수 있지만 잘하면 방향이 올바른 것입니다. 그들이 함수가 끝나는 경우에도 업데이트되도록하는 대신 C++에서 옵션하지 않은 값을 모두 반환 노력의

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에서 오류에 대한 줄 번호를 말하지 않습니다. 편집 : 방금 작동하도록있어,하지만 방정식을 통해 실행되지만 더 작은 숫자를 얻어야하기 때문에 나는 매우 큰 숫자를 가지고 무슨 일이 일어나고 있는지 전혀 모른다. –

관련 문제