簡體   English   中英

嵌套循環在隨機間隔后無限循環?

[英]Nested loop infinitely looping after random interval?

我正在編寫一個使用Java進行任務的連接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循環無限循環直到列滿,並且不打印出板。

你的無限循環是由你在do ... while循環中檢查條件board[1][columnChosen] != '*' 只要所選列的第二行到第一行被占用,程序將繼續向當前用戶詢問新移動。

更換:

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

附:

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

這應該可以幫助您解決剩余的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM