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

时间:2017-12-08 05:51:36

标签: java

我的checkWin方法返回false,直到通过4" checkers"在Connect 4游戏中获胜。在我的电路板阵列中水平,垂直或对角地连续排列。一旦获胜者,checkWin方法返回true,最近的if语句迭代,打印获胜者,然后终止整个循环(如果我正确编码)。但是,当我运行程序时,while循环只迭代一次,接受一个输入为红色,状态为红色赢,然后为黄色做同样的事情,然后终止。
我在这里想念的是什么?  以下是相关代码 谢谢。

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

2 个答案:

答案 0 :(得分:0)

根据我通过调试代码分析的内容,您没有将布尔变量设置为&#34; true&#34;将其切换为假之后。在您退出条件后,使该布尔变量&#34; true&#34;试。

愿这对你有所帮助。快乐编码

答案 1 :(得分:0)

你应该仔细看看这一行:

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] != ' ') {

您检查b[r][c] != ' ',但是您从未在char[][] board中添加空格,因此主板[?] [?]中的默认值为0.