RPG战斗系统无法正常工作
RPG fighting system doesn't work properly
我正在尝试建立一个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 ,因此每次使用random
或random2
时,您都会得到相同的数字:
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";
}
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程