So i am writing a program that plays the old memory card game (ie you have a 4x4 block of playing cards, flip 2, if they match they stay up, if not they flip back over).

I am having trouble generating the random pairs of cards (i got the 4x4 working and the choosing and flipping of 2 cards working, something is broken after that but ill come to that later)

I realized that i was generating single numbers, not pairs in my current code. My idea for a fix is to take another array and give it the numbers 1 to 8 then iterate through it twice and put it in the card array. however im not sure how to either randomly iterate or randomly place the numbers in the card array.

some sudo code that i think might work but not sure how to check for empty spots

rowNum= rand(1,4)
colNum=rand(1,4)
array[rowNum][colNum] = digit

my current code (maybe you can figure out why it only goes through the guess once....)

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    char comma;
    int row1, column1, row2, column2, cards[4][4];

    srand((unsigned)time(NULL));
    
	//fill board



    for (int r=0; r<4; r++)
    {
        for (int c=0; c<4; c++)
        {
            cards[r][c]=rand()%8+1;
            cout<<cards[r][c];
        }
        cout<<endl;
    }
    //display board
    cout<<"    1 2 3 4\n";
    cout<<"  ";
    for (int i=0; i<=8; i++)
    {
        cout<<"-";
    }
    cout<<endl;
    for (int r=0; r<4; r++)
    {
        cout<<r+1<<" | ";
        for (int c=0; c<4; c++)
        {
            cout<<"* ";
        }
        cout<<endl;
    }
    cout<<endl;
    //selection
    cout<<"Please insert the first card row and column seperated by a comma.\n";
    cin>>row1>>comma>>column1;
    cout<<"Please insert the second card row and column seperated by a comma.\n";
    cin>>row2>>comma>>column2;
    //fix
    row1--;
    column1--;
    row2--;
    column2--;
    //reveal
    cout<<"    1 2 3 4\n";
    cout<<"  ";
    for (int i=0; i<=8; i++)
    {
        cout<<"-";
    }
    cout<<endl;
    for (int r=0; r<4; r++)
    {
        cout<<r+1<<" | ";
        for (int c=0; c<4; c++)
        {
            if ((r==row1)&&(c==column1))
            {
                cout<<cards[r][c]<<" ";
            }
            else if((r==row2)&&(c==column2))
            {
                cout<<cards[r][c]<<" ";
            }
            else
            {
                cout<<"* ";
            }
        }
        cout<<endl;
    }
    //match?
    if (cards[row1][column1]==cards[row2][column2])
    {
    }
    else
    {
    }
    //this pushes the next board onto a blank screen
    for (int b=0; b<=20; b++)
        cout<<endl;
    //repeat
    return 0;
}

I did post a solution in the other thread you started here.
As far as implementation goes I'm sure if you have seen any of my posts I am better off taking your code and putting it all together then commenting some changes rather than telling you where to put stuff.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.