Jump to content
  • Advertisement
Sign in to follow this  
grumpyOldDude

loop, continue and reset issue

This topic is 996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
	    	  }
	     }

Share this post


Link to post
Share on other sites
Advertisement

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.

Edited by rip-off

Share this post


Link to post
Share on other sites

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);
	}
}

Share this post


Link to post
Share on other sites
`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. Edited by Álvaro

Share this post


Link to post
Share on other sites

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

Edited by alwaysGrey

Share this post


Link to post
Share on other sites

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;
}

Share this post


Link to post
Share on other sites

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!

Edited by alwaysGrey

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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;
        }
    }
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!