国际象棋,检查路径是否空闲无法正常工作
Chess, checking if path is free is not working correctly
我正在使用 Swing 在 Java 中下国际象棋,并且我已经弄清楚如何检查每个棋子的合法移动。那部分没有错。但是现在,我在检查路径是否空闲时遇到了问题。例如,国际象棋中的所有棋子都不能跳过一个棋子,除非它是马。如果一个棋子挡住了象的路径,它不能比棋子越过。我调用了检查路径是否空闲的方法isVectorFree()
。它适用于水平和垂直路径,但不适用于对角线路径。
这是 isVectorFree()
的代码:
public boolean isVectorFree(int x1, int y1, int x2, int y2) {
if (x1 == x2 && y1 > y2) { // horizontal north
for (int i = y1 - 1; i > y2; i--) {
if (piecePositions[i][x1] != null) {
return false;
}
}
} else if (x1 < x2 && y1 == y2) { // horizontal east
for (int i = x1 + 1; i < x2; i++) {
if (piecePositions[y1][i] != null) {
return false;
}
}
} else if (x1 == x2 && y1 < y2) { // horizontal south
for (int i = y1 + 1; i < y2; i++) {
if (piecePositions[i][x1] != null) {
return false;
}
}
} else if (x1 > x2 && y1 == y2) { // horizontal west
for (int i = x1 - 1; i > x2; i--) {
if (piecePositions[y1][i] != null) {
return false;
}
}
}
else if (x1 < x2 && y1 > y2) { // diagonal northeast
// these diagonals aren't working properly
for (int row = x1 + 1; row < x2; row++) {
for (int col = y1 - 1; col >= y2; col--) {
if (piecePositions[row][col] != null) {
return false;
}
}
}
} else if (x1 < x2 && y1 < y2) { // diagonal southeast
for (int row = x1 + 1; row < x2; row++) {
for (int col = y1 + 1; col < y2; col++) {
if (piecePositions[row][col] != null) {
return false;
}
}
}
} else if (x1 > x2 && y1 < y2) { // diagonal southwest
for (int row = x1 - 1; row >= x2; row--) {
for (int col = y1 + 1; col < y2; col++) {
if (piecePositions[row][col] != null) {
return false;
}
}
}
} else if (x1 > x2 && y1 > y2) { // diagonal northwest
for (int row = x1 - 1; row >= x2; row--) {
for (int col = y1 - 1; col >= y2; col--) {
if (piecePositions[row][col] != null) {
return false;
}
}
}
}
return true;
}
此方法采用矩阵 piecePositions
中棋子的 x 坐标 x1
和 y 坐标 y1
以及所需的 x 坐标 x2
和所需的 y 坐标 y2
以查看当前坐标和所需坐标之间的路径是否空闲,但正如我之前所说,这对对角线不起作用。具体来说,isVectorFree()
受到周围棋子的影响。在棋盘的起始位置,如果我将棋子移开为象让路,即使现在象的路径是空闲的,它仍然不能在整个路径中移动任何地方,出于某种原因。
我该如何解决这个问题?
在特定的对角线上,每一行和每一列都是唯一的,不是吗?
如果 (2,4) 在你的对角线上,(2,3) 就不能在同一条对角线上。
现在看看您的 for
循环,对于每个行值,您循环遍历多个列。
你应该有一个循环,比如说索引 i,然后 increment/decrement row 和 col 的值为 i。
您的嵌套 for 循环意味着您正在沿对角线方向(整个正方形)检查所有内容。所以,如果你在 (0,0) 并试图移动到 (3,3),你的代码正在检查 (1,1), (1,2), (1,3), (2,1), ( 2,2)、(2,3)、(3,1)、(3,2) 和 (3,3)。你只想检查路径 (1,1), (2,2), (3,3).
假设移动有效(即abs(y1 - y2) == abs(x1 - x2)
),遍历差异而不是坐标本身:
} else if (x1 < x2 && y1 > y2) { // diagonal northeast
for (int i = x2 - x1; i > 0; i--) {
if (piecePositions[x1 + i][y1 - i] != null) {
return false;
}
}
}
我正在使用 Swing 在 Java 中下国际象棋,并且我已经弄清楚如何检查每个棋子的合法移动。那部分没有错。但是现在,我在检查路径是否空闲时遇到了问题。例如,国际象棋中的所有棋子都不能跳过一个棋子,除非它是马。如果一个棋子挡住了象的路径,它不能比棋子越过。我调用了检查路径是否空闲的方法isVectorFree()
。它适用于水平和垂直路径,但不适用于对角线路径。
这是 isVectorFree()
的代码:
public boolean isVectorFree(int x1, int y1, int x2, int y2) {
if (x1 == x2 && y1 > y2) { // horizontal north
for (int i = y1 - 1; i > y2; i--) {
if (piecePositions[i][x1] != null) {
return false;
}
}
} else if (x1 < x2 && y1 == y2) { // horizontal east
for (int i = x1 + 1; i < x2; i++) {
if (piecePositions[y1][i] != null) {
return false;
}
}
} else if (x1 == x2 && y1 < y2) { // horizontal south
for (int i = y1 + 1; i < y2; i++) {
if (piecePositions[i][x1] != null) {
return false;
}
}
} else if (x1 > x2 && y1 == y2) { // horizontal west
for (int i = x1 - 1; i > x2; i--) {
if (piecePositions[y1][i] != null) {
return false;
}
}
}
else if (x1 < x2 && y1 > y2) { // diagonal northeast
// these diagonals aren't working properly
for (int row = x1 + 1; row < x2; row++) {
for (int col = y1 - 1; col >= y2; col--) {
if (piecePositions[row][col] != null) {
return false;
}
}
}
} else if (x1 < x2 && y1 < y2) { // diagonal southeast
for (int row = x1 + 1; row < x2; row++) {
for (int col = y1 + 1; col < y2; col++) {
if (piecePositions[row][col] != null) {
return false;
}
}
}
} else if (x1 > x2 && y1 < y2) { // diagonal southwest
for (int row = x1 - 1; row >= x2; row--) {
for (int col = y1 + 1; col < y2; col++) {
if (piecePositions[row][col] != null) {
return false;
}
}
}
} else if (x1 > x2 && y1 > y2) { // diagonal northwest
for (int row = x1 - 1; row >= x2; row--) {
for (int col = y1 - 1; col >= y2; col--) {
if (piecePositions[row][col] != null) {
return false;
}
}
}
}
return true;
}
此方法采用矩阵 piecePositions
中棋子的 x 坐标 x1
和 y 坐标 y1
以及所需的 x 坐标 x2
和所需的 y 坐标 y2
以查看当前坐标和所需坐标之间的路径是否空闲,但正如我之前所说,这对对角线不起作用。具体来说,isVectorFree()
受到周围棋子的影响。在棋盘的起始位置,如果我将棋子移开为象让路,即使现在象的路径是空闲的,它仍然不能在整个路径中移动任何地方,出于某种原因。
我该如何解决这个问题?
在特定的对角线上,每一行和每一列都是唯一的,不是吗?
如果 (2,4) 在你的对角线上,(2,3) 就不能在同一条对角线上。
现在看看您的 for
循环,对于每个行值,您循环遍历多个列。
你应该有一个循环,比如说索引 i,然后 increment/decrement row 和 col 的值为 i。
您的嵌套 for 循环意味着您正在沿对角线方向(整个正方形)检查所有内容。所以,如果你在 (0,0) 并试图移动到 (3,3),你的代码正在检查 (1,1), (1,2), (1,3), (2,1), ( 2,2)、(2,3)、(3,1)、(3,2) 和 (3,3)。你只想检查路径 (1,1), (2,2), (3,3).
假设移动有效(即abs(y1 - y2) == abs(x1 - x2)
),遍历差异而不是坐标本身:
} else if (x1 < x2 && y1 > y2) { // diagonal northeast
for (int i = x2 - x1; i > 0; i--) {
if (piecePositions[x1 + i][y1 - i] != null) {
return false;
}
}
}