22-04-2007, 07:52 AM
کد:
//Author:Mohammad
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main();
void display_table()
{
// system("cls");
int i,j;
cout<<"\n\n";
for(i=0;i<4;i++,cout<<endl<<endl)
for(j=0;j<4;j++,cout<<" ")
cout<<"\t\t"<<"*";
}
struct cell{
int num;
int state;
char face;
char value;
};
class minroob{
public:
minroob(int=3,int=3,int=3);
void game();
bool checkwin();
bool gameover(int,int);
void display();
void display1();
static void custom();
static void help();
private:
int row;
int colum;
cell **table;
};
minroob::minroob(int r,int c,int b)
{
row=r;
colum=c;
int i,j,n;
table=new cell*[row];
for(i=0;i<row;++i)
table[i]=new cell[colum];
int valuer[b],valuec[b] ;
for(i=0;i<b;i++)
{
valuer[i]=-1;
valuec[i]=-1;
}
srand(time(0));
for(i=0;i<b;i++)
{
srand(time(0));
n=rand()%row;
valuer[i]=n;
for(j=0;j<i;j++)
if(valuer[j]==n)
{
i--;
break;
}
}//end of or
srand(time(0));
for(i=0;i<b;i++)
{
srand(time(0));
n=rand()%colum;
valuec[i]=n;
for(j=0;j<i;j++)
if (valuec[j]==n)
{
i--;
break;
}
}//end of for
for(i=0;i<b;i++)
{
table[valuer[i]][valuec[i]].state=1;
table[valuer[i]][valuec[i]].value='@';
}
for(i=0;i<row;i++)
for(j=0;j<colum;j++)
if(table[i][j].value!='@')
table[i][j].value=0;
for(i=0;i<row;i++)
for(j=0;j<colum;j++)
{
table[i][j].face='*';
table[i][j].num=0;
table[i][j].state=0;
}
}
void minroob::custom()
{
int b,r,c;
do{
cout<<"please enter number of row:";
cin>>r;
cout<<"please enter number of colum:";
cin>>c;
cout<<"enter number of min for put int the table:";
cin>>b;
}while(b>r*c);
cout<<"please waite...";
minroob bug(r,c,b);
bug.game();
}
void minroob::game()
{
system("cls");
int r,c;
while(1)
{
display();
if(checkwin())
{
system("cls");
cout<<"\n\n\n\n\t\tGOOD LUCK .YOU WON THE GAME.";
getch();
display();
getch();
main();
exit(0);
}//end of if
cout<<"\tenter the row:";
cin>>r;
cout<<"\tenter the colum:";
cin>>c;
if(gameover(r,c))
{
// sound(100);
system("cls");
cout<<"\n\n\t\tsorry you lose the game. TRY AGAIN . ";
getch();
system("cls");
display1();
getch();
main();
exit(0);
}
} //end of while
}
bool minroob::checkwin()
{
int i,j,n=0;
for(i=0;i<row;i++)
for(j=0;j<colum;j++)
if(table[i][j].value!='@'&&table[i][j].face!='-')
return 0;
return 1;
}
bool minroob::gameover(int r,int c)
{
int i,j;
int n=0;
if(table[r][c].value=='@')
{
table[r][c].face=table[r][c].value;
return 1;
}
for(i=r-1;i<=r+1;i++)
for(j=c-1;j<=c+1;j++)
if(i>=0&&i<row&&j<colum&&table[i][j].value=='@')
n++;
if(n>0)
{
table[r][c].num=n;
table[r][c].state=1;
return 0;
}
else{
table[r][c].face='-';
// table[r][c].state=1;
return 0;
}
}
void minroob::help()
{
system("cls");
cout<<"this game is a simulation of microsoft minesweeper game "
"when the program run you will see a menu with 4 selection"
"you can choose one of theme."
"if you choose number 1 the game will start with a 3*3 table"
"with 3 min hide in table.then you must enter the row between(0-the "
"number of row-1) and a similar choose for coloum."
"if you choose custom you can specify the number of row and coloums "
"and number of min put int the table"
"for exit from the game choose number 4";
cout<<"\n\n\n\n\t\tALL RIGHT RESERVED BY MOHAMMAD YAZDANI";
cout<<"\n\n\t\tplease visit our site WWW.CODECORONA.COM";
cout<<"\n\n\t\tGOOD LUCK...";
getch();
}
void minroob::display()
{
system("cls");
int i,j;
cout<<"\n";
for(i=0;i<row;i++,cout<<endl<<endl)
for(j=0;j<colum;j++,cout<<" ")
if(table[i][j].state==1)
cout<<"\t"<<table[i][j].num<<" ";
else
cout<<"\t"<<table[i][j].face<<" ";
}
void minroob::display1()
{
system("cls");
int i,j;
cout<<"\n";
for(i=0;i<row;i++,cout<<endl<<endl<<endl<<endl)
for(j=0;j<colum;j++,cout<<" ")
{
if(table[i][j].value=='@')
cout<<table[i][j].value<<" ";
else if(table[i][j].state==1)
cout<<table[i][j].num<<" ";
else
cout<<table[i][j].face;
}
}
int main()
{
int ch;
// while(1)
// {
system("cls");
cout<<"1-PLAY "
"\n2-custom"
"\n3-HELP"
"\n4-EXIT";
display_table();
cout<<"please enter your choice:";
cin>>ch;
if(ch==1)
{
cout<<"please waite...";
minroob bug;
bug.game();
}
if(ch==2)
minroob::custom();
if(ch==3)
minroob::help();
if(ch==4)
exit(0);
/* switch(ch)
{
case 1:
minroob bug;
bug.game();
break;
case 2:
minroob:: custom();
break;
case 3:
minroob::help();
break;
case 4:
exit(0);
break;
}//end of switch
*/
// }//end of while
getch();
return 0;
}