-1

我想知道如何检查 4 Rs 或 Ys 是否在一行、一列和对角线上,以宣布我的 Connect 4 游戏的获胜者(使用代码中显示的字符串),我花了 5 个小时试图弄清楚但我'我对编码仍然太陌生,无法理解该做什么。请务必保留下面存在的所有代码(请不要使用私人课程或任何初学者在交给我的老师后不知道的东西)。感谢任何评论并给我回复的人。

package finalproject;


import java.util.*;

import java.util.Arrays;

public class ConnectFour

{

public static void main(String[] args) 
{
    System.out.println("Welcome to Connect 4, \n\nObjective:\nBe the first player to get four of your colored checkers in a row- in any direction.\nRemember, you have to drop your circle to the very bottom row, you can't leave circles floating in random areas.");
    String [] tokenColor = {"R","Y"};
    //methods arrays
    int playerselect = menu();
    System.out.println("Im in main");
    int currentPlayer = players(1);



    //arrays for each column of the board
    String [] column1 = {"O","O","O","O","O","O"};
    String [] column2 = {"O","O","O","O","O","O"};
    String [] column3 = {"O","O","O","O","O","O"};
    String [] column4 = {"O","O","O","O","O","O"};
    String [] column5 = {"O","O","O","O","O","O"};
    String [] column6 = {"O","O","O","O","O","O"};
    String [] column7 = {"O","O","O","O","O","O"};


    for (int i = 1; i<=42; i++)
    {
        //using a for loop to to change the board after every player move
        printboard(column1, column2, column3, column4, column5, column6, column7);

        System.out.println("before placement");
        tokenPlacement(tokenColor[currentPlayer], column1, column2, column3, column4, column5, column6, column7);
        System.out.println("after placement");

        printboard(column1, column2, column3, column4, column5, column6, column7);
        currentPlayer = players(currentPlayer);
        
        
        
    }

    




}





public static void printboard(String[] column1, String[] column2, String[] column3, String[] column4,
        String[] column5, String[] column6, String[] column7) 
{
    System.out.println(1234567);
    //prints out board using loop
    for (int i = 5; i>=0; i--) 
    {
        System.out.print(column1[i]);
        System.out.print(column2[i]);
        System.out.print(column3[i]);
        System.out.print(column4[i]);
        System.out.print(column5[i]);
        System.out.print(column6[i]);
        System.out.print(column7[i]);
        System.out.println();
    }

}



public static int menu() 
{ 
    //enters menu method



    //scanner looks for next input from user

    Scanner myInput = new Scanner (System.in);
    int playerX;

    //prints out menu, this menu give the user the option to pick either P1, P2 or to exit the game
    System.out.println("\nUse the corresponding numbers below to choose a eaither a player, or to exit the program.\n (Note that when you choose player 1 you will always go first)");
    System.out.println("\n            1.   Choose player 1 (R)" );
    System.out.println("            2.   Choose player 2 (Y)");
    System.out.println("            3.   Exit");
    System.out.println("\n                 Select an option");
    playerX = myInput.nextInt();

    //if statements for player selection
    if (playerX == 1)
    {   
        System.out.println("You are Red");
        System.out.println("\nPlayer 2 is Yellow");


    }

    else if (playerX == 2) 
    {

        System.out.println("You are Yellow");
        System.out.println("\nPlayer 2 is Red");

    }
    //exits program
    else if (playerX == 3)
    {
        System.out.println("You didn't choose a player! Shutting Down connect_four.exe");
        System.exit(0);
    }
    //exits program
    else 
    {
        System.out.println("Sorry, I dont't understand? Please re-run the program and choose again.");
        System.exit(0);
    }


    return playerX;






}//end menu


public static int players(int currentPlayer) {
    System.out.println("Im in the players method");



    //displays and decides who's turn it is 
    if (currentPlayer == 0)
    {
        currentPlayer = (1);
        System.out.println("\nIt's Yellow's Turn");
    }

    else if (currentPlayer == 1)
    {
        currentPlayer = (0);
        System.out.println("\nIt's Red's Turn");
    }


    System.out.println(currentPlayer);


    //loops on who goes


    return currentPlayer;   

}//end players  
public static void tokenPlacement(String tokenColor, String [] column1, String [] column2, String [] column3, String [] column4, String [] column5, String [] column6, String [] column7) 
{
    //scans for user's next input
    Scanner myInput = new Scanner (System.in);
    System.out.println("\nOut of the 7 columns above, choose a number between 1-7 to signify which column you choose");
    int columnChoice = myInput.nextInt();
    boolean placed = false;
    //if statement that runs a for loop if columnChoice/users's input == 1
    if (columnChoice == 1)
    {
        for (int i=0; i<=5; i++) 
        {   
            //looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
            if (column1[i].equals("O")) 
            {
                column1[i] = tokenColor;
                placed = true;
                break;
            }

        }
        //if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
        if (!placed)
        {
            System.out.println("Sorry that column is full. Choose another column");
            tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
        }
    }




    else if (columnChoice == 2)
    {
        for (int i=0; i<=5; i++) 
        {   
            //looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
            if (column2[i].equals("O")) 
            {
                column2[i] = tokenColor;
                placed = true;
                break;
            }

        }
        //if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
        if (!placed)
        {
            System.out.println("Sorry that column is full. Choose another column");
            tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
        }
    }




    else if (columnChoice == 3)
    {
        for (int i=0; i<=5; i++) 
        {   
            //looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
            if (column3[i].equals("O")) 
            {
                column3[i] = tokenColor;
                placed = true;
                break;
            }

        }
        //if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
        if (!placed)
        {
            System.out.println("Sorry that column is full. Choose another column");
            tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
        }
    }



    else if (columnChoice == 4)
    {
        for (int i=0; i<=5; i++) 
        {   
            //looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
            if (column4[i].equals("O")) 
            {
                column4[i] = tokenColor;
                placed = true;
                break;
            }

        }
        //if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
        if (!placed)
        {
            System.out.println("Sorry that column is full. Choose another column");
            tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
        }
    }




    else if (columnChoice == 5)
    {
        for (int i=0; i<=5; i++) 
        {   
            if (column5[i].equals("O")) 
            {
                column5[i] = tokenColor;
                placed = true;
                break;
            }

        }
        //if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
        if (!placed)
        {
            System.out.println("Sorry that column is full. Choose another column");
            tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
        }
    }




    else if (columnChoice == 6)
    {
        for (int i=0; i<=5; i++) 
        {   
            //looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
            if (column6[i].equals("O")) 
            {
                column6[i] = tokenColor;
                placed = true;
                break;
            }

        }
        //if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
        if (!placed)
        {
            System.out.println("Sorry that column is full. Choose another column");
            tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
        }
    }



    else if (columnChoice == 7)
    {
        for (int i=0; i<=5; i++) 
        {   
            //looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
            if (column7[i].equals("O")) 
            {
                column7[i] = tokenColor;
                placed = true;
                break;
            }

        }
        //if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
        if (!placed)
        {
            System.out.println("Sorry that column is full. Choose another column");
            tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
        }
    }








}//end tokenPlacement



}//end main
4

