拼死搏斗 三个人C++
Fight to death Three people C++
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--;
}
}
}
}
相关文章:
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 将两个数组中的差异记录在第三个数组中
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 将数字转换为字母(例如:123 转换为一二三)
- 如何从给定字符串中删除第二次和第三次出现的$
- 在更改for循环的第三部分后,未使用for循环结果
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 三向比较运算符成员与非成员实现
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 数数并选择 sqlite 中的前三名
- 我有三个 getline,但是一旦编译,输入就太多了
- 个人图书馆,数字正数或负数
- 有时打印一行,有时打印三行
- 多维数组存储三种不同的数据类型?
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 拼死搏斗 三个人C++