Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tic Tac Toe在Java中,试图找出如何重置程序_Java - Fatal编程技术网

Tic Tac Toe在Java中,试图找出如何重置程序

Tic Tac Toe在Java中,试图找出如何重置程序,java,Java,我一直在试图找出如何编写代码来重置程序/清除电路板,以便再次播放tic-tac-toe。在胜利/平局后,它应该有一个提示,询问“你想再玩一次吗?”。这是我想弄明白的最后一部分 董事会: public class Board { private char[][] board; public Board() { char[][] temp = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

我一直在试图找出如何编写代码来重置程序/清除电路板,以便再次播放tic-tac-toe。在胜利/平局后,它应该有一个提示,询问“你想再玩一次吗?”。这是我想弄明白的最后一部分

董事会:

public class Board {

    private char[][] board;


    public Board() {

        char[][] temp = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
        board = temp;

    }

    public void printBoard() {

        for (char[] row : board) {
            for (char cell : row) {
                System.out.printf("| %c ", cell);
            }
            System.out.println();
        }
    }

    public boolean isCellAvailable(int number) {

        if (1 <= number && number <= 9) {
            int row = (number - 1) / 3;
            int col = (number - 1) % 3;
            if (board[row][col] == 'X' || board[row][col] == 'O') return false;
            else return true;
        }
        return false;

    }

    public void place(int number, char marker) {
        int row = (number - 1) / 3;
        int col = (number - 1) % 3;
        board[row][col] = marker;
    }

    public boolean isWinner() {

        if (board[0][0] == board[0][1] && board[0][1] == board[0][2]) return true;
        else if (board[1][0] == board[1][1] && board[1][1] == board[1][2]) return true;
        else if (board[2][0] == board[2][1] && board[2][1] == board[2][2]) return true;

        else if (board[0][0] == board[1][0] && board[1][0] == board[2][0]) return true;
        else if (board[0][1] == board[1][1] && board[1][1] == board[2][1]) return true;
        else if (board[0][2] == board[1][2] && board[1][2] == board[2][2]) return true;

        else if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return true;
        else if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return true;

        return false;

    }
}

您需要另一个循环来管理“再次播放”选项。如果玩家想再次玩,将创建一个
新棋盘()
,并重置
移动。大概是这样的:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    while (true) {
        Board board = new Board();
        board.printBoard();
        int moves = 0;

        while(true){

            while (true) {
                System.out.print("Player 1: Enter your move: ");
                int cell = scanner.nextInt();
                if (board.isCellAvailable(cell)) {
                    board.place(cell, 'X');
                    board.printBoard();
                    moves += 1;
                    break;
                } else {
                    System.out.println("Cell not available.");
                }
            }
            if (board.isWinner()) {
                System.out.println("Player 1 wins.");
                break;
            }
            if (moves == 9) {
                System.out.println("Draw. Game ended.");
                break;
            }

            while (true) {
                System.out.print("Player 2: Enter your move: ");
                int cell = scanner.nextInt();
                if (board.isCellAvailable(cell)) {
                    board.place(cell, 'O');
                    board.printBoard();
                    moves += 1;
                    break;
                } else {
                    System.out.println("Cell not available.");
                }
            }
            if (board.isWinner()) {
                System.out.println("Player 2 wins.");
                break;
            }
        }
        System.out.println("Do you want to play again? Press 1, otherwise press 0")
        int option = scanner.nextInt();
        if(option == 0) break;
    }
}

考虑到<主< <代码>方法中的大部分逻辑处理从一开始到结束玩一个游戏的任务。现在你需要一个循环,在游戏结束后,它会询问用户是否想要另一个游戏,只要用户回答“是”,它就会继续运行。至于重置棋盘,我建议在每场游戏开始时创建一个新的
棋盘
对象。
public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    while (true) {
        Board board = new Board();
        board.printBoard();
        int moves = 0;

        while(true){

            while (true) {
                System.out.print("Player 1: Enter your move: ");
                int cell = scanner.nextInt();
                if (board.isCellAvailable(cell)) {
                    board.place(cell, 'X');
                    board.printBoard();
                    moves += 1;
                    break;
                } else {
                    System.out.println("Cell not available.");
                }
            }
            if (board.isWinner()) {
                System.out.println("Player 1 wins.");
                break;
            }
            if (moves == 9) {
                System.out.println("Draw. Game ended.");
                break;
            }

            while (true) {
                System.out.print("Player 2: Enter your move: ");
                int cell = scanner.nextInt();
                if (board.isCellAvailable(cell)) {
                    board.place(cell, 'O');
                    board.printBoard();
                    moves += 1;
                    break;
                } else {
                    System.out.println("Cell not available.");
                }
            }
            if (board.isWinner()) {
                System.out.println("Player 2 wins.");
                break;
            }
        }
        System.out.println("Do you want to play again? Press 1, otherwise press 0")
        int option = scanner.nextInt();
        if(option == 0) break;
    }
}