三方决斗模拟器工作不正常

时间:2015-10-23 23:57:31

标签: c++

我正在创建一个三方决斗模拟器。每个人都有一个精确级别(亚伦是33岁,鲍勃是50岁,查尔斯是100岁)。生成1到100之间的随机数,如果该数字小于人的技能等级,则该人射击具有最高技能等级的另一(活)玩家。它应该重复10000次,我打算打印结果。但是,每个人的分数都不正确。我一直把Bob的得分变为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;
}

2 个答案:

答案 0 :(得分:1)

你必须在运行每个决斗之前初始化标志。

移动

bool A_alive = true;
bool B_alive = true;
bool C_alive = true;

for循环开始后的右边。

for (int i = 0; i < number_of_duels; i++) {

答案 1 :(得分:0)

如果你原谅我的直言不讳,这对我来说并不是特别精彩的代码。我想我会写更多这样的东西:

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

例如,如果我们想要建立一个4方决斗而不是3方式的对决模型,那么我们可以通过向init添加另一个决斗者来实现这一点:

std::vector<duelist> init { { "Aaron", 33 }, { "Bob", 50 }, { "Charles", 100 }, { "Cassie", 80 } };