从头开始重新启动while循环/ java

时间:2018-12-10 10:34:27

标签: java

我有一个井字游戏,我需要一直玩,然后问用户是否想再次玩。

我不确定如何清除数组并从循环的开头开始。

我在代码的最底部有一个我认为可能可以使用的方法,但是运气不太好。

任何帮助都会令人惊讶!

public class TicTacToe {

    public static char[][] board = new char[3][3];
    static Scanner scan = new Scanner(System.in);
    public static String keepPlaying;
    public static char yesOrNo;

    public static void main(String[] args) {

        User human1 = new User();
        User human2 = new User();

        human1.getUser1Name();
        human2.getUser2Name();

        System.out.println("\nPlayer 1: " + human1.human1Name() + ": X's");
        System.out.println("Player 2: " + human2.human2Name() + ": O's\n");

        initializeBoard();
        displayBoard();

        boolean playing = true;

        while (playing) {
            System.out.println("\n" + human1.human1Name() + " it's your turn.");
            human1.getChoice();
            human1.convert();

            if (human1.returnRow() == 0 && human1.returnCol() == 0 && board[0][0] == ' ') {
                board[0][0] = 'X';
                displayBoard();
            } else if (human1.returnRow() == 0 && human1.returnCol() == 1 && board[1][0] == ' ') {
                board[1][0] = 'X';
                displayBoard();
            } else if (human1.returnRow() == 0 && human1.returnCol() == 2 && board[2][0] == ' ') {
                board[2][0] = 'X';
                displayBoard();
            } else if (human1.returnRow() == 1 && human1.returnCol() == 0 && board[0][1] == ' ') {
                board[0][1] = 'X';
                displayBoard();
            } else if (human1.returnRow() == 1 && human1.returnCol() == 1 && board[1][1] == ' ') {
                board[1][1] = 'X';
                displayBoard();
            } else if (human1.returnRow() == 1 && human1.returnCol() == 2 && board[2][1] == ' ') {
                board[2][1] = 'X';
                displayBoard();
            } else if (human1.returnRow() == 2 && human1.returnCol() == 0 && board[0][2] == ' ') {
                board[0][2] = 'X';
                displayBoard();
            } else if (human1.returnRow() == 2 && human1.returnCol() == 1 && board[1][2] == ' ') {
                board[1][2] = 'X';
                displayBoard();
            } else if (human1.returnRow() == 2 && human1.returnCol() == 2 && board[2][2] == ' ') {
                board[2][2] = 'X';
                displayBoard();
            }

            checkForWinner();

            if (checkForWinner() == true) {
                System.out.println("\n" + human1.human1Name() + " has won the game!!!");
                break;
            }

            if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' '
                    && board[1][1] != ' ' && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' '
                    && board[2][2] != ' ') {
                System.out.println("It's a draw!!!");
                break;
            }

            System.out.println("\n" + human2.human2Name() + " it's your turn.");
            human2.getChoice();
            human2.convert();

            if (human2.returnRow() == 0 && human2.returnCol() == 0/* && board[0][0] == ' ' */) {
                if (board[0][0] != ' ') {
                    System.out.println("\n" + human2.human2Name() + " that spot is already taken.");
                    human2.getChoice();
                    human2.convert();

                    continue;
                }
                board[0][0] = 'O';
                displayBoard();
            } else if (human2.returnRow() == 0 && human2.returnCol() == 1 && board[1][0] == ' ') {
                board[1][0] = 'O';
                displayBoard();
            } else if (human2.returnRow() == 0 && human2.returnCol() == 2 && board[2][0] == ' ') {
                board[2][0] = 'O';
                displayBoard();
            } else if (human2.returnRow() == 1 && human2.returnCol() == 0 && board[0][1] == ' ') {
                board[0][1] = 'O';
                displayBoard();
            } else if (human2.returnRow() == 1 && human2.returnCol() == 1 && board[1][1] == ' ') {
                board[1][1] = 'O';
                displayBoard();
            } else if (human2.returnRow() == 1 && human2.returnCol() == 2 && board[2][1] == ' ') {
                board[2][1] = 'O';
                displayBoard();
            } else if (human2.returnRow() == 2 && human2.returnCol() == 0 && board[0][2] == ' ') {
                board[0][2] = 'O';
                displayBoard();
            } else if (human2.returnRow() == 2 && human2.returnCol() == 1 && board[1][2] == ' ') {
                board[1][2] = 'O';
                displayBoard();
            } else if (human2.returnRow() == 2 && human2.returnCol() == 2 && board[2][2] == ' ') {
                board[2][2] = 'O';
                displayBoard();
            }

            checkForWinner();

            if (checkForWinner() == true) {
                System.out.println("\n" + human2.human2Name() + " has won the game!!!");
                break;
            }

            if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' '
                    && board[1][1] != ' ' && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' '
                    && board[2][2] != ' ') {
                System.out.println("It's a draw!!!");
                break;
            }

        }

    }

    public TicTacToe() {

        board = new char[3][3];

    }

    public static void initializeBoard() {

        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                board[row][col] = ' ';
            }
        }
    }

    public static void displayBoard() {

        System.out.println("    A   B   C");
        System.out.println("  -------------");

        for (int row = 0; row < 3; row++) {
            System.out.print((row + 1) + " | ");
            for (int col = 0; col < 3; col++) {
                System.out.print(board[row][col] + " | ");
            }
            System.out.println();
            System.out.println("  -------------");
        }
    }

    public static boolean checkForWinner() {

        if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][1] != ' ') {
            return true;
        }
        if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][1] != ' ') {
            return true;
        }
        if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][1] != ' ') {
            return true;
        }
        if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[1][0] != ' ') {
            return true;
        }
        if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[1][1] != ' ') {
            return true;
        }
        if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[1][2] != ' ') {
            return true;
        }
        if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') {
            return true;
        }
        if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {
            return true;
        }

        return false;
    }

    public static boolean playAgain() {

        System.out.println("\nWould you like to play again?");

        keepPlaying = scan.nextLine();

        yesOrNo = keepPlaying.toUpperCase().charAt(0);

        if (yesOrNo == 'N') {
            return false;
        } else if (yesOrNo == 'Y') {
            return true;
        }
        return false;
    }

}

0 个答案:

没有答案