【问题标题】:Messed up srand() and vector::iterator搞砸了 srand() 和 vector::iterator
【发布时间】:2012-03-05 01:08:08
【问题描述】:

我为“战斗”编写了一个练习代码,允许您选择战斗人员的数量、回合数和每名战士每回合掷骰子的数量,并将结果存储到 3D 矢量数组中。存储部分工作;但是, printResult() 函数是拙劣的(我在 main() 之前放了一个 // )并且 srand() 也不起作用。为方便起见,完整的程序如下:

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <ctime>
#include <iterator>
#include <vector>
#include <algorithm>

using namespace std;

class Combat{
    private:
        int numberOfRounds;
        int numberOfCombatants;
        int numberOfRolls;
        int sidesDie;
        vector <int> rollz;
        vector <vector <int> >combatant;
        vector <vector <vector <int> > > round;
    public:
        void printMenu();
        void battle();
        void printResult();
        Combat(int, int , int, int );
        Combat(int );
        Combat();
        int roll();
        int roll(int die);
};
void Combat::printMenu()
{
    cout<<setw (10)<<"Welcome to the Combat Menu";
    cout<<'\n'<<setw (10)<<"Choose:";
    cout<<'\n'<<setw (10)<<"Number of combatants: ";
    cin>>numberOfCombatants;
    cout<<'\n'<<setw (10)<<"Die sides:";
    cin>>sidesDie;
    cout<<'\n'<<setw (10)<<"Enter number of rounds: ";
    cin>>numberOfRounds;
    cout<<setw(10)<<"Enter number of rolls (per combatant per round):";
    cin>>numberOfRolls;
}
Combat::Combat(){
    numberOfRounds=8;
}
Combat::Combat(int x){
    x=numberOfRounds;
}
Combat::Combat(int rnds,int cmb,int rll, int sides){
    numberOfRounds=rnds;
    numberOfCombatants=cmb;
    numberOfRolls=rll;
    sidesDie=sides;
}
int Combat::roll(int die)
{
    die=sidesDie;
    srand(time(0));
    int r=(1+rand()%die);
    return r;

}
int Combat::roll(){
    srand(time(0));
    int r=(1+rand()%6);
    return r;
  }
void Combat::battle(){
    cout<<setw(10)<<" Computing results of battle ...\n";
    int i,j,k;
    for (i=0;i<numberOfRounds;++i){
        cout<<"\nRound number "<<i+1;
        round.push_back(combatant);
        for(j=0;j<numberOfCombatants;++j){
            cout<<"\nCombatant number "<<j+1;
            combatant.push_back(rollz);
            cout<<endl;

            for(k=0;k<numberOfRolls;++k){
                rollz.push_back(roll(sidesDie));
                cout<<rollz.at(k)<<'\t';
            }
        }
        cout<<endl<<endl;
    }

    cout<<endl;
}

void Combat::printResult(){
    cout<<endl;
    vector< vector <vector<int> > >::const_iterator it1;
    int combt, counter=0;
    for (it1=round.begin();it1 !=round.end();++it1){
        ++counter;

        cout<<"\nRound number "<<counter<<endl;
        for(vector<vector<int> >::const_iterator it2=combatant.begin();it2!=combatant.end();++it2){
            ++combt;
            cout<<"\nCombatant "<<combt<<" hits ";
                  for(vector<int>::const_iterator it3=rollz.begin();it3!=rollz.end();++it3){
                cout<<*it3<<'\t';
            }
        }
    }
}

int main ()
{
    Combat fight;
    fight.printMenu();
    fight.battle();
    //fight.printResult();
    cout<<endl;

}

【问题讨论】:

  • “不工作”是一个很难开始的地方。你能定义它是如何不工作的吗?例外?编译器错误?因为你的 srand 在我看来很好......
  • 通过单次运行程序,所有骰子在整个矩阵中的数字都相同
  • 刚刚注意到您每次都在使用 rand() 之前重新播种,您只需要在程序的生命周期中播种一次 RNG。也许这就是问题所在。
  • 这已解决了问题的一部分,尽管现在所有回合中每个战斗员的所有 rollz 都会重复。啊!另外,你能帮我处理 printResult() 函数吗?那也搞砸了。谢谢
  • 解释 Corey 的评论:您应该调用一次 srand() - 可能在 main() 的顶部 - 而不是在每次调用 rand() 之前。 (随机数生成器并不是真正随机的——它遵循一个序列。如果你用相同的数字播种它,它将重新启动该序列......)

标签: c++ vector iterator srand


【解决方案1】:

您有两个问题应该分别解决(并且在将它们放在一个代码库中之前应该分别解决)。 Loki Asteri 已经通过srand() 解决了这个问题。

看起来roundCombatant 的向量,而Combatant 是其他东西的向量,但看这里:

void Combat::printResult(){
    ...
    for (it1=round.begin();it1 !=round.end();++it1){
        ++counter;
        cout<<"\nRound number "<<counter<<endl;
        // You never use counter or it1 again.
        // You iterate over the same combatant for each it1:
        for(vector<vector<int> >::const_iterator it2=combatant.begin();
            it2!=combatant.end();++it2){
            ++combt;
            cout<<"\nCombatant "<<combt<<" hits "; // bad variable name at best
            // And now you iterate over the same rollz 
            // for every entry in combatant.
            for(vector<int>::const_iterator it3=rollz.begin();
                it3!=rollz.end();++it3){
                cout<<*it3<<'\t';
            }
        }
    }
}

【讨论】:

  • 是的,它搞砸了,我不知道如何编码 :) 试图使用 this-> 来获得正确的结果。没有把握。谢谢你们的帮助
【解决方案2】:

在应用程序中只调用一次 srand()。

int main()
{
    srand(time(0));
    // STUFF
}

【讨论】:

  • 完成了,只随机化一个 rollz 实例,然后重复。 (您可以自己检查此 prgrm)第二个问题是 printResult() 函数只输出大量的 '1',所以我已将其注释掉,直到我修复了 die-randomization 错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多