嵌套循环在随机间隔后无限循环吗?

时间:2019-03-25 18:39:21

标签: java arrays loops multidimensional-array

我正在使用Java编写Connect 4游戏以进行分配。但是,每当玩家2进场大约5步时,玩家2循环将无限循环。我找不到某种逻辑错误,这令人沮丧。什么是逻辑错误,什么是避免以后再犯同样错误的好方法?

我尝试更改do> while循环的变量,其中玩家1和玩家2尝试移动。但是,这没有影响。

import java.util.Arrays;

public class Lab6Shell {
    public static void main(String args[]) {
        // variables
        Scanner input = new Scanner(System.in);
        char[][] board = new char[7][8];
        boolean finished = false;
        boolean gameOver = false;
        int width = 7;
        int height = 8;
        char currentPlayer = 'X';
        int numMoves = 0;
        int bottom_row = width - 1;

        // loop until user wants to stop
        for (int row = 0; row < board.length; row++) {
            java.util.Arrays.fill(board[row], 0, board[row].length, '*');
        }
        do {

            // display the board

            DisplayBoard(board);

            // loop until this game is over
            do {
                // get the next move for the current player
                int columnChosen = 0;
                do {

                    if (currentPlayer == 'X') {

                        int counter = 1;

                        System.out.println("Player 1 turn");

                        System.out.println("Enter the column you want to place your piece.");
                        columnChosen = input.nextInt();
                        input.nextLine();

                        while (true) {
                            if (columnChosen > width) {
                                System.out.println("That's not a valid column");
                                break;
                            }

                            if ((board[bottom_row][columnChosen] == '*')) {
                                board[bottom_row][columnChosen] = 'X';
                                break;
                            } else if ((board[bottom_row][columnChosen] == 'X')
                                    || (board[bottom_row][columnChosen] == 'O')) {
                                if (board[bottom_row - counter][columnChosen] == '*') { // puts X if blank
                                    board[bottom_row - counter][columnChosen] = 'X';
                                    break;
                                }

                                counter += 1;
                                if (counter == width) {
                                    System.out.println("That column is full");
                                    break;
                                }
                            }

                        }
                    }

                    if (currentPlayer == 'O') {

                        int counter = 1;

                        System.out.println("Player 2's turn");

                        System.out.println("Enter the column you want to place your piece.");
                        columnChosen = input.nextInt();
                        input.nextLine();

                        while (true) {
                            if (columnChosen > width) {
                                System.out.println("That's not a valid column");
                                break;
                            }

                            if ((board[bottom_row][columnChosen] == '*')) {
                                board[bottom_row][columnChosen] = 'O';
                                break;
                            } else if ((board[bottom_row][columnChosen] == 'X')
                                    || (board[bottom_row][columnChosen] == 'O')) {
                                if (board[bottom_row - counter][columnChosen] == '*') { // puts O
                                    board[bottom_row - counter][columnChosen] = 'O';
                                    break;
                                }

                                counter += 1;
                                if (counter == width) {
                                    System.out.println("That column is full");
                                    break;
                                }
                            }

                        }
                    }

                } while (columnChosen < 0 || columnChosen > 8 || board[1][columnChosen] != '*');

                // place piece

                // increment number of moves
                numMoves++;
                // display the board
                DisplayBoard(board);

                // check for win
                if (checkWin(board)) {
                    // if winner, display congratulations and set gameOver true
                    System.out.println("Congratulations! You won!");
                    gameOver = true;
                } else if (numMoves == 42) {
                    // if tie, display result and set gameOver true
                    DisplayBoard(board);
                    System.out.println("Tie Game! Game over");
                    gameOver = true;
                } else if (checkWin(board) == false) {
                    if (currentPlayer == ('X')) {
                        currentPlayer = ('O');
                    } else {
                        currentPlayer = ('X');
                    }
                }

            } while (!gameOver);

            // ask if user wants to play again, set finished accordingly
            System.out.println("Would you like to play again?");
            input.nextLine();
            String decision = input.nextLine();

            if (decision.toLowerCase().equals("yes")) {
                finished = false;
            }

            else if (decision.toLowerCase().equals("no")) {
                finished = true;
            }

        } while (finished == false);
    }

    // this method displays the board passed in
    public static void DisplayBoard(char[][] board) {
        for (int i = 0; i < board.length; i++) {
            System.out.print("|");
            for (int j = 0; j < board[i].length; j++) {
                System.out.print(" " + board[i][j] + "|");

            }
            System.out.println("");
        }
    }

    public static boolean checkWin(char[][] board) {
        final int HEIGHT = board.length;
        final int WIDTH = board[0].length;
        final int EMPTY_SLOT = '*';
        for (int r = 0; r < HEIGHT; r++) { // iterate rows, bottom to top
            for (int c = 0; c < WIDTH; c++) { // iterate columns, left to right
                char player = board[r][c];
                if (player == EMPTY_SLOT)
                    continue; // don't check empty slots

                if (c + 3 < WIDTH && player == board[r][c + 1] && // look right
                        player == board[r][c + 2] && player == board[r][c + 3])
                    return true;
                if (r + 3 < HEIGHT) {
                    if (player == board[r + 1][c] && // look up
                            player == board[r + 2][c] && player == board[r + 3][c])
                        return true;
                    if (c + 3 < WIDTH && player == board[r + 1][c + 1] && // look up & right
                            player == board[r + 2][c + 2] && player == board[r + 3][c + 3])
                        return true;
                    if (c - 3 >= 0 && player == board[r + 1][c - 1] && // look up & left
                            player == board[r + 2][c - 2] && player == board[r + 3][c - 3])
                        return true;
                }
            }
        }
        return false; // no winner found
    }

}

预期结果是,每个玩家将演奏一块,直到同一块中的四个连续出现为止。然后,第一个连续四次获胜的选手被宣布为获胜者,比赛结束。但是,一旦游戏进入约5个循环,玩家2循环将无限循环直到一列已满,并且不会打印出棋盘。

1 个答案:

答案 0 :(得分:0)

您的无限循环是通过检查board[1][columnChosen] != '*'循环中的条件do ... while引起的。只要选定列的第二行到顶部行被占用,该程序将继续向当前用户询问新的操作。

替换:

do 
{
...
} while (columnChosen < 0 || columnChosen > 8 || board[1][columnChosen] != '*');

使用:

do
{
...
} while (columnChosen < 0 || columnChosen > 8)

这应该使您可以解决其余问题。