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

2016/6/11

is anyone here can help me writing a while loop for a Z pattern?

MLP MLP

2016/6/11

#
[Disallowed URL]
MLP MLP

2016/6/11

#
Anyone can help me by writing a while loop for a Z pattern similar to the image attached? thanks
danpost danpost

2016/6/11

#
MLP wrote...
Anyone can help me by writing a while loop for a Z pattern similar to the image attached? thanks
I would move to the top left starting point for the Z and place the first clover immediately. Then use a for loop to place the remaining top clovers. Using another for loop, I would place the ones on the diagonal. Finally, another for loop to place the remaining clovers. Using 'while' loops instead of 'for' loops is always possible. I did not describe where all the turns should be; but, they are not that hard to figure out (set-up turns and moves before each loop and those require within the loops). However, if you are trying the Z-pattern, I would presume that you have worked out other patterns already and have some experience with what is needed. Also, this is probably an assignment, and giving the complete code would not be appropriate. Besides that, your project is either not greenfoot related at all or is very specialized in that your code would be top-down for the life of the bug and normal greenfoot code would be at-the-moment code.
MLP MLP

2016/6/13

#
thanks mate for your help but this is not an assignment i'm practicing for my final exam. I'm wondering if my coding is right using while loops thanks move (); while ( !treeFront () ) { if ( !onLeaf () ) { putLeaf(); move(); } } if ( treeFront () ) { turnRight (); move(); turnRight (); move(); stepForward (); } while ( !treeFront() ) { turnLeft(); stepForward(); turnRight(); stepForward(); } turnLeft(); turnLeft(); move(); while ( !treeFront() ) { if ( onLeaf() ) { move(); } else { putLeaf(); } } } void stepForward () { move(); putLeaf(); move(); } }
danpost danpost

2016/6/13

#
The code appears to create a Z-pattern. The 'while' loops are apparently doing what they were intended to do. There are some things that could be improved on here, however. To help in the explanations, here is your code with line numbering (you can go to the link below the reply box 'Posting code? read this!' for instructions on this):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
move ();
         
        while ( !treeFront () )
        {
            if ( !onLeaf () )
            {
            putLeaf();
            move();
      }
    }
     
    if ( treeFront () )
    {
        turnRight ();
        move();
        turnRight ();
        move();
        stepForward ();
    }
     
    while ( !treeFront() )
       {          
        turnLeft();
        stepForward();
        turnRight();
        stepForward(); 
    }   
        turnLeft();
        turnLeft();
        move();       
    while ( !treeFront() )
        {
            if ( onLeaf() )
            {
                move();
            }               
                else
                {
                    putLeaf();
                }
        }
  }
   
  void stepForward ()
  {
      move();
      putLeaf();
      move();
    }
}
danpost danpost

2016/6/13

#
Line 5 is checking for 'onLeaf' which is not really necessary as this is the first actions being done and no leaves should be yet in the world. Lines 5, 6 and 9 can be removed. Line 12 is quite similar, in that the previous loop ends with a tree being in front, so this check is not needed as we already know one is there. Lines 12, 13 and 19 can be removed. Lines 28 through 30, turns Clara around and moves her back on top of the last leaf placed into the world. So, if we just move forward one more time after turning around, she will be off any leaf and the check on line 33 can be removed. So, insert a second 'move();' line at line 30 so that there are two moves made there and replace lines 33 through 40 with the following two lines:
1
2
putLeaf();
move();
MLP MLP

2016/6/14

#
thank you so much
You need to login to post a reply.