1 回答 1

0

分别写一个水平检查、垂直检查、向上对角检查和向下对角检查的方法。为每个棋盘位置调用 4 个方法。–吉尔伯特勒布朗 10 小时前

 public static void main(String[] args) {
   String [] [] board = new String[6][7];

   //set everything on the board to zero
    for (int i =0; i< board.length; i++) {
        for (int j = 0; j < board[i].length; j++) {
            board[i][j] = "0";
        }
        System.out.println();
    }
    //assign a random winning situation
    board[3][2] = "Y";
    board[3][3] = "Y";
    board[3][4] = "Y";
    board[3][5] = "Y";
    board[3][1] = "R";
    board[2][2] = "R";
    board[1][3] = "R";
    board[0][4] = "R";
    board[4][5] = "Y";
    board[3][4] = "Y";
    board[2][3] = "Y";
    board[1][2] = "Y";
    board[3][1] = "R";
    board[2][1] = "R";
    board[1][1] = "R";
    board[0][1] = "R";
    board[2][5] = "Y";
    board[1][5] = "Y";
    board[4][3] = "Y";
    board[5][2] = "Y";
    //print board
    printBoard(board);
    //check for right row
    System.out.println(isRightRowContain4(board,3,2));
    //check for up-right-diagonal
    System.out.println(isUpRightDiagonalContain4(board,3,1));
    //check for up-left-diagonal
    System.out.println(isUpLeftDiagonalContain4(board,4,5));
    //check for up-column
    System.out.println(isUpColumnContain4(board,3,1));
    //check for down-column
    System.out.println(isDownColumnContain4(board,1,5));
    //check for left row
    System.out.println(isLeftRowContain4(board,3,5));
    //down-left-diagonal
    System.out.println(isDownLeftDiagonalContain4(board,2,5));
    //down-right-diagonal
    System.out.println(isDownRightDiagonalContain4(board,1,2));
}
public static void printBoard(String [][] board){
    for (int i =0; i< board.length; i++){
        for(int j=0; j<board[i].length; j++){
           System.out.print(board[i][j]+" ");
        }
        System.out.println();
    }
}

