Memory Game

So i've been trying to piece together a memory game with the goal of using one array called symbols(ive changed the elements to integers instead) and one is a main blank one that i fill up with randomly placed index's of SYMBALS[].
First, i cant think of a way to only assign pairs of two of these elements to gameArray[][]and also using a struct to keep track of player guesses... I'm am starting to give up :( I've eventualy started using snipets of other ppls code to help me find that direction im missing but alas, i am lost. The goal is to be using functions the whole time and just pass everything around with the structs and arrays.
I understand goto's are bad but for now i just use it to help me test the first working portions of the program.
Some of my functions arnt in use, i plan on deleting or reforming those

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <cstdlib>  // for random numbers
#include <ctime>
using namespace std;
const int SIZE = 4;
const int SYM_SIZE = 16;

struct Position //Position of player to be passed everywhere
{
    int row, col;
    Position(int r = 0, int c = 0) : row(r), col(c) { }
};
Position playerMove;
Position playerMove2;
Position getPos1()
{
    cout << "Enter Row: ";
    cin >> playerMove.row;
    cout << "Enter Column: ";
    cin >> playerMove.col;
}
Position getPos2()
{
    cout << "Enter Row: ";
    cin >> playerMove2.row;
    cout << "Enter Column: ";
    cin >> playerMove2.col;
}
ostream& operator <<(ostream& os, const Position& pos);
void createSymArray(int myArray[][SIZE], int SYMBOLS[SYM_SIZE])
{
    int symCount = 0;
    int symIndex = 0;
    for (int i = 0; i < SIZE; i++)
    {
        //cout << SYMBOLS[i];
        for (int j = 0; j < SIZE; j++ )
        {
            Position RandIndex(rand() % 4, rand() % 4);   //Random index and Rand Index
            int randSmybolIndex = rand() % 9;
            //myArray[i][j] = rand()%8+1;
            myArray[RandIndex.row][RandIndex.col] = SYMBOLS[symIndex++];
            //*(myArray + i) = *(myArray + randNum); //Assign randNum to Array[i]
        }
    }


}
bool checkGame(int myArray[][SIZE], Position &playerMove1, Position &playerMove2)
{
    for (int r = 0; r < SIZE; r++)
    {
        for (int c = 0; c < SIZE; c++ )
        {
           if ((r == playerMove.row)&&(c == playerMove.col))
            {
                cout << myArray[r][c]<<" ";
            }
            else if((r == playerMove2.row)&&(playerMove2.col))
            {
                cout << myArray[r][c]<<" ";
            }
            else
            {
                cout<<"* ";
            }
        }
        cout<<endl;
    }
    cout << "You chose: " << myArray[playerMove1.row][playerMove1.col];
    if (myArray[playerMove1.row][playerMove1.col] == myArray[playerMove2.row][playerMove2.col])
    {
        return true;
    }
    else
    {
        return false;
    }
}


void displaySymbols(int myArray[][SIZE])
{
    for (int i = 0; i < SIZE; i++)
    {
        for(int j = 0; j < SIZE;j++)
        {
            cout << myArray[i][j] << " ";
        }
        cout << endl;
    }
}

void displayGame(int myArray[][SIZE])
{
    for (int r = 0; r < SIZE; r++)
    {
        for (int c = 0; c < SIZE; c++ )
        {
           if ((r == playerMove.row)&&(c == playerMove.col))
            {
                cout << myArray[r][c]<<" ";
            }
            else if((r == playerMove2.row)&&(playerMove2.col))
            {
                cout << myArray[r][c]<<" ";
            }
            else
            {
                cout<<"* ";
            }
        }
        cout<<endl;
    }
}

int main()
{
    srand((unsigned)time(NULL)); //Seed
    int gameArray[SIZE][SIZE] = {};
    int SYMBOLS[SYM_SIZE] = {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8};
    createSymArray(gameArray, SYMBOLS);
    //createGameArray(gameArray);
    cout << "Let's begin!" << endl;
    //displayGame(gameArray);
    displaySymbols(gameArray);
    cout << "Now for the game board" << endl;
    loop:
    displayGame(gameArray);
    Position getMove1 = getPos1();
    Position getMove2 = getPos2();
    bool check = checkGame(gameArray, getMove1, getMove2);
    if(check == true)
    {
        cout << "Good game!";
        return 0;
    }
    else
    {
        cout << "Try again!" << endl;
        goto loop;
    }
    return 0;
}

