【问题标题】:Chess movements in 2d array二维数组中的国际象棋运动
【发布时间】: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


【解决方案1】:

当然我看到你的代码有问题...首先我注意到(a-1) &lt; 7 当我保证你的意思是(a-1) &gt;= 0 小心复制/粘贴哈哈

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';
                }
            }

【讨论】:

  • 真的谢谢!我认为它在值中的读取方式有问题,因为我不明白为什么它不起作用。测试用例失败
【解决方案2】:

要减少许多 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}

在数组中 jump[8][2] 并迭代这些“跳跃距离”,检查棋盘边界。这也将避免许多 ifs...

【讨论】:

  • 虽然他们是右括号!
  • @austinwernli 一个人并没有真正迭代棋盘来寻找白棋或黑棋的棋子;它应该是对 W 或 B 剩余人员的一次迭代。第二个选项会将内部循环减少为单个循环。 - 无论如何:您正确指出的复制粘贴陷阱要少得多。
  • @laune 这种方法是在其他方法之外吗?还是什么?
  • @user3511198 我不知道你们班级的整体组织情况。 move 是一种方法,如果给定一个包含您正在使用的字母的类字段 char[][]b,它将找到一个包含“N”的元素并将其移动到包含“k”的“触手可及”的位置。我不知道您如何在帖子中容纳第二段代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 2022-06-17
  • 2016-07-06
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多