2016-03-03 81 views
0

阿龍打 Bob有打 查理打戰鬥到死,三人C++

亞倫拍攝,再鮑勃,然後查理,我們每個人的100%的機率爲50%的機率有30%的機率首先嚐試拍攝最佳效果的人。有人能解釋爲什麼Aaron沒有贏得任何回合?查理大概贏得了480次,鮑勃贏得了大約200次,但是它報告說,亞倫贏得了0分。阿隆應該贏得大約150 - 200次,而鮑勃比這個大一點。

這是我的代碼,任何幫助將不勝感激。提前致謝。

#include<iostream> 
#include<ctime> 
#include<cstdlib> 
#include<cmath> 

using namespace std; 

const double aShot = 30; 
const double bShot = 50; 
const double cShot = 100; 

void start(bool& aAlive, bool& bAlive, bool& cAlive, int& aCount, int& bCount, int& cCount, double result){ 

// Aaron Shoots at Charlie 
    if (aShot >= result){ 
      cAlive=false; 

} 
//Bob Shoots at Charlie 
    if (cAlive == false){ 
      cout<<"Charlie is dead, Bob shot at Aaron"<<endl; 
      if (bShot >= result) 
      aAlive = false; 
} 
    else if ((cAlive == true) && (bShot >= result)) 
      cAlive = false; 

//Charlie Shoots at Bob 
    if (cAlive == false){ 
    cout<<"Charlie is dead"<<endl; 
} 
    else if ((cAlive == true) && (cShot >= result)) 
      bAlive = false; 
//Aaron Shoots at Bob 
    if (bAlive == false){ 
      cout<<"Bob is dead, Aaron shoots at Charlie"<<endl; 
      if (aShot >= result) 
      cAlive = false;} 
    else if ((bAlive == true) && (aShot >= result)) 
      bAlive = false; 
//Bob Shoots at Aaron 
    if (bAlive == false) 
    cout<<"Bob is dead"<<endl; 
    else if ((bAlive == true) && (bShot >= result)) 
      aAlive = false; 
//Charlie Shoots at Aaron 
if (aAlive == false) 
    cout<<"Aaron is dead"<<endl; 
    else if ((aAlive == true) && (cShot >= result)) 
      aAlive = false; 

if ((aAlive == true) && (bAlive == false) && (cAlive == false)) 
aCount++; 
if ((aAlive == false) && (bAlive == true) && (cAlive == false)) 
bCount++; 
if ((aAlive == false) && (bAlive == false) && (cAlive == true)) 
cCount++; 

} 

int main(){ 
bool aAlive = true, bAlive = true, cAlive = true; 
int i, aCount = 0, bCount = 0, cCount = 0; 

      cout<<"Welcome to the game"<<endl; 
        srand (time(NULL)); 

    for (i=0; i<=1000; i++){ 
//Sets random number, or chance they hit their target 
        double result = rand() % 101; 
        cout<<result<<endl; 
//Sets all players to alive 
      aAlive = true, bAlive = true, cAlive = true; 

//Calling The Duel 
      start(aAlive, bAlive, cAlive, aCount, bCount, cCount, result); 
    } 
      cout<<"Aaron won: "<<aCount<<" times"<<endl; 
      cout<<"Bob won: "<<bCount<<" times"<<endl; 
      cout<<"Charlie won: "<<cCount<<" times"<<endl; 


} 
+1

你有問題嗎?更具體的東西? – shafeen

+1

這是謎題的邏輯。每個人都會射擊一次(如果他們輪到他們)。如果亞倫殺死查理,然後鮑勃射殺亞倫(無人射擊)。如果亞倫未能擊殺查理,那麼鮑勃將射殺查理(鮑勃仍然活着)。如果鮑勃未能殺死查理,查理殺死鮑勃(查理仍然活着)。所以亞倫永遠不會贏。 (如果亞倫還活着,至少還有一個人還活着)。 –

+0

爲什麼亞倫在節目結束時獲得0勝?我相信這是因爲每個鏡頭都沒有產生一個隨機數,但我不太確定。 – Beez

回答

0

這就是你如何執行你的隨機性。真的每個人都應該得到自己的「擲骰子」,看看他們是否射擊。

所以不是,我會建議更改開始爲類似以下內容:

void start(int& aCount, int& bCount, int& dCount){ 
    if (rand()%101 <= aShot){ // Aaron Shoots Charlie 
    while (true) { // Continue until winner 
     if (rand()%101 <= bShot){ // Bob Shoots Aaron and wins. 
     ++bCount; return; 
     } else if (rand()%101 <= aShot){ // Aaron Shoots at Bob and wins. 
     ++aCount; return; 
     } 
    } 
    } else if (rand()%101 <= bShot){ // Bob Shoots at Charlie -- Charlie dies 
    while (true){ // continue until winner. 
     if (rand()%101 <= aShot){ // Aaron kills Bob and wins 
     ++aCount; return; 
     } 
     if (rand()%101 <= bShot){ // Bob kills Aaron and wins 
     ++bCount; return; 
     } 
    } 
    } else { // Charlie is alive and kills bob. 
    if (rand()%101 <= aShot){ // Aaron kills Charlie and wins 
     ++aCount; 
    } else { // Aaron missed and looses to Charlie 
     ++cCount; 
    } 
    } 
} 

上面的代碼生成一個新的隨機數[0,100]在每次拍攝時(不含霹靂,因爲他總是贏)。並繼續,直到只有一個人離開。由於每次拍攝使用相同的隨機值,因此您的代碼始終會讓Aaron鬆動。

I.E.如果亞倫最初錯過了,他總會錯過,因此輸了。如果亞倫最初擊中查理,鮑勃將殺死亞倫(如果亞倫命中,鮑勃總是命中)。

0

主要的邏輯缺陷是您在調用start之前將值賦給結果

亞倫能贏的唯一場景是當result <= 30,在這種情況下他殺死了查理,但是保證鮑勃將射殺亞倫的result <= 50也是真的。

我會做的是分配一個隨機數在每個「鏡頭」之前結果。簡化,看起來像:

int alive = 2; //makes counter 
while (alive){ //a better way to loop 
    if(aAlive){ 
     if (cAlive){ 
      // Aaron Shoots at Charlie 
      result = rand()%101; 
      if (aShot >= result) { 
       cAlive=false; 
       alive--;} 
     } 
     else{ 
      //Aaron shoots at Bob 
      result = rand()%101; 
      if (aShot >= result) { 
       bAlive=false; 
       alive--;} 
     } 
    } 

    if (bAlive){ 
     if (cAlive){ 
      //Bob shoots at Charlie 
      result = rand()%101; 
       if (bShot >= result) { 
        cAlive=false; 
        alive--; 
       } 
     } 
     else { 
      //Bob shoots at Aaron 
      result = rand()%101; 
      if (bShot >= result) { 
       aAlive=false; 
       alive--; 
      } 
     } 
    } 

    if (cAlive){ 
     if (bAlive){ 
      //Charlie shoots at Bob 
      result = rand()%101; 
      if (cShot >= result){ 
       bAlive=false; 
       alive--; 
      } 
     } 
     else{ 
      //Charlie shoots at Aaron; 
      result = rand()%101; 
      if (cShot >= result){ 
       aAlive=false; 
       alive--; 
      } 
     } 
    } 
}