This site requires JavaScript, please enable it in your browser!
Greenfoot back
berzerker
berzerker wrote ...

2014/3/13

help with 'while' loops

berzerker berzerker

2014/3/13

#
hey guys i need help with a problem. I understand the for/while loops and its functions but am struggling as to how to start this problem. any help is appreciated image
danpost danpost

2014/3/13

#
You could start with ... 'while'! Ask yourself this: after all that you want done is done, what will be the final state? You are showing Clara initially on the left, at coordinates (1, 2) in the first image, and for the final state in the second image at (13, 2). So, as long as (or 'while') 'getX' returns a number less than 13, we want to process this loop. What needs to be done 'while' inside this loop? Clara needs to check for a tree to her right; if no tree is there, add a Clover; and proceed forward to the next location to repeat. Once she reaches the destination location, the loop will terminate. This will only work providing that the 'potholes' are all just one cell in depth (which they are, in your case) and one cell wide (which again is the case). It would be a bit more involved otherwise.
elias.groll elias.groll

2014/3/14

#
"Kara" is made for education in deterministic mashines so u probably have to create a deterministic mashine first (if u had that in school or want to impress ur teacher :P) So your while loop could go like this( dont know if it really works i havent testet it it was just my first thought :P): while(remainingFieldsToRepair()) /* or for(int x = remainingFields();x > -1; x++) */ { if(treeInFrontOfKara){ turnLeft(); } else if(emptyFieldOnTheRightSideOfKara){ turnRight(); goForwards(); putACloverleafOnTheGround(); } else{ goForwards(); } } //remainingFieldsToRepair() returns a boolean and remainigFields() Returns a int..
You need to login to post a reply.