How can I use "or" in an if statement in Java without having to retype the whole expression?

9.1k views Asked by At

I'm writing code for a minesweeper project for class and one method is numAdjMines, which counts the mines around a cell in the array, each type of cell has a different value, like mines are -2, while mines with a flag on them are -4. I want to just write one if statement, but I end up having to just write the same code twice, with different values at the end.

if (row >= 1 && col >= 1 && boardArray[row - 1][col - 1] == MINE)
    {
        adjMines = adjMines + 1;
    }
if (row >= 1 && col >= 1 &&
            boardArray[row - 1][col - 1] == FLAGGED_MINE)
    {
        adjMines = adjMines + 1;
    }

I tried using || for or and writing || boardArray[row-1][col-1] == FLAGGED_MINE at the end of the first one, but that then ignored the beginning with checking the row and column. Is there a short compact way for me to write this code?

6

There are 6 answers

0
irreputable On
for(    int r : array( row-1, row, row+1 ))
    for(int c : array( col-1, col, col+1 ))
    {
        if(r==row && c==col) // center
            continue; 

        if(0<=r&&r<ROWS && 0<=c&&c<COLS) // within bounds
        {
            int state = boardArray[r][c];
            if(state==MINE||state==FLAGGED_MINE)
                adjMines++;
        }
     }

int[] array(int... ints){ return ints; }

So you don't have to write 8 seperate cases. Not a very big deal here, but if you have a 3D mine...

1
The Scrum Meister On

You can use parentheses to group the conditions

if(row >= 1 && col >= 1 && 
  (boardArray[row - 1][col - 1] == MINE
    || boardArray[row - 1][col - 1] == FLAGGED_MINE))
{
    adjMines = adjMines + 1;
}
0
James Davies On

Your above code can actually be compressed into a single IF statement, however I presume your actual code contains more statements otherwise you would have done this already.

The easiest way to simplify such code would be to break it into two layers of IF statements. The outer one contains the common condition, and the inner ones contain the specific conditions.

if (row >= 1 && col >= 1 ){

    int cell = boardArray[row - 1][col - 1];

    if( cell == MINE ){
        // Code here
    }

    else if( cell == FLAGGED_MINE )
    {
        // Code here
    }

}
0
Bill the Lizard On

I'm not sure which part you're trying to avoid repeating, but you can avoid the repetion on the first part by nesting the if statements.

if( row >= 1 && col >= 1 ) {
    if( boardArray[row - 1][col - 1] == MINE || 
        boardArray[row - 1][col - 1] == FLAGGED_MINE ) {

        adjMines = adjMines + 1;
    }
}

I'd take it even one step further and make the inner if statement a method call.

0
pstanton On

To avoid repetition you can use nested if statements, ie both conditions rely on row & col being >= 1, so pull that out into it's own statement.

Then i'm guessing you want to avoid pulling the value out of the array multiple times, so the best thing to do is assign it to a variable. this probably isn't more efficient at runtime, however is nicer to look at.

if (row >= 1 && col >= 1)
{
    int value = boardArray[row - 1][col - 1];
    if (value == MINE || value == FLAGGED_MINE)
    {
        adjMines = adjMines + 1;
    }
}
0
Marcelo On

I am not sure if I entirely understand the question, but the following code is more compact and accomplish the same goal:

if (row >= 1 && col >= 1 && 
    (boardArray[row - 1][col - 1] == MINE || boardArray[row - 1][col - 1] == FLAGGED_MINE)) {
    adjMines++;
}

I am not sure if writing to your main array is a good idea for the flagged mines. I suppose a player can flag a free cell by mistake and that would mark the cell as a flagged mine and you won't know if a mine was actually there. Maybe you have logic for handling this, but it's always good to have your original board (the data structure) safe for future reference.