Pergunta

I have written a practice code for a 'battle' that allows you to select number of combatants, number of rounds and number of dice-rolls per fighter per round storing the result into a 3D vector array. The storage part is working; however, the printResult() function is botched (i have put a // before it in main() ) and the srand() isnt working either. The complete program is below for convenience:

#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;

}
Foi útil?

Solução

You have two problems you should tackle separately (and which you should have solved separately before you put them together into one code base). Loki Asteri has already addressed the problem with srand().

It looks as if round is a vector of Combatant, which is a vector of something else, but look here:

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';
            }
        }
    }
}

Outras dicas

Only call srand() once in an application.

int main()
{
    srand(time(0));
    // STUFF
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top