#include<iostream>
#include<windows.h>
using namespace std;
int comcolur=1,colur;//comcolur=1电脑走
//八个方向的偏移量。
int dr[8]={0, 1, 1, 1, 0, -1, -1, -1};
int dc[8]={-1, -1, 0, 1, 1, 1, 0, -1};
int map[8][8]={0};
int tmp;
void showmap();
void playgame();
int value(int row,int col,int k);
int rvalue(int row,int col,int k);
int canplay();
int isright(int x,int y);
int main()
{
map[3][3]=1;
map[3][4]=2;
map[4][3]=2;
map[4][4]=1;
showmap();
cout<<"请选择你的颜色 1表示黑色 2表示白色"<<endl;
cin>>tmp;
comcolur=tmp%2+1;
playgame();
return 0;
}
void showmap()
{
int i,j;
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
cout<<map[i][j];
cout<<" ";
}
cout<<endl;
}
}
void playgame()
{
int i,j,k;
int px,py;
int tmp3,tmp1,tmp2;
int sum[8][8]={0};
int ntmp1,ntmp2,flag=1;
int flag1=0;
int px1,py1;
int count1,count2;
while(canplay()==1||flag1==1)//如果还可以继续玩
{
if(comcolur==1)
{//电脑搜索每一个位置看能不能走
flag1=0;
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
sum[i][j]=0;
}
}
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
for(k=0; k<8; k++)
{
sum[i][j]=sum[i][j]+value(i,j,k);
}
}
}
tmp3= sum[0][0];
tmp1=0;
tmp2=0;
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
if(tmp3<sum[i][j])
{
tmp3=sum[i][j];
tmp1=i;
tmp2=j;
}
}
}
if(tmp3>0)
{
map[tmp1][tmp2]=tmp%2+1;
for(i=0; i<8; i++)
{
ntmp1=tmp1+dr[i];
ntmp2=tmp2+dc[i];
flag=0;
while(ntmp1>=0&&ntmp1<8&&ntmp2>=0&&ntmp2<8&&map[ntmp1][ntmp2]==tmp)
{
flag++;
ntmp1=ntmp1+dr[i];
ntmp2=ntmp2+dc[i];
}
if(flag!=0&&ntmp1>=0&&ntmp1<8&&ntmp2>=0&&ntmp2<8&&map[ntmp1][ntmp2]==tmp%2+1)
{
ntmp1=0;
ntmp2=0;
flag1=1;
for(j=0; j<flag; j++)
{
ntmp1=tmp1+dr[i]*(j+1);
ntmp2=tmp2+dc[i]*(j+1);
map[ntmp1][ntmp2]=tmp%2+1;
}
}
}
}
}
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
cout<<map[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
if(canplay())
{
flag1=1;
comcolur=1;
cin>>px>>py;
while(isright(px,py)==0)
{
cout<<"输入不合法,请重新输入"<<endl;
cin>>px>>py;
}
map[px][py]=tmp;
for(i=0; i<8; i++)
{
px1=px+dr[i];
py1=py+dc[i];
flag=0;
while(px1>=0&&px1<8&&py1>=0&&py1<8&&map[px1][py1]==tmp%2+1)
{
flag++;
px1=px1+dr[i];
py1=py1+dc[i];
}
if(flag!=0&&px1>=0&&px1<8&&py1>=0&&py1<8&&map[px1][py1]==tmp)
{
px1=0;
py1=0;
for(j=0; j<flag; j++)
{
px1=px+dr[i]*(j+1);
py1=py+dc[i]*(j+1);
map[px1][py1]=tmp;
}
}
}
}
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
if(map[i][j+1]==-1&&j+1<8)
{
cout<<map[i][j];
}
else
{
cout<<map[i][j]<<" ";
}
}
cout<<endl;
}
cout<<endl;
}
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
if(map[i][j]==tmp)
{
count1++;
}
else
{
count2++;
}
}
}
if(count1>count2)
{
cout<<"You Win"<<endl;
}
else if(count1==count2)
{
cout<<"平局"<<endl;
}
else
{
cout<<"You Lose"<<endl;
}
}
int value(int row,int col,int k)
{
int sum;
int nrow,ncol;
nrow=row+dr[k];
ncol=col+dc[k];
if(map[row][col]==0)
{
map[row][col]=tmp%2+1;
}
else
{
return 0;
}
sum=0;
if(nrow<0||nrow>=8||ncol>=8||ncol<0||map[nrow][ncol]*map[row][col]!=2)
{
map[row][col]=0;
return 0;
}
while(nrow>=0&&nrow<8&&ncol<8&&ncol>=0&&map[nrow][ncol]*map[row][col]==2)
{
sum++;
nrow=nrow+dr[k];
ncol=ncol+dc[k];
}
if(nrow>=0&&nrow<8&&ncol<8&&ncol>=0&&map[nrow][ncol]==map[row][col])
{
map[row][col]=0;
return sum;
}
else
{
map[row][col]=0;
return 0;
}
}
int canplay()
{
int i,j,k;
int bug[8][8];
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
bug[i][j]=0;
}
}
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
for(k=0; k<8; k++)
{
bug[i][j]=bug[i][j]+rvalue(i,j,k);
}
}
}
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
if(bug[i][j]!=0)
{
return 1;
}
}
}
return 0;
}
int rvalue(int row,int col,int k)
{
int nrow,ncol;
int sum=0;
if(map[row][col]==0)
{
sum=0;
nrow=row+dr[k];
ncol=col+dc[k];
while(nrow>=0&&nrow<8&&ncol>=0&&ncol<8&&map[nrow][ncol]==tmp%2+1)
{
sum++;
nrow=nrow+dr[k];
ncol=ncol+dc[k];
}
if(sum>0&&nrow>=0&&nrow<8&&ncol<8&&ncol>=0&&map[nrow][ncol]==tmp)
{
return sum;
}
else
{
return 0;
}
}
return 0;
}
int isright(int x,int y)
{
int right=0;
int i;
for(i=0; i<8; i++)
{
right=right+rvalue(x,y,i);
}
if(right==0)
{
return 0;
}
else
{
return 1;
}
}