Constantine_7 2021-11-04 23:06 采纳率: 20%
浏览 48
已结题

Java黑白棋问题,请求帮助

题目要求是
Input
The first line is an integer N , which means a total of N moves were made.

The second line is an integer color , indicating the color of the first move.

For the following 8 rows, there are 8 numbers in each row, representing the input 8×8 board.

For the next N rows, each row consists of two integers x and y, indicating the position of the move.
Output
Output of the board after playing chess.

If an invalid move is encountered, output the board before the first invalid move.

  • When output the chessboard, please use printf %3d as alignment

Don't forget to test if the move is valid, if a input move is invalid, output the chessboard before this invalid move.
(这是题目要求原文,我的翻译是第一行输入你要的移动步数,第二行输入你选择的数字(-1or1)选择的数字先走,然后接下来输入8*8的初始棋盘,接下来就是你分别选择的下棋位置,如果这个位置不能放置就直接输出棋盘停止判断。最后输出最后一步放置后的棋盘)
我自己写了代码,我自己输入的例子都完美运行,题给的例子也都没问题,但是上传检测又一直说答案错误(上传的网站不说哪里有问题所以我不知道自己问题在哪),我自己怎么都找不到自己的问题在哪里,想请各位大大帮我找下自己的代码哪里有问题(可能过于的长,因为我是直接从上一小题复制下来的所以就懒得设置新的方法来缩短行数了

```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int time= sc.nextInt();
int which= sc.nextInt();
int otherwhich=0;
if(which==1) {otherwhich=-1;
} else{otherwhich=1;
}
int[][] board=new int[8][8];
for(int y=0;y <8;y ++) {
for(int x=0;x< 8;x ++) {
board[x][y]= sc.nextInt();
}
}
int[][][] cloneboard=new int[time][8][8];
for(int keeper=0;keeper<time;keeper ++) {
for(int y=0;y <8;y ++) {
for(int x=0;x< 8;x ++) {
cloneboard[keeper][x][y]=0;
}
}
}
int[] xfirst=new int[60];
int[] yfirst=new int[60];
int[] xsecond=new int[60];
int[] ysecond=new int[60];
int first=0;
int second=0;
for(int keeper=0;keeper<time;keeper ++) {
if((keeper+2)%2==0) {
yfirst[first]= sc.nextInt();
xfirst[first]= sc.nextInt();
first ++;
}else {
ysecond[second] = sc.nextInt();
xsecond[second] = sc.nextInt();
second ++;
}
}
first =0;
second =0;
xunhuan:
for(int keeper=0;keeper<time;keeper ++) {
if((keeper+2)%2==0) {
for(int y=0;y <8;y ++) {
for(int x=0;x< 8;x ++) {
if(board[x][y] ==which) {
if(y>1) {
if(board[x][y-1] !=board[x][y]&&board[x][y-1]!=0) {
for(int i=y-2;i >= 0;i --) {
if(board[x][i]==0) {
cloneboard[keeper][x][i]=1;
break;
}
if (board[x][i] ==which) {
break;
}
}
}//UP
}
if(y <6) {
if(board[x][y+1] !=board[x][y]&&board[x][y+1]!=0) {
for(int i=y+2;i < 8;i ++) {
if(board[x][i]==0) {
cloneboard[keeper][x][i]=1;
break;
}
if (board[x][i] ==which) {
break;
}
}
} //Down
}
if(x>1) {
if(board[x-1][y] !=board[x][y]&&board[x-1][y]!=0) {
for(int i=x-2;i >= 0;i --) {
if(board[i][y]==0) {
cloneboard[keeper][i][y]=1;
break;
}
if (board[i][y] ==which) {
break;
}
}
}//Left
}
if(x < 6) {
if (board[x + 1][y] != board[x][y] && board[x + 1][y] != 0) {
for (int i = x + 2; i < 8; i++) {
if (board[i][y] ==0) {
cloneboard[keeper][i][y] = 1;
break;
}
if (board[i][y] ==which) {
break;
}
}
} //Right
}
if(x>1&&y>1) {
if (board[x - 1][y-1] != board[x][y] && board[x - 1][y-1] != 0) {
for (int i = 2; x-i >=0&&y-i>=0 ; i++) {
if (board[x-i][y-i] ==0) {
cloneboard[keeper][x-i][y-i] = 1;
break;
}
if (board[x-i][y-i] ==which) {
break;
}
}
}
} //Leftup
if(x<6&&y<6) {
if (board[x + 1][y+1] != board[x][y] && board[x + 1][y+1] != 0) {
for (int i = 2; x+i <8&&y+i<8 ; i++) {
if (board[x+i][y+i] ==0) {
cloneboard[keeper][x+i][y+i] = 1;
break;
}
if (board[x+i][y+i] ==which) {
break;
}
}
}
}// Rightdown
if(x>1&&y<6) {
if (board[x - 1][y+1] != board[x][y] && board[x - 1][y+1] != 0) {
for (int i = 2; x-i >=0&&y+i<8 ; i++) {
if (board[x-i][y+i] ==0) {
cloneboard[keeper][x-i][y+i] = 1;
break;
}
if (board[x-i][y+i] ==which) {
break;
}
}
}
}// Leftdown
if(x<6&&y>1) {
if (board[x + 1][y-1] != board[x][y] && board[x + 1][y-1] != 0) {
for (int i = 2; x+i <8&&y-i>=0 ; i++) {
if (board[x+i][y-i] ==0) {
cloneboard[keeper][x+i][y-i] = 1;
break;
}
if (board[x+i][y-i] ==which) {
break;
}
}
}
}//Rightup
}
}
}
if(cloneboard[keeper][xfirst[first]][yfirst[first]]==1) {
board[xfirst[first]][yfirst[first]]=which;
if(xfirst[first]-2>=0) {
for(int i=1;xfirst[first]-i-1>=0;i++) {
if (board[xfirst[first] - i][yfirst[first]] == otherwhich&&board[xfirst[first]-i-1][yfirst[first]]!=0) {
board[xfirst[first] - i][yfirst[first]] = which;
} else break;
}}
if (yfirst[first]+2<8) {
for(int i=1;yfirst[first]+i+1<8;i ++) {
if( board[xfirst[first]][yfirst[first]+i]==otherwhich&&board[xfirst[first]][yfirst[first]+i+1]!=0) {
board[xfirst[first]][yfirst[first]+i]=which;
}else break;
}}
if (xfirst[first]+2<8) {
for(int i =1;xfirst[first]+i+1<8;i ++) {
if( board[xfirst[first]+i][yfirst[first]]==otherwhich&&board[xfirst[first]+i+1][yfirst[first]]!=0) {
board[xfirst[first]+i][yfirst[first]]=which;
}else break;
}}
if (yfirst[first]-2>=0) {
for(int i=1;yfirst[first]-i-1>=0;i ++) {
if( board[xfirst[first]][yfirst[first]-i]==otherwhich&&board[xfirst[first]][yfirst[first]-i-1]!=0) {
board[xfirst[first]][yfirst[first]-i]=which;
}else break;
}}
if (xfirst[first]+2<8&&yfirst[first]+2<8) {
for(int i=1;xfirst[first]+i+1<8&&yfirst[first]+i+1<8;i ++) {
if( board[xfirst[first]+i][yfirst[first]+i]==otherwhich&&board[xfirst[first]+i+1][yfirst[first]+i+1]!=0) {
board[xfirst[first]+i][yfirst[first]+i]=which;
}else break;
}}
if (xfirst[first]-2>=0&&yfirst[first]-2>=0) {
for(int i=1;xfirst[first]-i-1>=0&&yfirst[first]-i-1>=0;i ++) {
if( board[xfirst[first]-i][yfirst[first]-i]==otherwhich&&board[xfirst[first]-i-1][yfirst[first]-i-1]!=0) {
board[xfirst[first]-i][yfirst[first]-i]=which;
}else break;
}}
if (xfirst[first]+2<8&&yfirst[first]-2>=0) {
for(int i=1;xfirst[first]+i+1<8&&yfirst[first]-i-1>=0;i ++)
if( board[xfirst[first]+i][yfirst[first]-i]==otherwhich&&board[xfirst[first]+i+1][yfirst[first]-i-1]!=0) {
board[xfirst[first]+i][yfirst[first]-i]=which;
}else break;
}
if (xfirst[first]-2>=0&&yfirst[first]+2<8) {
for(int i =1;xfirst[first]-i-1>=0&&yfirst[first]+i+1<8;i ++) {
if( board[xfirst[first]-i][yfirst[first]+i]==otherwhich&&board[xfirst[first]-i-1][yfirst[first]+i+1]!=0) {
board[xfirst[first]-i][yfirst[first]+i]=which;
}else break;
}}
}else {
break xunhuan;
}first ++;
}else {for(int y=0;y <8;y ++) {
for(int x=0;x< 8;x ++) {
if(board[x][y] ==otherwhich) {
if(y>1) {
if(board[x][y-1] !=board[x][y]&&board[x][y-1]!=0) {
for(int i=y-2;i >= 0;i --) {
if(board[x][i]==0) {
cloneboard[keeper][x][i]=1;
break;
}
if (board[x][i] ==otherwhich) {
break;
}
}
}//UP
}
if(y <6) {
if(board[x][y+1] !=board[x][y]&&board[x][y+1]!=0) {
for(int i=y+2;i < 8;i ++) {
if(board[x][i]==0) {
cloneboard[keeper][x][i]=1;
break;
}
if (board[x][i] ==otherwhich) {
break;
}
}
} //Down
}
if(x>1) {
if(board[x-1][y] !=board[x][y]&&board[x-1][y]!=0) {
for(int i=x-2;i >= 0;i --) {
if(board[i][y]==0) {
cloneboard[keeper][i][y]=1;
break;
}
if (board[i][y] ==otherwhich) {
break;
}
}
}//Left
}
if(x < 6) {
if (board[x + 1][y] != board[x][y] && board[x + 1][y] != 0) {
for (int i = x + 2; i < 8; i++) {
if (board[i][y] ==0) {
cloneboard[keeper][i][y] = 1;
break;
}
if (board[i][y] ==otherwhich) {
break;
}
}
} //Right
}
if(x>1&&y>1) {
if (board[x - 1][y-1] != board[x][y] && board[x - 1][y-1] != 0) {
for (int i = 2; x-i >=0&&y-i>=0 ; i++) {
if (board[x-i][y-i] ==0) {
cloneboard[keeper][x-i][y-i] = 1;
break;
}
if (board[x-i][y-i] ==otherwhich) {
break;
}
}
}
} //Leftup
if(x<6&&y<6) {
if (board[x + 1][y+1] != board[x][y] && board[x + 1][y+1] != 0) {
for (int i = 2; x+i <8&&y+i<8 ; i++) {
if (board[x+i][y+i] ==0) {
cloneboard[keeper][x+i][y+i] = 1;
break;
}
if (board[x+i][y+i] ==otherwhich) {
break;
}
}
}
}// Rightdown
if(x>1&&y<6) {
if (board[x - 1][y+1] != board[x][y] && board[x - 1][y+1] != 0) {
for (int i = 2; x-i >=0&&y+i<8 ; i++) {
if (board[x-i][y+i] ==0) {
cloneboard[keeper][x-i][y+i] = 1;
break;
}
if (board[x-i][y+i] ==otherwhich) {
break;
}
}
}
}// Leftdown
if(x<6&&y>1) {
if (board[x + 1][y-1] != board[x][y] && board[x + 1][y-1] != 0) {
for (int i = 2; x+i <8&&y-i>=0 ; i++) {
if (board[x+i][y-i] ==0) {
cloneboard[keeper][x+i][y-i] = 1;
break;
}
if (board[x+i][y-i] ==otherwhich) {
break;
}
}
}
}//Rightup
}
}
}
if(cloneboard[keeper][xsecond[second]][ysecond[second]]==1) {
board[xsecond[second]][ysecond[second]]=otherwhich;
if(xsecond[second]-2>=0) {
for(int i =1;xsecond[second]-i-1>=0;i ++) {
if( board[xsecond[second]-i][ysecond[second]]==which&&board[xsecond[second]-i-1][ysecond[second]]!=0) {
board[xsecond[second]-i][ysecond[second]]=otherwhich;
}else break;
}}
if (ysecond[second]+2<8) {
for(int i=1;ysecond[second]+i+1<8;i ++) {
if( board[xsecond[second]][ysecond[second]+i]==which&&board[xsecond[second]][ysecond[second]+i+1]!=0) {
board[xsecond[second]][ysecond[second]+i]=otherwhich;
}else break;
}}
if (xsecond[second]+2<8) {
for(int i=1;xsecond[second]+i+1<8;i ++) {
if( board[xsecond[second]+i][ysecond[second]]==which&&board[xsecond[second]+i+1][ysecond[second]]!=0) {
board[xsecond[second]+i][ysecond[second]]=otherwhich;
}else break;
}}
if (ysecond[second]-2>=0) {
for(int i=1;ysecond[second]-i-1>=0;i ++) {
if( board[xsecond[second]][ysecond[second]-i]==which&&board[xsecond[second]][ysecond[second]-i-1]!=0) {
board[xsecond[second]][ysecond[second]-i]=otherwhich;
}else break;
}}
if (xsecond[second]+2<8&&ysecond[second]+2<8) {
for(int i=1;xsecond[second]+i+1<8&&ysecond[second]+i+1<8;i ++) {
if( board[xsecond[second]+i][ysecond[second]+i]==which&&board[xsecond[second]+i+1][ysecond[second]+i+1]!=0) {
board[xsecond[second]+i][ysecond[second]+i]=otherwhich;
}else break;
}}
if (xsecond[second]-2>=0&&ysecond[second]-2>=0) {
for(int i=1;xsecond[second]-i-1>=0&&ysecond[second]-i-1>=0;i ++) {
if( board[xsecond[second]-i][ysecond[second]-i]==which&&board[xsecond[second]-i-1][ysecond[second]-i-1]!=0) {
board[xsecond[second]-i][ysecond[second]-i]=otherwhich;
}else break;
}}
if (xsecond[second]+2<8&&ysecond[second]-2>=0) {
for(int i=1;xsecond[second]+i+1<8&&ysecond[second]-i-1>=0;i ++) {
if( board[xsecond[second]+i][ysecond[second]-i]==which&&board[xsecond[second]+i+1][ysecond[second]-i-1]!=0) {
board[xsecond[second]+i][ysecond[second]-i]=otherwhich;
}else break;
}}
if (xsecond[second]-2>=0&&ysecond[second]+2<8) {
for(int i=1;xsecond[second]-i-1>=0&&ysecond[second]+i+1<8;i ++) {
if( board[xsecond[second]-i][ysecond[second]+i]==which&&board[xsecond[second]-i-1][ysecond[second]+i+1]!=0) {
board[xsecond[second]-i][ysecond[second]+i]=otherwhich;
}else break;
}}
}else {
break xunhuan;
}second ++;
}
}
for(int y=0;y <8;y ++) {
for(int x=0;x< 8;x ++) {
System.out.printf("%3d",board[x][y]);
}
System.out.println();
}
}
}

```)

  • 写回答

1条回答 默认 最新

  • 有问必答小助手 2021-11-08 09:57
    关注

    你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


    本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


    因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。

    评论

报告相同问题?

问题事件

  • 系统已结题 11月12日
  • 创建了问题 11月4日

悬赏问题

  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
  • ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
  • ¥15 io.jsonwebtoken.security.Keys
  • ¥15 急,ubuntu安装后no caching mode page found等
  • ¥15 联想交换机NE2580O/NE1064TO安装SONIC
  • ¥15 防火墙的混合模式配置