Home > OS >  Can a if-else_if-else ladder can be written in other way in java?
Can a if-else_if-else ladder can be written in other way in java?

Time:12-25

I want to modify the format of the code lines since they don't seem nice and I want to reduce the amount of lines.

Please tell how to shorten or write in other format of the below code

        if (token[0][0] == token[0][1] && token[0][1] == token[0][2] && token[0][2] == 0)
            return true;
        else if (token[1][0] == token[1][1] && token[1][1] == token[1][2] && token[1][2] == 0)
            return true;
        else if (token[2][0] == token[2][1] && token[2][1] == token[2][2] && token[2][2] == 0)
            return true;
        else if (token[0][0] == token[1][0] && token[1][0] == token[2][0] && token[2][0] == 0)
            return true;
        else if (token[0][1] == token[1][1] && token[1][1] == token[2][1] && token[2][1] == 0)
            return true;
        else if (token[0][2] == token[1][2] && token[1][2] == token[2][2] && token[2][2] == 0)
            return true;
        else if (token[0][0] == token[1][1] && token[1][1] == token[2][2] && token[2][2] == 0)
            return true;
        else if (token[0][2] == token[1][1] && token[1][1] == token[2][0] && token[2][0] == 0)
            return true;
        else
            return false;

It would be better if it would be like the below\

        if (board[0] == getUserRole) {
            if (board[1] == getUserRole)
                if (board[2] == getUserRole)
                    return true;
            if (board[3] == getUserRole)
                if (board[6] == getUserRole)
                    return true;
            if (board[4] == getUserRole)
                if (board[8] == getUserRole)
                    return true;
        }

It would be better if it can be written in one line mode, please try to code it within 5 lines (if possible.) P.S.-> Only diagonals or the straights like in tic tac toe.

CodePudding user response:

Instead of checking for zeroes you could have a boolean array initialized to all false. Then you could do something like the following.

if (token[0][0] && token[0][1] && token[0][2]) 
            return true;
if (token[1][0] && token[1][1] && token[1][2])
            return true;
if (token[2][0] && token[2][1] && token[2][2])
            return true;
// ...
// rest of similar conditions here
// ...

return false;

Of course, that depends on the overall design of your game.

CodePudding user response:

Looks like you have following check pattern:

000 ... ... 0.. .0. ..0 0.. ..0 
... 000 ... 0.. .0. ..0 .0. .0. 
... ... 000 0.. .0. ..0 ..0 0..

So, all you can do is to simplify your code removed duplication.

public static boolean check(int[][] token) {
    IntPredicate rowZero = row -> token[row][0] == 0 && token[row][1] == 0 && token[row][2] == 0;
    IntPredicate colZero = col -> token[0][col] == 0 && token[1][col] == 0 && token[2][col] == 0;
    IntPredicate diagonalZero = i -> i == 0 ? token[0][0] == 0 && token[1][1] == 0 && token[2][2] == 0
                                            : token[0][2] == 0 && token[1][1] == 0 && token[2][0] == 0;

    return rowZero.test(0) || rowZero.test(1) || rowZero.test(2) ||
            colZero.test(0) || colZero.test(1) || colZero.test(2) ||
            diagonalZero.test(0) || diagonalZero.test(1);
}
  • Related