搜索三步的黑白棋



#include<iostream>
#include<stdlib.h>

using namespace std;


int comcolor=1;//颜色1
int color=-1; //颜色2
int row = 0, col = 0;
int cb[8][8] = {0};
int dr[8] = {0,1,1,1,0,-1,-1,-1};
int dc[8] = {-1,-1,0,1,1,1,0,-1};
int flag = 0;
int can = 0;
int s[8][8]={0};
int num=0;


void playgame();
int comgo();
int com2go();
int isend();
void mango();
int canmove(int nowrow,int nowcol,int dire,int tmpcolor);
void clean(int nowrow,int nowcol,int tmpcolor);
void end();
void show();
int clean2(int nowrow,int nowcol,int tmpcolor);

int main()
{
 cb[3][3]=1;
 cb[4][4]=1;
 cb[3][4]=-1;
 cb[4][3]=-1;
 
 
  show();
 
 cout <<"选择你的棋子颜色,1表示黑棋,-1表示白棋"<<endl;
 cin >> color;
 
  comcolor = color *(-1);
 
 if(comcolor == 1)
 {
  comgo();
 }
 
 playgame();
 
 return 0;
}

void playgame()//游戏
{
 int v=0;
 system("cls"); 
 show(); 
 
 while(1)
 {
  num=0;
  flag = 0;
  can = 0;
  mango();
  comgo();
  num=0;
  system("cls");
  show();
  v++;
  
  if(can == 0)
  {
   break;
  }
 }
 end();
 
 cout<<v;
}

void show() //显示地图
{
 int i,j;
 
 for(i=0;i<8;i++)
 {
  for(j=0;j<8;j++)
  {
   if(cb[i][j] != -1)
   {
    cout<< cb[i][j]<<" ";
   }
   else
   {
    cout<<cb[i][j]+3<<" ";
   }
  }
  cout<< endl;
 }
}

int isend() //判断是否还能走
{
 int i,j,k;
 
 for(i=0;i<8;i++)
 {
  for(j=0;j<8;j++)
  {
   for(k=0;k<8;k++)
   {
    if(canmove(i,j,k,color))
    {
     return 1;
    }
   }
  }
 }
 return 0;
}


void mango() //用户走
{
 int i,j,k;
 int x,y;
 int q=0;
 if(isend() == 1)
 {
  while(1)
  {
   cin>>x >> y;
   q=0;
   
   for(i=0;i<8;i++)
   {
    if(cb[x-1][y-1] != 0 )
    {
     break;
    }
    if(canmove(x-1,y-1,i,color))
    {
     cb[x-1][y-1] = color;
     
     clean(x-1,y-1,color);
     
     q++;
     can++; 
     break; 
    }
   }
   
   if(q != 0)
   {
    break;
   }
  }
 }
}


void end()
{
 int i,j;
 int sum1,sum2;
 
 sum1 = 0;
 sum2 = 0;
 
 for(i=0;i<8;i++)
 {
  for(j=0;j<8;j++)
  {
   if(cb[i][j] == comcolor)
   {
    sum1++;
   }
   if(cb[i][j] == color)
   {
    sum2++;
   }
  }
 }
 
 if(sum1 >= sum2)
 {
  cout<<"第一个"<<endl;
 }
 else
 {
  cout<<"第二个"<<endl;
 }
}


int com2go() //电脑2走
{
 int i,j,k;
 int tmp1=-1,tmp2=-1,tmp3=0;
 
 for(i=0;i<8;i++)
 {
  for(j=0;j<8;j++)
  {
   for(k=0;k<8;k++)
   {
    if(cb[i][j] == 0 && canmove(i,j,k,color)!=0)
    {
     s[i][j] = s[i][j] - clean2(i,j,color);
     
    }
   }
  }
 }
 num++;
 
 comgo();
 
 return 0;
}


int comgo() //电脑走
{
 int i,j,k;
 int tmp1 = -1,tmp2 = -1,tmp3 = 0;
 
 for(i=0;i<8;i++)
 {
  for(j=0;j<8;j++)
  {
   for(k=0;k<8;k++)
   {
    if(cb[i][j] == 0 && canmove(i,j,k,comcolor)!=0)
    {
     s[i][j] = s[i][j] +  clean2(i,j,comcolor);
     
    }
   }
  }
 }
 if(num == 0)
 {
  com2go();
  num++;
 }
 
 if(num == 2)
 {
  for(i=0; i < 8;i ++)
  {
   for(j=0;j < 8; j++)
   {
    if(s[i][j] > 0)
    {
     if(s[i][j] > tmp3)
     {
      tmp1 = i;
      tmp2 = j;
      tmp3 = s[i][j];
     }
    }
   }
  }
  
  if(tmp1 >= 0 && tmp2 >= 0)
  {
   cb[tmp1][tmp2] = comcolor;
   can++;
   clean(tmp1,tmp2,comcolor);
  }
  
  for(i=0;i<8;i++)
  {
   for(j=0;j<8;j++)
   {
    s[i][j]=0;
   }
  }
 }
return 0;
}


int canmove(int nowrow,int nowcol,int dire,int tmpcolor) //判断是否能走
{
 int i,j;
 int row1 = 0,col1 = 0;
 int num=0;
 
 flag = 0;
 
 if(cb[nowrow][nowcol] != 0)
 {
  return 0;
 }
 
 nowrow = nowrow + dr[dire];
 nowcol = nowcol + dc[dire];
 
 while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)
 {
  nowrow = nowrow + dr[dire];
  nowcol = nowcol + dc[dire];
  num++;
 }
 
 if(cb[nowrow][nowcol] == tmpcolor && num > 0)
 {
  flag = 1;
 }
 if(nowrow >= 0 && nowrow < 8 && nowcol>=0 && nowcol<8)
 {
  return (flag);
 }
 
 return 0;
}

void clean(int nowrow,int nowcol,int tmpcolor) //清理被吃掉的棋子
{
 int i;
 int t1,t2;
 int p=0;
 
 t1=nowrow;
 t2=nowcol;
 
 for(i=0;i<8;i++)
 {
  nowrow=t1;
  nowcol=t2;
  
  nowrow = nowrow + dr[i];
  nowcol = nowcol + dc[i];
  p=0;
  while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)
  {     
   nowrow = nowrow + dr[i];
   nowcol = nowcol + dc[i];
   p++;
  }
  if(cb[nowrow][nowcol] == tmpcolor && p != 0)
  {  
   nowrow = nowrow - dr[i];
   nowcol = nowcol - dc[i];
   while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)
   {         
    cb[nowrow][nowcol] = tmpcolor;
    nowrow = nowrow - dr[i];
    nowcol = nowcol - dc[i];
   }
  }
 }
}


int clean2(int nowrow,int nowcol,int tmpcolor)  
{
 int i;
 int t1,t2;
 int p=0;
 int q;
 
 t1 = nowrow;
 t2 = nowcol;
 
 q = 0;
 for(i=0;i<8;i++)
 {
  nowrow=t1;
  nowcol=t2;
  
  nowrow = nowrow + dr[i];
  nowcol = nowcol + dc[i];
  p=0;
  while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)
  {
   nowrow = nowrow + dr[i];
   nowcol = nowcol + dc[i];
   p++;
  }
  if(cb[nowrow][nowcol] == tmpcolor && p != 0)
  {
   nowrow = nowrow - dr[i];
   nowcol = nowcol - dc[i];
   while(cb[nowrow][nowcol] == tmpcolor*(-1) && nowrow >= 0 && nowrow < 8 && nowcol >= 0 && nowcol <8)
   {
    q++;
    nowrow = nowrow - dr[i];
    nowcol = nowcol - dc[i];
   }
  }
 }
 return q;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值