6x6 pelmanism game showing symbols

Hello, I am a beginner in cpp and I am currently working on a card matching game.

The class function for 3x3 works fine and I wanted to replicate the code and modify it for 6x6. However when playing, some of the cards turn into symbols and letters instead of showing numbers such as 10,11,12 etc...

Below is the class for the 6x6



class memoryGame6
{
private:
int cards[6][6];
char face[6][6];
int pairs[36] = {1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18};


public:
memoryGame6()
{
// Initialization of array with through constructor
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 6; j++)
{
cards[i][j] = 0;
face[i][j] = '*';
}
}
}

// Function to play game
void play()
{
shuffle();

while(1)
{
// Game terminating after completing the game successfully
if(win())
{
table();
exit(1);
}

// Keep playing
else
{
table();
pick();
}
}

}

// For displaying shuffled cards (faced up and faced down)
void table()
{
cout << endl;
for(int i = 0; i < 6; i++)
{
cout << " ";
for(int j = 0; j < 6; j++)
{
cout << face[i][j] << " ";
}
cout << endl;
}
}

// Function for random shuffle of cards
void shuffle()
{
int x = -1;
int y = -1;
int z = 0;
srand(time(0));

// Run until all cards are shuffle
while((cards[0][0] == 0) || (cards[0][1] == 0) || (cards[0][2] == 0) || (cards[0][3] == 0) || (cards[0][4] == 0) || (cards[0][5] == 0) ||
(cards[1][0] == 0) || (cards[1][1] == 0) || (cards[1][2] == 0) || (cards[1][3] == 0) || (cards[1][4] == 0) || (cards[1][5] == 0) ||
(cards[2][0] == 0) || (cards[2][1] == 0) || (cards[2][2] == 0) || (cards[2][3] == 0) || (cards[2][4] == 0) || (cards[2][5] == 0) ||
(cards[3][0] == 0) || (cards[3][1] == 0) || (cards[3][2] == 0) || (cards[3][3] == 0) || (cards[3][4] == 0) || (cards[3][5] == 0) ||
(cards[4][0] == 0) || (cards[4][1] == 0) || (cards[4][2] == 0) ||(cards[4][3] == 0) || (cards[4][4] == 0) || (cards[4][5] == 0) ||
(cards[5][0] == 0) || (cards[5][1] == 0) || (cards[5][2] == 0) ||(cards[5][3] == 0) || (cards[5][4] == 0) || (cards[5][5] == 0))
{
// Random pick of array index
x = (rand() % 6);
y = (rand() % 6);

// Checking if array index is empty or not
if(cards[x][y] == 0)
{
cards[x][y] = pairs[z];
z++;
}
}
}

// Function to let player pick cards through index position
void pick()
{
bool picked1, picked2;
int x1, x2, y1, y2;

picked1 = picked2 = false;
x1 = x2 = y1 = y2 = -1;

while((picked1 == false) || (picked2 == false))
{
// First Pick
if(picked1 == false)
{
cout << "\nEnter the row (1 to 6) and col (1 to 6) position of the pair: \n" ;
cin >> x1 >> y1;

// Changing input from 1-4 to 0-3 for array
x1--;
y1--;

// Checking invalid position
if( (x1 < 0 || x1 > 5) || (y1 < 0 || y1 > 5))
{
cout << "\nInvalid position.\n";
continue;
}

// Checking if already face up or not
else if (face[x1][y1] != '*')
{
cout << "\nCard at this position already faced up. Select option again.\n";
continue;
}
// Turning picked card face up
else
{
face[x1][y1] = cards[x1][y1] + '0';
picked1 = true;
table();
}
}

// Second Pick
else
{
cout << "\nEnter the row (1 to 6) and col (1 to 6) position of the pair: \n" ;
cin >> x2 >> y2;

// Changing input from 1-4 to 0-3 for array
x2--;
y2--;

// User input error
if(x2 == -1 || y2 == -1)
{
cout << "\nBoth number must be entered.\nPlease try again.\n";
continue;
}

// Checking invalid position
else if( (x2 < 0 || x2 > 5) || (y2 < 0 || y2 > 5))
{
cout << "\nInvalid position.\n";
continue;
}

// Checking if already face up or not
else if (face[x2][y2] != '*')
{
cout << "\nCard at this position already faced up. Select option again.\n";
continue;
}
// Turning picked card face up
else
{
face[x2][y2] = cards[x2][y2] + '0';
picked2 = true;
table();
}
}

}

// Checking either pair matched or not
if(face[x1][y1] == face[x2][y2])
{
cout << "\nPair matched.\n";
}
else
{
cout << "\nPair do not match. Select again!\n";
face[x1][y1] = face[x2][y2] = '*';
}
}

