Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 三向双模拟器工作不正常_C++ - Fatal编程技术网

C++ 三向双模拟器工作不正常

C++ 三向双模拟器工作不正常,c++,C++,我正在创建一个三向决斗模拟器。每个人都有一个精确等级(亚伦的是33,鲍勃的是50,查尔斯的是100)。将生成一个介于1和100之间的随机数,如果该数字小于该人的技能水平,则该人将射杀另一个(活着的)具有最高技能水平的玩家。它应该重复10000次,我将打印结果。但是,每个人的分数都不正确。我一直认为鲍勃的分数是10000,而其他人的分数是0。提前感谢您的帮助 #include <iostream> #include <stdlib.h> #include <ctime

我正在创建一个三向决斗模拟器。每个人都有一个精确等级(亚伦的是33,鲍勃的是50,查尔斯的是100)。将生成一个介于1和100之间的随机数,如果该数字小于该人的技能水平,则该人将射杀另一个(活着的)具有最高技能水平的玩家。它应该重复10000次,我将打印结果。但是,每个人的分数都不正确。我一直认为鲍勃的分数是10000,而其他人的分数是0。提前感谢您的帮助

#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std; 

bool at_least_two_alive (bool A_alive, bool B_alive, bool C_alive) {

int i = 0;

if (A_alive) {
    i++;
}
if (B_alive) {
    i++;
}
if (C_alive) {
    i++;
}
if (i >= 2) {
    return true;
}
else {
    return false;
}
}

void Aaron_shoots (bool& B_alive, bool& C_alive ) {

int Aaron_precision = 33;

int shoot_target_result = rand()%100;

if (shoot_target_result <= Aaron_precision) {

    if (C_alive) {
        C_alive = false;
    }

    else {
        B_alive = false;
    }

}
}

void Bob_shoots (bool& A_alive, bool& C_alive ) {

int Bob_precision = 50;

int shoot_target_result = rand()%100;

if (shoot_target_result <= Bob_precision) {

    if (C_alive) {
        C_alive = false;
    }

    else {
        A_alive = false;
    }
}
}

void Charles_shoots (bool& A_alive, bool& B_alive ) {

int Charles_precision = 100;

int shoot_target_result = rand()%100;

if (shoot_target_result <= Charles_precision) {

    if (B_alive) {
        B_alive = false;
    }

    else {
        A_alive = false;
    }
}
}

void duel() {

bool A_alive = true;
bool B_alive = true;
bool C_alive = true;
int A_wins = 0;
int B_wins = 0;
int C_wins = 0;
int number_of_duels = 10000;

for (int i = 0; i < number_of_duels; i++) {
    while (at_least_two_alive(A_alive, B_alive, C_alive)) {
        if (A_alive) {
            Aaron_shoots(B_alive, C_alive);
        }
        if (B_alive ) {
            Bob_shoots(A_alive, C_alive);
        }
        if (C_alive ) {
            Charles_shoots(A_alive, B_alive);
        }
    }
    if (A_alive) {
        A_wins++;
    }
    if (B_alive ) {
        B_wins++;
    }
    if (C_alive ) {
        C_wins++;
    }
}
}

void menu() {
string input;
cout << "\n*** Welcome to Duel Simulator ***\n\n"
        "Ready to run Duel Simulator (run 10000 times)? (y/n): ";

cin >> input;

if (input == "y") {
    cout << "\n1000 duels completed"
    "\n2000 duels completed"
    "\n3000 duels completed"
    "\n4000 duels completed"
    "\n5000 duels completed"
    "\n6000 duels completed"
    "\n7000 duels completed"
    "\n8000 duels completed"
    "\n9000 duels completed"
    "\n10000 duels completed";
    duel();
}
else {
    if (input == "n") {
        menu();
    }
    else {
        cout << "\nPlease enter a valid option!\n";
        menu();

    }

}
}

