Java 带有二维数组对角线检查的 Connect4 游戏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18899576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:08:35  来源:igfitidea点击:

Connect4 game with 2D arrays diagonal check

java

提问by Lilith Deficiency

So Im pretty new at java and i have an assignment to create a connect 4 game. I create a board 6 rows and 7 columns full of characters like this one -> '-', when the user inputs the desire column it replaces the '-' with a B or R (Red or black checker) well whatever this is just a background. Everything works except for the part that my code for checking diagonals its awfully long, i couldnt figure out a way to go through all the possible 4 sets of diagonals in which a player can win, except by doing each one in a different for loop... I know its horrible hope you can help me to do it shorter :(

所以我在 Java 方面很新,我有一个任务来创建一个连接 4 游戏。我创建了一个 6 行 7 列的板子,里面装满了这样的字符 -> '-',当用户输入所需的列时,它会将 '-' 替换为 B 或 R(红色或黑色棋盘格),无论这是什么一个背景。除了我用于检查对角线的代码非常长的部分之外,一切正常,我无法想出一种方法来遍历玩家可以获胜的所有可能的 4 组对角线,除非在不同的 for 循环中执行每组。 ..我知道这太可怕了,希望你能帮我把它做得更短:(

this is the code for diagonal check: (lol only looking at it makes me feel sad)

这是对角线检查的代码:(笑只是看着它让我感到难过)

    public class Connect4 {
          public static void main(String[] args) {

      //Create board

    Scanner input = new Scanner(System.in);
    char[][] grid = new char[6][7];

    for (int i = 0; i < grid.length; i++) {
      for (int j = 0; j < grid[0].length; j++) {
        grid[i][j] = '-';
      }
    }

 public static void checkWinner(char[][] grid) {      
try{

  //A LOT OF FOR LOOPS FOR DIAGONAL CHECKS
    for (int i = 5; i > 1; i-- ) {
     for(int j = 0; j < 4; j++) {
           if (   grid[i][j] == 'R' &&
                  grid[i-1][j+1] == 'R' &&
                  grid[i-2][j+2] == 'R' &&
                  grid[i-3][j+3] == 'R') {
                  System.out.println("Player 1 Wins!");
                  System.exit(0);
           }
           else if (  grid[i][j] == 'B' &&
                      grid[i-1][j+1] == 'B' &&
                      grid[i-2][j+2] == 'B' &&
                      grid[i-3][j+3] == 'B') {
                      System.out.println("Player 2 Wins!");
                      System.exit(0);
           }
         }
     }


     for (int i = 5; i > 1; i--) {
         for (int j = 1; j < 4; j++) {
                  if (grid[i][j] == 'R' &&
                      grid[i-1][j+1] == 'R' &&
                      grid[i-2][j+2] == 'R' &&
                      grid[i-3][j+3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
              }
             else if (grid[i][j] == 'B' &&
                      grid[i-1][j+1] == 'B' &&
                      grid[i-2][j+2] == 'B' &&
                      grid[i-3][j+3] == 'B') {
                      System.out.println("Player 2 Wins!");
                      System.exit(0);
         }
     }
   }

     for (int i = 0; i < 4; i++) {
         for (int j = 4; j < 7; j++) {
             if (   grid[i][j] == 'R' &&
                      grid[i+1][j-1] == 'R' &&
                      grid[i+2][j-2] == 'R' &&
                      grid[i+3][j-3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
         }
             else if (grid[i][j] == 'B' &&
                      grid[i+1][j-1] == 'B' &&
                      grid[i+2][j-2] == 'B' &&
                      grid[i+3][j-3] == 'B') {
                      System.out.println("Player 2 Wins!");
                      System.exit(0);
         }
     }
   }

     for (int i = 1; i < 2; i++) {
         for (int j = 6; j > 5; j--) {
             if (   grid[i][j] == 'R' &&
                      grid[i+1][j-1] == 'R' &&
                      grid[i+2][j-2] == 'R' &&
                      grid[i+3][j-3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
                        }
             else if (grid[i][j] == 'B' &&
                      grid[i+1][j-1] == 'B' &&
                      grid[i+2][j-2] == 'B' &&
                      grid[i+3][j-3] == 'B') {
                      System.out.println("Player 2 Wins!");
                      System.exit(0);
         }
        }
     }

     for (int i = 4; i < 5; i++){
         for (int j = 2; j < 3; j++){
                  if (grid[i][j] == 'R' &&
                      grid[i-1][j+1] == 'R' &&
                      grid[i-2][j+2] == 'R' &&
                      grid[i-3][j+3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
                  }
                  else if (grid[i][j] == 'B' &&
                          grid[i-1][j+1] == 'B' &&
                          grid[i-2][j+2] == 'B' &&
                          grid[i-3][j+3] == 'B') {
                          System.out.println("Player 2 Wins!");
                          System.exit(0);
                      }
        }
     }

     for (int i = 0; i < 4; i++) {
         for (int j = 3; j > 0; j--) {
                  if (grid[i][j] == 'R' &&
                      grid[i+1][j+1] == 'R' &&
                      grid[i+2][j+2] == 'R' &&
                      grid[i+3][j+3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
                  }
                  else if (grid[i][j] == 'B' &&
                          grid[i+1][j+1] == 'B' &&
                          grid[i+2][j+2] == 'B' &&
                          grid[i+3][j+3] == 'B') {
                          System.out.println("Player 2 Wins!");
                          System.exit(0);
                      }
     }
   }

     for(int i =0; i < 1; i++) {
         for (int j = 0; j <1; j++) {
                  if (grid[i][j] == 'R' &&
                      grid[i+1][j+1] == 'R' &&
                      grid[i+2][j+2] == 'R' &&
                      grid[i+3][j+3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
             }
                  else if (grid[i][j] == 'B' &&
                          grid[i+1][j+1] == 'B' &&
                          grid[i+2][j+2] == 'B' &&
                          grid[i+3][j+3] == 'B') {
                          System.out.println("Player 2 Wins!");
                          System.exit(0);
                 }
         }
     }

     for (int j = 0; j < 1; j++) {
         for(int i =1 ; i < 3; i++) {
                  if (grid[i][j] == 'R' &&
                      grid[i+1][j+1] == 'R' &&
                      grid[i+2][j+2] == 'R' &&
                      grid[i+3][j+3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
                  }
                  else if (grid[i][j] == 'B' &&
                          grid[i+1][j+1] == 'B' &&
                          grid[i+2][j+2] == 'B' &&
                          grid[i+3][j+3] == 'B') {
                          System.out.println("Player 2 Wins!");
                          System.exit(0);
                      }
        }
     }

     for (int j = 3; j < 4; j++) {
     for (int i = 0; i < 3; i++) {
                  if (grid[i][j] == 'R' &&
                      grid[i+1][j+1] == 'R' &&
                      grid[i+2][j+2] == 'R' &&
                      grid[i+3][j+3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
                  }
                  else if (grid[i][j] == 'B' &&
                          grid[i+1][j+1] == 'B' &&
                          grid[i+2][j+2] == 'B' &&
                          grid[i+3][j+3] == 'B') {
                          System.out.println("Player 2 Wins!");
                          System.exit(0);
                      }
         }
     }

     for (int i = 2; i < 3; i++) {
         for (int j = 2; j > 0; j--) {
                  if (grid[i][j] == 'R' &&
                      grid[i+1][j+1] == 'R' &&
                      grid[i+2][j+2] == 'R' &&
                      grid[i+3][j+3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
                  }
                  else if (grid[i][j] == 'B' &&
                          grid[i+1][j+1] == 'B' &&
                          grid[i+2][j+2] == 'B' &&
                          grid[i+3][j+3] == 'B') {
                          System.out.println("Player 2 Wins!");
                          System.exit(0);
                     }
         }
     }

     for (int i = 1; i < 2; i++) {
         for (int j = 2; j < 3; j++){
                  if (grid[i][j] == 'R' &&
                      grid[i+1][j+1] == 'R' &&
                      grid[i+2][j+2] == 'R' &&
                      grid[i+3][j+3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
                  }
                  else if (grid[i][j] == 'B' &&
                          grid[i+1][j+1] == 'B' &&
                          grid[i+2][j+2] == 'B' &&
                          grid[i+3][j+3] == 'B') {
                          System.out.println("Player 2 Wins!");
                          System.exit(0);
                   }
         }
     }

     for (int i = 1; i < 2; i++) {
         for (int j = 1; j < 2; j++){
                  if (grid[i][j] == 'R' &&
                      grid[i+1][j+1] == 'R' &&
                      grid[i+2][j+2] == 'R' &&
                      grid[i+3][j+3] == 'R') {
                      System.out.println("Player 1 Wins!");
                      System.exit(0);
                   }
                  else if (grid[i][j] == 'B' &&
                          grid[i+1][j+1] == 'B' &&
                          grid[i+2][j+2] == 'B' &&
                          grid[i+3][j+3] == 'B') {
                          System.out.println("Player 2 Wins!");
                          System.exit(0);
                   }
         }
     }
 }
catch(ArrayIndexOutOfBoundsException e){
    System.out.println("Exception thrown  :" + e);
 }
}

采纳答案by Mike 'Pomax' Kamermans

Instead of checking every possible bit on the board, play the game based on how humans play it - It's only connect 4 when someone drops in a piece that then forms a line of four tiles. So: don't check every possible tile, use the tile that just got dropped in, and only check for lines involving that tile:

与其检查棋盘上的每一个可能的位,不如根据人类的玩法来玩游戏——当有人掉进一块然后形成一行四块瓷砖时,它只会连接 4。所以:不要检查每个可能的瓷砖,使用刚刚放入的瓷砖,只检查涉及该瓷砖的行:

  • does this new tile form a horizontal? that means checking column-3 through column+3 to cover all possible horizontals.
  • does this new tile form a vertical? that means only checking the three tiles below it.
  • does this new tile form a diagonal? that means checking {column-3, row-2} through {column+3, row+3} to cover all possible diagonals. We could even check for this at the same time we're checking for horizontals, because it traverses the same column-3 through column+3 range.
  • 这个新瓷砖是否形成水平的?这意味着检查第 3 列到第 3 列以覆盖所有可能的水平线。
  • 这个新瓷砖是否形成垂直?这意味着只检查它下面的三个瓷砖。
  • 这个新瓷砖是否形成对角线?这意味着检查 {column-3, row-2} 到 {column+3, row+3} 以覆盖所有可能的对角线。我们甚至可以在检查水平线的同时检查这一点,因为它遍历相同的第 3 列到第 3 列范围。

You already know what "color" the tile has, so your checks (in fake code) would simply be of the form:

您已经知道磁贴的“颜色”是什么,因此您的支票(在假代码中)将采用以下形式:

int stretch = 0;
if( <THE CHECK TILE COLOR>.equals(<DROPPED TILE COLOR>)) {
  // check the next possible tile
  stretch++;
} else { stretch = 0; }

If the largest stretch of tiles you find that have the same color is 4, done. If not, no connect-4.

如果您发现具有相同颜色的最大一块瓷砖是 4,则完成。如果没有,则没有connect-4。

That said, this is a homework assignment: S.O. is here for you when you have a problem while programming, but we're not here to do your homework for you. If you get stuck, ask your fellow students or even your teacher. The internet isn't the only place to ask help, especiallywhen you're taking a course.

也就是说,这是一项家庭作业:当您在编程时遇到问题时,SO 会在这里为您服务,但我们不是来为您做作业的。如果你被卡住了,问问你的同学,甚至你的老师。互联网并不是寻求帮助的唯一场所,尤其是在您参加课程时。

回答by MrSmith42

Instead of coding it in a if-statement, I would e.g for the \ diagonal simply start at the position of the coin and count in direction right-lower, how many of the same color are there until the color changes, or the border is reached. Same in the left-upper direction.

而不是在 if 语句中对其进行编码,我会例如,对于 \ 对角线,只需从硬币的位置开始并沿右下方向计数,在颜色改变之前有多少相同的颜色,或者边界是到达。左上方向相同。

At the end I would simply check if the sum of both counts plus one is greater or equal 4.

最后,我会简单地检查两个计数加一的总和是否大于或等于 4。

Same method for the / diagonal.

/ 对角线的方法相同。

Warning: Your implementation of - and | have no array-boundary checks yet. I would recommend the counting approach for them too.

警告:您对 - 和 | 的实现 还没有数组边界检查。我也会为他们推荐计数方法。

P.s. And to make your code more clear, put each check in a separate methood:

Ps 为了让你的代码更清晰,把每个检查放在一个单独的方法中:

isHorizonallWin(x,y,color,grid)
isVerticalWin(x,y,color,grid)
isLdiagonalLeftUpper2RightLowerWin(x,y,color,grid)
isLdiagonalRightUpper2leftLowerWin(x,y,color,grid)