lundi 6 août 2018

Java if-statement multiple AND & ORs of Array

So I have a program that checks if a number in an array, which corresponds to a size of a bar that an arm must pass over (like a claw machine).

I need it to go over, but only just. I have an if-statement that checks if the size is 3 (size 1 and 2 behave differently, these work), then checks if there is a block of size 3 already on the previous column, if so, and that column is the biggest, don't move up.

if (!(currentSize == 3 && this.barHeightsPlaced[0] == 1 && this.barHeights[0] == largestBar)) {
    while (this.height < largestBar + currentSize + 1 && this.height < Control.MAX_HEIGHT) {
        robot.up();
        this.height++;
        gapSize++;
    }
}

So this works for the first bar, but say that it is putting something on the 3rd bar (this.barHeights[2]), so it needs to check if this.barHeights[1] is largest and it's placed counter part is equal to 1.

I thought I could do that with:

if (!(currentSize == 3 && (this.barHeightsPlaced[0] == 1 && this.barHeights[0] == largestBar) || (this.barHeightsPlaced[1] == 1 && this.barHeights[1] == largestBar) || (this.barHeightsPlaced[2] == 1 && this.barHeights[2] == largestBar) || (this.barHeightsPlaced[3] == 1 && this.barHeights[3] == largestBar))

But this doesn't work.

Is there a way to do it without many if-statements. A apologize if this has been done before, I'm new to java, and everything I looked for didn't use arrays.

Aucun commentaire:

Enregistrer un commentaire