gpt4 book ai didi

c++ - RPG 格斗系统无法正常工作

转载 作者:行者123 更新时间:2023-11-28 01:26:00 26 4
gpt4 key购买 nike

我正在尝试制作一个角色扮演游戏战斗系统,您可以在其中与地精作战。不幸的是,我用来制作运气系统的随机发生器并没有像我想要的那样工作。出于某种原因,当我希望它每次都是不同的值时,随机变量在每个循环中总是相同的。我尝试了很多东西但没有成功。同样在将其发布在这里之前,我意识到我的 while 循环在满足停止要求时不会停止。

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

最佳答案

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

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

如果您想创建一组可以使用的骰子,您可以将一个分布与对您的生成器的引用绑定(bind)在一起,以创建一个仿函数,一个可调用对象,您可以像函数一样使用它。示例:

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

或者,您可以创建 lambda 来实现相同的效果:

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

关于c++ - RPG 格斗系统无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53813483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com