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

2016/7/24

similar senario like ant senario

NIRO17 NIRO17

2016/7/24

#
i want to develop a senario like ant senario , my requirement is robots are moving randomly and each robot have an ability to grab different type of foods and come back to a specific location..how can i make this in greenfoot. ant algorithem is littlebit diiferent me to understand.
danpost danpost

2016/7/24

#
The Creature class has a field that references an Anthill object (or home actor). This is used to direct the ants back to drop off the food (using the location of the home actor). This home actor, in turn, contain a field to count the number of foods dropped off. All this should be quite similar to what you need in your robot scenario. I would think the only real difference might be the way the robot moves (as compared to how an ant moves). Attempt some code for what you want and if you run into problems, post the attempted code and explain the issue (what it currently does and how that differs from what you want it to do). Then we can help to fix your code. Unfortunately, the Creature class was made specifically for the Ant scenario (meaning it is not "generic" for any creature). If needed, I could supply a more versatile Creature class that can be used for any type creature (even a robot).
NIRO17 NIRO17

2016/7/26

#
can you supply that most versatile creature class? how can i get it from you?
danpost danpost

2016/7/26

#
NIRO17 wrote...
can you supply that most versatile creature class? how can i get it from you?
It is not much different. It was just a matter of renaming some variables and descriptions to generalize the home location (no references to ants or ant hills). The major difference is that the 'getHome' method does not return an object of any specific type (just an Actor type object) -- this means that if you use it in the Robot class to access a field or method in the class that creates a home object, it will need to be cast to the specific type (for example, if you have a Home class for the home object and some 'count' int field that needs incremented, you would use:
((Home)getHome()).count++;
from the Robot class). Here it is:
import greenfoot.*;

public class Creature extends Actor
{
    private static final int SPEED = 3;
    
    private int deltaX;
    private int deltaY;
    private Actor home;

    /**
     * Set a home for this creature.
     */
    public void setHome(Actor homeLoc)
    {
        home = homeLoc;
    }
    
    /**
     * Get the home of this creature.
     */
    public Actor getHome()
    {
        return home;
    }
    
    /**
     * Walk around randomly (random direction and speed).
     */
    public void randomWalk()
    {
        if (randomChance(50))
        {
            deltaX = adjustSpeed(deltaX);
            deltaY = adjustSpeed(deltaY);
        }
        walk();
    }
    
    /**
     * Try to walk home. Sometimes creatures get distracted or encounter small obstacles, so
     * they occasionally head in a different direction for a moment.
     */
    public void walkTowardsHome()
    {
        if(home == null) return;
        if (randomChance(2)) randomWalk();
        else
        {
            headRoughlyTowards(home);
            walk();
        }
    }
    
    /**
     * Try to walk away from home. (Goes occasionally off course a little.)
     */
    public void walkAwayFromHome()
    {
        if(home == null) return;
        if (randomChance(2)) randomWalk();
        else
        {
            headRoughlyTowards(home);
            deltaX = -deltaX;
            deltaY = -deltaY;
            walk();
        }
    }
    
    /**
     * Adjust the walking direction to head towards the given co-ordinates.
     */
    public void headTowards(Actor target)
    {
        deltaX = capSpeed(target.getX()-getX());
        deltaY = capSpeed(target.getY()-getY());
    }
    
    /**
     * Walk forward in the current direction with the current speed.
     * (Does not change direction or speed.)
     */
    public void walk()
    {
        setLocation(getX()+deltaX, getY()+deltaY);
        setRotation((int)(Math.atan2(deltaY, deltaX)*180/Math.PI));
    }
    
    /**
     * Adjust the walking direction to head somewhat towards the given co-ordinates. This does not
     * always head in the same direction. The heading is slightly random (but likely to be somewhat
     * towards the target) to make it look more natural.
     */
    private void headRoughlyTowards(Actor target)
    {
        int distanceX = Math.abs(getX()-target.getX());
        int distanceY = Math.abs(getY()-target.getY());
        boolean moveX = distanceX > 0 && Greenfoot.getRandomNumber(distanceX+distanceY) < distanceX;
        boolean moveY = distanceY > 0 && Greenfoot.getRandomNumber(distanceX+distanceY) < distanceY;
        deltaX = computeHomeDelta(moveX, getX(), target.getX());
        deltaY = computeHomeDelta(moveY, getY(), target.getY());
    }
    
    /**
     * Compute and return the direction (delta) that we should steer in when
     * we're on our way home.
     */
    private int computeHomeDelta(boolean move, int current, int home)
    {
        if (move) return current > home ? -SPEED : SPEED; else return 0;
    }
    
    /**
     * Adjust the speed randomly (start moving, continue or slow down).
     * The speed returned is in the range [-SPEED .. SPEED].
     */
    private int adjustSpeed(int speed)
    {
        speed = speed+Greenfoot.getRandomNumber(2*SPEED-1)-SPEED+1;
        return capSpeed(speed);
    }
    
