我如何在骑士巡演[java]的棋盘上移动

我如何在骑士巡演[java]的棋盘上移动,java,Java,我想做一个骑士巡演节目。现在我有了它,所以骑士从棋盘中间开始绕着一个圈移动。但是现在如果我想让骑士从0,0开始,我只会得到一个错误,说数组超出范围。我猜我需要一种方法来检查移动下一点是否安全,但我不太确定 这是我现在的代码。 public class Knight1 { public static void main(String[] args) { int board[][] = new int[8][8]; int horizontal[] = ne

我想做一个骑士巡演节目。现在我有了它,所以骑士从棋盘中间开始绕着一个圈移动。但是现在如果我想让骑士从0,0开始,我只会得到一个错误,说数组超出范围。我猜我需要一种方法来检查移动下一点是否安全,但我不太确定

这是我现在的代码。

public class Knight1 {

    public static void main(String[] args) {
        int board[][] = new int[8][8];
        int horizontal[] = new int[8];
        int vertical[] = new int[8];

        horizontal[0] = 2;
        horizontal[1] = 1;
        horizontal[2] = -1;
        horizontal[3] = -2;
        horizontal[4] = -2;
        horizontal[5] = -1;
        horizontal[6] = 1;
        horizontal[7] = 2;

        vertical[0] = -1;
        vertical[1] = -2;
        vertical[2] = -2;
        vertical[3] = -1;
        vertical[4] = 1;
        vertical[5] = 2;
        vertical[6] = 2;
        vertical[7] = 1;

        int moveNumber = 0;
        int currentRow = 0;
        int currentCol = 0;
        int counter = 1;
        int x = 0;
        int y = 7;
        int xOffset[] = {x + 1, x + 1, x + 2, x + 2, x - 1, x - 1, x - 2, x - 2};
        int yOffset[] = {y - 2, y + 2, y - 1, y + 1, y - 2, y + 2, y - 1, y + 1};
        for (int i = 0; i < 8; i++) {
            if (xOffset[i] >= 0 && yOffset[i] >= 0 && xOffset[i] < 8 && yOffset[i] < 8) {
                currentRow = xOffset[i];
                currentCol =  yOffset[i];
                Board[currentRow][currentCol] = counter;
                counter++;
            }
        }
        printBoard(board);
    }

    public static void printBoard(int[][] board) {
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++) {
                System.out.print("  " + board[x][y]);
            }
            System.out.println();
        }
    }
}
公共等级骑士1{
公共静态void main(字符串[]args){
国际板[][]=新国际[8][8];
int水平[]=新int[8];
整数垂直[]=新整数[8];
水平[0]=2;
水平[1]=1;
水平[2]=-1;
水平[3]=-2;
水平[4]=-2;
水平[5]=-1;
水平[6]=1;
水平[7]=2;
垂直[0]=-1;
垂直[1]=-2;
垂直[2]=-2;
垂直[3]=-1;
垂直[4]=1;
垂直[5]=2;
垂直[6]=2;
垂直[7]=1;
int moveNumber=0;
int currentRow=0;
int currentCol=0;
int计数器=1;
int x=0;
int y=7;
int xOffset[]={x+1,x+1,x+2,x+2,x-1,x-1,x-2,x-2};
int yOffset[]={y-2,y+2,y-1,y+1,y-2,y+2,y-1,y+1};
对于(int i=0;i<8;i++){
如果(xOffset[i]>=0&&yOffset[i]>=0&&xOffset[i]<8&&yOffset[i]<8){
currentRow=xOffset[i];
currentCol=yOffset[i];
线路板[currentRow][currentCol]=计数器;
计数器++;
}
}
印刷板;
}
公共静态无效打印板(int[][]板){
对于(int x=0;x<8;x++){
对于(int y=0;y<8;y++){
系统输出打印(“+board[x][y]);
}
System.out.println();
}
}
}

问题处于while状态,解决方法提示是当currentRow/Column为0时必须排除-2和-1(如果为1,则仅排除-2),当currentRow/Column为7时也必须排除+1和+2(如果为6,则仅排除+2),否则您的board[currentRow][currentCol]中的索引=计数器;将被禁止入内。
以下是骑士移动的规则:

    int x = 0;
    int y = 7;
    int xOffset[] = {x + 1, x + 1, x + 2, x + 2, x - 1, x - 1, x - 2, x - 2};
    int yOffset[] = {y - 2, y + 2, y - 1, y + 1, y - 2, y + 2, y - 1, y + 1};
    for (int i = 0; i < 8; i++) {
        if (xOffset[i] >= 0 && yOffset[i] >= 0 && xOffset[i] < 8 && yOffset[i] < 8) {
            // X and Y are currentRow and currentColumn in your example
            // Code is copied from my java chess game and I was lazy to change variable everywhere
            x = xOffset[i];
            y = yOffset[i];
        }
    }
intx=0;
int y=7;
int xOffset[]={x+1,x+1,x+2,x+2,x-1,x-1,x-2,x-2};
int yOffset[]={y-2,y+2,y-1,y+1,y-2,y+2,y-1,y+1};
对于(int i=0;i<8;i++){
如果(xOffset[i]>=0&&yOffset[i]>=0&&xOffset[i]<8&&yOffset[i]<8){
//在您的示例中,X和Y是currentRow和currentColumn
//代码是从我的java国际象棋游戏中复制的,我懒得到处更改变量
x=xOffset[i];
y=偏移量[i];
}
}

哪一行给出了
数组索引OutofBoundsException
?在while循环中放入一些
System.out.println
,并检查那些
currentRow
currentCol
的值。你的步骤列表肯定有问题(事实上,写得很混乱)当我将currentCol/currentRow设置为0时,我得到了ArrayIndexOutofBounds异常。看起来你把一个移动标识符(骑士的八种可能移动之一)和一个移动编号(每一个移动增加一个)混在一起了。例如,从(7,4)开始,您的第一步不一定是“移动标识符0”,即(-1,+2)。。。这会把你带到(6,6)。对于骑士之旅,您需要在棋盘上搜索要执行的正确移动,可能需要使用递归。8x8棋盘上的骑士巡演总共包括64个移动,当然,这比骑士的8个可能移动要大得多。啊,好吧,我想我明白你的意思了,我尝试使用递归,看看会发生什么。我将棋盘[]添加到if块中,这样我可以设置移动数。它似乎只做了一步。即使我在if块中添加while循环,它也只经过一次。可能是我遗漏了什么/做错了什么。你能发布更新的代码吗?这样我们就可以看到问题所在了?更新了我发布的原始代码。它仍然有点凌乱你的代码对我有效,这里是x=0和y=5:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0