# posting cards in memory game

I am making a memory game using opengl and c++. I am able to post playing cards to the screen. I am having a problem posting two identical cards randomly to the screen. I am able to post 10 cards to the screen but the second 10 cards are posted blank. As you can see from my code the vector a creates the sequence of 20 values which is 10 pairs that is outputted randomly.

void init()
{
// Step 1
int nPairs = 10;
int nCards = nPairs * 2;
vector<int> a;

// Step 2. creates the sequence 0 0 1 1 2 2 3 3 4 4 ...
for (int i = 0; i < nCards; i++)
{
a.push_back(i / 2);     // or v.push_back(i >> 1);
}

// Step 3
random_device rd;
mt19937 g(rd());
shuffle(a.begin(), a.end(), g);

// Step 4
for (int i = 0; i < nCards; i++)
{
cout << a[i] << " ";
}
}

• What exactly is the desired result? Do you want 20 different cards loaded into the texture array ,10 random pairs of cards or something else? I've pointed out why you're getting the error you see, but I can't provide recommendations on how to correct the problem without knowing what the expected result should be. Jan 12 at 19:36
• well I want to post 10 random cards in pairs like 2,1,2,3,5,3,4,5,0... overall I want to use 20 cards in 10 pairs Jan 12 at 19:52
• Okay, and what do you mean by a pair? When I've played memory, we considered "the 2 of clubs & the 2 of spades" to be a pair. Or do you want to duplicate cards (for example "the 2 of clubs & the 2 of clubs") to be considered a pair? Your example has a mix of red & black cards & not every club has a matching diamond, so it's not clear what your intent was. Jan 12 at 21:51
• Well I want to duplicate cards such as 2 of clubs paired with 2 of clubs considered as a pair. Jan 12 at 22:45
• ok, I think that gives me enough to provide an alternative solution. I should probably be able to post something by the end of tomorrow. 2 days ago

a[i] contains the values 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 in some random order.

So when you use that as the index into texture, you're only ever loading textures into the first 10 locations in the texture array.

To see this more clearly, comment out the code between step 3 & step 4 and then manually step through the code in step 4. What you have is:

texture[0] = loadTex("C:\\Users\\Owner\\Desktop\\2_of_clubs.png");
...


The array locations texture[10] to texture[19] never get initialized, which is presumably why the second half of the array gives blank cards.

Here's a possible alternative:

void init()
{
// Step 1, some initializations
int nPairs = 10;
int nCards = nPairs * 2;
vector<int> a;

// Step 2, creates the sequence 0 0 1 1 2 2 3 3 4 4 ... 8 8 9 9
for (int i = 0; i < nCards; i++)
{
a.push_back(i / 2);
}

// Step 3, shuffles a
random_device rd;
mt19937 g(rd());
shuffle(a.begin(), a.end(), g);

// step 4, initialize textures

// step 5, build selection of cards using randomized sequence
for(int i=0; i<nCards; i++)
{
card[i] = texture[a[i]];
}
}


This will fill the card array with a randomized collection of 10 different pairs of cards drawn from texture.
Note: wherever you declare texture, you'll need to make sure to also add the necessary code to declare the card array.

• what do you mean by step 3 and step 4? Jan 12 at 2:25
• I was referring to the comments in your code. // Step 3 etc. Jan 12 at 3:47