Last edited on
Lunchbox, there're no questions in your post.
What do you need help about?

Please, describe your problem (not your code).
Is it possible you want is to display a 16 x 16 (or a 4 x 4 ?) grid where there are two symbols which represent two players?

Try to ask a clear question and you're likely to get good answers.

Happy coding!


Hello Lunchbox,

Enoizat makes a good point there is not really a clear question here.

If I understand what yo are trying to do then the function "createSymArray" is all wrong. As you have it you can create a row and column that has already been used. Before you store a number in "myArray" you need to check that the row and column has not been used yet.

I do not know why you felt the need to create an object of the struct here, but it is not needed and you are making more work for yourself. After you check to see if the row and column you have randomly generated is able to be used this is all you need: myArray[row][col] = SYMBOLS[symIndex++];.

The rest of the program appears to work OK.

To avoid the use of goto in "main" you could do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool play{ true };

do
{
	displayGame(gameArray);

	Position getMove1 = getPos1();
	Position getMove2 = getPos2();

	if (checkGame(gameArray, getMove1, getMove2))
	{
		cout << "Good game!";
		play = false;
	}
	else
		cout << "Try again!" << endl;

} while (play);

return 0;

I changed the variable "check" to "play" because "check" is not needed and "play" is a better word for the while condition. You can change the variable name if you like.

By now you should know that what is between the () of an if statement needs to evaluate to true. Since the function returns a bool variable this works because the function is done first and then the return value is evaluated. This is a way to shorten and make better use of your code.

Also notice the the else statement has no {}s. For a single line of code they are not needed. Only when there are two or more lines of code are the {}s used.

Some tips you might find useful:

For an if statement, for loop or while with a single line of code the {}s are not necessary, but OK if you use them.

You and I know that arrays start at zero or zero zero for a 2D array, but the average person is more likely to start at 1 not zero. Your code needs to compensate for this and it would be a good idea to check that the row and column does not go less than zero or greater then the max element of the array. I found that the functions "getPos1" and "getPos2" the best place to make a change to what is entered.

When defining a variable such as: int gameArray[SIZE][SIZE] = {}; from C++11 on the "=" is not necessary just the {}s. Empty {}s will initialize to zero and for an array the empty {}s will initializa all elements to zero.

The functions "checkGame", "displaySymbols" and "displayGame" appear to be working, but without some idea of what it should look like I can not say if they are all working correctly.

Hope that helps,

Andy
Hello folks, thanks for replying so soon! I webt to bed immediately after posting at 3am but woke up with my answer! Of course i did not make my question clear, i was so tired im sorry, but i wanted to get my project on a working structure, a base to work off. Meaning i wanted to at least get the hidden array"*" to display the correct entered index's then decide whether or not these index's are matches...

Before---- If i typed row:3 col:1 and for playerMove2 row:2 col:2 id get this returned....
* * * *
* * 8 2
* * * 7
* 0 * * Random index's are being exposed here, instead of what i chose.

But my fix's output this now

Let's begin!
5 2 4 6
2 0 5 7
1 0 4 0
8 7 0 8
Now for the game board
* * * *
* * * *
* * * *
* * * *

Enter Row: 1
Enter Column: 2
Enter Row 2: 3
Enter Column 2: 1
* * * *
* * 5 *
* * * *
* 7 * *
-------I could not get this to print out right before... It would unveild random index's instead of what the player chose.

To get this i changed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void displayGame(int myArray[][SIZE])
{
    for (int r = 0; r < SIZE; r++)
    {
        for (int c = 0; c < SIZE; c++ )
        {
           if ((r == playerMove.row)&&(c == playerMove.col))
            {
                cout << myArray[r][c]<<" ";
            }
            else if((r == playerMove2.row)&&(playerMove2.col))
            {
                cout << myArray[r][c]<<" ";
            }
            else
            {
                cout<<"* ";
            }
        }
        cout<<endl;
    }
}

