Advertisement

loop, continue and reset issue

Started by January 02, 2016 07:30 PM
7 comments, last by Alberth 8 years, 11 months ago

At this stage in my puzzle game I'm searching for a pattern in a bunch of pixels.... the issue here is with the loop.

I'm seeing unexpected results and i traced it the line commented //reset to zero , the loop continues without resetting to zero because the order should be as in the next code below the first, but this flags as error

So is there any way around this? (code in JAVA)


	     for(int j=1; j<=15; j++){
	    	  outOfRowColor=0; inRowColor=0;               // reset to zero
                  loop1:
	    	  for(int i=1; i<=15; i++){
                     ...
                 
                     ...
                     if( outOfRowColor >= 3 )continue loop1;	
	    	  }
	     }

outOfRowColor=0; and inRowColor=0; need to reset at beginning of the next cycle of the loop, and should be in this order but this wouldn't compile. Looking for alternative means to get this done

Any ideas , thanks


	     for(int j=1; j<=15; j++){
	    	  loop1:
	    	  outOfRowColor=0; inRowColor=0;        // reset to zero, Error because loop1 should be next to for(){
	    	  for(int i=1; i<=15; i++){	
                      ...

                      ...
                      if( outOfRowColor >= 3 )continue loop1;
	    	  }
	     }

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

A simple solution would be to extract the inner loop, along with the variable resets, into a helper method which has an early return statement. If possible move the declarations of these variables into this method.

However I suspect there might be other ways to solve this issue, but I'd need to see the full code to be sure.

Advertisement

Rest of the code,

could you expand more on how such helper method would work? thanks


	void findRPixels( Bitmap bm_obj, int tY, int tX ){
		
		sumPixsXi = 0;  sumPixsYj = 0; cnt=0; avrX=0.0f; avrY=0.0f;
		outOfRowColor=0;  inRowColor=0; endCntP=0; endCntN=0;
		loop2:
	        for(int j=1; j<=15; j++){
	    	    outOfRowColor=0; inRowColor=0;
	    	    loop1:
	    	    for(int i=1; i<=15; i++){	
	    		  if( markPixel[tY+j][tX+i] == 0){
		    		  if( checkForDKGREEN( bm_obj, tX+i, tY+j) == true ){
		    			  
		    			  markPixel[tY+j][tX+i] = 1;
		    			  sumPixsXi  =  sumPixsXi + tX+i;
		    			  sumPixsYj  =  sumPixsYj + tY+j;
		    			  cnt        = cnt + 1;
		    			  outOfRowColor = 0;
		    			  inRowColor++;
		    			  bm_obj.setPixel(tX+i, tY+j, Color.GREEN);
		    		  }
		    		  else{
		    			  outOfRowColor++;          //after leaving boun vertically
		    			  if( inRowColor == 0 ){
		    				  endCntP++;
		    				  if( endCntP >= 2 )break loop2;
		    			  }
		    			  if( outOfRowColor >= 3 )continue loop1;
		    		//	  bm_obj.setPixel(tX+i, tY+j, Color.YELLOW);
		    		  }/**/
	    		  }
	    	  }
	     }
	     
	     if( cnt >= 4 ){
	    	 avrX = (float)(sumPixsXi)/(cnt);
	    	 avrY = (float)(sumPixsYj)/(cnt);
	    	 newRoofPt++;
	     }

	     bm_obj.setPixel( (int)avrX, (int)avrY, Color.WHITE);
	}
}

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

`continue' and `break' can be seen just as particularly common instances of `goto'.

for (...) {
  // ...
  // You can do `goto CONTINUE_LABEL' instead of `continue'
  // and `goto BREAK_LABEL' instead of `break' here, even from nested loops
  // ...
  CONTINUE_LABEL:;
}
BREAK_LABEL:;
However, most of the time you can separate the inner loop into a separate function and avoid the issue. If you can give this function a good name and the resulting code is clear, write the function. If not, there is nothing wrong with using `goto' here.

However, most of the time you can separate the inner loop into a separate function and avoid the issue. If you can give this function a good name and the resulting code is clear, write the function. If not, there is nothing wrong with using `goto' here.

@Alvaro many thanks, i would try using goto... eer... I don't think this syntax exist in Java

EDIT: Yeah, just googled now and Java doesn't have a "goto" statement

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

This seems rather obvious so apologies if this is a stupid suggestion (I am not very familiar with Java) but can't you reset those variables before doing continue loop1?

if( outOfRowColor >= 3 ) {
   outOfRowColor=0; inRowColor=0;
   continue loop1;
}

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Advertisement

This seems rather obvious so apologies if this is a stupid suggestion (I am not very familiar with Java) but can't you reset those variables before doing continue loop1?

if( outOfRowColor >= 3 ) {
outOfRowColor=0; inRowColor=0;
continue loop1;
}

Not stupid question at all, very valid.

The reason this wouldn't work is is ...... is thats not the only condition, it also needs to work at the natural end of the loop

.... actually this would work, together with the current position of the statement, smart man!

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

That is a particularly complex method, in that there appear to be many non-local variables modified that may be used by other methods. I suspect some of those variables are not used elsewhere, and could be converted into locals, but even then I'm not sure that extracting a method will actually simplify anything here.

Possibly restructuring some of the higher level code, particularly with an eye to reducing the need for so much mutable state here, would enable this approach.

The solution by Nanoha was also the first one I came up with.

If you have lots of these cases, it becomes a bit messy, so my second solution was


for(int j=1; j<=15; j++) {
    boolean resetColors = true;

    loop1:
    for(int i=1; i<=15; i++){
        if (resetColors) {
            outOfRowColor=0; inRowColor=0; // reset to zero
            resetColors = false;
        }
        ...

        ...
        if( outOfRowColor >= 3 ) {
            resetColors = true;
            continue loop1;
        }
    }
}

This topic is closed to new replies.

Advertisement