我的这道题的代码还是比较简洁的哈哈。
version 1:
#include<cstdio>
#include<cstring>
using namespace std;
int n,wn,bn;
int dxs[3] = {0,1,-1};
int dys[3] = {0,1,-1};
char c,gamer,cmd[5],game[9][9];
char readchar(){
for(;;){
c = getchar();
if(c != '\n' && c != '\r') return c;
}
}
int isLegal(int x,int y){
if(game[x][y] != '-') return 0;
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
if(!dxs[i] && !dys[j]) continue;
int dx = dxs[i],dy = dys[j];
int mark = 0,cx = x,cy = y;
while(cx+dx>0 && cx+dx<9 && cy+dy>0 && cy+dy<9){
cx += dx,cy += dy;
if(game[cx][cy] == '-') break;
if(game[cx][cy] != gamer) mark = 1;
else{
if(mark) return 1;
else break;
}
}
}
}
return 0;
}
void possMove(){
int first = 1;
for(int i = 1;i < 9;i++){
for(int j = 1;j < 9;j++){
if(isLegal(i,j)){
if(!first) putchar(' ');
printf("(%d,%d)",i,j);
first = 0;
}
}
}
if(first) printf("No legal move.");
putchar('\n');
}
void move(char cmd[]){
int x = cmd[1] - '0',y = cmd[2] - '0';
if(!isLegal(x,y)) gamer = (gamer == 'B' ? 'W' : 'B');
game[x][y] = gamer;
gamer == 'B' ? bn++ : wn++;
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
if(!dxs[i] && !dys[j]) continue;
int dx = dxs[i],dy = dys[j];
int mark = 0,cx = x,cy = y;
while(cx+dx>0 && cx+dx<9 && cy+dy>0 && cy+dy<9){
cx += dx,cy += dy;
if(game[cx][cy] == '-') break;
if(game[cx][cy] != gamer) mark = 1;
else{
if(mark){
while(cx - dx != x || cy - dy != y){
//while(cx-dx>0 && cx-dx<9 && cy-dy>0 && cy-dy<9 && game[cx-dx][cy-dy] != gamer){
cx -= dx,cy -= dy;
game[cx][cy] = gamer;
if(gamer == 'W') wn++,bn--;
else wn--,bn++;
}
break;
}
else break;
}
}
}
}
printf("Black - %2d White - %2d\n",bn,wn);
gamer = (gamer == 'B' ? 'W' : 'B');
}
int main(){
int kase = 0;
scanf("%d",&n);
while(n--){
wn = bn = 0;
for(int i = 1;i < 9;i++){
for(int j = 1;j < 9;j++){
c = readchar();
game[i][j] = c;
if(c == 'W') wn++;
if(c == 'B') bn++;
}
}
gamer = readchar();
if(++kase > 1) putchar('\n');
while(scanf("%s",cmd) && cmd[0] != 'Q'){
if(cmd[0] == 'L') possMove();
else move(cmd);
}
for(int i = 1;i < 9;i++){
for(int j = 1;j < 9;j++){
putchar(game[i][j]);
}
putchar('\n');
}
}
return 0;
}
version 2:
#include<cstdio>
#include<cstring>
using namespace std;
int n,wn,bn,cmark = 0;//cmark 使用search函数检查是否合法还是进行移动
int dxs[3] = {0,1,-1};//每个棋子检查8个方向
int dys[3] = {0,1,-1};
char c,gamer,cmd[5],game[9][9];
char readchar(){
for(;;){
c = getchar();
if(c != '\n' && c != '\r') return c;
}
}
int search(int x,int y){
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
if(!dxs[i] && !dys[j]) continue;
int dx = dxs[i],dy = dys[j];
int mark = 0,cx = x,cy = y;
while(cx+dx>0 && cx+dx<9 && cy+dy>0 && cy+dy<9){
cx += dx,cy += dy;
if(game[cx][cy] == '-') break;
if(game[cx][cy] != gamer) mark = 1;
else{
if(mark){
if(!cmark) return 1;//只是检查合法的话直接返回就好
while(cx - dx != x || cy - dy != y){//原路返回,将棋子置反
cx -= dx,cy -= dy;
game[cx][cy] = gamer;
if(gamer == 'W') wn++,bn--;
else wn--,bn++;
}
break;
}else break;
}
}
}
}
return 0;
}
int isLegal(int x,int y){
if(game[x][y] != '-') return 0;
if(search(x,y)) return 1;
return 0;
}
void possMove(){
int first = 1;
for(int i = 1;i < 9;i++){
for(int j = 1;j < 9;j++){
if(isLegal(i,j)){
if(!first) putchar(' ');
printf("(%d,%d)",i,j);
first = 0;
}
}
}
if(first) printf("No legal move.");
putchar('\n');
}
void move(char cmd[]){
int x = cmd[1] - '0',y = cmd[2] - '0';
if(!isLegal(x,y)) gamer = (gamer == 'B' ? 'W' : 'B');
cmark = 1;//cmark只在移动的时候置1
game[x][y] = gamer;
gamer == 'B' ? bn++ : wn++;
search(x,y);
cmark = 0;
printf("Black - %2d White - %2d\n",bn,wn);
gamer = (gamer == 'B' ? 'W' : 'B');//移动之后更换gamer
}
int main(){
int kase = 0;
scanf("%d",&n);
while(n--){
wn = bn = 0;
for(int i = 1;i < 9;i++){
for(int j = 1;j < 9;j++){
c = readchar();//输入要注意
game[i][j] = c;
if(c == 'W') wn++;
if(c == 'B') bn++;
}
}
gamer = readchar();
if(++kase > 1) putchar('\n');
while(scanf("%s",cmd) && cmd[0] != 'Q'){
if(cmd[0] == 'L') possMove();
else move(cmd);
}
for(int i = 1;i < 9;i++){
for(int j = 1;j < 9;j++){
putchar(game[i][j]);
}
putchar('\n');
}
}
return 0;
}