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

2015/5/2

Need help asap, problem with calculating

randombruh randombruh

2015/5/2

#
Hey guys so I have a uni homework thing, and I need help. Our goal in GreenFoot is to make the user enter a number of the height and width of a basket then clara which is a lady beattle, must move and create the dimensions we gave it. I have done that already, but inside we must put leafs inside the basket, but it must be the same as the height and width of the basket given. I tried to make a seperate Integer by adding both the basket height and basket width together but didn't work. This is what it should look like and my code: Also this is also what we are allowed to use in code, I'm a beginner so please don't give any complicated code. This is what we can use: * PERMITTED COMMANDS * Actions: move(), turnLeft(), turnRight(), putLeaf(), removeLeaf(), showWarning(), intInput() * Sensors: onLeaf(), treeFront() * JAVA: if, else, while, for, !, &&, || */ This is my code: public void run() { // TODO: Write your code below //basketHeight integer will make the user enter the height for the basket int basketWidth = readInt("Enter basket width"); //basketWidth integer will make the user enter the width for the basket int basketHeight = readInt("Enter basket Height"); int putCake = basketHeight * basketWidth; //if clara is facing east then east is true otherwise false boolean east = true; //this will increase the number of leaves entered by the user and place them putLeaves(basketWidth); putLeaves(basketHeight -1); for(int i = 0; i < basketHeight -2; i++) { move(); } turnLeft(); //this is used to make sure Clara doesn't put a extra leaf when number is entered by user putLeaves(basketHeight -1); for(int a = 0; a < putCake; a++) { putLeaf(); move(); move(); if(onLeaf() && east == true) { turnLeft(); move(); turnLeft(); move(); east = false; } if(onLeaf() && east == false) { turnRight(); move(); turnRight(); move(); east = true; } } } //This method putLeaves is used to put down leaves to create a basket public void putLeaves(int numberLeaves) { for(int i = 0; i < numberLeaves; i++) { putLeaf(); if(i == numberLeaves -1) { turnLeft(); } move(); } } } And this is the result: Keep in mind, that I have made the outer basket, and the leafs that you see inside the basket I don't know how to make it so its at the height and width of the basket it self. http://tinypic.com/view.php?pic=29djlar&s=8#.VUSxp_mqpBe
danpost danpost

2015/5/2

#
Line 18 of the 'run' method has clara moving the height distance across to the left, where it should move the width distance. Then, instead of using 'putCake' as the limit in the 'for' loop at line 25 of the 'run' method, better would be to have a 'for' loop to go across inside a 'for' loop to go up to fill the basket.
// code to set-up
for (int y=0; y<basketHeight()-1) // going up
{
    for (int x=0; x<basketWidth-3) // going across
    {
        // code to add leafs across
    }
    // code to adjust direction for next row
}
As a hint for coding, I will say that the actions on each step depend on whether clara is on a leaf or not and that only one move should be done each step.
You need to login to post a reply.