0

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");
 }
RoninDaMerc
  • 13
  • 1
  • 3
  • What "multiple ideas" have you tried? – paddy Oct 12 '16 at 02:04
  • Why to never use [**`using namespace std;`**](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [**`system();` functions**](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) – amanuel2 Oct 12 '16 at 02:28
  • I was taught in class to use these functions, I read through the linked and I never knew how inconvenient they were in the long run o-o @amanuel2. Edit: Thanks m8 – RoninDaMerc Oct 12 '16 at 03:12
  • @paddy I've tried manipulating the cards[x][y] 2D array in my first for loop in my begin() function, but to no prevail. – RoninDaMerc Oct 12 '16 at 03:12
  • @RoninDaMerc Have you tried [**`std::vector`**](http://en.cppreference.com/w/cpp/container/vector) ? :) . Very convenient in the long run , if you can use libraries that is! – amanuel2 Oct 12 '16 at 03:30
  • @amanuel2 yeah we're just getting to that part of the course actually, arrays as vectors are amazing in terms of simplicity B) I gotta turn this in tomorrow so I don't want to alter my code too much and end up in a worse position atm – RoninDaMerc Oct 12 '16 at 03:50
  • @RoninDaMerc Ahh i feel you. Well Good Luck. And you should go checkout the [**teenagers room**](http://chat.stackoverflow.com/rooms/85048/teenage-territory) ! – amanuel2 Oct 12 '16 at 03:54
  • @amanuel2 Yeahhh, sure it sounds pretty cool, ill bookmark it for later, thanks again! – RoninDaMerc Oct 12 '16 at 04:34
  • @RoninDaMerc seems you don't have the reputation to talk yet. We are pretty active there usually, not now though. But once you gain the reputation, you will be able to chat!! – amanuel2 Oct 12 '16 at 04:35
  • @RoninDaMerc - what do you mean you want to shift the position of the cards? I'm not aware of a memory game that does that. – MarcD Oct 12 '16 at 19:48

1 Answers1

1

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.

MarcD
  • 588
  • 3
  • 11
  • The OP Is asking why doesn't his answer work , not an alternative method to his question. Suggestions should always be left as a comment not an answer. – amanuel2 Oct 12 '16 at 03:32
  • Edited as per your comment. – MarcD Oct 12 '16 at 03:49
  • The OP Might want to look at the answer now! – amanuel2 Oct 12 '16 at 03:55
  • @MarcD I see what you're doing, the new array you added, its basically just grabbing from random cards from the deck (my original 2D array). I tried it out and it still didn't change the order of the chars. I feel like I can tweak with this but it'll have to wait till tomorrow, thx a ton, learned a lot from everyone here – RoninDaMerc Oct 12 '16 at 04:31