2016-03-03 49 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--; 
      } 
     } 
    } 
}