-------------------------to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for (int r = 0; r < SIZE; r++)
    {
        for (int c = 0; c < SIZE; c++ )
        {
            if ((r == playerMove.row) && (c == playerMove.col))
            {
                cout << myArray[r][c] << " ";
            }
            else if (( r == playerMove.row2)&&(c == playerMove.col2))
            {
                cout << myArray[r][c]<<" ";
            }
            else
            {
                cout<<"* ";
            }
        }
            cout << endl;
    }
            cout<<endl;
}

Then i deleted playerMove2 and added row2 and col2 to the struct.
I think because playerMove1 and playerMove2 shared the same row and col in the Position {} struct it was wigging out.
Before
1
2
3
4
5
struct Position //Position of player to be passed everywhere
{
    int row, col;
    Position(int r = 0, int c = 0) : row(r), col(c) { }
};

After---------------
1
2
3
4
5
struct Position //Position of player to be passed everywhere
{
    int row, col, row2, col2;
    Position(int r = 0, int c = 0) : row(r), col(c) { }
};

bool check was def a mess, now that i have the grid returning the moves accurately i will work on getting check to work right.

Thanks Andy for those tips, i forgot to work out boundary tracking, thats what ill be working on today as well to compensate.
Last edited on
Ok so here'es my new code with new problems... The problem is, after i get a pair of numbers right, it outputs the same numbers of rows and columns instead of taking those choices out of the game board(the array). Of course i have no code implemented to fix this.
Can anyone help me figure out how do delete elemnts from this 2day array? Or at least keep the MATCHED pairs flipped so no one can choose them. Also i need to figure out how to only output the integers that are in the array... I want only two of each integer(hens why i put 2 of the same number up to 8 in the array) to be on the options list...
Right now the options looks something like
0 4 0 8 --->See how there are 4 zeros... 0 isnt even in the SYMBOl array
5 0 6 7
2 3 0 8
3 0 6 7
Heres what it looks like...

0 1 2 3
---------
0 * * * *
1 * * * *
2 * * * *
3 * * * *
---------
Enter Row: 0
Enter Column: 1
Enter Row 2: 2
Enter Column 2: 1
At this point i want the zeros to be taken out of the array so i dont choose them again
* 0 * *
* * * *
* 0 * *
* * * *

So now i want it to display ...
You found a match!You have 1 points!
0 1 2 3
---------
0 * * *
1 * * * *-> The zeros have been taken out and it is visibily clear how many choices are left
2 * * *
3 * * * *
---------
Enter Row:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <cstdlib>  // for random numbers
#include <ctime>
using namespace std;
const int SIZE = 4;
const int SYM_SIZE = 16;

struct Position //Position of player to be passed everywhere
{
    int row, col, row2, col2;
    int points = 0;
    Position(int r = 0, int c = 0) : row(r), col(c) { }
};
struct IndexCount
{
    int count;
};
Position playerMove;
IndexCount COUNT;
IndexCount getCount()
{
    COUNT.count = 0;
}
Position getPos1()
{
    cout << "Enter Row: ";
    cin >> playerMove.row;
    cout << "Enter Column: ";
    cin >> playerMove.col;
}
Position getPos2()
{
    cout << "Enter Row 2: ";
    cin >> playerMove.row2;
    cout << "Enter Column 2: ";
    cin >> playerMove.col2;
}
ostream& operator <<(ostream& os, const Position& pos);
void createSymArray(int myArray[][SIZE], int SYMBOLS[SYM_SIZE])
{
    int symCount = 0;
    int symIndex = 0;
    for (int i = 0; i < SIZE; i++)
    {
        //cout << SYMBOLS[i];
        for (int j = 0; j < SIZE; j++ )
        {
            Position RandIndex(rand() % 4, rand() % 4);   //Random index and Rand Index
            int randSmybolIndex = rand() % 9;
            //myArray[i][j] = rand()%8+1;
            myArray[RandIndex.row][RandIndex.col] = SYMBOLS[symIndex++];
            //*(myArray + i) = *(myArray + randNum); //Assign randNum to Array[i]
        }
    }


}
bool checkGame(int myArray[][SIZE], Position &playerMove1, Position &playerMove2)
{
    for (int r = 0; r < SIZE; r++)
    {
        for (int c = 0; c < SIZE; c++ )
        {
           if ((r == playerMove.row)&&(c == playerMove.col))
            {
                cout << myArray[r][c]<<" ";
            }
            else if((r == playerMove.row2)&&(playerMove.col2))
            {
                cout << myArray[r][c]<<" ";
            }
            else
            {
                cout<<"* ";
            }
        }
        cout<<endl;
    }
    cout << "You chose: " << myArray[playerMove.row][playerMove.col];
    if (myArray[playerMove.row][playerMove.col] == myArray[playerMove.row2][playerMove.col2])
    {
        return true;
    }
    else
    {
        return false;
    }
}


void displaySymbols(int myArray[][SIZE])
{
    for (int i = 0; i < SIZE; i++)
    {
        for(int j = 0; j < SIZE;j++)
        {
            cout << myArray[i][j] << " ";
        }
        cout << endl;
    }
}

void displayGame(int myArray[][SIZE])
{
    cout << endl;
    cout<<"  0 1 2 3\n";
    cout<<"";
    for (int i = 0; i <= 8; i++)
    {
        cout << "-";
    }
    cout << endl;
    cout<<"0 * * * *\n1 * * * *\n2 * * * *\n3 * * * *\n";
    cout<<"";
    for (int i = 0; i <= 8; i++)
    {
        cout << "-";
    }

    cout<<endl;
    /**
    for (int r = 0; r < SIZE; r++)
    {
        for (int c = 0; c < SIZE; c++ )
        {
                cout<<"* ";
        }
        cout << endl;
        }
        cout<<endl;
        **/
}
void displayMove(int myArray[][SIZE], Position &playerMove1, Position &playerMove2)
{

    for (int r = 0; r < SIZE; r++)
    {
        for (int c = 0; c < SIZE; c++ )
        {
            if ((r == playerMove.row) && (c == playerMove.col))
            {
                cout << myArray[r][c] << " ";
            }
            else if (( r == playerMove.row2)&&(c == playerMove.col2))
            {
                cout << myArray[r][c]<<" ";
            }
            else
            {
                cout<<"* ";
            }
        }
            cout << endl;
    }
            cout<<endl;
}
bool checkMove(int myArray[][SIZE], Position &playerMove1, Position &playerMove2)
{
    if (myArray[playerMove.row][playerMove.col] == myArray[playerMove.row2][playerMove.col2])
    {
        return true;
    }
    return false;
}
bool GAMEOVER(int myArray[][SIZE], IndexCount &COUNT)
{
    COUNT.count++;
    int countsleft = 16 - COUNT.count;
    cout << "You have, " << countsleft << ", tries left." << endl;
    if (COUNT.count == 16)

        return false;
    else

        return true;
}

int main()
{
    srand((unsigned)time(NULL)); //Seed
    int gameArray[SIZE][SIZE] = {};
    int SYMBOLS[SYM_SIZE] = {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8};
    createSymArray(gameArray, SYMBOLS);
    //createGameArray(gameArray);
    cout << "Let's begin!" << endl;
    //displayGame(gameArray);
    displaySymbols(gameArray);
    cout << "Now for the game board" << endl;
    IndexCount newCount = getCount();
    loop:
    displayGame(gameArray);
    Position getMove1 = getPos1();
    Position getMove2 = getPos2();
    newCount;
    displayMove(gameArray, getMove1,getMove2);
    bool check = checkMove(gameArray, getMove1, getMove2);
    bool game = GAMEOVER(gameArray, newCount);
    if(check == true)
    {
        cout << "You found a match!";
        if (game == true)
        {
            playerMove.points++;
            cout << "You have " << playerMove.points << " points!";
            goto loop;
        }
    }
        else
        {
            cout << "No match!" << endl;
            if (game == false)
            {
                cout << "You are out of tries, you earned," << playerMove.points << endl;
            }
            else
            {
                goto loop;
            }
        }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.