我一直在编写这个游戏,只是随机攻击机会输入人类和骷髅的数量。如果在反复运行程序时输入相同的金额,则会生成相同的金额。为什么每次while循环重启时hitChance /随机攻击量都没有变化?当我在while循环中打印attackChance(randGen)时它会改变。为什么randGen不会改变战斗胜利者的输出?
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int getNumSkeletons();
int getNumHumans();
void finishedBattleStats(int numHumans, int startHumans, int numSkeletons, int startSkeletons);
void simulatedBattle(int &numSkeletons, int &numHumans, int &startHumans, int &startSkeletons);
int main() {
int startHumans;
int startSkeletons;
int numHumans;
int numSkeletons;
cout << "------------------------------------\nSkeletons V Humans\n\n";
//Gets Number of Humans
numHumans = getNumHumans();
startHumans = numHumans;
//Gets Number of Skeletons
numSkeletons = getNumHumans();
startSkeletons = numSkeletons;
//Simulates Battle
simulatedBattle(numSkeletons, numHumans, startHumans, startSkeletons);
//End Battle Stats
finishedBattleStats(numHumans, startHumans, numSkeletons, startSkeletons);
cin.get();
return 0;
}
int getNumHumans() {
int numHumans;
cout << "Enter number of Humans: \n";
cin >> numHumans;
return numHumans;
}
int getNumSkeletons() {
int numSkeletons;
cout << "Enter number of Skeletons: \n";
cin >> numSkeletons;
return numSkeletons;
}
void simulatedBattle(int &numSkeletons, int &numHumans, int &startHumans, int &startSkeletons) {
static mt19937 randGen(time(NULL));
uniform_real_distribution<float> attackChance(0.0f, 1.0f);
//HumanProperties
float humanDamage = 30.0f;
float humanHitChance = 0.6f;
float humanCritChance = 0.2f;
float humanHealth = 50.0f;
float startHumanHealth = humanHealth;
float currentHuman = startHumanHealth;
//Skeleton Properties
float skeletonDamage = 20.0f;
float skeletonHitChance = 0.7f;
float skeletonCritChance = 0.2f;
float skeletonHealth = 40.0f;
float startSkeletonHealth = skeletonHealth;
float currentSkeleton = startSkeletonHealth;
char turn = 'H';
float hitChance;
while (numSkeletons > 0 && numHumans > 0) {
hitChance = attackChance(randGen);
if (turn == 'H') {
if (hitChance > humanHitChance) {
currentSkeleton -= humanDamage;
turn = 'S';
if (currentSkeleton < 0) {
numHumans--;
currentSkeleton = startSkeletonHealth;
turn = 'S';
}
}
}
else {
if (hitChance > skeletonHitChance) {
currentHuman -= skeletonDamage;
turn = 'H';
}
if (currentHuman < 0) {
numSkeletons--;
currentHuman = startHumanHealth;
turn = 'H';
}
}
}
}
void finishedBattleStats(int numHumans, int startHumans, int numSkeletons, int startSkeletons) {
if (numHumans == 0) {
cout << "Skeletons won! \n\n";
cout << "Skeletons left: " << numSkeletons << endl;
cout << "Skeleton Casualties: " << startSkeletons - numSkeletons << endl;
cout << "All " << startHumans << " humans are dead! \n\n";
cout << "Game Over!";
cin.get();
}
else {
cout << "Humans Won! \n\n";
cout << "Humans left: " << numHumans << endl;
cout << "Human Casualties: " << startHumans - numHumans << endl;
cout << "All " << startSkeletons << " skeletons are dead! \n\n";
cout << "Game Over!";
cin.get();
}
}
答案 0 :(得分:2)
恕我直言,你在战斗计算中有一个缺陷。也就是说,当人类的健康状况发生时,&lt; 0你减去一个骨架而不是一个人,反之亦然。以下是更正。有了演示。我尝试了很多次演示并给出了不同的结果(ENJOY):
void simulatedBattle(int &numSkeletons, int &numHumans, int &startHumans, int &startSkeletons) {
uniform_real_distribution<float> attackChance(0.0f, 1.0f);
static mt19937 randGen(time(NULL));
//HumanProperties
float humanDamage = 25.0f;
float humanHitChance = 0.2f;
float humanCritChance = 0.2f;
float humanHealth = 50.0f;
float startHumanHealth = humanHealth;
float currentHuman = startHumanHealth;
//Skeleton Properties
float skeletonDamage = 20.0f;
float skeletonHitChance = 0.8f;
float skeletonCritChance = 0.2f;
float skeletonHealth = 40.0f;
float startSkeletonHealth = skeletonHealth;
float currentSkeleton = startSkeletonHealth;
char turn = 'H';
float hitChance;
while (numSkeletons > 0 && numHumans > 0) {
hitChance = attackChance(randGen);
if (turn == 'H') {
if (hitChance > humanHitChance) {
currentSkeleton -= humanDamage;
if (currentSkeleton < 0) {
--numSkeletons;
currentSkeleton = startSkeletonHealth;
}
turn = 'S';
}
}
else {
if (hitChance > skeletonHitChance) currentHuman -= skeletonDamage;
if (currentHuman < 0) {
--numHumans;
currentHuman = startHumanHealth;
}
turn = 'H';
}
}
}
答案 1 :(得分:0)
使用前必须使用seed
mersenne twister,否则您将始终拥有相同的输出。
通常的做法是使用内部时钟播种:
// obtain a seed from the timer
myclock::duration d = myclock::now() - beginning;
unsigned seed2 = d.count();
generator.seed (seed2);