// Checking either all cards are shuffled or not
bool win()
{
if((face[0][0] == '*') || (face[0][1] == '*') || (face[0][2] == '*') || (face[0][3] == '*') || (face[0][4] == '*') || (face[0][5] == '*') ||
(face[1][0] == '*') || (face[1][1] == '*') || (face[1][2] == '*') || (face[1][3] == '*') || (face[1][4] == '*') || (face[1][5] == '*') ||
(face[2][0] == '*') || (face[2][1] == '*') || (face[2][2] == '*') || (face[2][3] == '*') || (face[2][4] == '*') || (face[2][5] == '*') ||
(face[3][0] == '*') || (face[3][1] == '*') || (face[3][2] == '*') || (face[3][3] == '*') || (face[2][4] == '*') || (face[3][5] == '*') || (face[4][0] == '*') || (face[4][1] == '*') || (face[4][2] == '*') || (face[4][3] == '*') || (face[4][4] == '*') || (face[4][5] == '*')|| (face[5][0] == '*') || (face[5][1] == '*') || (face[5][2] == '*') || (face[5][3] == '*') || (face[5][4] == '*') || (face[5][5] == '*'))

{
return false;
}
else
{
return true;
}
}
};
1. Use code tags when posting code.
https://www.cplusplus.com/articles/jEywvCM9/
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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class memoryGame6 {
private:
  int cards[6][6];
  char face[6][6];
  int pairs[36] =
      { 1, 1, 2, 2, 3, 3, 
        4, 4, 5, 5, 6, 6, 
        7, 7, 8, 8, 9, 9, 
        10, 10, 11, 11, 12, 12, 
        13, 13, 14, 14, 15, 15, 
        16, 16, 17, 17, 18, 18 };

public:
  memoryGame6() {
// Initialization of array with through constructor
    for (int i = 0; i < 6; i++) {
      for (int j = 0; j < 6; j++) {
        cards[i][j] = 0;
        face[i][j] = '*';
      }
    }
  }

// Function to play game
  void play() {
    shuffle();

    while (1) {
// Game terminating after completing the game successfully
      if (win()) {
        table();
        exit(1);
      }
// Keep playing
      else {
        table();
        pick();
      }
    }
  }

// For displaying shuffled cards (faced up and faced down)
  void table() {
    cout << endl;
    for (int i = 0; i < 6; i++) {
      cout << " ";
      for (int j = 0; j < 6; j++) {
        cout << face[i][j] << " ";
      }
      cout << endl;
    }
  }

// Function for random shuffle of cards
  void shuffle() {
    int x = -1;
    int y = -1;
    int z = 0;
    srand(time(0));

// Run until all cards are shuffle
    while ((cards[0][0] == 0) || (cards[0][1] == 0) || (cards[0][2] == 0) || (cards[0][3] == 0) || (cards[0][4] == 0)
           || (cards[0][5] == 0) || (cards[1][0] == 0) || (cards[1][1] == 0) || (cards[1][2] == 0) || (cards[1][3] == 0)
           || (cards[1][4] == 0) || (cards[1][5] == 0) || (cards[2][0] == 0) || (cards[2][1] == 0) || (cards[2][2] == 0)
           || (cards[2][3] == 0) || (cards[2][4] == 0) || (cards[2][5] == 0) || (cards[3][0] == 0) || (cards[3][1] == 0)
           || (cards[3][2] == 0) || (cards[3][3] == 0) || (cards[3][4] == 0) || (cards[3][5] == 0) || (cards[4][0] == 0)
           || (cards[4][1] == 0) || (cards[4][2] == 0) || (cards[4][3] == 0) || (cards[4][4] == 0) || (cards[4][5] == 0)
           || (cards[5][0] == 0) || (cards[5][1] == 0) || (cards[5][2] == 0) || (cards[5][3] == 0) || (cards[5][4] == 0)
           || (cards[5][5] == 0)) {
// Random pick of array index
      x = (rand() % 6);
      y = (rand() % 6);

// Checking if array index is empty or not
      if (cards[x][y] == 0) {
        cards[x][y] = pairs[z];
        z++;
      }
    }
  }

// Function to let player pick cards through index position
  void pick() {
    bool picked1, picked2;
    int x1, x2, y1, y2;

    picked1 = picked2 = false;
    x1 = x2 = y1 = y2 = -1;

    while ((picked1 == false) || (picked2 == false)) {
// First Pick
      if (picked1 == false) {
        cout << "\nEnter the row (1 to 6) and col (1 to 6) position of the pair: \n";
        cin >> x1 >> y1;

// Changing input from 1-4 to 0-3 for array
        x1--;
        y1--;

// Checking invalid position
        if ((x1 < 0 || x1 > 5) || (y1 < 0 || y1 > 5)) {
          cout << "\nInvalid position.\n";
          continue;
        }
// Checking if already face up or not
        else if (face[x1][y1] != '*') {
          cout << "\nCard at this position already faced up. Select option again.\n";
          continue;
        }
// Turning picked card face up
        else {
          face[x1][y1] = cards[x1][y1] + '0';
          picked1 = true;
          table();
        }
      }
// Second Pick
      else {
        cout << "\nEnter the row (1 to 6) and col (1 to 6) position of the pair: \n";
        cin >> x2 >> y2;

// Changing input from 1-4 to 0-3 for array
        x2--;
        y2--;

// User input error
        if (x2 == -1 || y2 == -1) {
          cout << "\nBoth number must be entered.\nPlease try again.\n";
          continue;
        }
// Checking invalid position
        else if ((x2 < 0 || x2 > 5) || (y2 < 0 || y2 > 5)) {
          cout << "\nInvalid position.\n";
          continue;
        }
// Checking if already face up or not
        else if (face[x2][y2] != '*') {
          cout << "\nCard at this position already faced up. Select option again.\n";
          continue;
        }
// Turning picked card face up
        else {
          face[x2][y2] = cards[x2][y2] + '0';
          picked2 = true;
          table();
        }
      }

    }

// Checking either pair matched or not
    if (face[x1][y1] == face[x2][y2]) {
      cout << "\nPair matched.\n";
    } else {
      cout << "\nPair do not match. Select again!\n";
      face[x1][y1] = face[x2][y2] = '*';
    }
  }

// Checking either all cards are shuffled or not
  bool win() {
    if ((face[0][0] == '*') || (face[0][1] == '*') || (face[0][2] == '*') || (face[0][3] == '*') || (face[0][4] == '*')
        || (face[0][5] == '*') || (face[1][0] == '*') || (face[1][1] == '*') || (face[1][2] == '*')
        || (face[1][3] == '*') || (face[1][4] == '*') || (face[1][5] == '*') || (face[2][0] == '*')
        || (face[2][1] == '*') || (face[2][2] == '*') || (face[2][3] == '*') || (face[2][4] == '*')
        || (face[2][5] == '*') || (face[3][0] == '*') || (face[3][1] == '*') || (face[3][2] == '*')
        || (face[3][3] == '*') || (face[2][4] == '*') || (face[3][5] == '*') || (face[4][0] == '*')
        || (face[4][1] == '*') || (face[4][2] == '*') || (face[4][3] == '*') || (face[4][4] == '*')
        || (face[4][5] == '*') || (face[5][0] == '*') || (face[5][1] == '*') || (face[5][2] == '*')
        || (face[5][3] == '*') || (face[5][4] == '*') || (face[5][5] == '*')) {
      return false;
    } else {
      return true;
    }
  }
};


2. At least add a minimal main to test it with, and specify what input(s) cause it to break.
https://www.sscce.org/

> int cards[6][6];
You should have
const int SIZE = 6;
and replace all those magic 5 and 6 you have scattered through the code with a meaningful constant.
For one thing, if you forgot or missed one of your "3 to 6" edits, you could easily have array overrun issues.

> // Run until all cards are shuffle
> // Checking either all cards are shuffled or not
This mass of copy/paste logic should be replaced with pairs of nested loops.
3x3 is 9 checks
6x6 is 36 checks
If it gets any bigger, you'll be editing forever.


void pick() does way too much work for it's own good.
Simplify into more discrete steps. pick1 and pick2 are essentially duplicated copy/paste code.

If you're copy/pasting code, then you should be pasting that code into a new function, then making the things you planned to edit into function parameters.





some of the cards turn into symbols and letters


face[x2][y2] = cards[x2][y2] + '0';

This will only work if cards[x2][y2] has a value 0 to 9. If it has a higher value (eg 11) then it won't.

If the value of cards[x2][y2] is in the range 11 - 18, what character do you want displayed? As face is only of type char - it can only hold a single char so not 10, 11 etc. What about letters? You an do this:

 
face[x2][y2] = "0123456789ABCDEFGHIJ"[cars[x2][y2]];


