### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126`` ``````#include #include #include 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"<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<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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131`` ``````#include #include #include //#include 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"<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<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.