HI members;
This is a memory game with c++ concole application.
This is the code:

#include<iostream.h>			//MEMORY GAME.	
#include <algorithm>			//DESIGNED BY GLADIATOR1919.	
#include<ctype.h>				//GLADIATOR1919@GMAIL.COM.
#include<time.h>				//ALL RIGHTS RESERVED.
#include<windows.h>
int e,i,j,q,m,n,x=0,row_1,column_1,row_2,column_2,score=0;
char rand_1[16]={'a','g','h','j','k','a','s','d','f','g','s','d','f','h','j','k'};
struct mem_char
{
char ch[5][5];
};
	char re_play;
mem_char char_1[2];
void end_game();
void input_first_point();
void input_second_point();
void delay(long seconds) ;
void gotoxy(int x, int y);
void score_1();
void new_line(int y);
void output_1();
void num_1();
void num_2();
void main_1();
void intro();
void slide_horz_right( int x ,int y , char ch[]);
void sleep(unsigned int mseconds);
/***********************************************************************************************/
void main()
{
	srand((unsigned)time(0));
	using std::random_shuffle;
	random_shuffle(rand_1,rand_1+16);
    for( m=1 ; m<5 ; m++)
	{
		for ( n=1 ; n<5 ; n++)
		{
			do
			{
				char_1[1].ch[m][n]=*(rand_1+x);
				x++;
				break;
			}while(x<16);
		}
	}
char_1[0].ch[0][0]=' ';
	for ( int u=1 ; u<5 ; u++)
	{
		char_1[0].ch[0][u]=48+u;
		char_1[0].ch[u][0]=48+u;
		for(int y=1 ; y<5 ; y++)
		{
			char_1[0].ch[u][y]='*';
		}
	}
intro();
delay(700);
{
	cout<<"       \t"<<"     1    2    3    4"<<endl<<endl<<endl;
	for( i=1 ; i<5 ; i++)
	{
		cout<<"       \t\t\t";
		for ( j=0 ; j<5 ; j++)
		{
			cout<<char_1[0].ch[i][j]<<"    ";
		}
		cout<<endl<<endl<<endl;
	}
}
new_line(3);
main_1();
new_line(4);
}
/***********************************************************************************************/
void num_1()
{
input_first_point();
system("cls");
output_1();
score_1();
new_line(1);
num_2();
}
/***********************************************************************************************/
void num_2()
{
input_second_point();
system("cls");
if(row_1==row_2&&column_1==column_2)
{
	char_1[0].ch[row_1][column_1]='*';
	output_1();
	score_1();
	new_line(3);
    cout<<"Error\n\n#Don't Enter the co-ordinates of the same point\n\nYour score will be decreased by 5"<<endl<<endl<<endl;
	delay(3000);
	system("cls");
	output_1();
	score_1();
	new_line(3);
	num_1();
	system("cls");
	score-=5;
}
}
/***********************************************************************************************/
void input_first_point()										//to input the first point
{
cout<<"# Enter the Co-ordinates of the selected point to show \n";
cout<<"#Row ";
cin>>row_1;
cout<<"#Column ";
cin>>column_1;
if(column_1>4||row_1>4)
{
	cout<<"\nThere's no point here with this co-ordinates\n\nEnter the co-ordinates between 1 and 4\n\n";
    input_first_point();
}
cout<<endl<<endl;
if(char_1[0].ch[row_1][column_1]==' ')
{
	system("cls");
	output_1();
	cout<<"Error\n\nThis point have been hidden by you\n\n";
    input_first_point();
}
cout<<endl<<endl;
char_1[0].ch[row_1][column_1]=char_1[1].ch[row_1][column_1];
}
/***********************************************************************************************/
void input_second_point()										//to input the second point 
{
	new_line(2);
cout<<"# Enter the Co-ordinates of the second selected point to show \n\n";
cout<<"#Row ";
cin>>row_2;
cout<<"#Column ";
cin>>column_2;
if(column_2>4||row_2>4)
{
	cout<<"\nThere's no point here with this co-ordinates\n\nEnter the co-ordinates between 1 and 4\n\n";
    input_second_point();
}
cout<<endl<<endl;
if(char_1[0].ch[row_2][column_2]==' ')
{
	system("cls");
	output_1();
	cout<<"Error\n\nThis point have been hidden by you\n\n";
    input_second_point();
}
cout<<endl<<endl;
char_1[0].ch[row_2][column_2]=char_1[1].ch[row_2][column_2];
}
/***********************************************************************************************/
void output_1()													//To display the main screen of 
{																//the game
	for( e=0 ; e<5 ; e++)
{
	cout<<"       \t\t\t";
	for(q=0 ; q<5 ; q++)
	{
			cout<<char_1[0].ch[e][q]<<"    ";
	}
	cout<<endl<<endl<<endl;
}
}
/***********************************************************************************************/
void main_1()
{
	score_1();
	gotoxy(1,20);
	num_1();
output_1();
score_1();
if(char_1[1].ch[row_1][column_1]==char_1[1].ch[row_2][column_2])
{
	gotoxy(1,20);
   cout<<"Good Man"<<endl;
   char_1[0].ch[row_1][column_1]=' ';
   char_1[0].ch[row_2][column_2]=' ';
   score+=10;
}
else
{
	gotoxy(1,20);
	cout<<"Try Again";
	new_line(5);
	score-=4;
}
if(char_1[1].ch[row_1][column_1]!=char_1[1].ch[row_2][column_2])
{
	char_1[0].ch[row_1][column_1]='*','*';
	char_1[0].ch[row_2][column_2]='*','*';
}
sleep(1600);
system("cls");
output_1();
score_1();
for (i=1 ; i<2 ; i++)
{
	for ( j=1 ; j<2 ; j++)
	{
		if (char_1[0].ch[i][j]==' '&&char_1[0].ch[i][j+1]==' '&&char_1[0].ch[i][j+2]==' '&&char_1[0].ch[i][j+3]==' '&&char_1[0].ch[i+1][j]==' '&&char_1[0].ch[i+1][j+1]==' '&&char_1[0].ch[i+1][j+2]==' '&&char_1[0].ch[i+1][j+3]==' '&&char_1[0].ch[i+2][j]==' '&&char_1[0].ch[i+2][j+1]==' '&&char_1[0].ch[i+2][j+2]==' '&&char_1[0].ch[i+2][j+3]==' '&&char_1[0].ch[i+3][j]==' '&&char_1[0].ch[i+3][j+1]==' '&&char_1[0].ch[i+3][j+2]==' '&&char_1[0].ch[i+3][j+3]==' ')
		{
		cout<<"\n\nYou have finished with score "<<score<<endl<<endl;
		cout<<"Do you want to play again?(y/n)";
		cin>>re_play;
		re_play=tolower(re_play);
		while(isdigit(re_play))
		{
			cout<<"Please enter a character";
			cin>>re_play;
		}
		if (re_play=='y')
		{
			score=0;
			x=0;
			system("cls");
			main();
		}
		if (re_play=='n')
		{
			cout<<"Thank you for playing memory";
			exit(0);
		}
		else
		{
			exit(0);
		}
		}
	}
}
main_1();
}
/***********************************************************************************************/
void new_line(int y)											//Make a number of new lines 
{
	for (e=0 ; e<y ; e++)
		cout<<endl;
}
/***********************************************************************************************/
void delay(long seconds)										//This function is to make a
{																//delay on the screen	
	clock_t time1 = clock();  // use clock time
   	clock_t time2 = time1 + seconds;
  	while(time1 < time2)
   	time1 = clock();
  	return;
}
/***********************************************************************************************/
void score_1(){													//This function is to move the 
	gotoxy(64,17);												//cursor to a specified place
	cout<<"Score = "<<score<<endl;								//and write the score
}
/***********************************************************************************************/
void sleep(unsigned int mseconds)								//This function is to make a
{																//delay on the screen
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
/***********************************************************************************************/
void gotoxy(int x, int y)
{
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);			//This function is to set
	COORD point;												//the cursor to a specified 
   	point.X = x-1;												//place on the screen
	point.Y = y-1;
   	SetConsoleCursorPosition(hConsole, point);
}
/***********************************************************************************************/
void intro()
{
system("cls");
slide_horz_right(40,25,"Sayed");
slide_horz_right(32,25,"Mohamed");
slide_horz_right(28,25,"Eng");
slide_horz_right(25,25,"By");
slide_horz_right(16,25,"Designed");
delay(500);
system("cls");
}
/***********************************************************************************************/
void slide_horz_right( int x ,int y , char ch[])                 //This function is to move text
{												                 //on the screen		
	for ( int i=1 ; i<x ; i++)
	{	
		gotoxy(i,y);
		{
			cout<<ch<<endl;
		cout<<"         ";
		delay(25);
		}
		gotoxy(64,47);	
	}	
}
/***********************************************************************************************/

