Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* PERMITTED COMMANDS
- move, turnLeft, turnRight, treeLeft, treeRight, treeFront, onLeaf, putLeaf, removeLeaf, mushroomFront
- JAVA
- if, while, for
- To use this code in Greenfoot copy everything below to */
- class MyClara extends Clara {
- void run() {
- int b = 0;
- //Clara uses AI() function, along with the variable b, which helps her to
- //change the lane and her direction( East or West ). She starts with facing
- //towards East and then store 1 as a value in b, so when next time loop
- //executes turns towards West, changing the value of b to 0. This loop
- //continues until their is a tree infront of her.
- while(!treeFront())
- {
- AI();
- if (b == 0)
- {
- if(!treeRight())
- {
- turnRight();
- move();
- turnRight();
- b = 1;
- }
- }
- else if (b == 1)
- {
- if(!treeLeft())
- {
- turnLeft();
- move();
- turnLeft();
- b = 0;
- }
- }
- }
- }
- //Using this function clara moves until their is a tree infront of her, along
- //with executing IQ() function.
- void AI()
- {
- while(!treeFront())
- {
- IQ();
- move();
- }
- IQ();
- }
- //This function is for clara to remove leaf if their is one underneath her and if
- //not, she is places a leaf undernath her.
- void IQ()
- {
- if(!onLeaf())
- {
- putLeaf();
- }
- else
- {
- removeLeaf();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement