uva 220 趣味黑白棋 精简代码

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
char map[20][20];
int row[2][8]={1,1,0,-1,-1,-1,0,1,
                       0,1,1,1,0,-1,-1,-1};//八个方向向量
char change(char sb)
{
 if(sb=='W')return 'B';
 else return 'W';
}
void statistics()//统计黑子白子数目并输出
{
 int B=0,W=0;
 for(int i=1;i<=8;i++)
 {
  for(int j=1;j<=8;j++)
  {
   if(map[i][j]=='B')B++;
   if(map[i][j]=='W')W++;
  }
 }
 printf("Black - %2d White - %2d\n",B,W);
}
void print()//输出棋盘
{
 for(int i=1;i<=8;i++)
 {
  for(int j=1;j<=8;j++)
  {
   putchar(map[i][j]);
  }
  cout<<"\n";
 }
}
void getmap()//输入棋盘
{
 for(int i=1;i<=8;i++)
 for(int j=1;j<=8;j++)
 cin>>map[i][j];
}
int yuejie(int x,int y)//如果越界返回1 否则返回0
{
 if(x<1||x>8||y<1||y>8)return 1;
 else return 0;
}
int tothere(char sb,int X,int Y)//sb可以走在这里返回1  否则返回0
{
 if(map[X][Y]!='-')return 0;
 int x,y;
 char other;
 other=change(sb);
 for(int i=0;i<8;i++)
 {
  x=X+row[0][i];
  y=Y+row[1][i];
  if(map[x][y]!=other)continue;
  while(map[x][y]==other)
  {
   x+=row[0][i];
   y+=row[1][i];
   if(map[x][y]==sb)return 1;
  }
 }
 return 0;
}
int list(char sb)//列出sb可以走的位置 无法走输出 "No legal move."
{
 int mark=0,wh=1;
 for(int i=1;i<=8;i++)
 {
  for(int j=1;j<=8;j++)
  {
   if(tothere(sb,i,j))
   {
    if(wh!=1)cout<<' ';//除了第一对数据,都要在前面输出空格,这样才能保证仅两对数据间有空格
    wh=5;
    printf("(%d,%d)",i,j);
    mark=1;
   }
  }
 }
 if(mark==0)cout<<"No legal move.";
 cout<<"\n";
}
void downthis(char sb,int X,int Y)//往某处走一颗子 注意要在主函数用tothere()确认能走在这里
{
 int x,y;
 char other;
 other=change(sb);
 map[X][Y]=sb;
 for(int i=0;i<8;i++)
 {
  x=X+row[0][i];
  y=Y+row[1][i];
  if(map[x][y]!=other)continue;
  while(map[x][y]==other)
  {
   x+=row[0][i];
   y+=row[1][i];
   if(map[x][y]==sb)
   {
    x-=row[0][i];
    y-=row[1][i];
    while(map[x][y]==other)
    {
     map[x][y]=sb;
     x-=row[0][i];
     y-=row[1][i];
    }
   }
  }
 }
}
int main()
{
 //freopen("input.txt","r",stdin);//提交时删除
 int counts;
 char dowhat,whogo;
 cin>>counts;
 while(counts--)
 {
  memset(map,0,sizeof(map));
  getmap();
  cin>>whogo;
  while(1)
  {
   cin>>dowhat;
   if(dowhat=='Q')
   {
    print();
    if(counts!=0)cout<<'\n';
    goto fina;
   }
   if(dowhat=='L')
    list(whogo);
   if(dowhat=='M')
   {
    int a,b;
    a=getchar()-'0';
    b=getchar()-'0';
    if(!tothere(whogo,a,b))whogo=change(whogo);
    downthis(whogo,a,b);
    whogo=change(whogo);
    statistics();
   }
  }
  fina:;
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会写代码的孙悟空

赠人玫瑰 手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值