Skip to content
Advertisement

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 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. I gave it a try, but IntelliJ(the program I’m using) said it doesn’t have a return statement. I don’t why it has errors. Please help me. Thanks.

Advertisement

Answer

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. 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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement