Results 1 to 13 of 13
  1. #1
    D.good is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Help!!!! infinite loop

    Hello folks

    So I am working on my first assignment for Stanfords see program. The title of the of the first assignment that I choose to do first that is, is problem 3 checkerboardkarel. infinite loop has me stumped-images.jpg
    here is my code for the first leg, as well as my problem
    Java Code:
    /*
     * File: CheckerboardKarel.java
     * ----------------------------
     * When you finish writing it, the CheckerboardKarel class should draw
     * a checkerboard using beepers, as described in Assignment 1.  You
     * should make sure that your program works for all of the sample
     * worlds supplied in the starter folder.
     */
    
    import stanford.karel.*;
    
           public class CheckerboardKarel extends SuperKarel {
           public void run(){
    	    turnLeft();
    	    layRow();
    }
       private void layRow(){
    	while(frontIsClear()){
    			putBeeper();
    			move();
    			if(frontIsBlocked()){
    				turnRight();
    		}else
    			move();
    		}
    }
    }
    for some reason this code is creating an infinite loop causing karel to lay beepers around the border.
    Can some one please!! point me in the right direction to end the infinite loop thanks
    Last edited by Norm; 02-16-2012 at 09:07 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: infinite loop has me stumped

    Where is the infinite loop? Is it the while() statement?
    When do you want the loop to exit? Can you add a test for the condition when you want the loop to exit and issue a break statement?
    if(exit the loop condition)
    break; // exit the loop

    When does the frontIsClear() method return false?

  3. #3
    D.good is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: infinite loop has me stumped

    Quote Originally Posted by Norm View Post
    Where is the infinite loop? Is it the while() statement?
    When do you want the loop to exit? Can you add a test for the condition when you want the loop to exit and issue a break statement?
    if(exit the loop condition)
    break; // exit the loop

    When does the frontIsClear() method return false?
    I'm not sure, I do believe the infinite loop is in the while I just don't understand why. The code I put is my test because I kept running into a infinite loop in the while for some reason the if doesn't break the loop it seems to add to it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: infinite loop has me stumped

    When should the loop end? Can you test for when and use a break statement?
    To insure the loop ends, use a counter. When the vaue of the counter gets big enough, break
    Java Code:
    int cntr = 0;
    
    while....{
    if(cntr++ > 200)
      break;
    ...
    }
    Last edited by Norm; 02-16-2012 at 05:41 PM.

  5. #5
    D.good is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: infinite loop has me stumped

    I'm learning Java through Stanford engineering every where program. So I haven't learned how to do test yet, and I can't use integers
    The while is suppose to end when front becomes blocked then the if statement is suppose to take over. The problem is that the if is fulfilled so I guess then the infinet loop is in the if statement but I'm not sure how
    Last edited by D.good; 02-16-2012 at 08:40 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: infinite loop has me stumped

    You should ask your instructor
    or post all of the code that is being used.
    For example the frontIsClear() method and move() and turnRight etc

    Unless we see all the code there is no way to know what it is doing.

  7. #7
    D.good is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: infinite loop has me stumped

    i can't ask the instructor its a self paced course. I have pasted all the code I have written that caused the infinite loop

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: infinite loop has me stumped

    Then you have to read the API doc for the methods and classes you are using.
    I told you earlier some ways to end the loop.
    Without the code for all the classes and methods, there is no way to suggest what is wrong.

  9. #9
    D.good is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: infinite loop has me stumped

    i don't understand, the code I put is all there is, in the constructs of Karl the robot the above code is the classes and methods but thank you kind sir

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help!!!! infinite loop

    Quote Originally Posted by D.good View Post
    Java Code:
    	while(frontIsClear()){
    			move();
    			if(frontIsBlocked()){
    				turnRight();
    		}else
    			move();
    		}
    Imagine you were the robot and you had to do the above; would'nt you be running in circles (squares) ad nauseam?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: infinite loop has me stumped

    Take a piece of paper and draw the maze that the Mover is in.
    Place the Mover on the maze and play computer with the program that you have posted. Follow its statements one by one and seeif the program is wrong or if the maze has no solution.
    Is it possible that there is not a solution? Can the Mover just go in circles and never escape?

  12. #12
    D.good is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help!!!! infinite loop

    Quote Originally Posted by JosAH View Post
    Imagine you were the robot and you had to do the above; would'nt you be running in circles (squares) ad nauseam?

    kind regards,

    Jos

    I have done just that, before I came to the forums. Am I using the wrong if statement?

  13. #13
    D.good is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

Similar Threads

  1. infinite loop
    By javapink in forum New To Java
    Replies: 19
    Last Post: 03-06-2011, 02:28 AM
  2. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 08:59 AM
  3. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-13-2010, 11:45 PM
  4. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:34 AM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 10:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •