Converting ugly method to a for loop *solved*

Hey JGO. :smiley:
A few months back I spent a decent amount of time in JMonkeyEngine, now after trying a few things in LWJGL via code I’ve written I’ve decided it would be easier for me to produce a quality game via JMonkeyEngine.

While the issue I’m having doesn’t have any relevance to JME, it is relevant to Java, that’s why I figured I’d post this question here instead of the 3rd party engines board.

What I’m doing
:
Rendering a grid of 25x20 (Volume: 1) cubes, same height level for all just different depth and x axis offsets.

Notes:
The variables x1, x2, x3 etc are just used so after 25 cubes are looped through, their offsets starting value is reset to zero.
(So when it’s incremented by the width / depth of the cube, the cube doesn’t start with a improperly offset position)
It works ;D ;D, but I’d like to throw it into a simple for loop, I just don’t know how I’d write it at the moment.
I’m stumped, hoping somebody could help.

How it currently looks :clue::


		/*
		 * Add a new Box to the list of floorBoxes. 
		 * Add a new Geometry to the list of Geometry's for the box. 
		 * Set the floorBoxes material. 
		 * Attach the floorBoxes geometry the terrain's static geometry node.
		 * Position each Box properly.
		 */
		for (int i = 0; i < floorCubes; i++) {
			floorBoxes.add(new Box(Vector3f.ZERO, new Vector3f(floorCubeWidth, floorCubeHeight, floorCubeDepth)));
			floorGeoms.add(new Geometry("Box " + i, floorBoxes.get(i)));
			floorGeoms.get(i).setMaterial(floorMaterial);
			floorNode.attachChild(floorGeoms.get(i));
			if (i < 25) {
				floorGeoms.get(i).setLocalTranslation(i, 0, 0);
			} else if (i > 24 && i < 50) {
				floorGeoms.get(i).setLocalTranslation(x1++, 0, -floorCubeDepth);
			} else if (i > 49 && i < 75) {
				floorGeoms.get(i).setLocalTranslation(x2++, 0, -floorCubeDepth * 2);
			} else if (i > 74 && i < 100) {
				floorGeoms.get(i).setLocalTranslation(x3++, 0, -floorCubeDepth * 3);
			} else if (i > 99 && i < 125) {
				floorGeoms.get(i).setLocalTranslation(x4++, 0, -floorCubeDepth * 4);
			} else if (i > 124 && i < 150) {
				floorGeoms.get(i).setLocalTranslation(x5++, 0, -floorCubeDepth * 5);
			} else if (i > 149 && i < 175) {
				floorGeoms.get(i).setLocalTranslation(x6++, 0, -floorCubeDepth * 6);
			} else if (i > 174 && i < 200) {
				floorGeoms.get(i).setLocalTranslation(x7++, 0, -floorCubeDepth * 7);
			} else if (i > 199 && i < 225) {
				floorGeoms.get(i).setLocalTranslation(x8++, 0, -floorCubeDepth * 8);
			} else if (i > 224 && i < 250) {
				floorGeoms.get(i).setLocalTranslation(x9++, 0, -floorCubeDepth * 9);
			} else if (i > 249 && i < 275) {
				floorGeoms.get(i).setLocalTranslation(x10++, 0, -floorCubeDepth * 10);
			} else if (i > 274 && i < 300) {
				floorGeoms.get(i).setLocalTranslation(x11++, 0, -floorCubeDepth * 11);
			} else if (i > 299 && i < 325) {
				floorGeoms.get(i).setLocalTranslation(x12++, 0, -floorCubeDepth * 12);
			} else if (i > 324 && i < 350) {
				floorGeoms.get(i).setLocalTranslation(x13++, 0, -floorCubeDepth * 13);
			} else if (i > 349 && i < 375) {
				floorGeoms.get(i).setLocalTranslation(x14++, 0, -floorCubeDepth * 14);
			} else if (i > 374 && i < 400) {
				floorGeoms.get(i).setLocalTranslation(x15++, 0, -floorCubeDepth * 15);
			} else if (i > 399 && i < 425) {
				floorGeoms.get(i).setLocalTranslation(x16++, 0, -floorCubeDepth * 16);
			} else if (i > 424 && i < 450) {
				floorGeoms.get(i).setLocalTranslation(x17++, 0, -floorCubeDepth * 17);
			} else if (i > 449 && i < 475) {
				floorGeoms.get(i).setLocalTranslation(x18++, 0, -floorCubeDepth * 18);
			} else if (i > 474 && i < 500) {
				floorGeoms.get(i).setLocalTranslation(x19++, 0, -floorCubeDepth * 19);
			}
		}

Looking forward to any feedback / hints on how I can convert this / clean it up, thanks JGO.

I can’t look at that properly. It hurts my eyes.

Try using the modulus operator.

XD, I was in pain while writing it O.O 500 cubes lol.
I’ll go look into implementing the Modulus Operator in here somehow (Never really used it to be honest :o :o :o), in the meantime feedback’s still welcome guys.
Thanks.

The usage of 19 variables (x1…x19) makes it impossible to turn into a loop (without reflection).

just-use-arrays


int[] xValues = new int[19];

for(int i = 0; i < floorCubes; i++) {
    floorBoxes.add(new Box(Vector3f.ZERO, new Vector3f(floorCubeWidth, floorCubeHeight, floorCubeDepth)));
    floorGeoms.add(new Geometry("Box " + i, floorBoxes.get(i)));
    floorGeoms.get(i).setMaterial(floorMaterial);
    floorNode.attachChild(floorGeoms.get(i));
    
    if(i < 25)
        floorGeoms.get(i).setLocalTranslation(i, 0, 0);
    else
        floorGeoms.get(i).setLocalTranslation(xValues[i/25 - 1]++, 0, -floorCubeDepth * (i/25));
}

This guy… XD!!
Thanks ra4king, 46 lines of code turned into 10 works perfect.

Now to do this for the walls and a few static boxes :point: