拼死搏斗 三个人C++

Fight to death Three people C++

本文关键字:三个人 C++      更新时间:2023-10-16

Aaron 有 30% 的命中几率鲍勃有50%的命中几率查理有100%的命中率

亚伦先开枪,然后是鲍勃,然后是查理,每个人都试图先射杀表现最好的人。有人可以解释为什么亚伦没有赢得任何回合吗?查理大约赢了 480 次,鲍勃赢了大约 200 次,但它报告说亚伦赢了 0。Aaaron 应该赢得大约 150 - 200 次,而 Bob 比这大一点。

这是我的代码,任何帮助将不胜感激。提前谢谢。

#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;

}

这是你执行随机性的方式。真的每个人都应该有自己的"掷骰子",看看他们是否开枪。

因此,我建议将 start 更改为如下所示的内容:

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 松散,因为你对每个镜头使用相同的随机值。

即如果亚伦最初错过了,他总是会错过,因此会失败。如果亚伦最初击中查理,鲍勃会杀死亚伦(如果亚伦击中,鲍勃总是击中)。

主要的逻辑缺陷是在调用 start 之前为 result 赋值。

亚伦唯一能赢的场景是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--;
            }
        }
    }
}