public static boolean isRightRowContain4(String [] [] board,int row, int col){
    boolean result = false;
    int count =0;
    String tokenValue = board[row][col];

    for(int k=0; k<4; k++) {
        if (tokenValue.equalsIgnoreCase(board[row][col + k])) {
            count++;
        }
    }
       if (count == 4)
           result = true;

    return result;
}
public static boolean isLeftRowContain4(String [] [] board,int row, int col){
    boolean result = false;
    int count =0;
    String tokenValue = board[row][col];

    for(int k=0; k<4; k++) {
        if (tokenValue.equalsIgnoreCase(board[row][col - k])) {
            count++;
        }
    }
    if (count == 4)
        result = true;

    return result;
}
public static boolean isUpRightDiagonalContain4(String [] [] board,int row, int col){
    boolean result = false;
    int count =0;
    String tokenValue = board[row][col];
    for(int k=0; k<4; k++) {
        if (tokenValue.equalsIgnoreCase(board[row-k][(col+k)])) {
                count++;
        }
    }
    if (count == 4)
        result = true;

    return result;
}
public static boolean isUpLeftDiagonalContain4(String [] [] board,int row, int col){
    boolean result = false;
    int count =0;
    String tokenValue = board[row][col];
    for(int k=0; k<4; k++) {
        if (tokenValue.equalsIgnoreCase(board[row-k][(col-k)])) {
            count++;
        }
    }
    if (count == 4)
        result = true;

    return result;
}
public static boolean isUpColumnContain4(String [] [] board,int row, int col) {
    boolean result = false;
    int count = 0;
    String tokenValue = board[row][col];
    for (int k = 0; k < 4; k++) {
        if (tokenValue.equalsIgnoreCase(board[row - k][col])) {
            count++;
        }
    }
    if (count == 4)
        result = true;

    return result;
}
public static boolean isDownColumnContain4(String [] [] board,int row, int col) {
    boolean result = false;
    int count = 0;
    String tokenValue = board[row][col];
    for (int k = 0; k < 4; k++) {
        if (tokenValue.equalsIgnoreCase(board[row + k][col])) {
            count++;
        }
    }
    if (count == 4)
        result = true;

    return result;
}
public static boolean isDownRightDiagonalContain4(String [] [] board,int row, int col){
    boolean result = false;
    int count =0;
    String tokenValue = board[row][col];
    for(int k=0; k<4; k++) {
        if (tokenValue.equalsIgnoreCase(board[row+k][(col+k)])) {
            count++;
        }
    }
    if (count == 4)
        result = true;

    return result;
}
public static boolean isDownLeftDiagonalContain4(String [] [] board,int row, int col){
    boolean result = false;
    int count =0;
    String tokenValue = board[row][col];
    for(int k=0; k<4; k++) {
        if (tokenValue.equalsIgnoreCase(board[row+k][(col-k)])) {
            count++;
        }
    }
    if (count == 4)
        result = true;

    return result;
}
于 2020-11-11T16:10:09.373 回答