Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my if-else block ever getting hit, even though it should be? (Just need another pair of eyes.)

I am making a Falling Sand style game in Java, and I'm having weird issues with an if-else block that I have. In my doGravity() method, I have an various blocks of conditions that will cause different things to happen, and for some odd reason, one block is NEVER getting hit.

When I have this block count how many times each condition is hit, the left and right blocks are hit almost evenly:

else if(world[x][y+1]==EMPTY && (x-1 >= 0) && world[x-1][y+1] == EMPTY && (x+1 < world.length) && world[x+1][y+1]==EMPTY) {
    int r = rand.nextInt(50);
    if(r == 0) {
        world[x-1][y+1] = world[x][y];
        //System.out.println("GO: right");
        countRight++;
    }
    else if(r == 1) {
        world[x+1][y+1] = world[x][y];
        //System.out.println("GO: left");
        countLeft++;
    }
    else {
        world[x][y+1] = world[x][y];
        countCenter++;
    }
    world[x][y] = EMPTY;
}

Next comes this condition, which also equally distributes left and right.

else if((x-1 >= 0) && world[x-1][y+1] == EMPTY && (x+1 < world.length) && world[x+1][y+1]==EMPTY) {
    if(rand.nextBoolean()) {
        world[x-1][y+1] = world[x][y];
        //countLeft++;
    }
    else {
        world[x+1][y+1] = world[x][y];
        //countRight++;
    }
    world[x][y] = EMPTY;
}

But when I count these blocks, the left block NEVER gets hit, even when the space to the left is open. I feel like its probably just some stupid typo that I can't see for some reason.

else if((x-1 >= 0) && world[x-1][y+1] == EMPTY) {
    world[x-1][y+1] = world[x][y];
    world[x][y] = EMPTY;
    countLeft++;
    System.out.println("Hit Left");
}
else if((x+1 < world.length) && world[x+1][y+1] == EMPTY) {
    world[x+1][y+1] = world[x][y];
    world[x][y] = EMPTY;
    countRight++;
    System.out.println("Hit Right");
}

UPDATE: If I remark out the left block at the end, absolutely nothing changes. The sand acts exactly the same. If I remark out the right block at the end, it acts the same as if I remark out both blocks. I cannot figure this out. It should work... but it doesn't.

UPDATE: Here's the full source code. I have no idea what this could possibly be. It will, in fact, drive me insane. http://pastebin.com/mXCbCvmb

like image 973
Nick Anderegg Avatar asked Nov 22 '25 07:11

Nick Anderegg


1 Answers

Your pastebin code does show "Hit left", you just need to change the creation of world (line 65 pastebin) to

    world = new Color[worldWidth][worldHeight+1];

Because of the y+1 part i would suppose. Other than that it grows both to the left and to the right.

EDIT: http://pastebin.com/GVmSzN4z I twiddled a little with your doGravity to make the drops a little more symmetric.

like image 80
Captain Giraffe Avatar answered Nov 23 '25 22:11

Captain Giraffe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!