Game program help!!

Write a program to play the memory game. Use a two dimensional array of 4 rows and 4 columns to use a deck of 16 cards with 8 matching pairs and you can use numbers 1 to 8 to mark the cards. (If you use a 6 by 6 array, then you will need 18 matching pairs, and so on.) Use random number generators to randomly store the pairs in the array. Use appropriate functions in your program, and the main program should be merely a call to functions.
I am very stuck if anyone could help I would be grateful

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
#include <iostream>
#include <stdlib.h>
#include <windows.h>

using namespace std;

#define ROWS  4
#define COLS  4
#define CARDS (ROWS*COLS/2)
class MemoryGame{
    private:
        int array[ROWS][COLS];
        int hidden[ROWS][COLS];
        int pr, pc;
        int prev_card;
        int cards_revealed;
        int moves;

    public:
        MemoryGame(void){
            resetdeck();
        }
        //For shuffling the cards
        void resetdeck(void){
            int cnt[CARDS], card_no;
            for( int i=0; i< CARDS; i++ )
                cnt[i] = 0;

            for( int i=0; i< ROWS; i++ )
                for( int j=0; j< COLS; j++){
                    do{
                        card_no = rand() % CARDS+1;
                    }while ( cnt[card_no-1] == 2);

                    array[i][j] = card_no;
                    hidden[i][j] = 1;
                    cnt[card_no-1]++;
                }
            cards_revealed = 0;
            pr =0;
            pc =0;
            prev_card =0;
            moves =0;
        }

        int pickCard(int r, int c){
            if( r<0 | r>= ROWS | c<0 | c>=COLS){
                cout << "Index out of bounds"<<endl;
                Sleep(1000);
                return -1;
            }
            if( hidden[r][c] == 0){
                cout<< "Card already in revealed position"<<endl;
                Sleep(2000);
                return -2;
            }
            if( prev_card == 0 ){
                hidden[r][c] = 0;
                pr = r;
                pc = c;
                prev_card = array[pr][pc];
            }
            else if( array[r][c] == prev_card ){
                hidden[r][c] = 0;
                hidden[pr][pc] = 0;
                prev_card = 0;
                pr=0;
                pc=0;
                cards_revealed+=2;

                displaycards();
                cout<<"===>Matched";
                Sleep(2000);

            }
            else{
                hidden[r][c] = 0;
                displaycards();
                cout<<"===>Unmatched";
                Sleep(2000);
                hidden[r][c] = 1;
                hidden[pr][pc] = 1;
                prev_card =0;
                pr=0;
                pc=0;
            }
            moves++;
            if( cards_revealed == CARDS*2)
                return 1;
            else
                return 0;
        }
        void displaycards(void){
            for( int i=0; i< 50; i++)
                cout<<endl;
            for( int i=0; i<ROWS; i++){
                for( int j=0; j<COLS; j++)
                    if( hidden[i][j])
                        cout<< "*" << "\t";
                    else
                        cout << array[i][j] << "\t";
                cout<<endl;
            }
            cout<< "Cards Matched: "<<cards_revealed << " Moves: "<< moves<<endl;
         }
};

int main()
{
    MemoryGame* m = new MemoryGame();
    int exit = 0;
    int r, c, ret_code;
    while(!exit){
        m->displaycards();
        cout << "Enter row: ";
        cin >> r;
        cout<< "Enter Col:";
        cin >>c;

        ret_code = m->pickCard(r-1, c-1);
        if( ret_code == 1){
            cout<<"GAME_OVER";
            exit = 1;
        }
    };
}
It works fine for me. I had to comment out Windows.h and Sleep() and use an old POSIX sleep as I'm on a Linux distro. Then in line 47 I had to add a second | to each of yours as the OR is ||.

Here is the working code. Just comment out or remove the include of unistd. and usleep() and uncomment the windows.h and Sleep() calls then your code should work.

So what is it you are stuck on?

Working Screenshot: http://prntscr.com/2887wz
Beaten Screenshot: http://prntscr.com/288ber

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
#include <iostream>
#include <cstdlib>
#include <unistd.h>
//#include <windows.h>

using namespace std;

#define ROWS  4
#define COLS  4
#define CARDS (ROWS*COLS/2)
class MemoryGame{
    private:
        int array[ROWS][COLS];
        int hidden[ROWS][COLS];
        int pr, pc;
        int prev_card;
        int cards_revealed;
        int moves;

    public:
        MemoryGame(void){
            resetdeck();
        }
        //For shuffling the cards
        void resetdeck(void){
            int cnt[CARDS], card_no;
            for( int i=0; i< CARDS; i++ )
                cnt[i] = 0;

            for( int i=0; i< ROWS; i++ )
                for( int j=0; j< COLS; j++){
                    do{
                        card_no = rand() % CARDS+1;
                    }while ( cnt[card_no-1] == 2);

                    array[i][j] = card_no;
                    hidden[i][j] = 1;
                    cnt[card_no-1]++;
                }
            cards_revealed = 0;
            pr =0;
            pc =0;
            prev_card =0;
            moves =0;
        }

        int pickCard(int r, int c){
            if( r<0 || r>= ROWS || c<0 || c>=COLS){
                cout << "Index out of bounds"<<endl;
                //Sleep(1000);
                usleep(1000);
                return -1;
            }
            if( hidden[r][c] == 0){
                cout<< "Card already in revealed position"<<endl;
                //Sleep(2000);
                usleep(2000);
                return -2;
            }
            if( prev_card == 0 ){
                hidden[r][c] = 0;
                pr = r;
                pc = c;
                prev_card = array[pr][pc];
            }
            else if( array[r][c] == prev_card ){
                hidden[r][c] = 0;
                hidden[pr][pc] = 0;
                prev_card = 0;
                pr=0;
                pc=0;
                cards_revealed+=2;

                displaycards();
                cout<<"===>Matched";
                //Sleep(2000);
                usleep(2000);

            }
            else{
                hidden[r][c] = 0;
                displaycards();
                cout<<"===>Unmatched";
                //Sleep(2000);
                usleep(2000);
                hidden[r][c] = 1;
                hidden[pr][pc] = 1;
                prev_card =0;
                pr=0;
                pc=0;
            }
            moves++;
            if( cards_revealed == CARDS*2)
                return 1;
            else
                return 0;
        }
        void displaycards(void){
            for( int i=0; i< 50; i++)
                cout<<endl;
            for( int i=0; i<ROWS; i++){
                for( int j=0; j<COLS; j++)
                    if( hidden[i][j])
                        cout<< "*" << "\t";
                    else
                        cout << array[i][j] << "\t";
                cout<<endl;
            }
            cout<< "Cards Matched: "<<cards_revealed << " Moves: "<< moves<<endl;
         }
};

int main()
{
    MemoryGame* m = new MemoryGame();
    int exit = 0;
    int r, c, ret_code;
    while(!exit){
        m->displaycards();
        cout << "Enter row: ";
        cin >> r;
        cout<< "Enter Col:";
        cin >>c;

        ret_code = m->pickCard(r-1, c-1);
        if( ret_code == 1){
            cout<<"GAME_OVER";
            exit = 1;
        }
    };
}
Last edited on by closed account z6A9GNh0
thank you very much your a life saver
Topic archived. No new replies allowed.