RPG战斗系统无法正常工作

RPG fighting system doesn't work properly

本文关键字:常工作 工作 系统 RPG      更新时间:2023-10-16

我正在尝试建立一个RPG战斗系统,您将在其中与妖精战斗。不幸的是,我用来制造运气系统的随机器无法像我想要的那样工作。由于某种原因,当我希望每次都会成为不同的值时,随机变量总是相同的。我尝试了很多事情,但没有成功。同样在此处发布此信息之前,我意识到,在满足要求停止的要求时,我的wir循环不会停止。

#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main(){
    std::mt19937 generator;
    generator.seed(std::time(0));
    std::uniform_int_distribution<uint32_t> d100(0, 100);
    int random = d100(generator);
    std::mt19937 generator2;
    generator2.seed(std::time(0));
    std::uniform_int_distribution<uint32_t> d20(1, 20);
    int random2 = d20(generator2);
    string action;
    int enmyHP = 100;
    int plyrHP = 120;
    while(enmyHP >= 1 || plyrHP >= 1){
        cout << "You are fighting a goblin. What will you do?"<< endl;
        cout << "|| ATTACK ||       || SPELL ||      || RUN ||"<< endl;
        cin >> action;
        if(action == "attack" || action == "Attack" || action == "ATTACK"){
            if(random >= 95){
                cout << "CRITICAL HIT!" << endl;
                cout << "You did 50 damage!" << endl;
                enmyHP - 50;
            } else if(random < 95 && random > 15){
                cout << "You did " << random2 << " damage" << endl;
                enmyHP = enmyHP - random2;
                cout <<"The goblin has: "<< enmyHP << " HP left" << endl;
            } else if(random > 15 && random < 1){
                cout << "You miss" << endl;
            } else {
                cout << "You hit yourself" << endl;
                plyrHP = plyrHP - 20;
                cout << "You have: " << plyrHP << " HP" << endl;
            }
        }
    }
    return 0;
}

在这里,您实际上是在生成随机数,您没有创建两个具有不同分布的dice ,因此每次使用randomrandom2时,您都会得到相同的数字:

int random = d100(generator);
int random2 = d20(generator2); // only one generator per thread should usually be used

如果您想创建可以使用的一组骰子,则可以用引用发电机来绑定分布,以创建函数,可呼叫,可以像函数一样使用。示例:

#include <iostream>
#include <random>
#include <iomanip>    // std::setw
#include <functional> // std::bind
int main() {
    std::random_device rd;
    std::mt19937 generator(rd());
    auto d6 = std::bind(std::uniform_int_distribution<uint16_t>(1, 6), std::ref(generator));
    auto d20 = std::bind(std::uniform_int_distribution<uint16_t>(1, 20), std::ref(generator));
    auto d100 = std::bind(std::uniform_int_distribution<uint16_t>(1, 100), std::ref(generator));    
}

另外,您可以创建lambdas以达到相同的效果:

std::uniform_int_distribution<uint16_t> dist6(1, 6);
std::uniform_int_distribution<uint16_t> dist20(1, 20);
std::uniform_int_distribution<uint16_t> dist100(1, 100);
// capture the distributions and generator by reference
auto d6 = [&](){ return dist6(generator); };
auto d20 = [&](){ return dist20(generator); };
auto d100 = [&](){ return dist100(generator); };

编辑:正如建议的,您还可以做一个Dice类:

class Dice {
    // static is implied below but added to make it clear: one instance
    // is created and shared between instances of Dice.
    //
    // thread_local makes one instance of the variable per thread
    // in case you add mutithreading later.
    static thread_local std::random_device rd;
    static thread_local std::mt19937 generator;
    std::uniform_int_distribution<uint32_t> dist;
public:    
    Dice(uint32_t low, uint32_t high) : dist(low, high) {}
    // make instances of the class callable:
    uint32_t operator()() {
        return dist(generator);
    }
};
thread_local std::random_device Dice::rd;
thread_local std::mt19937 Dice::generator = std::mt19937(Dice::rd());
Dice d6(1, 6);
Dice d20(1, 20);
Dice d100(1, 100);

任何版本都可以像这样使用:

while(true) {
    std::cout << d6() << std::setw(3) << d20() << std::setw(4) << d100() << "n";
}