【发布时间】:2015-01-19 17:58:49
【问题描述】:
我在这个方法中有一个方法 public static char myMethod(char[][] board),我试图让它插入一个 8x8 的字符数组板,空格是“-”标记。我的方法是遍历棋盘并寻找像“p”这样的黑棋子并检查它可以移动的地方。如果那里有一个“K”白王,则该方法返回“p”,告诉我白王被黑兵控制。对于下面的骑士方法,我尝试了它如何移动的所有 8 种组合,但它不起作用。还有
for (int i = 0; i < 8; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'N') { // where can i fix the boundaries?
// a = i;
// b = j;
whiteKnight = board[i + 1][j + 2];
if (whiteKnight == 'k') {
return 'N';
}
}
我什至尝试过这种方式的边界,但没有区别
int a = 0;
int b = 0;
char whiteKnight = ' ';
for (int i = 0; i < 8; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'N') { // where can i fix the boundaries?
a = i;
b = j;
if ((a + 1) < 7 && (b + 2) < 7) {
whiteKnight = board[a + 1][b + 2];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a + 1) < 7 && (b - 2) < 7) {
whiteKnight = board[a + 1][b - 2];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a - 1) < 7 && (b + 2) < 7) {
whiteKnight = board[a - 1][b + 2];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a - 1) < 7 && (b - 2) < 7) {
whiteKnight = board[a - 1][b - 2];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a + 2) < 7 && (b + 1) < 7) {
whiteKnight = board[a + 2][b + 1];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a + 2) < 7 && (b - 1) < 7) {
whiteKnight = board[a + 2][b - 1];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a - 2) < 7 && (b + 1) < 7) {
whiteKnight = board[a - 2][b + 1];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a - 2) < 7 && (b - 1) < 7) {
whiteKnight = board[a - 2][b - 1];
if (whiteKnight == 'k') {
return 'N';
}
}
【问题讨论】:
-
我很困惑你的问题是什么
-
@austinwernli 我只是想知道是否有人认为这有什么问题,我不认为这对我来说很有意义
标签: java multidimensional-array chess