13-06-2007, 06:35 PM
کد:
/*
Name:Puzzle
Copyright: www.codecorona.com
Author: Mohammad yazdani
Description:At this game you must find 2 cell with similar content to delete
thme from puzzle for instance in every choice you input the cordinates of
2 cell from puzzle and the content of that cells will show to you if
they were like each other they will remove from puzzle otherwise
the game will countinue untill you find and remove all similar
charcater 2by2 and remove them from puzzle then
you will win the game.
*/
//*********header files*******************************************************
#include <iostream>
#include <conio.h>
#include <vector>
#define row 10
#define col 10
using namespace std;
//********************designing the structure of evry cell of the puzzle********
class cell{
friend class puzzle;
char data; //content of the cell
char face; //face will show in draw function
};
//**********************designing class puzzle**********************************
class puzzle{
public:
puzzle();
void game();
bool check_win();
bool delet(int,int,int,int);
void shift();
void draw();
void draw(int,int,int,int);
private:
cell table[row][col];
};
//********************implementing member functions of puzzle*******************
puzzle::puzzle()
{
int i,j;
vector <char> v;
char ch=65;
for(i=0;i<row*col;i++)
{
v.push_back(ch++);
if(i==49)
ch=65;
}
srand(time(0));
int x=row*col,n,value;
for(i=0,j=0;i<row*col;i++,j++)
{
n=rand()%x--;
value=v[n];
v.erase(v.begin()+n);
table[0][j].data=value;
table[0][j].face='*';
}
}
//*****************************************************************************
void puzzle::game()
{
int i,j,k,m;
char ch=0;
while(1)
{
draw();
cout<<"\npleae enter row and coloum for first cell (row &&colom between 0-9):";
cout<<"\nenter row :";
cin>>i;
cout<<"\nenter coloum :";
cin>>j;
cout<<"\nplease enter the location of secons cell (row && "
"colom between 0-9):";
cout<<"\nenter row:";
cin>>k;
cout<<"\nenter coloum:";
cin>>m;
draw(i,j,k,m);
if(delet(i,j,k,m))
shift();
if(check_win())
{
cout<<"congratulation .you win the game.";
getch();
break;
}//end of if
}//end of while
}
//*****************************************************************************
bool puzzle::delet(int m,int n,int p,int z)
{
if(table[m][n].data==table[p][z].data)
{
table[m][n].data=table[p][z].data=' ';
table[m][n].face=table[p][z].face=' ';
return true;
}
return false;
}
//******************************************************************************
void puzzle::shift()
{
int i,j;
for(int k=0 ; k < 2 ; ++k)
for(i=9;i>0;i--)
for(j=9;j>=0;j--)
if(table[i][j].data==' ')
if(table[i-1][j].data!=' ')
{
table[i][j].data=table[i-1][j].data;
table[i][j].face='* ';
table[i-1][j].data=table[i-1][j].face=' ';
}
}
//******************************************************************************
bool puzzle::check_win()
{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(table[i][j].data!=' ')
return false;
return true;
}
//******************************************************************************
void puzzle::draw()
{
int i,j;
cout<<endl;
system("color 70");
cout<<"\n\n\n\n\n\n ";
for(i=0;i<row;i++,cout<<endl<<endl<<" ")
for(j=0;j<col;j++)
cout<<table[i][j].face<<" ";
}
//******************************************************************************
void puzzle::draw(int i,int j,int k,int m)
{
int i1,j1;
cout<<"\n\n\n\n\n\n ";
for(i1=0;i1<row;i1++,cout<<endl<<endl<<" ")
for(j1=0;j1<col;j1++)
{
if((i1==i&&j1==j) ||(i1==k&&j1==m))
cout<<table[i1][j1].data<<" ";
else
cout<<table[i1][j1].face<<" ";
}//end of for
getch();
}
//*****************Main Function***********************************************
int main()
{
puzzle p;
p.game();
getch();
return 0;
}
//*****************************End of program***********************************