int main () {

menu();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
至少有两个活着(一个活着,一个活着,一个活着){
int i=0;
如果(A_活着){
i++;
}
如果(B_活着){
i++;
}
如果(C_活着){
i++;
}
如果(i>=2){
返回true;
}
否则{
返回false;
}
}
无效阿龙射门(布尔&布鲁活着,布尔&布鲁活着){
int Aaron_精度=33;
int shoot_target_result=rand()%100;

如果(射击\目标\结果你必须在每次决斗前初始化标志

移动

for
循环开始后的右侧

for (int i = 0; i < number_of_duels; i++) {
for(int i=0;i
请原谅我的直言不讳,我觉得这并不是特别精彩的代码。我想我应该写一些类似的东西:

#include <vector>
#include <random>
#include <string>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <functional>

class generator {
    std::mt19937 rng;
    std::uniform_int_distribution<int> uni;
public:
    generator(int low, int high) : rng(std::random_device()()), uni(low, high) { }
    int operator()() { return uni(rng); }
} gen(1, 100);

class duelist {
    std::string name;
    int skill;
    bool dead;
public:
    duelist(std::string const &name, int skill) 
        : name(name), 
        skill(skill), 
        dead(false)
    {
    }

    void round(std::vector<duelist> &competitors) { 
        if (dead) return;
        int value = gen();

        auto c = competitors.rbegin();
        while (*this == *c || c->dead) {
            ++c;
            if (c == competitors.rend())
                return;
        }

        std::cout << name << " shoots ";
        if (value < skill) {
            std::cout << "and kills: " << *c << "\n";
            c->dead = true;
        }
        else
            std::cout << "and misses\n";
    }

    operator bool() const { return !dead; }

    friend std::ostream &operator<<(std::ostream &os, duelist const &d) { return os << d.name; }

    // sorts in descending order by skill
    bool operator<(duelist const &other) const { return skill < other.skill; }
private:

    // but comparison for equality is by name
    bool operator==(duelist const &other) const { return name == other.name; }
};

bool at_least_two(std::vector<duelist> const &competitors) {
    return std::count_if(competitors.begin(), competitors.end(), [](bool b) { return b; }) > 1;
}

int main() {
    std::vector<duelist> init { { "Aaron", 33 }, { "Bob", 50 }, { "Charles", 100 } };

    std::sort(init.begin(), init.end());

    std::vector<int> wins(init.size());

    for (int i = 0; i < 1000; i++) {
        std::vector<duelist> competitors = init;

        while (at_least_two(competitors)) {
            for (auto c : competitors)
                c.round(competitors);
            for (int i = 0; i < competitors.size(); i++)
                if (competitors[i])
                    ++wins[i];
        }
        std::cout << "\n";
    }
    std::cout << "Survival stats: ";
    for (int i = 0; i < init.size(); i++)
        std::cout << init[i] << " : " << wins[i] << "\n";
}
#include <vector>
#include <random>
#include <string>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <functional>

class generator {
    std::mt19937 rng;
    std::uniform_int_distribution<int> uni;
public:
    generator(int low, int high) : rng(std::random_device()()), uni(low, high) { }
    int operator()() { return uni(rng); }
} gen(1, 100);

class duelist {
    std::string name;
    int skill;
    bool dead;
public:
    duelist(std::string const &name, int skill) 
        : name(name), 
        skill(skill), 
        dead(false)
    {
    }

    void round(std::vector<duelist> &competitors) { 
        if (dead) return;
        int value = gen();

        auto c = competitors.rbegin();
        while (*this == *c || c->dead) {
            ++c;
            if (c == competitors.rend())
                return;
        }

        std::cout << name << " shoots ";
        if (value < skill) {
            std::cout << "and kills: " << *c << "\n";
            c->dead = true;
        }
        else
            std::cout << "and misses\n";
    }

    operator bool() const { return !dead; }

    friend std::ostream &operator<<(std::ostream &os, duelist const &d) { return os << d.name; }

    // sorts in descending order by skill
    bool operator<(duelist const &other) const { return skill < other.skill; }
private:

    // but comparison for equality is by name
    bool operator==(duelist const &other) const { return name == other.name; }
};

bool at_least_two(std::vector<duelist> const &competitors) {
    return std::count_if(competitors.begin(), competitors.end(), [](bool b) { return b; }) > 1;
}

int main() {
    std::vector<duelist> init { { "Aaron", 33 }, { "Bob", 50 }, { "Charles", 100 } };

    std::sort(init.begin(), init.end());

    std::vector<int> wins(init.size());

    for (int i = 0; i < 1000; i++) {
        std::vector<duelist> competitors = init;

        while (at_least_two(competitors)) {
            for (auto c : competitors)
                c.round(competitors);
            for (int i = 0; i < competitors.size(); i++)
                if (competitors[i])
                    ++wins[i];
        }
        std::cout << "\n";
    }
    std::cout << "Survival stats: ";
    for (int i = 0; i < init.size(); i++)
        std::cout << init[i] << " : " << wins[i] << "\n";
}
std::vector<duelist> init { { "Aaron", 33 }, { "Bob", 50 }, { "Charles", 100 }, { "Cassie", 80 } };