Home > Software design >  How can I keep it running after checking the if condition?
How can I keep it running after checking the if condition?

Time:08-22

This is a program tic tac toe. It's a computer operation. I want it to check all conditions. If it meets the condition, it will check again if the position of the computer to work. Can add O or not if it can't add O. had the computer check the next condition completely, if it didn't meet the condition it would randomly add O, but now it doesn't work because it happens that sometimes the computer randomly added it in the first place. and then when the players Played the condition that the computer had to prevent the player from winning, but it had a random O In the previous round, it was unable to fill the O at all, thus preventing it from continuing.

private static void computerNormalTurn(char[][] board) {
        Random rand = new Random();
        int computerMove;
        while (true) {
            if(board [2][0] == 'X' && board [2][1] == 'X' ||
                    board [1][1] == 'X' && board [0][0] == 'X' ||
                    board [1][2] == 'X' && board [0][2] == 'X'){
                if(board[2][2] != ' ') {
                    continue;

                }else{
                    computerMove = 3;
                }
            } else if (board [2][0] == 'X' && board [2][2] == 'X' ||
                    board [1][1] == 'X' && board [0][1] == 'X') {
                if(board[2][1] != ' ') {
                    continue;

                }else{
                    computerMove = 2;
                }
            } else if (board [2][1] == 'X' && board [2][2] == 'X' ||
                    board [1][0] == 'X' && board [0][0] == 'X' ||
                    board [1][1] == 'X' && board [0][2] == 'X' ) {
                if(board[2][0] != ' ') {
                    continue;

                }else{
                    computerMove = 1;
                }
            } else if (board[2][0] == 'X' && board[0][0] == 'X' ||
                    board [1][1] == 'X' && board [1][2] == 'X' ) {
                if(board[1][0] != ' ') {
                    continue;

                }else{
                    computerMove = 4;
                }
            } else if (board [2][0] == 'X' && board [0][2] == 'X' ||
                    board [2][1] == 'X' && board [0][1] == 'X' ||
                    board [2][2] == 'X' && board [0][0] == 'X' ||
                    board [1][0] == 'X' && board [1][2] == 'X' ) {
                if(board[1][1] != ' ') {
                    continue;

                }else{
                    computerMove = 5;
                }
            } else if (board[2][2] == 'X' && board[0][2] == 'X' ||
                    board [1][0] == 'X' && board [1][1] == 'X') {
                if(board[1][2] != ' ') {
                    continue;

                }else{
                    computerMove = 6;
                }
            } else if (board[2][0] == 'X' && board[1][0] == 'X' ||
                    board [2][2] == 'X' && board [1][1] == 'X' ||
                    board [0][1] == 'X' && board [0][2] == 'X') {
                if(board[0][0] != ' ') {
                    continue;

                }else{
                    computerMove = 7;
                }
            } else if (board [2][1] == 'X' && board [1][1] == 'X' ||
                    board [0][0] == 'X' && board [0][2] == 'X' ) {
                if(board[0][1] != ' ') {
                    continue;

                }else{
                    computerMove = 8;
                }
            } else if (board[2][0] == 'X' && board[1][1] == 'X' ||
                    board [2][2] == 'X' && board [1][2] == 'X' ||
                    board [0][0] == 'X' && board [0][1] == 'X' ) {
                if(board[0][2] != ' ') {
                    continue;

                }else{
                    computerMove = 9;
                }
            }else {
                computerMove = rand.nextInt(9)   1;
            }
            if (isValidMove(board, Integer.toString(computerMove))) {
                break;
            }
        }
        placeMove(board, Integer.toString(computerMove), 'O');
        System.out.println("Computer chose "   computerMove);

    }

CodePudding user response:

I think the problem is that if your board is empty the computer will still have a move because of the else condition else { computerMove = rand.nextInt(9) 1; } Then the following if statement will give a valid move and you will break the while loop and place the move on the board. So if you want to prevent the computer from playing first, you should make a function that checks if the board is empty and if it is then do nothing.

public boolean isEmpty(){
    for(int i = 0; i<3; i  )
       for(int j = 0; j<3; j  )
           if(board[i][j] != ' ') return false;
     return true
}

If needed you can pass the board as a parameter in the function.

Hope I helped. Good luck!

CodePudding user response:

There's a problem with the continues. The continue causes the next iteration. Because nothing changed, the game stays in the same state, the same condition will be met and the loop will continue endless.

Rewrite your conditions, so instead of e.g.

            if(board [2][0] == 'X' && board [2][1] == 'X' ||
                    board [1][1] == 'X' && board [0][0] == 'X' ||
                    board [1][2] == 'X' && board [0][2] == 'X'){
                if(board[2][2] != ' ') {
                    continue;

                }else{
                    computerMove = 3;
                }
            } else ...

write

            if((board [2][0] == 'X' && board [2][1] == 'X' ||
                    board [1][1] == 'X' && board [0][0] == 'X' ||
                    board [1][2] == 'X' && board [0][2] == 'X') && 
                    board [2][2] == ' '){
                computerMove = 3;
            } else ...
  •  Tags:  
  • java
  • Related