#include <iostream>
#include <iomanip>
using namespace std;
int chessboard[3][3];
int scan() //判断结局
{
int i=0,j=0;
//横成一条
for(i=0;i<3;i++)
{
if((chessboard[i][0]==chessboard[i][1])&&(chessboard[i][0]==chessboard[i][2])
&&(chessboard[i][0]!=100))
{
if(chessboard[i][0]==0)
{cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
return 0;
}
}
//竖成一条
for(i=0;i<3;i++)
{
if(chessboard[0][i]==chessboard[1][i]&&chessboard[0][i]==chessboard[2][i]
&&chessboard[0][i]!=100)
{
if(chessboard[0][i]==0)
{cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
return 0;
}
}
//斜成一条
if(chessboard[0][0]==chessboard[1][1]&&chessboard[0][0]==chessboard[2][2]
&&chessboard[0][0]!=100)
{
if(chessboard[0][0]==0)
{cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
return 0;
}
if(chessboard[0][2]==chessboard[1][1]&&chessboard[0][2]==chessboard[2][0]
&&chessboard[0][2]!=100)
{
if(chessboard[0][2]==0)
{cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
return 0;
}
//平局
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{if(chessboard[i][j]==100)return 1;}
}
cout<<setw(15)<<setfill(' ')<<"draw!"<<endl;
return 0;
}
int next() //计算机下一步,
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(chessboard[i][j]==100)
{
chessboard[i][j]=0;
return 0;
}
}
}
return 0;
}
void print() //打印状态
{
int i=0,j=0;
for(i=0;i<3;i++)
{
cout<<setw(25)<<setfill(' ')<<' ';
for(j=0;j<3;j++)
{
switch(chessboard[i][j])
{
case 0: cout<<" O";break;
case 1: cout<<" X";break;
case 100: cout<<" _";break;
default: cout<<"error!"<<endl;
}
}
cout<<endl;
}
cout<<endl;
}
void init() //初始化
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
chessboard[i][j]=100;
}
int main()
{
int x,y;
init();
while(1)
{
cout<<setw(15)<<setfill(' ')<<' '<<setw(50)<<setfill('*')<<' '<<endl;
cout<<setw(20)<<setfill(' ')<<' ';
cout<<"enter the position:";
cin>>x>>y;
if(x>2||y>2)
{
cout<<setw(15)<<setfill(' ')<<"ERROR!"<<endl;
return 0;
}
cout<<endl;
chessboard[x][y]=1;
if(!scan())break;
next();
if(!scan())break;
print();
}
print();
}
注释:这是最幼稚的一种写法,在后面会给出更出色的算法。
#include <iomanip>
using namespace std;
int chessboard[3][3];
int scan() //判断结局
{
int i=0,j=0;
//横成一条
for(i=0;i<3;i++)
{
if((chessboard[i][0]==chessboard[i][1])&&(chessboard[i][0]==chessboard[i][2])
&&(chessboard[i][0]!=100))
{
if(chessboard[i][0]==0)
{cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
return 0;
}
}
//竖成一条
for(i=0;i<3;i++)
{
if(chessboard[0][i]==chessboard[1][i]&&chessboard[0][i]==chessboard[2][i]
&&chessboard[0][i]!=100)
{
if(chessboard[0][i]==0)
{cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
return 0;
}
}
//斜成一条
if(chessboard[0][0]==chessboard[1][1]&&chessboard[0][0]==chessboard[2][2]
&&chessboard[0][0]!=100)
{
if(chessboard[0][0]==0)
{cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
return 0;
}
if(chessboard[0][2]==chessboard[1][1]&&chessboard[0][2]==chessboard[2][0]
&&chessboard[0][2]!=100)
{
if(chessboard[0][2]==0)
{cout<<setw(15)<<setfill(' ')<<' '<<"You lose!"<<endl;}
else {cout<<setw(15)<<setfill(' ')<<' '<<"You win!"<<endl;}
return 0;
}
//平局
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{if(chessboard[i][j]==100)return 1;}
}
cout<<setw(15)<<setfill(' ')<<"draw!"<<endl;
return 0;
}
int next() //计算机下一步,
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(chessboard[i][j]==100)
{
chessboard[i][j]=0;
return 0;
}
}
}
return 0;
}
void print() //打印状态
{
int i=0,j=0;
for(i=0;i<3;i++)
{
cout<<setw(25)<<setfill(' ')<<' ';
for(j=0;j<3;j++)
{
switch(chessboard[i][j])
{
case 0: cout<<" O";break;
case 1: cout<<" X";break;
case 100: cout<<" _";break;
default: cout<<"error!"<<endl;
}
}
cout<<endl;
}
cout<<endl;
}
void init() //初始化
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
chessboard[i][j]=100;
}
int main()
{
int x,y;
init();
while(1)
{
cout<<setw(15)<<setfill(' ')<<' '<<setw(50)<<setfill('*')<<' '<<endl;
cout<<setw(20)<<setfill(' ')<<' ';
cout<<"enter the position:";
cin>>x>>y;
if(x>2||y>2)
{
cout<<setw(15)<<setfill(' ')<<"ERROR!"<<endl;
return 0;
}
cout<<endl;
chessboard[x][y]=1;
if(!scan())break;
next();
if(!scan())break;
print();
}
print();
}
注释:这是最幼稚的一种写法,在后面会给出更出色的算法。