2012-03-24 274 views
0

我想寫一個簡單的蒙特卡羅模擬程序。確切地說,我想分析戰鬥結果,這取決於進攻和防守時不同的部隊規模 - 有些是這樣的:http://en.wikipedia.org/wiki/Risk_(game)#Dice_probabilities訪問衝突?

現在...風險II同一時間規則提供了不同類型的挑戰:變化軍隊規模意味着不同的骰子顏色(這意味着數字的不同分佈函數)總之,你的軍隊規模越小,你最終會以1秒結束,而你的軍隊規模越大,您越有可能以較高的卷數結束。

由於使用if語句的所有可能條件是最好的巨大的愚蠢,我列出了所有可能的滾動結果在5x12陣列。 (所有骰子12面,5個不同強度,所以你得到5×12)

我想爲每個進攻/防守組合進行10000次模擬,但是一旦我意識到這將意味着超過900萬的計算,我決定每100個組合減少100個。

以下是代碼;一旦我運行它,它會給我訪問衝突錯誤。我不知道我在哪裏犯了一個錯誤。如果有任何建議可以提供,我也會很感激。提前致謝。

/* Risk II Combat Result Table 

For starter, we shall consider one-direction attack in RISK II 
and generate the combat table for use. 

Machine: Pentium Dual Core E6600 
Ram: 6G 
OS: Windows 7 
Compiler: Visual Studio 2010 

Jimmy Smith, 24-March-2012 

*/ 

#include <cstdio> 
#include <cstdlib> 
#include <cmath> 

#include <iostream> 
#include <algorithm> 
using namespace std; 

/* Initializing: 

    Range legend: 

White = 1 ~ 6 
Yellow = 7 ~ 12 
Orange = 13 ~ 20 
Red = 21 ~ 30 
Black = 31 ~ 

First row of Dice array corresponds to white dice, and so on. 

*/ 

int Dice[5][12] = { {1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6}, 
        {1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6}, 
        {1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6}, 
        {1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6}, 
        {1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6} }; 

int Roll_Index [30]= {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4}; 


int main() { 

float Table[30][30]; 

for (int i = 0; i < 30; i ++) 
    for (int j = 0; j < 30; j ++) 
     Table [i][j] = 0.0; 

int Result[100]; 

for (int i = 0; i < 100; i++) Result[i] = 0;  

float prob = 0.0; 

int Atk = 0; 
int Def = 0; //Number of attackers and defenders 


int A_Ind = 0; 
int D_Ind = 0; //Dice side index 

int A_Roll_Index = 0; 
int D_Roll_Index = 0; //Roll index on both sides 

int A_Dice = 0; 
int D_Dice = 0; //Roll result 

int Damage = 0; 

int Sum = 0; //Internal sum 

FILE* fp; 


//Time for hard core Monte-Carlo shit! 100 simulation for each situation 

for (Atk = 0; Atk<30; Atk++) { 

    for (Def = 0; Def < 30; Def++) { 

     for (int i = 0; i < 100; i++) { 

      int Attacker = Atk +1; 
      int Defender = Def +1; 

      while((Attacker>0)&&(Defender>0)) { 

       A_Ind = (int)(rand()*12); 
       D_Ind = (int)(rand()*12); //The die is cast! 

       A_Roll_Index = Roll_Index[Attacker-1]; 
       D_Roll_Index = Roll_Index[Defender-1]; 

       A_Dice = Dice[A_Roll_Index][A_Ind]; 
       D_Dice = Dice[D_Roll_Index][D_Ind]; 

       Damage = min(A_Roll_Index, D_Roll_Index) + 1; 

       if (A_Dice >= D_Dice) { 
        Defender -= Damage; 
        if (Defender == 0) Result[i] = 1; 
       } 
       else { 
        Attacker -= Damage; 
        if (Attacker == 0) Result[i] = 0; 
       } 
      } 
     } 

    for (int i = 0; i < 100; i++) Sum+=Result[i]; 

    prob = (float)(Sum/100); 

    Table[Atk][Def] = prob; 

    } 
} 
/* open new file for output and write a title */ 

fp = fopen("Combat.dat", "w+"); 

if(NULL == fp) { 
    printf("cannot open file\n"); 
    return(0); 
} 

for (Atk = 0; Atk < 30; Atk++){ 
    for (Def = 0; Def < 30; Def++) 
     fprintf(fp, "%16.8f", Table[Atk][Def]); 
    fprintf (fp, "\n"); 
} 
fclose(fp); 

return(EXIT_SUCCESS); 

}

+4

你應該在調試器中運行它。這將確定哪一行代碼導致錯誤。 – 2012-03-24 11:43:07

+0

int結果[100]; for(int i = 0; i <10000; i ++)Result [i] = 0; ? – inf 2012-03-24 11:44:00

+1

現在這裏是一些真正的核心蒙特卡洛狗屎。 @Bamboon,現在你已經破壞了所有的調試樂趣。 :( – bzlm 2012-03-24 11:44:21

回答

0

您的代碼

A_Ind = (int)(rand()*12); 
D_Ind = (int)(rand()*12); 

表明您似乎認爲rand()在區間[0,1),這是不是這種情況返回一個數字。相反,INT返回整數範圍[0, RAND_MAX],所以你需要的東西,如:

A_Ind = rand() * 12.0/RAND_MAX; 

如果你的模擬必須是精確的統計,你可能從<random>庫中隨機數生成器更好:

#include <random> 

typedef std::mt19937 rng_type; 
std::uniform_int_distribution<std::size_t> udist(0, 11); 

rng_type rng; 

// ... 

// seed rng first: 
rng.seed(some_seed_value); 

// roll dice 
std_size_t A_Ind = udist(rng), D_Ind = udist(rng); 
+0

真是一個驚喜。 A_Ind畢竟是罪魁禍首。謝謝您的幫助。 *我沒想到真的* – 2012-03-24 12:18:10