#include <iostream>
#include <iomanip>
using namespace std;
int chessboard[3][3],computer[4][3][3],player[4][3][3],chess=0;
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 H_value (int i,int j) //计算行权值
{
int counter=0;
if(chessboard[i][j]!=100)return 0;
else
{
int P_i,N_i,P_j,N_j;
P_i=N_i=i;P_j=N_j=j;
for(;P_j<3;)
{
P_j++;
if(chessboard[i][P_j]!=chess||P_j>=3)break;
counter++;
}
for(;N_j>=0;)
{
N_j--;
if(chessboard[i][N_j]!=chess||N_j<0)break;
counter++;
}
return counter;
}
}
int V_value(int i,int j) //计算列权值
{
int counter=0;
if(chessboard[i][j]!=100)return 0;
else
{
int P_i,N_i,P_j,N_j;
P_i=N_i=i;P_j=N_j=j;
for(;P_i<3;)
{
P_i++;
if(chessboard[P_i][j]!=chess||P_i>=3)break;
counter++;
}
for(;N_i>=0;)
{
N_i--;
if(chessboard[N_i][j]!=chess||N_i<0)break;
counter++;
}
return counter;
}
}
int RC_value(int i,int j) //计算右斜权值
{
int counter=0;
if(chessboard[i][j]!=100)return 0;
else
{
int P_i,N_i,P_j,N_j;
P_i=N_i=i;P_j=N_j=j;
for(;P_i<3&&P_j<3;)
{
P_i++;P_j++;
if(chessboard[P_i][P_j]!=chess||P_i>=3||P_j>=3)break;
counter++;
}
for(;N_i>=0&&N_j>=0;)
{
N_i--;N_j--;
if(chessboard[N_i][N_j]!=chess||N_i<0||N_j<0)break;
counter++;
}
return counter;
}
}
int LC_value(int i,int j) //计算左斜权值
{
int counter=0;
if(chessboard[i][j]!=100)return 0;
else
{
int P_i,N_i,P_j,N_j;
P_i=N_i=i;P_j=N_j=j;
for(;N_i>=0&&P_j<3;)
{
N_i--;P_j++;
if(chessboard[N_i][P_j]!=chess||N_i<0||P_j>=3)break;
counter++;
}
for(;P_i<3&&N_j>=0;)
{
P_i++;N_j--;
if(chessboard[P_i][N_j]!=chess||P_i>=3||N_j<0)break;
counter++;
}
return counter;
}
}
void value() //计算机及棋手权值输入数组
{
int i,j,k;
for(chess=0;chess<2;chess++)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<4;k++)
{
if(chess==0)
{
switch(k)
{
case 0: computer[k][i][j]=H_value(i,j);break;
case 1: computer[k][i][j]=V_value(i,j);break;
case 2: computer[k][i][j]=RC_value(i,j);break;
case 3: computer[k][i][j]=LC_value(i,j);break;
//default: cout<<"error!"<<endl;break
}
}
else
{
switch(k)
{
case 0: player[k][i][j]=H_value(i,j);break;
case 1: player[k][i][j]=V_value(i,j);break;
case 2: player[k][i][j]=RC_value(i,j);break;
case 3: player[k][i][j]=LC_value(i,j);break;
//default: cout<<"error!"<<endl;
}
}
}
}
}
}
}
int next() //计算机下一步
{
int i,j,k,M_i=0,M_j=0,M_k=0,max=0,temp=0,P_max=0,PM_i=0,PM_j=0;
value();
if(chessboard[1][1]==100)// 天元点
{
chessboard[1][1]=0;return 0;
}
for(k=0;k<4;k++)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
temp=computer[k][i][j]+player[k][i][j];
if(player[k][i][j]==2)
{chessboard[i][j]=0;return 0;}
else if(temp>max)
{
max=temp;
M_i=i;M_j=j;M_k=k;
}
if(player[k][i][j]>P_max)
{
PM_i=i;
PM_j=j;
}
}
}
}
if(chessboard[M_i][M_j]==100) //换位思考,下对方的棋
{chessboard[M_i][M_j]=0;}
else chessboard[PM_i][PM_j]=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() //初始化
{
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
chessboard[i][j]=100;
for(k=0;k<4;k++)
{
computer[k][i][j]=0;
player[k][i][j]=0;
}
}
}
int main()
{
int x,y;
char dec;
do
{
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||chessboard[x][y]!=100)
{
cout<<setw(15)<<setfill(' ')<<"ERROR!"<<endl;
break;
}
cout<<endl;
chessboard[x][y]=1;
if(!scan())break;
next();
if(!scan())break;
print();
}
print();
cout<<setw(15)<<setfill(' ')<<' '<<"continue?(y/n):";
cin>>dec;
cout<<endl;
}while(dec=='y');
return 0;
}
注释:
由于是对方先下子,顾电脑输的的可能性很大,这与五子棋先下子的选手占优势是一致的。
废话不多说,代码已在vc6中编译通过,并经过本人的精心测试。
#include <iomanip>
using namespace std;
int chessboard[3][3],computer[4][3][3],player[4][3][3],chess=0;
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 H_value (int i,int j) //计算行权值
{
int counter=0;
if(chessboard[i][j]!=100)return 0;
else
{
int P_i,N_i,P_j,N_j;
P_i=N_i=i;P_j=N_j=j;
for(;P_j<3;)
{
P_j++;
if(chessboard[i][P_j]!=chess||P_j>=3)break;
counter++;
}
for(;N_j>=0;)
{
N_j--;
if(chessboard[i][N_j]!=chess||N_j<0)break;
counter++;
}
return counter;
}
}
int V_value(int i,int j) //计算列权值
{
int counter=0;
if(chessboard[i][j]!=100)return 0;
else
{
int P_i,N_i,P_j,N_j;
P_i=N_i=i;P_j=N_j=j;
for(;P_i<3;)
{
P_i++;
if(chessboard[P_i][j]!=chess||P_i>=3)break;
counter++;
}
for(;N_i>=0;)
{
N_i--;
if(chessboard[N_i][j]!=chess||N_i<0)break;
counter++;
}
return counter;
}
}
int RC_value(int i,int j) //计算右斜权值
{
int counter=0;
if(chessboard[i][j]!=100)return 0;
else
{
int P_i,N_i,P_j,N_j;
P_i=N_i=i;P_j=N_j=j;
for(;P_i<3&&P_j<3;)
{
P_i++;P_j++;
if(chessboard[P_i][P_j]!=chess||P_i>=3||P_j>=3)break;
counter++;
}
for(;N_i>=0&&N_j>=0;)
{
N_i--;N_j--;
if(chessboard[N_i][N_j]!=chess||N_i<0||N_j<0)break;
counter++;
}
return counter;
}
}
int LC_value(int i,int j) //计算左斜权值
{
int counter=0;
if(chessboard[i][j]!=100)return 0;
else
{
int P_i,N_i,P_j,N_j;
P_i=N_i=i;P_j=N_j=j;
for(;N_i>=0&&P_j<3;)
{
N_i--;P_j++;
if(chessboard[N_i][P_j]!=chess||N_i<0||P_j>=3)break;
counter++;
}
for(;P_i<3&&N_j>=0;)
{
P_i++;N_j--;
if(chessboard[P_i][N_j]!=chess||P_i>=3||N_j<0)break;
counter++;
}
return counter;
}
}
void value() //计算机及棋手权值输入数组
{
int i,j,k;
for(chess=0;chess<2;chess++)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<4;k++)
{
if(chess==0)
{
switch(k)
{
case 0: computer[k][i][j]=H_value(i,j);break;
case 1: computer[k][i][j]=V_value(i,j);break;
case 2: computer[k][i][j]=RC_value(i,j);break;
case 3: computer[k][i][j]=LC_value(i,j);break;
//default: cout<<"error!"<<endl;break
}
}
else
{
switch(k)
{
case 0: player[k][i][j]=H_value(i,j);break;
case 1: player[k][i][j]=V_value(i,j);break;
case 2: player[k][i][j]=RC_value(i,j);break;
case 3: player[k][i][j]=LC_value(i,j);break;
//default: cout<<"error!"<<endl;
}
}
}
}
}
}
}
int next() //计算机下一步
{
int i,j,k,M_i=0,M_j=0,M_k=0,max=0,temp=0,P_max=0,PM_i=0,PM_j=0;
value();
if(chessboard[1][1]==100)// 天元点
{
chessboard[1][1]=0;return 0;
}
for(k=0;k<4;k++)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
temp=computer[k][i][j]+player[k][i][j];
if(player[k][i][j]==2)
{chessboard[i][j]=0;return 0;}
else if(temp>max)
{
max=temp;
M_i=i;M_j=j;M_k=k;
}
if(player[k][i][j]>P_max)
{
PM_i=i;
PM_j=j;
}
}
}
}
if(chessboard[M_i][M_j]==100) //换位思考,下对方的棋
{chessboard[M_i][M_j]=0;}
else chessboard[PM_i][PM_j]=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() //初始化
{
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
chessboard[i][j]=100;
for(k=0;k<4;k++)
{
computer[k][i][j]=0;
player[k][i][j]=0;
}
}
}
int main()
{
int x,y;
char dec;
do
{
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||chessboard[x][y]!=100)
{
cout<<setw(15)<<setfill(' ')<<"ERROR!"<<endl;
break;
}
cout<<endl;
chessboard[x][y]=1;
if(!scan())break;
next();
if(!scan())break;
print();
}
print();
cout<<setw(15)<<setfill(' ')<<' '<<"continue?(y/n):";
cin>>dec;
cout<<endl;
}while(dec=='y');
return 0;
}
注释:
由于是对方先下子,顾电脑输的的可能性很大,这与五子棋先下子的选手占优势是一致的。
废话不多说,代码已在vc6中编译通过,并经过本人的精心测试。