Connect4游戏与2D阵列对角线检查

时间:2013-09-19 15:58:23

标签: java

所以我是java的新手,我有一个创建连接4游戏的任务。我创建了一个6行和7列充满字符的电路板,如下所示 - > ' - ',当用户输入欲望列时,它用B或R(红色或黑色检查器)替换' - ',无论这只是一个背景。一切都有效,除了我检查对角线的代码很长的部分,我无法找到一种方法来通过所有可能的4组对角线,其中一个玩家可以赢,除了在不同的for循环中做每一个。 ..我知道你可以帮助我缩短它的可怕希望:(

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

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

2 个答案:

答案 0 :(得分:3)

不是检查棋盘上的每一个可能位,而是根据人类如何玩它来玩游戏 - 只有当有人掉入一块然后形成一条四条瓷砖的线条时才会连接4。所以:不要检查每个可能的磁贴,使用刚刚放入的磁贴,只检查涉及该磁贴的行:

  • 这个新瓷砖是否形成水平?这意味着检查第3列到第3列以覆盖所有可能的水平线。
  • 这个新瓷砖是否形成垂直?这意味着只检查它下面的三个瓷砖。
  • 这个新瓷砖是否形成对角线?这意味着检查{column-3,row-2}到{column + 3,row + 3}以覆盖所有可能的对角线。我们甚至可以在检查水平线的同时检查这一点,因为它横穿同一列-3到柱+3范围。

你已经知道了瓷砖的“颜色”,所以你的支票(假代码)只是形式:

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

如果您找到的具有相同颜色的最大一块瓷砖是4,那么就完成了。如果没有,没有连接-4。

那就是说,这是一项家庭作业:S.O。当你在编程时遇到问题时,就在这里,但我们不是为你做功课。如果你遇到困难,请问你的同学甚至你的老师。当你上一门课程时,互联网并不是唯一可以提供帮助的地方,尤其是

答案 1 :(得分:1)

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

最后,我只是检查两个计数加一的总和是否大于或等于4.

/ diagonal的方法相同。

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

P.S。为了使您的代码更清晰,请将每张支票放在一个单独的方法中:

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