RoninDaMerc
RoninDaMerc

Reputation: 13

Memory Match Game ++ How to randomize chars in an array

Hello I'm having issue with this assignment. My program is a Memory Match Game in C++, and it runs, however I need to get the chars in my 2D array to shift positions after every time the user inputs a valid or invalid response. They're staying at the same position as initialized. I've tried multiple ideas but have come to no conclusions. Even some hints that might help are appreciated.

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

class Game //Memory Match Game
{
private:
     char cards[4][4] = {   { '@', '#', '$', '%' },
                        {'^', '&', '*', '+'},
                        {'@', '#', '$', '%'},
                        {'^', '&', '*', '+' } };

char deck[16] = { '@','#', '$', '%', '^','&', '*', '+' }; //not being used atm
int r1, r2, c1, c2;
public:
    Game()
    {
        r1 = 0; r2 = 0; c1 = 0; c2 = 0;
    }

void begin()
{
    srand((unsigned)time(NULL));
    for (int x = 0; x < 4; x++) //begin designating cards 
    {
        for (int y = 0; y < 4; y++)
        {
            cards[rand() % x][rand() % y]; // <:::::[:::] FIX
            cout << " " << cards[x][y] << " ";
        }
        cout << endl;
    }
    cout << " \n    1 2 3 4\t\n";
    cout << "   ";
    for (int x = 0; x <= 8; x++)
    {
        cout << "_";
    }
    cout << endl;

    for (int x = 0; x < 4; x++) //appropriate positioning
    {
        cout << x + 1 << " | ";
        for (int y = 0; y < 4; y++)
        {
            cout << "? ";
        }
        cout << endl;
    }
    cout << endl;
    input();
}

void input()
{
    srand(time(0));
    cout << "Please type in the row and column of choice.\n"; //begin user play
    cin >> r1;
    cin >> c1;
    cout << "Please type in the row and column of choice\n";
    cin >> r2;
    cin >> c2;

    r1--; r2--; c1--; c2--;
    cout << "     1 2 3 4\n";
    cout << "   ";
    for (int x = 0; x <= 8; x++)
    {
        cout << "_";  
    }
    cout << endl;
    for (int x = 0; x < 4; x++)
    {
        cout << x + 1 << " | ";
        for (int y = 0; y < 4; y++)
        {
            if ((x == r1) && (y == c1))
            {
                cout << cards[x][y] << " ";
            }
            else if ((x == r2) && (y == c2))
            {
                cout << cards[x][y] << " ";
            }
            else cout << "? ";
        }
        cout << endl;
    }
    match();

}
  void match()
  {
      if (cards[r1][c1] == cards[r2][c2])
    {
        cout << "Valid Match!\n";
        input();
    }
     else
        cout << "Invalid Match!\n";
        input();

}

};

 int main()
{
     Game memoryMatch;
    memoryMatch.begin();

    system("pause");
 }

Upvotes: 0

Views: 2858

Answers (1)

MarcD
MarcD

Reputation: 618

One way to 'shuffle' the cards is to create a list with all the indexes of the array in it. Randomly select an item from the list and store it in a vector. Remove that item and repeat until the list is empty. You then have a vector of 'shuffled' indexes into the array. Don't modify the array directly but store a vector of indices into the array.

See std::list and std::vector, as well as the erase method.

https://www.sgi.com/tech/stl/List.html

https://www.sgi.com/tech/stl/Vector.html

As per the comment below, I gave you an alternative, and didn't explain why your code wasn't working. As to that, there are several reasons.

void begin()
{
    srand((unsigned)time(NULL));
    for (int x = 0; x < 4; x++) //begin designating cards 
    {
        for (int y = 0; y < 4; y++)
        {
            cards[rand() % x][rand() % y]; // <:::::[:::] FIX
            cout << " " << cards[x][y] << " ";
        }
        cout << endl;
    }

The main reason this doesn't work is because you are assigning to a random x and y element in the array. You might assign one slot one, multiple times, or not at all. What you need to do is assign a random item to each element in the array.

Something more like this would be closer to what you want :

char shuffled_cards[4][4];
for (int y = 0 ; y < 4 ; ++y) {
    for (int x = 0 ; x < 4 ; ++x) {
        shuffled_cards[y][x] = cards[rand()%4][rand()%4];
    }
}

The main thing to grasp is that you should work on another set of cards, in this case shuffled_cards and assign them a random card from your set of cards.

This is where the shuffling idea I posted to begin with comes in.

Upvotes: 1

Related Questions