14-04-2007, 07:46 PM
کد:
//Author:soheil setayeshi
//dooz
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
//***********************************
class dooz{
private:
int a[3][3];
int k;
int n;
public:
dooz();
void nobat(int*,int*);
void check(int*,int*);
void checkwin();
void show();
};
//*****************************//implement constructor
dooz::dooz()
{
int i,j;
k=1;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=0;
}
//*****************************//function implementation
void dooz::show()
{
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++)
cout<<" ["<<a[i][j]<<"] ";
cout<<"\n\n";}
cout<<"\n\n";
}
//*****************************
void dooz::nobat(int *x,int*y)
{
int i,j,m=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(a[i][j])
m++;
if(m==9)
{
cout<<"The game doesn't have a winner.";
getch();
exit(0);
}
if(k==1)
{
cout<<"\nPlayer one:";
k*=-1;
n=1;
}
else
{
cout<<"\nPlayer two:";
k*=-1;
n=2;
}
do
{
cin>>*x>>*y;
cout<<"\n\n";
*x-=1;
*y-=1;
if(*x>2||*y>2||*x<0||*y<0)
cout<<"This place not exist please enter another place.";
}
while(*x>2||*y>2||*x<0||*y<0);
}
//***********************************
void dooz::check(int *x,int *y)
{
int b;//,X,Y;
b=1;
while(b)
{
if(a[*x][*y])
{
cout<<"This place is busy please enter another place:";
cin>>*x>>*y;
*x-=1;
*y-=1;
}
else
{
a[*x][*y]=n;
b=0;
}
}
}
//***********************************
void dooz::checkwin()
{
int i,j;
for(i=0;i<3;i++)
if(a[i][0]==a[i][1]&&a[i][1]==a[i][2]&&a[i][0]!=0&&a[i][1]!=0&&a[i][2]!=0)
{
cout<<"*** Number "<<a[i][0]<<" is winner. ***";
getch();
exit(0);
}
for(j=0;j<3;j++)
if(a[0][j]==a[1][j]&&a[1][j]==a[2][j]&&a[0][j]!=0&&a[1][j]!=0&&a[2][j]!=0)
{
cout<<"*** Number "<<a[0][j]<<" is winner. ***";
getch();
exit(0);
}
if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[0][0]!=0&&a[1][1]!=0&&a[2][2]!=0)
{
cout<<"*** Number "<<a[0][0]<<" is winner. ***";
getch();
exit(0);
}
if(a[0][2]==a[1][1]&&a[1][1]==a[2][0]&&a[0][2]!=0&&a[1][1]!=0&&a[2][0]!=0)
{
cout<<"*** Number "<<a[0][2]<<" is winner. ***";
getch();
exit(0);
}
}
//***********************************
int main()
{
int *x,*y,i,j;
x=new int;
y=new int;
if(!x)
{
cout<<"Allocation failure.";
exit(0);
}
if(!y)
{
cout<<"Allocation failure.";
exit(0);
}
dooz game;
while(1)
{
game.nobat(x,y);
game.check(x,y);
game.show();
game.checkwin();
}
getch();
return 0;
}