0
\$\begingroup\$

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] << " ";
    }
        texture[a[0]] = loadTex("C:\\Users\\Owner\\Desktop\\2_of_clubs.png");
        texture[a[1]] = loadTex("C:\\Users\\Owner\\Desktop\\3_of_clubs.png");
        texture[a[2]] = loadTex("C:\\Users\\Owner\\Desktop\\4_of_clubs.png");
        texture[a[3]] = loadTex("C:\\Users\\Owner\\Desktop\\5_of_clubs.png");
        texture[a[4]] = loadTex("C:\\Users\\Owner\\Desktop\\6_of_clubs.png");
        texture[a[5]] = loadTex("C:\\Users\\Owner\\Desktop\\7_of_clubs.png");
        texture[a[6]] = loadTex("C:\\Users\\Owner\\Desktop\\8_of_clubs.png");
        texture[a[7]] = loadTex("C:\\Users\\Owner\\Desktop\\9_of_clubs.png");
        texture[a[8]] = loadTex("C:\\Users\\Owner\\Desktop\\10_of_clubs.png");
        texture[a[9]] = loadTex("C:\\Users\\Owner\\Desktop\\jack_of_clubs2.png");
        texture[a[10]] = loadTex("C:\\Users\\Owner\\Desktop\\queen_of_clubs2.png");
        texture[a[11]] = loadTex("C:\\Users\\Owner\\Desktop\\king_of_clubs2.png");
        texture[a[12]] = loadTex("C:\\Users\\Owner\\Desktop\\ace_of_clubs.png");
        texture[a[13]] = loadTex("C:\\Users\\Owner\\Desktop\\2_of_diamonds.png");
        texture[a[14]] = loadTex("C:\\Users\\Owner\\Desktop\\3_of_diamonds.png");
        texture[a[15]] = loadTex("C:\\Users\\Owner\\Desktop\\4_of_diamonds.png");
        texture[a[16]] = loadTex("C:\\Users\\Owner\\Desktop\\5_of_diamonds.png");
        texture[a[17]] = loadTex("C:\\Users\\Owner\\Desktop\\6_of_diamonds.png");
        texture[a[18]] = loadTex("C:\\Users\\Owner\\Desktop\\7_of_diamonds.png");
        texture[a[19]] = loadTex("C:\\Users\\Owner\\Desktop\\8_of_diamonds.png");
}
\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$
    – Pikalek
    Jan 12 at 19:36
  • \$\begingroup\$ 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 \$\endgroup\$
    – gamer67
    Jan 12 at 19:52
  • \$\begingroup\$ 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. \$\endgroup\$
    – Pikalek
    Jan 12 at 21:51
  • \$\begingroup\$ Well I want to duplicate cards such as 2 of clubs paired with 2 of clubs considered as a pair. \$\endgroup\$
    – gamer67
    Jan 12 at 22:45
  • \$\begingroup\$ 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. \$\endgroup\$
    – Pikalek
    2 days ago
1
\$\begingroup\$

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");
texture[0] = loadTex("C:\\Users\\Owner\\Desktop\\3_of_clubs.png");
texture[1] = loadTex("C:\\Users\\Owner\\Desktop\\4_of_clubs.png");
texture[1] = loadTex("C:\\Users\\Owner\\Desktop\\5_of_clubs.png");
...
texture[9] = loadTex("C:\\Users\\Owner\\Desktop\\7_of_diamonds.png");
texture[9] = loadTex("C:\\Users\\Owner\\Desktop\\8_of_diamonds.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
    texture[0] = loadTex("C:\\Users\\Owner\\Desktop\\2_of_clubs.png");
    texture[1] = loadTex("C:\\Users\\Owner\\Desktop\\3_of_clubs.png");
    texture[2] = loadTex("C:\\Users\\Owner\\Desktop\\4_of_clubs.png");
    texture[3] = loadTex("C:\\Users\\Owner\\Desktop\\5_of_clubs.png");
    texture[4] = loadTex("C:\\Users\\Owner\\Desktop\\6_of_clubs.png");
    texture[5] = loadTex("C:\\Users\\Owner\\Desktop\\7_of_clubs.png");
    texture[6] = loadTex("C:\\Users\\Owner\\Desktop\\8_of_clubs.png");
    texture[7] = loadTex("C:\\Users\\Owner\\Desktop\\9_of_clubs.png");
    texture[8] = loadTex("C:\\Users\\Owner\\Desktop\\10_of_clubs.png");
    texture[9] = loadTex("C:\\Users\\Owner\\Desktop\\jack_of_clubs2.png");
    
    // 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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .