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

2018/4/5

making a method in my crab class, that permits setting of the number of worms in my world.

Miguel.Valdez Miguel.Valdez

2018/4/5

#
i am trying to make it so that my crab generates the worms at random locations.... this is my code so far. in crab class
    /**
     * special constructor that also sets num of worms
     */
    public Crab(int numWorms) //self note, this is a constructor.
    {
        setNumberOfWorms(numWorms);
        setImages();
    }
    
    /**
     * set the number of worms in the world
     */
    public  void setNumberOfWorms(int numWorms)
    {
        if (numWorms > 0)
        {
            wormsInWorld = numWorms;
        }
    }
in my world class
static int numWorms = Greenfoot.getRandomNumber(maxWorms-minWorms+1)+minWorms;
        // Add my crab
        populateCrab(crabXPosition, crabYPosition);
    /**
     * Place a crab in the world at xPosition and yPosition
     */
    private void populateCrab(int xPosition, int yPosition)
    {
       Crab myCrab = new Crab(numWorms);
       addObject(myCrab, xPosition, yPosition);       
    }
danpost danpost

2018/4/5

#
Miguel.Valdez wrote...
i am trying to make it so that my crab generates the worms at random locations.... this is my code so far. << Code Omitted >>
I do not understand why you would try to control the number of worms from the Crab class. It is a game state (albeit, an initial one) and by all rights belongs in your world class.
Miguel.Valdez Miguel.Valdez

2018/4/5

#
Danpost THANK YOU!!! that is the same thing i am saying.... because that makes no sense what is being asked to do. word for word "in your Crab class include new methods to do the following: "A method that permits the setting of the number worms in the world" and as well "A constructor that permits setting the number of worms upon creation and use the constructor in the World Class." right now i have this(code) to generate my worms, and it works fine. So why trying fixing something that isnt broken? is what i say. or am i thinking what is being asked to do incorrectly..... if you want i can post all my code of both my CrabWorld and Crab.
    /**
     * Place specified number of worms randomly in the world
     */
    private void populateWorms(int numberOfWorms)
    {
        int loopNumber = 0;
        
        while (loopNumber < numberOfWorms) 
        {
            // add a worm to the world, at a random location
            addObject(new Worm(), Greenfoot.getRandomNumber(xDimension), Greenfoot.getRandomNumber(yDimension)); 
            
            loopNumber++;
        }
    }
danpost danpost

2018/4/5

#
Okay. Well, an assignment is an assignment. This problem does have some teaching benefits. Go with it. Basically, you move the numberOfWorms field into the Crab class and add a method that assigns a random number to the field that is called by the Crab constructor. Another method can be added to the Crab class to return the value of the field. In the world constructor, you would first create the crab, then add it to the world and call the method that returns the value of numberOfWorms, which is then used to populate the worms into the world.
Miguel.Valdez Miguel.Valdez

2018/4/5

#
what can i add in this section of code in the CRAB CLASS to generate the random positions of worms?
/**
 * set the number of worms in the world
 */
public  void setNumberOfWorms(int numWorms)
{
    if (numWorms > 0)
    {
        wormsInWorld = numWorms;
    }
}
danpost danpost

2018/4/5

#
Miguel.Valdez wrote...
what can i add in this section of code in the CRAB CLASS to generate the random positions of worms? << Code Omitted >>
Nothing -- that method should just assign a new random value to numberOfWorms (it will not need any arguments -- or any conditions). The populateWorms method in your world class generates the random positions of the worms.
Miguel.Valdez Miguel.Valdez

2018/4/5

#
I believe I have accomplished all my tasks, but not quite 100% sure. I have labeled them to what consists to what. 2) Modify your code to include new methods for the Crab class (5 pts): a. A method that permits the setting of the number worms in the world b. A method that returns the number of worms in the world c. A method that returns the number of worms that the Crab has eaten d. A constructor that permits setting the number of worms upon creation and use the constructor in the World Class. e. Animating the image of the Crab, with the first image being crab2.png
public class CrabWorld extends World
{
    static final int xDimension = 1200;        // X Dimension of our World
    static final int yDimension = 800;        // Y Dimension of our World  
    private int score;
    static int minWorms = 8;    //min of worms that will spawn
    static int maxWorms = 12;   //max of worms that will spawn
    static int numWorms = Greenfoot.getRandomNumber(maxWorms-minWorms+1)+minWorms; //generates a random number of worms to spawn from the min & max worm values.
    
    /**
     * Create the crab world. Our world has a size 
     * of xDimension x yDimension cells, where every cell is just 1 pixel.
     */
    public CrabWorld() 
    {
        super(xDimension, yDimension, 1);
        
        // Populuate with a Crab in the the center of my world
        int crabXPosition = xDimension/2;
        int crabYPosition = yDimension/2;
        
        // Add my crab
        populateCrab(crabXPosition, crabYPosition);
        
        // Add some worms
        populateWorms(numWorms);
        
        // Add some lobsters
        populateLobsters();
        
        // Adds a score tracker
        showScore();  
    }
    
    /**
     * Place specified number of worms randomly in the world
     */
    private void populateWorms(int numberOfWorms)
    {
        int loopNumber = 0;
        
        while (loopNumber < numberOfWorms) 
        {
            // add a worm to the world, at a random location
            addObject(new Worm(), Greenfoot.getRandomNumber(xDimension), Greenfoot.getRandomNumber(yDimension)); 
            
            loopNumber++;
        }
    }
    
    /**
     * 2.D Place a crab in the world at xPosition and yPosition
     */
    private void populateCrab(int xPosition, int yPosition)
    {
       Crab myCrab = new Crab(numWorms);
       addObject(myCrab, xPosition, yPosition);
    }
public class Crab extends Actor
{
    private int forwardAmount = 8;  //number of pixels it will move
    private int wormsInWorld;
    private int wormsEaten;
    private GreenfootImage image1; //our first image
    private GreenfootImage image2; //our second image
    
    /**
     * Act - do whatever the Crab wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        controls();
        moveForward();
        eatWorm();
    }
    
    /**
     * 2.A special constructor that also sets num of worms
     */
    public Crab(int numWorms) //self note, this is a constructor.
    {
        setNumberOfWorms(numWorms);
        setImages();
    }
    
    /**
     * 2.B set the number of worms in the world
     */
    public  void setNumberOfWorms(int numWorms)
    {
        if (numWorms > 0)
        {
            wormsInWorld = numWorms;
        }
    }
    
    /**
     * 2.C Tell how many worms we have eaten.
     */
    public int getWormsEaten()
    {
        return wormsEaten;
    }    
    
    /**
     * 2.E set the images for our world.
     */
    private void setImages()
    {
        image1 = new GreenfootImage("crab.png");
        image2 = new GreenfootImage("crab2.png");
        setImage(image2);
    }
danpost danpost

2018/4/5

#
This is not an exercise out of the Greenfoot book; so, I cannot know for sure exactly what is wanted. (a) currently is a round-about way and I believe not what is wanted (could be wrong, though); (b) seems missing (or not done); (c) done; (d) part of the round-about doubtful code; (e) appears to be done (insufficient code to be certain).
Miguel.Valdez Miguel.Valdez

2018/4/5

#
how could i change this over from my world(erasing this method from my world) to my Crab class so that my crab sets the number of worms?
    private void populateWorms(int numberOfWorms)
    {
        int loopNumber = 0;
         
        while (loopNumber < numberOfWorms) 
        {
            // add a worm to the world, at a random location
            addObject(new Worm(), Greenfoot.getRandomNumber(xDimension), Greenfoot.getRandomNumber(yDimension)); 
             
            loopNumber++;
        }
    }
from there i feel like then i could make A method that returns the number of worms in the world.
danpost danpost

2018/4/5

#
Miguel.Valdez wrote...
how could i change this over from my world(erasing this method from my world) to my Crab class so that my crab sets the number of worms? << Code Omitted >> from there i feel like then i could make A method that returns the number of worms in the world.
That method can stay as is. It is what populates the world with worms. It does not generate a random number of worms to put in the world. For the crab to set the number of worms, your setNumberOfWorms method should assign a valid random value to wormsInWorld (not what you currently have it doing).
Miguel.Valdez Miguel.Valdez

2018/4/5

#
so the code in here needs to be replaced?
    /**
     * set the number of worms in the world
     */
    public  void setNumberOfWorms(int numWorms)
    {
        if (numWorms > 0)
        {
            wormsInWorld = numWorms;
        }
    }
if so, im drawing blanks with how to possibly do this. im going over my notes and in the book ,as well google, and im coming up with nothing. Maybe also from being 2AM lol
danpost danpost

2018/4/5

#
Miguel.Valdez wrote...
so the code in here needs to be replaced? << Code Omitted >> if so, im drawing blanks with how to possibly do this. im going over my notes and in the book ,as well google, and im coming up with nothing. Maybe also from being 2AM lol
You mean 12AM, don't you (in CDT)? I think they want to to move lines 6 through 8 of your CrabWorld class and not assign numberOfWorms (which is wormsInWorld in the Crab class) in the declaration line, but in the setNumberOfWorms method. At least it appears that way.
Miguel.Valdez Miguel.Valdez

2018/4/5

#
so move all this
    static int minWorms = 8;    //min of worms that will spawn
    static int maxWorms = 12;   //max of worms that will spawn
    static int numWorms = Greenfoot.getRandomNumber(maxWorms-minWorms+1)+minWorms;
to my Crab class? then how would
        // Add some worms
        populateWorms(numWorms);
        
this just is making sense in how i should write it, there NO way it can be this difficult to make a dumb crab set the number of worms. be able to get the amount of worms to populate?
danpost danpost

2018/4/5

#
Miguel.Valdez wrote...
so move all this
    static int minWorms = 8;    //min of worms that will spawn
    static int maxWorms = 12;   //max of worms that will spawn
    static int numWorms = Greenfoot.getRandomNumber(maxWorms-minWorms+1)+minWorms;
to my Crab class? then how would
        // Add some worms
        populateWorms(numWorms);
        
this just is making sense in how i should write it, there NO way it can be this difficult to make a dumb crab set the number of worms. be able to get the amount of worms to populate?
I know, it is not efficient; but, whatever. The populateWorms(numWorms) will end up being:
populateWorms(crab.getWormsInWorld());
or something like that, where crab is a reference to the Crab object that was just put in the world.
You need to login to post a reply.