简体   繁体   中英

Why won't my while loop if condition work correctly?

My checkWin method returns false until there's a winner in the Connect 4 game by putting 4 "checkers" in a row horizontally, vertically, or diagonally in my board array. Once there's a winner, the checkWin method returns true, the nearest if statement iterates, printing the winner, then terminating the entire loop (if I coded it all correctly). However, when I run the program, the while loop iterates only once, accepts one input for red, states red won, then does the same thing for yellow, then terminates.
What am I missing here?
Below is the relevant code.
Thank you.

public static void main(String[] args) {
    char[][] board = new char[6][7];
    boolean loop = true;
    // loop to alternate players until there's a winner
    while (loop) {
        printData(board);
        red(board);
        if (checkWin(board) == true) {
            printData(board);
            System.out.print("Red wins!");
            loop = false;
        }
        printData(board);
        yellow(board);
        if (checkWin(board) == true) {
            printData(board);
            System.out.print("Yellow wins!");
            loop = false;
        }
    }
}

public static void printData(char[][] tbl) {
    for (int r = 0; r < tbl.length; r++) {
        for (int c = 0; c < tbl[r].length; c++) {
            if (tbl[r][c] == 0) {
                System.out.print("| ");
            } else {
                System.out.print("|" + tbl[r][c]);
            }
        } // end for col loop
        System.out.println("|");
    } // end for row loop
    System.out.println("---------------");
} // end printData method

public static void red(char[][] f) {
    System.out.println("Place a red checker at column (0-6)");
    Scanner in = new Scanner(System.in);
    int c = in.nextInt();
    for (int i = 5; i >= 0; i--) {
        if (f[i][c] == 0) {
            f[i][c] = 'R';
            break;
        }
    }
}

public static void yellow(char[][] f) {
    System.out.println("Place a yellow checker at column (0-6)");
    Scanner in = new Scanner(System.in);
    int c = in.nextInt();
    for (int i = 5; i >= 0; i--) {
        if (f[i][c] == 0) {
            f[i][c] = 'Y';
            break;
        }
    }
}

// Method to check for a winner. Receives 2-D array as parameter. Returns
// boolean value.
public static boolean checkWin(char[][] b) {
    // Create four boolean variables, one for each set of rows. Initialize
    // all of them to false.
    boolean foundRow = false;
    boolean foundCol = false;
    boolean foundMjrD = false;
    boolean foundMinD = false;

    // Check to see if four consecutive cells in a row match.
    // check rows
    for (int r = 0; r <= 5; r++) {
        for (int c = 0; c <= 3; c++) {
            if (b[r][c] == b[r][c + 1] && b[r][c] == b[r][c + 2] && b[r][c] == b[r][c + 3] && b[r][c] != ' ') {
                foundRow = true;
                break;
            }
        }
    }

    // Check to see if four columns in the same row match
    // check columns
    for (int r = 0; r <= 2; r++) {
        for (int c = 0; c <= 6; c++) {
            if (b[r][c] == b[r + 1][c] && b[r][c] == b[r + 2][c] && b[r][c] == b[r + 3][c] && b[r][c] != ' ') {
                foundCol = true;
                break;
            }
        }
    }

    // Check to see if four diagonals match (top left to bottom right)
    // check major diagonal
    for (int r = 0; r <= 2; r++) {
        for (int c = 0; c <= 3; c++) {
            if (b[r][c] == b[r + 1][c + 1] && b[r][c] == b[r + 2][c + 2] && b[r][c] == b[r + 3][c + 3]
                    && b[r][c] != ' ') {
                foundMjrD = true;
                break;
            }
        }
    }

    // Check to see if four diagonals in the other direction match (top
    // right to bottom left)
    // check minor diagonal
    for (int r = 0; r <= 2; r++) {
        for (int c = 3; c <= 6; c++) {
            if (b[r][c] == b[r + 1][c - 1] && b[r][c] == b[r + 2][c - 2] && b[r][c] == b[r + 3][c - 3]
                    && b[r][c] != ' ') {
                foundMinD = true;
                break;
            }
        }
    }

    // If ONE of the booleans is true, we have a winner.
    // checks boolean for a true
    if (foundRow || foundCol || foundMjrD || foundMinD)
        return true;
    else
        return false;
} // end checkWin method

By what I've analyzed by debugging your code , you have not set boolean variable to "true" after toggling it to false. After you are coming out of condition make that boolean variable "true" again.

May this help you. Happy Coding

You should take a closer look at this line:

if (b[r][c] == b[r][c + 1] && b[r][c] == b[r][c + 2] && b[r][c] == b[r][c + 3] && b[r][c] != ' ') {

You check for b[r][c] != ' ' , but you never put a space in char[][] board , therefore the default value in board[?][?] is 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM