2017-06-01 35 views
0

對不起,如果問題不明確,英語不是我的主要語言,所以我不知道如何撰寫它。我的老師做了一個練習來計算誰會在決鬥中贏得他們的健康和攻擊。我想擴大一點,並增加護甲,關鍵機率和關鍵傷害作爲額外的統計數據,現在我試圖弄清楚如何在傷害中應用一個關鍵的機會。這是我到目前爲止的地方。如何計算應用獎勵的機率(即暴擊機率)?

#include <stdio.h> 
#include <stdlib.h> 

typedef struct { 
    int HP; 
    int damage; 
    int armor; 
    float crit_damage; 
    float crit_chance; 
} atributes; 

//how to implement crit_chance? 
//using a function? 
//divide 100 for the crit_chance 
//Ex: crit_chance = 25 ; 100/25 = 4 
//generate a random number between 1 and 4 
//if the number is 4 the crit_chance is sucessfull 
//if not then the crit_chance fails 
//aply then the result of the funcion to character[i].crit_damage 

int main() { 
    int i, rounds = 0; 
    atributes character[2]; 

    for(i = 0; i < 2; i++) { 
     printf("\tCharacter %d atributes\n", i+1); 
     printf(" Damage = "); 
     scanf("%d", &character[i].HP); 
     printf(" HP = "); 
     scanf("%d", &character[i].damage); 
     printf(" Armor = "); 
     scanf("%d", &character[i].armor); 
     printf(" Crit Damage = "); 
     scanf("%f", &character[i].crit_damage); 
     printf(" Crit Chance = "); 
     scanf("%f", &character[i].crit_chance); 
     printf("\n"); 
    } 

    while ((character[0].HP > 0) || (character[1].HP > 0)) { 
     character[0].HP -= (character[1].damage * (character[1].crit_damage) - character[0].armor * 2); 
     character[1].HP -= (character[0].damage * (character[0].crit_damage) - character[1].armor * 2); 
     rounds++; 
    }; 

    if (character[0].HP > character[1].HP) printf("\tCharacter 1 won after %d rounds!", rounds); 
     else if (character[0].HP < character[1].HP) printf("\tCharacter 2 won after %d rounds!", rounds); 
      else printf("\tThe duel ended in a tie after %d rounds", rounds); 

    return 0; 
} 
+0

你需要更具體。 – Martin

+0

你應該使用暴擊機率百分比來決定何時發生。在例子中:滾動一個0到100之間的隨機數,如果它小於你的暴擊機率造成暴擊傷害。如果沒有造成正常傷害。我很抱歉,我不能爲你編碼,但如果你需要更多的細節,當我回去工作時,我可以提供幫助 – koksalb

+0

我不確定你的問題/問題是什麼,但這可能有所幫助:https:// www .tutorialspoint.com/c_standard_library/c_function_rand.htm - 我認爲你**將不得不使用此功能 – nounoursnoir

回答

1

類似下面的工作:

int is_critical = (rand()%4==3); 

rand()是由stdlib.h其包含地返回0RAND_MAX(在stdlib.h定義的常數)之間的值提供的功能。

rand()%4給你當此值被除以4餘數:這將是數字0,1,2,或3。

詢問rand()%4==3詢問如果結果是等於3(儘管你可以只以及選擇0,1或2)。由於3代表25%的結果,所以這對應於你想要的和is_critical是1,如果有一個命中。

一個警告,雖然,因爲RAND_MAX可能不是均勻地被4整除一些模成績比別人發生的稍微較高的機會,所以你永遠不會想要生成隨機數這樣的事情真的事項(科學,金融,密碼,& c)。

可以按如下概括這個方法:

int is_critical = (rand()/(float)RAND_MAX)>0.75; 

由於兩個rand()RAND_MAX給整數和RAND_MAXrand()較大,將其劃分給0,所以我們把RAND_MAX浮動。結果是在0-1範圍內的「統一」分佈數字。這大於0.75,有25%的機會,這是你想要的。

+0

我如何根據字符改變機會,就像'character [0] .crit_chance'接收到的一樣35和'character [1] .crit_chance'獲得50 – flycher