循环不会破裂

时间:2014-06-05 23:56:33

标签: java

嘿,我有一个简单的tic tac toe游戏进行日食,但循环不会破坏。循环用2s填满我的电路板。循环是在结束方法computerInput()。在while(!done)范围内设置done = true后,循环仍然不会中断。我必须等一会儿。谢谢你的帮助!我会在几个小时后回来。

package lab15;

public class WinnerCheck {

    public static int PL_ONE = 1;
    public static int PL_TWO = 2;
    static Console console   = new Console();
    static final int PAUSE   = 500;
    public static int i = 3;
    public static int j = 3;
    public static int[][] b  = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };


    public static void main(String[] args) {
        boolean done = false;

     while (!done) {
    printBoard(b);
    playerOneInput();
    pause(PAUSE);
    computerInput(b);
    detectWinner(b);
    console.clear();
    int result = detectWinner(b);
    if(result > 0) done = true;
}

}

    public static void pause(int ms) {
        try {
            Thread.sleep(ms);
        } catch (Exception ex) {

        }
    }


    public static boolean placePiece(int[][] b, int r, int c, int player) {
        b[r][c] = player;
        return false;
    }

    public static boolean isWinner(int[][] b, int player) {
        // check for a horizontal winner
        for (int i = 0; i < b.length; i++) {
            int count = 0;
            for (int j = 0; j < b[i].length; j++) {
                if (b[i][j] == player
                    || b[j][i] == player ) {
                    count++;
                }
            }
            if (count == b[i].length)
                return true;
        }
        return false;
    }

    public static int detectWinner(int[][] b) {
        if (isWinner(b, PL_ONE)) {
            return 1;
        } else if (isWinner(b, PL_TWO)) {
            return 2;
        } else {
            return 0;
        }
    }

    public static void printBoard(int[][] b) {
        System.out.println("------");
        for (int[] row : b) {
            for (int element : row) {
                System.out.print(element + " ");
            }
            System.out.println("");
        }
        System.out.println("------");
        System.out.println("winner: " + detectWinner(b));
    }


    public static void playerOneInput() {
        switch (console.getKey()) {
        case '1':
            placePiece (b, 0, 0, 1);
            break;
        case '2':
            placePiece (b, 0, 1, 1);
            break;
        case '3':
            placePiece (b, 0, 2, 1);
            break;
        case '4':
            placePiece (b, 1, 0, 1);
            break;
        case '5':
            placePiece (b, 1, 1, 1);
            break;
        case '6':
            placePiece (b, 1, 2, 1);
            break;
        case '7':
            placePiece (b, 2, 0, 1);
            break;
        case '8':
            placePiece (b, 2, 1, 1);
            break;
        case '9':
            placePiece (b, 2, 2, 1);
            break;
        }
    }

    public static void computerInput() {
    boolean placePiece = false;
        for (i = 0; i < b.length; i++) {
            for (j = 0; j < b[i].length; j++) {
                if (b[i][j] == 0) {
                    placePiece(b, i, j, PL_TWO);
                    placePiece = true;
                    if (placePiece == true) {
                        break;
                }
                if (placePiece == true) {
                    break;
                }

                }
                }
            if (placePiece == true) {
                break;
            }
        }
    }   
}

4 个答案:

答案 0 :(得分:0)

while (!done) {
    printBoard(b);
    playerOneInput();
    pause(PAUSE);
    computerInput();
    detectWinner(b);
    console.clear();

您未将done设置为true

答案 1 :(得分:0)

您没有使用来自detectWinner方法的返回值

int result = detectWinner(b); if(result > 0) isDone = true;

答案 2 :(得分:0)

对于computerInput循环,你在一个方法中,你可以使用return而不是break

答案 3 :(得分:0)

你能清楚几件事吗?可能是我说得不对。

computerInput(b);

ComputerInput是一个静态方法,它不带参数,你提供一个int [] []作为参数。它不应该首先编译。

detectWinner(b);

可以显示方法detect的代码,它将int [] []作为参数并返回一个整数。

int result = detectWinner(b);