shuffle() can be simplified. Something like (not tried):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Function for random shuffle of cards
    void shuffle() {
        int required {6 * 6};

        // Run until all cards are shuffle
        while (required) {
            // Random pick of array index
            const auto x {rand() % 6};
            const auto y {rand() % 6};

            // Checking if array index is empty or not
            if (cards[x][y] == 0)
                cards[x][y] = pairs[--required];
        }
    }


pick1 and pick2 code is essentially the same. This could be put into a private member function and called twice.

Also win can be simplified. Possibly something like:

1
2
3
4
5
6
7
8
9
    // Checking either all cards are shuffled or not
    bool win() {
        for (int i = 0; i < 6; ++i)
            for (int j = 0; j < 6; ++j)
                if (face[i][j] == '*')
                    return false;

        return true;
    }


Also note that srand() would usually go at the beginning of main to seed the random number generator - not in shuffle.
Possibly something like this [NOT tried]:

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

constexpr int BoardSize {6};

class memoryGame6 {
private:
    int cards[BoardSize][BoardSize] {};
    char face[BoardSize][BoardSize] {};
    int pairs[BoardSize * BoardSize] {1, 1, 2, 2, 3, 3,
      4, 4, 5, 5, 6, 6,
      7, 7, 8, 8, 9, 9,
      10, 10, 11, 11, 12, 12,
      13, 13, 14, 14, 15, 15,
      16, 16, 17, 17, 18, 18};

    void dopick(int& x, int&y) {
        bool bad {};

        do {
            bad = false;
            std::cout << "\nEnter the row (1 to " << BoardSize << ") and col (1 to " << BoardSize << ") position of the pair : ";
            std::cin >> x >> y;

            // Changing input from 1-4 to 0-3 for array
            --x;
            --y;

            // Checking invalid position
            bad = (x < 0 || x >= BoardSize || y < 0 || y >= BoardSize) && (std::cout << "\nInvalid position.\n");

            if (!bad)
                bad = face[x][y] != '*' && (std::cout << "\nCard at this position already faced up. Select option again.\n");
        } while (bad);

        face[x][y] = "0123456789ABCDEFGHIJ"[cards[x][y]];
        table();
    }

    // For displaying shuffled cards (faced up and faced down)
    void table() {
        std::cout << '\n';

        for (int i = 0; i < BoardSize; i++) {
            for (int j = 0; j < BoardSize; j++)
                std::cout << face[i][j] << " ";

            std::cout << '\n';
        }
    }

    void shuffle() {
        int required {BoardSize * BoardSize};

        // Run until all cards are shuffle
        while (required) {
            // Random pick of array index
            const auto x {rand() % BoardSize};
            const auto y {rand() % BoardSize};

            // Checking if array index is empty or not
            if (cards[x][y] == 0)
                cards[x][y] = pairs[--required];
        }
    }

