简体   繁体   English

如果条件正常工作,为什么我的while循环不会?

[英]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. 我的checkWin方法返回false直到在Connect 4游戏中获胜为止,方法是将4个“棋子”水平,垂直或对角地放置在我的棋盘阵列中。 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). 一旦有赢家, checkWin方法将返回true,然后迭代最接近的if语句,打印出赢家,然后终止整个循环(如果我对所有代码进行了正确编码)。 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. 但是,当我运行程序时,while循环仅迭代一次,为红色接受一个输入,声明红色为赢,然后为黄色执行相同的操作,然后终止。
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. 根据我通过调试您的代码所进行的分析,在将其切换为false之后,您尚未将boolean变量设置为“ true”。 After you are coming out of condition make that boolean variable "true" again. 当您脱离疾病后,再次使该布尔变量为“ true”。

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. 您检查b[r][c] != ' ' ,但从未在char[][] board放置空格,因此board [?] [?]的默认值为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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