首页 > 解决方案 > tictactoe 在玩家之间循环

问题描述

我正在做一个 *n tictactoe,玩家可以选择棋盘的大小。但是,当我试图在 checkWinner() 方法中确定获胜者时,无论玩家 1 还是玩家 2 获胜,它总是打印出获胜者是玩家 1。playerTurn 的解析有什么问题吗?当我尝试在 CheckIfWinner 方法中打印 playerNumber 时,它会打印出 2 行 Player number is: 1 and Player number is : 2

private Matrix cells;
    // '-' - not occupied,
    // 'x' - taken up player 1,
    // 'o' - taken up by player 2
    char c = '-';
    int n;

    public TicTacToe() {
        // prompt user to choose size of tictactoe
        print("Please set the size of cells(e.g. 4 = 4x4 ): ");
        Scanner sc = new Scanner(System.in);
        String playerInput = sc.nextLine();
        n = Integer.parseInt(playerInput);
        println("n: " + n);
        cells = new Matrix(n);
    }

    public void executeGame() {

        // should allow the player to set the cells to other size (e.g. 4x4 or 5x5)
        int turn = 0;
        int playerTurn = 1;
        int winner = 0;
        cells.set(n - 1, n - 1, c);
        while (turn < n * n) {
            // while turn is less than n*n
            // print cells
            cells.display();
            // get and set tick from Player 1
            askGetAndAskPlayerForTick(playerTurn);
            println("player turn is : " + playerTurn);
            // check if there is a winner - if there is winner break from the while loop
            winner = checkWinner();

            if (winner != 0) {
                break;
            }

            if (playerTurn == 1) {
                playerTurn = 2;
            } else {
                playerTurn = 1;
            }
            turn++;

        }

        // display draw or the winner
        cells.display();
        if (winner != 0) {
            println("The winner is : Player " + winner);
        } else {
            println("There is no winner");
        }
    }


    private void askGetAndAskPlayerForTick(int playerNumber) {
        // should check if the cell is taken up
        // set player 1 to 'x' and player 2 to 'o'
        if (playerNumber == 1) {
            c = 'x';
        } else if (playerNumber == 2) {
            c = 'o';
        }
        print("Player " + playerNumber + "(" + c + ")" + ":");
        Scanner keyboard = new Scanner(System.in);
        String playerInput = keyboard.nextLine();
        String[] inputs = playerInput.split(" ");
        int row = Integer.parseInt(inputs[0]);
        int col = Integer.parseInt(inputs[1]);
        if (row - 1 < 0 || col - 1 < 0 || row >= n + 1 || col >= n + 1) {
            println("The index is out of bounds of the matrix! Please enter a valid range!");
            //askGetAndAskPlayerForTick(playerNumber);
        } else if (cells.get(row - 1, col - 1) != '-') {
            println("This position is filled! Please try again.");
            //askGetAndAskPlayerForTick(playerNumber);
        }
        cells.set(row - 1, col - 1, c);

    }

    // 0 - no winner, 1 - player 1 is the winner, 2 - player 2 is the winner
    private int checkWinner() {

        int winner = 0;
        // check for player 1
        if (checkIfWinner(1)) {
            winner = 1;
        } else if (checkIfWinner(2)) {
            winner = 2;
        }

        // check for player 2
        return winner;
    }

    private boolean checkIfWinner(int playerNumber) {

        boolean win = false;
        println("player number is: " + playerNumber);
        // check for row win
        for (int i = 0; i < n; i++) {
            boolean row = true;
            char value = (char) cells.get(i, 0);
            // check for unfilled space in a row
            if (value == '-') {
                row = false;
            } else {
                for (int j = 0; j < n; j++) {
                    if (cells.get(i, j) != value) {
                        row = false;
                        break;
                    }
                }
            }
            if (row) {
                win = true;
            }
        }

        return win;
    }

标签: javatic-tac-toe

解决方案


推荐阅读