EpicProgrammer99
EpicProgrammer99

Reputation: 11

Can I have 3 conditional statements in an if statement in Java?

I am writing a tic tac toe game program and I need to check three cells of an array to check for a win. I wanted to write my code such that

if(cell1 == cell2 == cell3 ||
   cell4 == cell5 == cell6 || etc..) return true;

but it doesn't seem to let me include three conditional statements in one part. I know I could rewrite this code using &&, but is there a way to write a conditional statement with three things?

Upvotes: 0

Views: 677

Answers (4)

user12929063
user12929063

Reputation:

Yes, in an if statement, you can set more conditions but making it only an if statement would be better, you don't want else if in an if statement, that would only work outside the condition

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299008

I would probably take a more high level approach, because it tends to make things more manageable.

First, here's an enum of all valid winning sequences:

enum Sequence implements Function<int[],int[]> {
    TOP_ROW(0,1,2),
    MIDDLE_ROW(3,4,5),
    BOTTOM_ROW(6,7,8),
    LEFT_COL(0,3,6),
    MIDDLE_COL(1,4,7),
    RIGHT_COL(2,5,8),
    DOWN_DIAG(0,4,8),
    UP_DIAG(2,4,6);

    private final int[] positions;

    Sequence(final int ... positions) {
        this.positions = positions;
    }

    @Override
    public int[] apply(final int[] ints) {
        int[] cells = new int[positions.length];
        for (int i = 0; i < positions.length; i++) {
            cells[i] = ints[positions[i]];
        }
        return cells;
    }
}

Now, assuming that your data model is an int[9] where all values are 0 (empty), 1 (player 1), or 2 (player 2), I'd use a method like this to check whether anyone has won (1 or 2 means the respective player has won, 0 means no one has won yet):

int winningPlayer(int[] game) {
    return Arrays.stream(Sequence.values())// for all known sequences
                 .map(s -> s.apply(game)) // extract the sequence from the game array
                 .filter(
                     // check whether all values are the same, and not neutral
                     a -> Arrays.stream(a).distinct().count() == 1 && a[0] > 0)
                 .findFirst() // if I find a matching array
                 .map(a -> a[0]) // then extract the player
                 .orElse(0); // otherwise return the neutral player

}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201477

The answer is yes, but the syntax is verbose. Stream the three elements. Perform a distinct. Count the number of distinct elements. If it's less than 2 all of the elements are equal. Like,

if (Stream.of(cell1, cell2, cell3).distinct().count() < 2
        || Stream.of(cell4, cell5, cell6).distinct().count() < 2
        || etc... ) {

}

Upvotes: 0

user13306412
user13306412

Reputation: 128

Your problem is

  cell1 == cell2 == cell3

I assume these are integers. Then cell1 == cell2 yields a boolean result, and then you're trying to compare that boolean result to an integer, which isn't allowed and makes little sense.

You need cell1 == cell2 && cell2 == cell3.

By the way, that's one conditional expression, not three conditional statements. The conditional expression is part of an if statement.

Upvotes: 1

Related Questions