我的随机生成器是否不工作,或者我决定人/骨架是否击中对手的方式是否有错误

Is my random generator not working, or is there an error with how I am deciding whether a human/skeleton hits their opponent?

本文关键字:是否 方式 对手 有错误 随机 工作 我的 或者 决定      更新时间:2023-10-16

无论哪种方式,如果我每次都投入相同数量的人类和僵尸,我都会得到相同的结果。我认为这可能是我的其他代码的问题,因为当我将变量部分更改为人类/僵尸序列时,结果仍然相同。但我的朋友有一个类似的程序,他的问题是他的随机数生成器,我以前从未使用过mt19937。这是我的节目。用户应该输入僵尸的数量,然后输入骷髅的数量,程序应该使用随机数生成器来决定哪个团队获胜以及获胜的数量。我们将不胜感激。

#include <iostream>
#include <random>
#include <ctime>
using namespace std;
//Human Specifications
float humanAttack = 0.8f;
float humanDamage = 10.0f;
float maxHumanHealth = 15.0f;
float currentHumanHealth = maxHumanHealth;
int humanNumber;
//Zombie Specifications
float zombieAttack = 0.5f;
float zombieDamage = 15.0f;
float maxZombieHealth = 10.0f;
float currentZombieHealth = maxZombieHealth;
int zombieNumber;
char turn = 'H';
int attackResult;
int main() {
mt19937 randomGenerator(time(NULL));
//default_random_engine randomGenerator(time(NULL));
uniform_real_distribution<float> attack(0.0f, 1.0f);
//Setting Numbers
cout << "~*~*~*~*-Humans VS Zombies-*~*~*~*~" << endl << endl;
cout << "Set the number of humans: ";
cin >> humanNumber;
cout << endl << "Set the number of zombies: ";
cin >> zombieNumber;
while ((zombieNumber >0) && (humanNumber >0)) {
//Dice Roll
attackResult = attack(randomGenerator);
//Humans Turn
if (turn == 'H') {
if (attackResult < humanAttack) {
currentZombieHealth = currentZombieHealth - humanAttack;
if (currentZombieHealth <= 0) {
zombieNumber --;
currentZombieHealth = maxZombieHealth;
}
}
turn = 'Z';
}
//Zombies Turn
else {
if (attackResult < zombieAttack) {
currentHumanHealth = currentHumanHealth - zombieAttack;
if (currentHumanHealth <= 0) {
humanNumber --;
currentHumanHealth = maxHumanHealth;
}
}
turn = 'H';
}

}
cout << endl << "[The noise of battle...]" << endl << endl << endl;
cout << "BATTLE IS OVER" << endl << endl;
if (zombieNumber > 0) {
cout << "The zombies have won the battle!" << endl;
}
else {
cout << "The humans have won the battle!" << endl;
}
cout << "There are " << humanNumber << " humans and " << zombieNumber << " zombies left alive." << endl;
return 0;
}

您的编译器可能会对以下行发出警告:

attackResult = attack(randomGenerator);

你应该看到这样的东西:

将浮点截断为整数

请注意编译器警告,它们是免费的bug捕获器。

您的问题是attackResult是一个整数,所以0.01.0之间的浮点数总是被截断为0的整数。

相关文章: