首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法识别连接4个java水平

无法识别连接4个java水平
EN

Stack Overflow用户
提问于 2019-05-20 13:35:42
回答 1查看 23关注 0票数 0

除了我的水平取胜条件之外,所有的东西都可以工作,我发现这很奇怪,因为我的垂直方向工作得很好,但是我不能解释为什么水平方向的取胜条件不能,但是我的垂直取胜条件可以假设你可以为行和列切换for循环,反之亦然,但是它似乎在这里失败了。

我试着查找资料,但到处都在说要做我已经做过的事情

代码语言:javascript
复制
public class Connect4 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // DON'T MODIFY THE MAIN METHOD UNLESS FOR DEBUGGING
  //MAKE SURE YOU GET RID OF YOUR MODIFICATIONS HERE BEFORE 
SUBMISSION

String[][] board = createEmptyBoard();

Scanner input = new Scanner(System.in);

boolean bl = true;

printPattern(board);

while(bl) {

    int player1 = 1 , player2 = 2 ,  userInput;


   System.out.println("Please drop a RED disk at the column between 
0 and 6:");
    userInput = input.nextInt();
    dropDisk(board, userInput , player1);
    printPattern(board);

   System.out.println("Please drop a YELLOW disk at the column 
between 0 and 6:");
   userInput  = input.nextInt();
   dropDisk(board, userInput , player2);
   printPattern(board);  


    String win = checkWinner(board);

    if(!win.equals(""))
        {
            System.out.println(win + " is the winner!");
            break;
        }
    /*
    Write code to announce  if there is  winner and end the game
    */

     } 
    }
     public static String[][] createEmptyBoard() {
   /* This method prints the first empty pattern for the game
   DON'T MODIFY THIS METHOD
   */
    String[][] f = new String[7][15];
    for (int i =0;i<f.length;i++) {
          for (int j =0;j<f[i].length;j++) {

             if (j% 2 == 0) f[i][j] ="|";
             else f[i][j] = " ";
             if (i==6) f[i][j]= "-";
         }
       }
    return f;

     } // end of createEmptyBoard 


     public static void printPattern(String[][] brd) {

         for (int i = 0; i < 7; i++){

      System.out.println(brd[i][0] + brd[i][1]+ brd[i][2]+ brd[i]
[3]+ brd[i][4]+ brd[i][5]+ brd[i][6]+ brd[i][7]+ brd[i][8]+ brd[i]
[9]+ 

brd[i][10]+ brd[i][11]+ brd[i][12]+ brd[i][13]+ brd[i][14]);


     }

     } // end of printPattern

     public static String dropDisk(String[][] brd, int position, 
int player) {

    if(position < 0 || position > 6){

             return null;

         }

    String disk = player == 1 ? "R" : "Y";

         int col = 2 * position + 1;
         // start looking for a free slot at the very bottom

         int row = 5;

         while (row >= 0 && !brd[row][col].equals(" ")) {

         // move one row up

         row--;

         }

// free slot found, disk can be placed

if (row >= 0) {

    brd[row][col] = disk;

    return disk;

}

return null;

  } // end of dropDisk


   public static String checkWinner(String[][] brd) {      

for(int i = 0; i < 6; i++){

        int count = 0;

        for(int j = 0; j < 14; j++){

            if(brd[i][j].equals("R")){

                count++;

            }
            else {
                count = 0;
            }

            if(count == 4){

                return "R";

            }

        }        

    }

    //Vertical Check for "Y"
    for(int i = 0; i < 6; i++){

        int count = 0;

        for(int j = 0; j < 14; j++){

            if(brd[i][j].equals("Y")){

                count++;

            }

            else{
                count = 0;
            }

            if(count == 4){

                return "Y";

            }
        }        
    }

    // Horizontal Check for "R"
    for(int i = 0; i < 14; i++){

        int count = 0;

        for(int j = 0; j < 6; j++){

            if(brd[j][i].equals("R")){

                count++;

            }

            else {

                count = 0;

            }

            if(count == 4){

                return "R";

            }

        }     

    }

    // Horizontal Check for "Y"
    for(int i = 0; i < 14; i++){

        int count = 0;

        for(int j = 0; j < 6; j++){

            if(brd[j][i].equals("Y")){

                count++;

            }

            else {
                count = 0;
            }

            if(count == 4){

                return "Y";

            }

        }    

    }

    // Check for a diagonal to the bottom and right
    for(int i = 0; i < brd.length-4; i++){

        for(int j = 1; j < brd[i].length-7; j = j + 2){

            if(brd[i][j].equals("R")){

                if(brd[i+1][j+2].equals("R") && brd[i+2]
[j+4].equals("R") && brd[i+3][j+6].equals("R")){

                    return "R";

                }

            }
            else if(brd[i][j].equals("Y")){

                if(brd[i+1][j+2].equals("Y") && brd[i+2]\
[j+4].equals("Y") && brd[i+3][j+6].equals("Y")){

                    return "Y";

                }

            }

        }

    }

    // Check for a diagonal to the bottom and left
    for(int i = 0; i < brd.length-4; i++){

        for(int j = brd[i].length-2; j >= 7; j = j - 2){

            if(brd[i][j].equals("R")){

                if(brd[i+1][j-2].equals("R") && brd[i+2][j-
4].equals("R") && brd[i+3][j-6].equals("R")){

                    return "R";
                }
            }
            else if(brd[i][j].equals("Y")){

                if(brd[i+1][j-2].equals("Y") && brd[i+2][j-
4].equals("Y") && brd[i+3][j-6].equals("Y")){

                    return "Y";

                }
            }
        }
    }

    return "";

}

}
EN

回答 1

Stack Overflow用户

发布于 2019-05-20 14:29:52

在您的电路板设置中,您具有:

代码语言:javascript
复制
 if (j% 2 == 0) f[i][j] ="|";

换句话说,您的列在"R"/"Y"/“”播放位置和"|“列标记之间交替。

所以你永远不能在一条水平线(或对角线)上有4个"R“或”Y“--你应该有"R","|",”R“。

更好的方法是从数组中删除"|“--你不需要存储它们,这真的会使你的所有处理变得复杂。只需让数组存储您的棋子,并且仅在打印输出时使用"|“。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56214808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档