0

我已经为“战斗”编写了一个练习代码,它允许您选择战斗人员的数量、回合数和每个战士每回合掷骰子的数量,并将结果存储到 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;

}
4

2 回答 2

0

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

int main()
{
    srand(time(0));
    // STUFF
}
于 2012-03-05T01:29:56.707 回答
0

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

看起来好像round是 的向量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';
            }
        }
    }
}
于 2012-03-05T01:38:51.057 回答