查找方形2D数组中任意随机点的对角线

时间:2016-03-15 15:13:43

标签: java arrays loops matrix intellij-idea

我必须编写设置8x8矩阵,棋盘的代码,然后询问用户他们想要放置女王的行和列。然后,我必须在女王可以移动的每个方格上放一个*。在女王可以移动到的行和列上加*并不困难,但是我无法正确地将对角线标记为女王可以移动的对角线。这是我到目前为止尝试找到对角线的代码:

    char[][] chessboard = new char[8][8];
            System.out.print("What row do you want to place the queen on? ");
            int row = console.nextInt();
            System.out.print("What column do you want to place the queen on? ");
            int column = console.nextInt();

            char queen = 'Q';
            chessboard[row - 1][column - 1] = queen;

            // column and rows
            for (int i = 0; i < chessboard.length; i++) {
                for (int j = 0; j < chessboard[0].length; j++) {
                    if ((i == 2 || j == 6) && chessboard[i][j] != queen) {
                        chessboard[i][j] = '*';
                    }
                }
            }

            if ((row - 1) != 0) {
                // left diagonal
                for (int i = 0; i < row; i++) {
                    for (int j = (column - 1 - (row - 1)); ((column - 1) - (row - 1)) <= j && j < column; j++) {
                        if (chessboard[i][j] != queen) {
                            chessboard[i][j] = '*';
                        }
                    }
                }
                for (int i = (row - 1) + (8 - (column)); i >= row - 1; i--) {
                    for (int j = 7; j >= (column - 1); j--) {
                        if (chessboard[i][j] != queen) {
                            chessboard[i][j] = '*';
                        }
                    }
                }

                // right diagonal
                for (int i = 7; i >= row - 1; i--) {
                    for (int j = (column - 1 - (row - 1)); 0 <= j && j < column; j--) {
                        if (chessboard[i][j] != queen) {
                            chessboard[i][j] = '*';
                        }
                    }
                }

            }
 for (int i = 0; i < chessboard.length; i++) {
            for (int j = 0; j < chessboard[0].length; j++) {
                System.out.print(chessboard[i][j] + " ");
            }
            System.out.println();
        }
    }

当我输入实验值时,例如第3行和第7列,我得到一个非常混乱的输出。对于上面的数字,我得到:

[][][][] * * * [] 
[][][][] * * * [] 
* * * *  * * Q  * 
* * * * * [] *  * 
* * * * * [] * [] 
* * * * * [] * [] 
* * * * * [] * [] 
* * * * * [] * []

有人能告诉我哪里出错了吗? *这是一个家庭作业问题,请仅在答案中编码。谢谢!

1 个答案:

答案 0 :(得分:2)

如果是我,我只需循环遍历棋盘的每个方格,并测试每个方格的有效性,即女王可以移动到的三个可能的规则,即

square.x == queen.x ||
square.y == queen.y ||
|square.x - queen.x| == |square.y - queen.y|

如果您的方格符合上述任何规则,那么它就是一个有效的方格移动,否则它不是。省略女王目前居住的广场,当然

square.x != queen.x && square.y != queen.y

伪代码:

for (int i = 0; i < chessboard.length; i++) {
    for (int j = 0; j < chessboard[0].length; j++) {
        // You could encapsulate these lines in a isValidSquareForQueenMove(x, y) method.
        boolean isSquareValid = false;
        isSquareValid = isSquareValid || x == queen.x;
        isSquareValid = isSquareValid || y == queen.y;
        isSquareValid = isSquareValid || Math.abs(queen.x - x) == Math.abs(queen.y - y);
        isSquareValid = isSquareValid && x != queen.x && y != queen.y;

        // Do stuff here.
    }
}