samedi 23 novembre 2019

How to "store" a 3d vector [-array] if created/generated in a private class to access the same values again instead of always generating new values

I am "building" a program after a card game called "ligretto". It has 4 players, 4 different colors and 10 numbers (1-10) each and I want to put it in a vector instead of an array since i am learning vectors and classes rn and want to figure out how it all works.

I got the vector figured out: _card_deck[4][4][10] //4 players,4 colors and 10 numbers in random order.

The problem rn is, that every time I try to call the private member a new vector with new card numbers is generated every time _mix_cards() is called through getCards() and I figure it supposed to be like this...

...so my first Question is:

How do I "store" the values in a vector i generated over a private class call once and keep the values (cards) over the game until the deck has to be "shuffled" again in the next round - meaning I want to keep the values of the vector generated in _mix_cards accessible by public class members instead of getting generated all new and shuffled over and over again. Like is there a way to maybe use static_cast or const

I searched a lot but didn't found that much about 3 dimensional vectors at all and nothing about the usage of classes with vectors.

/############################/

My second question is about the warning: implicit conversion changes signedness: 'int' to 'std::vector::size_type' (aka 'unsigned long').

I reduced 12 warnings down to 4 by replacing int with size_t to let the compiler chose what is proper for the system it has to be compiled on and is used by the stl but 4 warnings of this type still remain at:

_card_deck[i][j].push_back( k ); //1 2 3 4 5 6 7 8 9 10
random_shuffle (_card_deck[i][j].begin(), _card_deck[i][j].end(), myrandom );

And I can't figure out how to "remove" the warnings for high quality code.

This is the code I wrote for now and compiles and prints out all values from my 3d vector and some string passing over to get printed via class function:

#include <iostream>     // cout
#include <algorithm>    // random_shuffle
#include <vector>       // vector
#include <ctime>        // time
#include <cstdlib>      // rand, srand>

using namespace std;


class deck
{
   string comment;

public:
   void output(string);
   size_t getCards(size_t playnum, size_t color, size_t number);

private:
   vector < vector < vector < size_t > > > _card_deck;           //assign 3d vector
   void _mix_cards();
};

size_t myrandom (size_t k) //random function for random_shuffle.mix_cards()
{
    return rand()%k;
}

void deck :: _mix_cards()
{
      srand( static_cast<unsigned int>(time(nullptr)));     //assign srand

      for(size_t i = 0; i < 4; i++)
      {
        vector < vector < size_t > > d1;
        _card_deck.push_back( d1 );
        for(int j = 0; j < 4; j++)
        {
          vector < size_t > d2;
          _card_deck[i].push_back( d2 );
          for(size_t k = 1; k < 11; k++)
          {
            _card_deck[i][j].push_back( k ); //1 2 3 4 5 6 7 8 9 10
            random_shuffle (_card_deck[i][j].begin(), _card_deck[i][j].end(), myrandom );
          }
        }
      }
        //display the full _card_deck
      for (size_t i = 0; i < _card_deck.size(); i++)
      {
        for (size_t j = 0; j < _card_deck[i].size(); j++)
        {
          for (size_t k = 0; k < _card_deck[i][j].size(); k++)
          {
            cout << "_card_deck[" << i << "][" << j << "][" << k << "] = " << _card_deck[i][j][k] << endl;
          }
        }
    }
}

size_t deck::getCards(size_t playnum, size_t color, size_t number)
{
    _mix_cards();
    return  _card_deck[playnum][color][number];
}

void deck::output(string comment)
{
    cout << comment  << " cards are used for " << endl;
}

int main()
{
    deck say,mix,show;
    say.output ("Welcome Player ... and Player ... ");  //text, number of players
    cout << show.getCards(0,2,2) << endl;

   return 0;
}

Thanks for reading and any help upfront. If you have any questions or tips regarding the code I am happy to learn and get better :)

Aucun commentaire:

Enregistrer un commentaire