Working with multiple instances of a constructor (creating a game of dominos)

So im trying to create a game of dominos which includes generating 91 tiles and showing the longest chain of dominos that can be played. The tiles contain a number on each side which are between 0 and 12 using a random generator. This is the code i have for the constructor:

1
2
3
4
5
6
7
8
9
  Domino::Domino()
{
}
  Domino::Domino(int left, int right)
{
}
  Domino::~Domino()
{
}


This is what i have to create the tiles:

1
2
3
4
5
6
7
8
9
10
11
  void GamePlay::initialize()
{
  for(int i = 0; i < 91; i++)
  {
    int dLeft = rand() % 13;
    int dRight = rand() % 13;
    allDominos.push_back(Domino(dLeft, dRight));
  }

  this->maxLength = this->allDominos.size();
}


maxLength and allDominos are given in a header file. Would this successfully create all 91 tiles with correct values? Ill note that there are no copies of a tile, ie each tile is unique in the values that it has on it. And if this is correct, how can i refer to each tile created so that i can deal them to players and retrieve information about each?
Would this successfully create all 91 tiles with correct values?
No
how can i refer to each tile created so that i can deal them to players and retrieve information about each?
Instead of relying on randomness, just generate all combinations yourself:
1
2
3
4
for(int i = 0; i < 13; ++i)
    for(int j = 0; j <= i; ++j)
        allDominos.emplace_back(i, j);
maxLength = allDominos.size();
I have not used emplace_back before as i am new to c++. How does this differ from push_back?
It constructs element in place as opposed to copying (or moving) them. Sometimes it is more effective and often is less verbose. It is a C++11 feature, so if you might need to turn on compiler support for it. If you do not want to, replace it with push_back and explicit construction of temporary.
I tried emplace_back but it was giving my errors so i tried push_back and it worked fine. My next question might be less of a technical question but now that i have my tiles created, i need to "deal" 12 tiles to one player and "deal" another 12 to a second player. These player's hands will be another vector<Domino> so i know i could just push back the first 12 of allDomino to one hand and the second 12 in line to the second hand but this would take out a huge component of the game. This is what i have to deal to the first player, but how can make sure different tiles are dealt to the second player? (myDominos is a vector which will contain the first players tiles)

1
2
3
4
5
6
7
  const int handSize = 12;
  random_shuffle(allDominos.begin(), allDominos.end());
  //dealing first 12
  for(int i = 0; i <= handSize; i++)
  {
    myDominos.push_back(allDominos[0]);
  }
You can remove already taken elements from allDominos, or simply shuffle array once and then deal dominoes from different areas:
1
2
3
4
5
6
using iter = std::vector<Domino>::const_iterator
iter start = allDominos.begin();
iter middle = std::advance(start, 12);
iter end = std::advance(middle, 12);
myDominos.insert(myDominos.end(), start, middle);
hisDominos.insert(hisDominos.end(), middle, end);
Last edited on
Topic archived. No new replies allowed.