    // Function to let player pick cards through index position
    void pick() {
        int x1 {}, x2 {}, y1 {}, y2 {};

        dopick(x1, y1);
        dopick(x2, y2);

        // Checking either pair matched or not
        if (face[x1][y1] == face[x2][y2])
            std::cout << "\nPair matched.\n";
        else {
            std::cout << "\nPair do not match. Select again!\n";
            face[x1][y1] = face[x2][y2] = '*';
        }
    }

    // Checking either all cards are shuffled or not
    bool win() {
        for (int i = 0; i < BoardSize; ++i)
            for (int j = 0; j < BoardSize; ++j)
                if (face[i][j] == '*')
                    return false;

        return true;
    }

public:
    memoryGame6() {
        // Initialization of array with through constructor
        for (int i = 0; i < BoardSize; i++)
            for (int j = 0; j < BoardSize; j++)
                face[i][j] = '*';
    }

    // Function to play game
    void play() {
        shuffle();

        while (!win()) {
            table();
            pick();
        }
    }
};

int main() {
    srand(static_cast<unsigned>(time(nullptr)));

    memoryGame6 mg6;

    mg6.play();
}

Using std::shuffle then possibly:

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
#include <iostream>
#include <string>
#include <random>
#include <algorithm>
#include <cstring>

constexpr unsigned BoardSize {6};
constexpr unsigned NoElems {BoardSize * BoardSize};
constexpr char display[] {"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"};

static_assert(NoElems / 2 < sizeof(display));  // Max allowed for char display

class memoryGame6 {
private:
    unsigned cards[BoardSize][BoardSize] {};
    char face[BoardSize][BoardSize] {};

    void dopick(unsigned& x, unsigned& y) {
        bool bad {};

        do {
            bad = false;
            std::cout << "\nEnter the row (1 to " << BoardSize << ") and col (1 to " << BoardSize << ") position of the pair : ";
            std::cin >> x >> y;

            // Checking invalid position
            bad = (x < 1 || x > BoardSize || y < 1 || y > BoardSize) && (std::cout << "\nInvalid position.\n");
            if (!bad)
                bad = face[--x][--y] != '*' && (std::cout << "\nCard at this position already faced up. Select option again.\n");
        } while (bad);

        face[x][y] = display[cards[x][y]];
        table();
    }

    // For displaying shuffled cards (faced up and faced down)
    void table() {
        std::cout << '\n';

        for (unsigned i = 0; i < BoardSize; ++i) {
            for (unsigned j = 0; j < BoardSize; ++j)
                std::cout << face[i][j] << " ";

            std::cout << '\n';
        }
    }

    void shuffle() {
        for (unsigned i = 0; i < NoElems; ++i)
            (reinterpret_cast<int*>(cards))[i] = (i + 2) / 2;

        std::shuffle(reinterpret_cast<int*>(cards), reinterpret_cast<int*>(cards) + NoElems, std::default_random_engine(std::random_device {}()));
    }

    // Function to let player pick cards through index position
    void pick() {
        unsigned x1 {}, x2 {}, y1 {}, y2 {};

        dopick(x1, y1);
        dopick(x2, y2);

        // Checking either pair matched or not
        if (face[x1][y1] == face[x2][y2])
            std::cout << "\nPair matched.\n";
        else {
            std::cout << "\nPair do not match. Select again!\n";
            face[x1][y1] = face[x2][y2] = '*';
        }
    }

    // Checking either all cards are shuffled or not
    bool win() {
        return std::count(reinterpret_cast<char*>(face), reinterpret_cast<char*>(face) + NoElems, '*') == 0;
    }

public:
    memoryGame6() {
        std::memset(reinterpret_cast<char*>(face), '*', NoElems);
    }

    void play() {
        shuffle();

        while (!win()) {
            table();
            pick();
        }
    }
};

int main() {
    memoryGame6 mg6;

    mg6.play();
}

Last edited on
Topic archived. No new replies allowed.