    /**
     * Make sure the speed returned is in the range [-SPEED .. SPEED].
     */
    private int capSpeed(int speed)
    {
        if (speed < -SPEED) return -SPEED;
        if (speed > SPEED) return SPEED;
        return speed;
    }
    
    /**
     * Return 'true' in exactly 'percent' number of calls. That is: a call
     * randomChance(25) has a 25% chance to return true.
     */
    private boolean randomChance(int percent)
    {
        return Greenfoot.getRandomNumber(100) < percent;
    }
}
The algorithms are not as important for you as what the descriptions of the methods are. The methods (or the calling of them) are what you will be working with in the Robot class as a subclass of the Creature class. You can experiment to see how different codes will make the robot move.
NIRO17 NIRO17

2016/7/29

#
thank you very much..
NIRO17 NIRO17

2016/7/29

#
can you please help me to develop my scenario step by step when I tell the requirement orderly?
danpost danpost

2016/7/29

#
NIRO17 wrote...
can you please help me to develop my scenario step by step when I tell the requirement orderly?
We generally will help fix any attempted code here. Effort must usually be shown before any assistance is provided. Just telling us the requirements is not sufficient.
NIRO17 NIRO17

2016/8/3

#
here my problem is men are moving randomly..and if any man touch the barrel it takes the barrel and suddenly go to the house(like ants going to the ant hill) can you please tell me how to change my man class according to my senario.. here is my man class

public class Man extends Actor
{
    public void act(){
        moveAround();
        }
        
        public void moveAround(){
            
            move(2);
            if(Greenfoot.getRandomNumber(100)<10){
                turn(Greenfoot.getRandomNumber(180)-90);
            }
        }
        }

NIRO17 NIRO17

2016/8/3

#
here is my myworld class
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
    
    private void prepare(){
      
      House house=new House();
      addObject(house, 40, 45);
      Barrel[] barel=new Barrel[1];
      for(int i=0;i<barel.length;i++){
       barel[i]=new Barrel();
       int manX=Greenfoot.getRandomNumber(getWidth());
       int manY=Greenfoot.getRandomNumber(getHeight());
       addObject(barel[i],manX,manY);
        }
        
      Man[] man=new Man[5];
      for(int i=0;i<man.length;i++){
       man[i]=new Man();
       int manX=Greenfoot.getRandomNumber(getWidth());
       int manY=Greenfoot.getRandomNumber(getHeight());
       addObject(man[i],manX,manY);
        }
}}

danpost danpost

2016/8/3

#
NIRO17 wrote...
here my problem is men are moving randomly..and if any man touch the barrel it takes the barrel and suddenly go to the house(like ants going to the ant hill) can you please tell me how to change my man class according to my senario.. < Code Omitted >
Well, this really does not show any attempt. However, basically you are wanting to change line 4 of the Man class code given to something like:
if (hasBarrel())
{
    walkTowardsHome();
    checkIfHome();
}
 else
{
    moveAround();
    checkForBarrel();
}
Then, you would add the methods 'hasBarrel', which should return a boolean value, "checkIfHome', which determines if at home yet and does what is needed if home, and 'checkForBarrel', which checks for barrel and does what is needed when one is found.
NIRO17 NIRO17

2016/8/3

#
there is an error in my code can you correct it?

public class Man extends Actor
{
    public void act(){
        moveAround();
        if (hasBarrel())
        {
            walkTowardsHome();
            checkIfHome();
        }
        else
        {
            moveAround();
            checkForBarrel();
        }
    }

    public void hasBarrel(){
        
    }

    public void walkTowardsHome(){

    }
        
    public void checkIfHome(){

    }
    public void checkForBarrel(){
        
    }

    public void moveAround(){

        move(2);
        if(Greenfoot.getRandomNumber(100)<10){
            turn(Greenfoot.getRandomNumber(180)-90);
        }
    }
}

danpost danpost

2016/8/3

#
NIRO17 wrote...
there is an error in my code can you correct it? < Code Omitted >
Line 5 is an 'if' statement using 'hasBarrel()' as a condition -- meaning that the method needs to return a boolean value. Line 17, therefore must not declare a 'void' return type, but a 'boolean' return type and the method must have a 'return true;' and a 'return false;' or a 'return < boolean expression >;' within it. You will need a field of some type to track whether the man currently has a barrel or not. This could be a boolean field whose value is changed when picking up and dropping off a barrel; or, you could have a specific image that is set to the actor when carrying a barrel that can be checked for (the image would then be what the field holds). Anyway, since you currently do not have any fields in the class, it would be impossible to create a condition for determining whether the man currently has a barrel or not. Line 4 in the Man class above should be removed. Movement is created by line 7 or 12 depending on the "has barrel" condition.
NIRO17 NIRO17

2016/8/4

#
thanks a lot
You need to login to post a reply.