我有一个方法public static char myMethod(char [] [] board)在这个方法中,我试图让它插入一个char数组板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';
}
}
答案 0 :(得分:0)
当然,我发现你的代码出了问题......首先我注意(a-1) < 7
当我保证你的意思(a-1) >= 0
时要小心复制/粘贴lol
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) >= 0) {
whiteKnight = board[a + 1][b - 2];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a - 1) >= 0 && (b + 2) < 7) {
whiteKnight = board[a - 1][b + 2];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a - 1) >= 0 && (b - 2) >= 0) {
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) >= 0) {
whiteKnight = board[a + 2][b - 1];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a - 2) >= 0 && (b + 1) < 7) {
whiteKnight = board[a - 2][b + 1];
if (whiteKnight == 'k') {
return 'N';
}
}
if ((a - 2) >= 0 && (b - 1) >= 0) {
whiteKnight = board[a - 2][b - 1];
if (whiteKnight == 'k') {
return 'N';
}
}
答案 1 :(得分:0)
要减少许多if语句,你可以使用这种模式(这也适用于其他西洋棋棋子):
void move(){
for( int r = 0; r < 8; ++r ){
for( int c = 0; c < 8; ++c ){
if( b[r][c] == 'N' ){
for( int y = Math.max( 0, r-2 ); y <= Math.min(7,r+2);++y ){
for( int x = Math.max( 0, c-2 ); x <= Math.min(7,c+2);++x ){
if( Math.abs(x-r)+Math.abs(x-c)==3 ){
if( b[y][x] == 'k' ){
b[y][x] = 'N';
b[r][c] = ' ';
return;
}
}
}
}
}
}
}
}
另一种方法是存储
{-2,-1}, {-1,-2}, {1,-2}, {2,-1}, {2,1}, {1,2}, {-1,2}, {-2,1}
在数组中跳转[8] [2]并迭代这些&#34;跳跃距离&#34;,检查电路板边界。这也可以避免许多ifs ......