Dani AI

Generated

Nice share, . If you plan to turn this into a reusable snippet (as suggested), a quick refactor will make it easier to read, port, and maintain. Consider: use standard headers (<iostream>, not <iostream.h>), return int main(), and replace globals with a small Game struct holding the board, score, and selection state. Unify indexing (either 0-based everywhere or 1-based for I/O and convert internally), reject identical second picks early, and avoid recursion in the main loop to prevent stack growth. For timing, prefer std::this_thread::sleep_for over busy-wait delays, and for screen handling avoid system("cls"); a portable approach is to print ANSI sequences (you can enable VT mode on Windows, or just redraw without cursor tricks).

A compact model is: a flat vector of 16 tiles, a parallel vector<bool> of matched cells, and a print function that shows * for hidden cells and the letter for revealed/matched ones. Shuffling then becomes explicit and reproducible:

#include <vector>
#include <random>
#include <algorithm>

constexpr int N = 4; // 4x4
std::vector<char> tiles = {'A','A','B','B','C','C','D','D','E','E','F','F','G','G','H','H'};
std::mt19937 rng(std::random_device{}());
std::shuffle(tiles.begin(), tiles.end(), rng);

auto idx = [](int r, int c) { return r * N + c; }; // r,c are 0-based

A few quick correctness/UX checks to add before posting as a snippet:

  • Validate coordinates and that a cell is not already matched before revealing.
  • When two picks mismatch, hide both after a short pause; do not rely on the comma operator when resetting cells; assign explicitly.
  • Keep scoring updates in one place and show a consistent board header (row/column labels).

Wrap it with a short description (rules, controls, scoring) and you will have a clean snippet others can compile and learn from.

Recommended Answers

All 8 Replies

There's a code snippet section. Try to contribute there.

but why do you say this?

but why do you say this?

Because you didn't ask any question regarding code. It appears you are trying to share your knowledge. So, it's better to put it in right place.

where is this snippet?

and where's the right place?

and where's the right place?

Here's the Code Snippet Section.
Before you contribute there make sure you take opinion on it.

i've taken my opinion about this game

i've taken my opinion about this game

Many people won't read further after reading 1st line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.