uva 220 趣味黑白棋 精簡程式碼
阿新 • • 發佈:2019-02-03
#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;
}
#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;
}