簡體   English   中英

我對 java tic tac toe 方法之一有疑問

[英]I have a question about one of the method java tic tac toe

public static void main(String[] args) {
    // create scanner to read user input
    // create char[] array to store board values
    // set positions of board from 1 to 9
    // 2 players: 'X' and 'O', start with 'X'
    Scanner input = new Scanner(System.in);
    char board[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    char player = 'X';
    char player2 = 'O';
    // display the playing board in 3x3 layout
    display(board);
    // play game until gameOver is true
    boolean gameOver = false;
    while (!gameOver) {
        // get current player choice, and update board value
        receiveUserChoice(player, input, board);
        // display the updated board
        display(board);
        // switch player 'X' -> 'O' or 'O' -> 'X'
        player = (player == 'X') ? 'O' : 'X';
        // check for winner, draw or game is not over yet
        gameOver = isGameOver(board);

        // ** important ** remove the following statement
        // I added it to avoid infinite loop
        // gameOver value should return from isGameOver() method as above
        gameOver = true; // TODO: remove this
    }
}
// display board in 3x3 layout
// note that array indices from 0 to 8 = positions from 1 to 9
public static void display(char[] board) {
    System.out.println("");
    System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
    System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
    System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
    System.out.println("");
}
// get current player choice, and update board value
public static void receiveUserChoice(char player, Scanner input, char[] board) {
    // Step 1: get user input, a position from 1 to 9
    // Step 2: make sure it is a valid position, and the position is not taken yet
    //    2.1: if it is valid input, mark the board position with the current player
    //    2.2: if it is not valid, print message repeat Step 1. until a valid input is obtained
    boolean loop = true;
    while(loop)
    {
        System.out.print("Which position do you want?");
        int n = input.nextInt();
        if(n<10&&n>0&&Character.isDigit(board[n-1]))
        {
            board[n-1]=player;
            loop=false;     //After update, loop is false.
            //Whenever the board value is updated, everytime we will check if the game is over or if it's still continued.
        }
        else
        {
            System.out.println("Invalid value!Repeat Step1!");
        }
    }
    // TODO: Add statements
}
// check for winner, draw or game is not over yet
public static boolean isGameOver(char[] board) {
    // Step 1: if there is a winner, print winner message, return true
    // Step 2: if it is a draw, print draw message, return true
    // Step 3: else the game is not over yet, return false
    if(board[0]==board[1]&&board[1]==board[2])//It's == cuz we are comparing these value.
    {
        System.out.println(board[0]+" wins.");      //Board 0 can be O or X.
        return true;
    }
    else if(board[3]==board[4]&&board[4]==board[5])
    {
        System.out.println(board[3]+" wins.");
        return true;
    }
    else if(board[6]==board[7]&&board[7]==board[8])
    {
        System.out.println(board[6]+" wins.");
        return true;
    }
    else if(board[0]==board[3]&&board[3]==board[6])
    {
        System.out.println(board[0]+" wins.");
        return true;
    }
    else if(board[1]==board[4]&&board[4]==board[7])
    {
        System.out.println(board[1]+" wins.");
        return true;
    }
    else if(board[2]==board[5]&&board[5]==board[8])
    {
        System.out.println(board[2]+" wins.");
        return true;
    }
    else if(board[0]==board[4]&&board[4]==board[8])
    {
        System.out.println(board[0]+" wins.");
        return true;
    }
    else if(board[2]==board[4]&&board[4]==board[6])
    {
        System.out.println(board[2]+" wins.");
        return true;
    }
    else        //It has step 2 and step 3.
    {
        int cnt=0;
        for(int i=0;i<board.length;i++)
        {
            if(board[i]=='O'||board[i]=='X')
            {
                cnt+=1;
            }
        }
        if(cnt==9)
        {
            System.out.println("Draw!");
            return true;
        }
    }
}

這是我根據教授所說的編寫的關於井字游戲的代碼。 他說我可以把第2步和第3步都放在else語句中,但我不知道該怎么做。 我試了一下,但 IntelliJ(我正在使用的程序)說它沒有返回語句。 我不明白為什么它有錯誤。 請幫我。 謝謝。

問題正是你的 IDE 告訴你的,你有一個 function 和 IDE 無法驗證你有來自每條可能路徑的返回語句。 在您的情況下,將最后一個 if(the cnt<9) 更改為前一個 if(the cnt==9) 的 else,因為它必須是其中之一。

暫無
暫無

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

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