0% found this document useful (0 votes)
1K views

Memory Game C++ Program

This document contains the code for a C++ memory game. It generates random numbers, displays them on the screen, and then prompts the user to input the numbers back in the correct order. If the user inputs any number incorrectly, the game ends and their score is displayed. High scores are stored in a text file. The code includes functions for generating and displaying the random numbers, getting the user input, comparing it to the correct order, and drawing the game board rectangles.

Uploaded by

AnjannAnjanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Memory Game C++ Program

This document contains the code for a C++ memory game. It generates random numbers, displays them on the screen, and then prompts the user to input the numbers back in the correct order. If the user inputs any number incorrectly, the game ends and their score is displayed. High scores are stored in a text file. The code includes functions for generating and displaying the random numbers, getting the user input, comparing it to the correct order, and drawing the game board rectangles.

Uploaded by

AnjannAnjanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

//THE MEMORY GAME

#include<graphics.h>
#include<dos.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
int a[10],c[10],point=0,flag=1,p[50],s;
char ans='y',con;
int compare(int);
int mem_game (int);
void rec();
void main()
{
clrscr();
int ch;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\TC\\BGI");

start:
clrscr();
settextstyle(1,0,3);
setcolor(1);
outtextxy(125,100,"THE MEMORY GAME");
outtextxy(60,175,"1.Play Game 2.High Scores 3.Instructions 4.Exit");
cin>>ch;
switch(ch)

{
case 1:
{
for(int y=3;y<10;y++)
{
clrscr();
cout<<"Level "<<y-2<<endl; sleep(1);
if(y==3)
point=0;
mem_game(y);
clrscr();
s=compare(y);
if(s==0)goto start;
mem_game(y);
clrscr();
s=compare(y);
if(s==0)
goto start;
clrscr();
cout<<"Level "<<y-2<<" cleared!"<<endl;sleep(1);
clrscr();
}
Cout<<�Thank you for playing the game !�<<endl;
sleep(1);
break;
}
case 2:
{
int big,pos,tmp,i=0;
fstream pt("Hscore.txt",ios::in);
while(!pt.eof())
{
pt>>p[i];

i++;
}
for(int j=0;j<i;j++)
{
big=p[i];
pos=i;
for(int k=j+1;k<i;k++)
{
if(p[k]>big)
{
big=p[k]; pos=k;
}
}
tmp=p[j];
p[j]=p[pos];
p[pos]=tmp;
}
clrscr();
for(j=0;j<i;j++)
cout<<j+1<<" . "<<p[j]<<endl;
sleep(3);
goto start;
}
case 3: clrscr();
cout<<"\tHello. This is a very simple. A list of numbers will be printed and the
player has tokeep them in mind and reproduce them when asked.\nFor every correct
answer the playergets awarded 10 points .If the player makes one wrong input he/she
is out.\nHave Fun!"<<endl;
cout<<"\n\nEnter y to continue"<<endl;cin>>con;
if(con=='y'||con=='Y')
goto start;
else goto start;
case 4:
clrscr();

break;
default : clrscr();
cout<<"Wrong choice! Restarting";
sleep(2);
goto start;
}
clrscr();
getch();
}
int mem_game(int n)
{
randomize();
int x;
for(int i=0;i<n;i++)
{
x=random(9)+1;
c[i]=x;
clrscr();
switch(x)
{
case
1:rec();settextstyle(1,0,3);outtextxy(225,115,"1");sleep(1);clrscr();rec();break;
case
2:rec();settextstyle(1,0,3);outtextxy(291,115,"2");sleep(1);clrscr();rec();break;
case
3:rec();settextstyle(1,0,3);outtextxy(357,115,"3");sleep(1);clrscr();rec();break;
case
4:rec();settextstyle(1,0,3);outtextxy(225,181,"4");sleep(1);clrscr();rec();break;
case
5:rec();settextstyle(1,0,3);outtextxy(291,181,"5");sleep(1);clrscr();rec();break;
case
6:rec();settextstyle(1,0,3);outtextxy(357,181,"6");sleep(1);clrscr();rec();break;
case
7:rec();settextstyle(1,0,3);outtextxy(225,247,"7");sleep(1);clrscr();rec();break;
case
8:rec();settextstyle(1,0,3);outtextxy(291,247,"8");sleep(1);clrscr();rec();break;
case
9:rec();settextstyle(1,0,3);outtextxy(357,247,"9");sleep(1);clrscr();rec();break;
}
}
return 0;
}
int compare(int n)
{
int i=0;cout<<"Enter the numbers in the correct order : "<<endl;
while(i<n)
{
cin>>a[i];
if(a[i]!=c[i])
{
cout<<"Wrong!!! GAME OVER!\n";
cout<<"Score : "<<point;flag=0;
fstream pt("Hscore.txt",ios::out|ios::app);
pt<<point<<endl;pt.close();
sleep(4);
return 0;
}
elsepoint+=10;i++;
}
return 1;
}

void rec()
{
rectangle(200,100,400,300);
rectangle(200,100,266,300);
rectangle(200,100,332,300);
rectangle(200,100,400,166);
rectangle(200,166,400,232);
rectangle(200,232,400,300);
}

You might also like