简体   繁体   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;
        }
    }
}

This is the code I wrote about Tic Tac Toe based on what my professor said.这是我根据教授所说的编写的关于井字游戏的代码。 He said I can put both step 2 and step 3 in else statement, but I don't know how to do it.他说我可以把第2步和第3步都放在else语句中,但我不知道该怎么做。 I gave it a try, but IntelliJ(the program I'm using) said it doesn't have a return statement.我试了一下,但 IntelliJ(我正在使用的程序)说它没有返回语句。 I don't why it has errors.我不明白为什么它有错误。 Please help me.请帮我。 Thanks.谢谢。

The problem is exactly as your IDE told you, you have a function and the IDE can't verify you have a return statement from every possible path.问题正是你的 IDE 告诉你的,你有一个 function 和 IDE 无法验证你有来自每条可能路径的返回语句。 In your case change the last if(the cnt<9) to be the else of the previous if(the cnt==9) as it must be one of them.在您的情况下,将最后一个 if(the cnt<9) 更改为前一个 if(the cnt==9) 的 else,因为它必须是其中之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM