我是c ++的初学者,我正在做在线教程,我正在做第一次挑战。这是一场游戏:玩家进入游戏配置,然后他必须对他的敌人战略做出反应。
问题在于,有时游戏结束时不会显示任何策略。 stratergyRoll
似乎无法每次都让我相信问题出在我的随机数生成器上。
这是代码
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int AmountOfYourMen(string FactionName1);
int AmountOfEnemyMen(string FactionName2);
int EndStatment(string GeneralName, string FactionName1, string FactionName2, int orgfaction1size, int faction1size, int orgfaction2size, int faction2size);
void BattleSim(int &faction2hp, int &faction1hp, int &faction1attack, int &faction2attack, int &faction1size, int &faction2size, int &orghpfaction1, int &orghpfaction2);
int main()
{
int faction1;
int faction2;
int faction1hp;
int faction2hp;
int faction1attack;
int faction2attack;
int faction1size;
int faction2size;
int orgfaction1size;
int orgfaction2size;
int orghpfaction1;
int orghpfaction2;
string FactionName1;
string FactionName2;
string GeneralName;
int ArmyMove;
mt19937 RandomGenerator(time(NULL));
uniform_int_distribution<int> StratergyRoll(1,4);
uniform_real_distribution<float> yourmove(0.0,1);
uniform_real_distribution<float> trapsuccess(0.0,1);
cout<<"Welcome General What Is Your Name"<<endl;
cin>>GeneralName;
cout<<"Welcome General "<< GeneralName <<endl;
cout<<"choose your faction Rome(1) Spartans(2) Carthage(3) Egypt(4) Greece(5)"<<endl;
cin>> faction1;
cout<<"now choose the enemy faction Rome(1) Spartans(2) Carthage(3) Egypt(4) Greece(5)"<<endl;
cin>> faction2;
if(faction1 == 1){
FactionName1 = "Romans";
faction1hp = 120;
orghpfaction1 = 100;
faction1attack = 100;
}
else if(faction1 == 2){
FactionName1 = "Spartans";
faction1hp = 45;
orghpfaction1 = 45;
faction1attack = 100;
}
else if(faction1 == 3){
FactionName1 = "Carthaginians";
faction1hp = 80;
orghpfaction1 = 80;
faction1attack = 60;
}
else if(faction1 == 4){
FactionName1 = "Egyptions";
faction1hp = 80;
orghpfaction1 = 80;
faction1attack = 70;
}
else if(faction1 == 5){
FactionName1 = "Greeks";
faction1hp = 80;
orghpfaction1 = 80;
faction1attack = 80;
}
if(faction2 == 1){
FactionName2 = "Romans";
faction2hp = 120;
orghpfaction2 = 100;
faction2attack = 100;
}
else if(faction2 == 2){
FactionName2 = "Spartans";
faction2hp = 45;
orghpfaction2 = 45;
faction2attack = 100;
}
else if(faction2 == 3){
FactionName2 = "Carthaginians";
faction2hp = 80;
orghpfaction2 = 80;
faction2attack = 60;
}
else if(faction2 == 4){
FactionName2 = "Egyptions";
faction2hp = 80;
orghpfaction2 = 80;
faction2attack = 70;
}
else if(faction2 == 5){
FactionName2 = "Greeks";
faction2hp = 80;
orghpfaction2 = 80;
faction2attack = 80;
}
faction1size = AmountOfYourMen(FactionName1);
orgfaction1size = faction1size;
faction2size = AmountOfEnemyMen(FactionName2);
orgfaction2size = faction2size;
if(StratergyRoll(RandomGenerator) ==1) {
cout<<"the "<< FactionName2 << " are charging strait ahead using no apparent stratergy what will you do"<<endl;
cout<<"(1)charge them head on"<<endl;
cout<<"(2)attempt to surround them"<<endl;
cin>> ArmyMove;
if(ArmyMove ==1){
cout<<"you charge foward meeting your enemy head on"<<endl;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
else if(ArmyMove == 2){
if(yourmove(RandomGenerator) >=0.4){
cout<<"you have successufuly surrounded your enemy your men get stat boosts"<<endl;
faction1hp += 40;
faction1attack +=40;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
while(faction1size >= 1 && faction2size >=1);
}
else if(yourmove(RandomGenerator) < 0.4){
cout<<"your attempt at surrounding your enemy have failed your men loose stats"<<endl;
faction1hp -= 50;
faction1attack -= 50;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
else if(StratergyRoll(RandomGenerator)==2){
cout<<"the "<< FactionName2 << " your enemy is advancing with their cavelry positoned far out on each side and have their infantry in the middle what will you do"<<endl;
cout<<"(1) ignore the cavlery and charge their infantry head on"<<endl;
cout<<"(2)tell your cavelry to meet the enemy cavelry head on and tell your infantry to move foward to engage"<<endl;
cout<<"(3)hold your ground put spearmen on your flanks and at the back of your army"<<endl;
cin>> ArmyMove;
if(ArmyMove ==1){
cout<<"you charge foward meeting your enemy head on"<<endl;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
else if(ArmyMove == 2){
if(yourmove(RandomGenerator) >=0.5){
cout<<"your cavelry have defeated the enemy cavelry and are now out flanking your enemy. your men get stat boosts"<<endl;
faction1hp +=50;
faction1attack +=50;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
else if(yourmove(RandomGenerator) < 0.5){
cout<<"your cavelry was defeated and the enemy cavelry is now out flanking your men. stats lost "<<endl;
faction1hp -= 60;
faction1attack -= 60;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
else if(ArmyMove == 3){
if(yourmove(RandomGenerator) >= 0.1)
cout<<"your men hold their ground and their cavelry crashed into your speers getting slaughtered"<<endl;
cout<<" this was a good plan stopping the flanking and increases your stats"<<endl;
faction2attack -=50;
faction2hp -= 50;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
else if(yourmove(RandomGenerator) <= 0.1){
cout<<"your men hold and their cavelry crashed into your speers"<<endl;
cout<<"this seemed like a good plan however the cavelry broke through and you are now surounded"<<endl;
cout<<"your men loose stats"<<endl;
faction1attack -=50;
faction1hp -= 50;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
else if(StratergyRoll(RandomGenerator)==3){
cout<<"the "<< FactionName2 << " are holding position what will you do"<<endl;
cout<<"(1)chage foward. if your enemy wont come to you, you go to them"<<endl;
cout<<"(2)tell your archers to hide on the flanks further up than your army and send in a platoon of men to attack and try to draw your enemy into a trap"<<endl;
cout<<"(3)approach slowly keeping your gard up"<<endl;
cin>> ArmyMove;
if(ArmyMove ==1){
cout<<"you charge foward meeting your enemy head on"<<endl;
if(trapsuccess(RandomGenerator)<0.4){
cout<<"the enemy were hoping you would do this and have a trap in place. You take archer fire from each side"<<endl;
cout<<"your men loose stats"<<endl;
faction1hp -= 60;
faction1attack -= 60;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
else if(trapsuccess(RandomGenerator)>0.4){
cout<<"you were lucky the enemy had no plan in place"<<endl;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
else if(ArmyMove == 2){
if(yourmove(RandomGenerator) >=0.41){
cout<<"your trap was a success your men get stat boosts"<<endl;
faction1hp += 50;
faction1attack +=50;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
else if(yourmove(RandomGenerator) <= 0.4){
cout<<"your trap failed the enemy realised what you where doing and countered. stats lost "<<endl;
faction2hp += 20;
faction2attack += 40;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
else if(ArmyMove == 3){
if(yourmove(RandomGenerator) >= 0.25)
cout<<"your men approach causiosly and spot a trap being set so you counter. stat boost"<<endl;
faction1attack +=40;
faction1hp += 40;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
else if(yourmove(RandomGenerator) < 0.25){
cout<<"your men are causious but dont detect the trap. stats lost"<<endl;
faction2attack +=40;
faction2hp += 40;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
else if(faction2 == 1) {
if(StratergyRoll(RandomGenerator) == 3){
cout<<"the Romans are forming the Testudo formation"<<endl;
cout<<"what will you do"<<endl;
cout<<"(1) barrage them with arrows they dont stand a chance"<<endl;
cout<<"(2) charge tell all your men to charge them"<<endl;
cout<<"(3) fire arrows and do a cavelry charge"<<endl;
cin>>ArmyMove;
if(ArmyMove = 1){
if(yourmove(RandomGenerator) >= 0.95){
cout<<"you got lucky the Testudo formation was not set properly"<<endl;
cout<<"you cut them down with your arrows. stat boost"<<endl;
faction1attack += 60;
faction1hp +=80;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
else if(ArmyMove == 2){
cout<<"you charge your enemy head on"<<endl;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
else if(ArmyMove == 3){
cout<<"good job the legions not at fighting cavelry and skirmishers seperatly"<<endl;
cout<<"but not together. stat boost"<<endl;
faction1attack += 50;
faction1hp += 50;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
else if(StratergyRoll(RandomGenerator) == 4){
cout<<"your enemy has chosen the battle field carefully"<<endl;
cout<<"the ground is flat and perfect for the enemy chariots"<<endl;
cout<<"what will you do"<<endl;
cout<<"(1) who cares they dont stand a chance against me"<<endl;
cout<<"(2) shift your cavelry to more ruff ground and hope they follow"<<endl;
cout<<"(3) bring spearmen to the front so they can take on the chariots"<<endl;
cin>>ArmyMove;
if(ArmyMove == 1){
cout<<"the battle ground sutes the enemy they get stat boost"<<endl;
faction2attack +=60;
faction2hp +=60;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
else if(ArmyMove == 2){
if(yourmove(RandomGenerator)>= 0.4){
cout<<"your tactics work and your enemy follow you off to ruffer terrain"<<endl;
cout<<"losing their battle advantage and gaining advantage for yourself"<<endl;
faction1hp += 60;
faction1attack += 60;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}else
if(yourmove(RandomGenerator) < 0.4){
cout<<"your plan fails and the enemy sees what you are doing"<<endl;
cout<<"he drives your army back to their chosen battlefield"<<endl;
cout<<"stats lost"<<endl;
faction1hp -=30;
faction1attack -=30;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
else if(ArmyMove == 3){
if(yourmove(RandomGenerator) >= 90){
cout<<"you got lucky the enemy chariots were untrained and your spearmen provail"<<endl;
cout<<"stats increase"<<endl;
faction1attack += 30;
faction1hp += 30;
cout<<" "<<endl;
cout<<"<battle sounds>"<<endl;
cout<<" "<<endl;
BattleSim(faction2hp, faction1hp, faction1attack, faction2attack, faction1size, faction2size, orghpfaction1, orghpfaction2);
}
}
}
}
if(faction1size <= 0){
cout<<FactionName2 << " are victorious"<<endl;
cout<<" "<<endl;
}
else if(faction2size <=0){
cout<<FactionName1 << " are victorious"<<endl;
cout<<" "<<endl;
}
EndStatment(GeneralName, FactionName1, FactionName2, orgfaction1size, faction1size, orgfaction2size, faction2size);
return 0;
}
int AmountOfYourMen(string FactionName1)
{
//amount of your men
int faction1size;
cout<<"how many "<< FactionName1 <<":";
cin>>faction1size;
return faction1size;
}
int AmountOfEnemyMen(string FactionName2)
{
//amount of enemy men
int faction2size;
cout<<"how many "<< FactionName2 <<":";
cin>>faction2size;
return faction2size;
}
int EndStatment(string GeneralName, string FactionName1, string FactionName2, int orgfaction1size, int faction1size, int orgfaction2size, int faction2size)
{
cout<<" "<<endl;
cout<<FactionName1<< " casulties:"<< orgfaction1size - faction1size<<endl;
cout<<FactionName1<<" men remaining:"<< faction1size<<endl;
cout<<" "<<endl;
cout<<FactionName2<< " casulties:"<< orgfaction2size - faction2size<<endl;
cout<<FactionName2<<" men remaining:"<< faction2size<<endl;
cout<<" "<<endl;
if(faction1size <= 0){
cout<<"General "<< GeneralName <<" you have been defeated and discraced"<<endl;
cout<<"you should must regain your honor by playing again and wining"<<endl;
}
else if(faction2size<=0){
cout<<"General "<< GeneralName << " you were VICTORIOUS"<<endl;
cout<<"continue your military superiority by playing again"<<endl;
}
}
void BattleSim(int &faction2hp, int &faction1hp, int &faction1attack, int &faction2attack, int &faction1size, int &faction2size, int &orghpfaction1, int &orghpfaction2)
{
uniform_real_distribution<float> AttackRoll(0.0,1);
mt19937 RandomBattle(time(NULL));
do {
if(AttackRoll(RandomBattle) >= 0.5){
faction2hp -= faction1attack;
if(faction2hp <= 0){
faction2size -=1;
faction2hp = orghpfaction2;
}
}
if(AttackRoll(RandomBattle) >= 0.5){
faction1hp -= faction2attack;
if(faction1hp <= 0){
faction1size -=1;
faction1hp = orghpfaction1;
}
}
}while(faction1size >= 1 && faction2size >=1);
}
答案 0 :(得分:0)
问题似乎是您使用StratergyRoll(RandomGenerator)
的方式:您必须明白每次调用它时,它都会返回另一个数字。
问题:
mt19937 RandomGenerator(time(NULL)); // ok ! You have a seeded random generator
uniform_int_distribution<int> StratergyRoll(1, 4); // and you set the bounds
...
if(StratergyRoll(RandomGenerator) == 1) { // Suppose result is 3
...
}
else if(StratergyRoll(RandomGenerator) == 2) { // Suppose result is 1 :OUCH !!!
...
}
...
因此,您不是获取一个随机数并对其值进行反应,而是计算1到4个随机数。因此,如果有一点点误会,你的分支可能不像你想象的那样,并且不会在一轮中执行任何动作!
解决方案:
使用变量存储卷并对其进行分析:
...
int currentRoll = StratergyRoll(RandomGenerator); // Store it
if( currentRoll == 1) { // Suppose result is 3
...
}
else if(currentRoll == 2) { // Now it wont change and you're sure one of the branch will fire!
...
}
...
<强>说明:强>
重要提示:您在使用yourmove
和trapsuccess
时遇到了类似的问题。你需要以同样的方式解决这个问题。
重要编辑:您有两个if (yourmove...)
的陈述{
,而后续行的缩进表明它们属于同一个阻止(我在记下你的缩进时注意到它)。这会影响3个或更多的strategyRoll。
顺便说一句,您可以考虑在此使用if/else
语句,而不是使用switch
的长链。
函数EndStatment()
应返回其声明所承诺的int
。或者,如果您从未期望返回(这里似乎是这种情况),只需将其设为void
函数即可。