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

2015/4/27

Greedy Behavior

1
2
wallace989 wallace989

2015/4/29

#
while(!getNeighbours(2, 2, Leaf.class).isEmpty())
Gives me incompatible types: int cannot be converted to boolean
danpost danpost

2015/4/29

#
Look at the Actor class documentation on how to use the 'getNeighbours' method -- it is not (int, int, class).
Super_Hippo Super_Hippo

2015/4/29

#
Yeah, right, I mixed that up. Just took what he got at the beginning for correct...
wallace989 wallace989

2015/4/30

#
        while(!getNeighbours((1,true), (1,false), Leaf.class).isEmpty()) {
            //turn towards nearest leaf and move in that direction
        }
This is the last way I can think of to use this method after reading the documentation a few times, "Thus, a distance/diagonal specification of (1,false) will inspect four cells, (1,true) will inspect eight cells."
Super_Hippo Super_Hippo

2015/4/30

#
Greenfoot API wrote...
protected java.util.List getNeighbours(int distance, boolean diagonal, java.lang.Class cls)
So as parameters, you need "int, boolean, class". You use "int, boolean, int, boolean, class" (and use these brackets around them). Every cell is checked which can be reached in the number of steps which you pass as the 'distance' int. With the boolean, you allow diagonal steps (or not). The class is the class which is checked in these cells. At the end, it returns a list which all found objects of the class in it (or empty list, if not). Hopefully you understand how to use the method now. Here is an example:
while(!getNeighbours(2, false, Leaf.class).isEmpty())
wallace989 wallace989

2015/4/30

#
horray it now compiles! However, how do I get it to face in the direction of the found leaf?
danpost danpost

2015/5/1

#
You probably do not want it to face the leaf directly, but face in the direction of movement toward the leaf. I am trying to say that I do not think you want it to face one of the ordinal directions (left, right, up or down -- in the direction it moves to get closer to the closest leaf), not at changing angles as it approaches the closest leaf (or maybe you do!?). At any rate, once the list is not empty (which means your while loop will execute while the list IS empty and it should also increase the range on each iteration of the loop), you can extract a leaf from a recreated list and get its location to determine which way to move.
wallace989 wallace989

2015/5/2

#
Not sure what to do this is what I have come up with so far, probably
    /**
     * The Wombat behavior has to be implemented in this method.
     * The Wombat should always take one step in the direction of the closest Leaf.
     */
    public void wombatBehavior() {

        if(foundLeaf()) {
            eatLeaf();
        }
        else if(!foundLeaf()) {
            
            move(1);
        }
        while(!getNeighbours(2, true, Leaf.class).isEmpty()) {
            //turn towards nearest leaf and move in that direction
            getOneIntersectingObject(Leaf.class);
            if ((getNeighbours(2, true, Leaf.class).isEmpty()) == true);
            move(1);
            
        }
    }
completely wrong. I have created an Infinite loop and the wombat dose not move.
danpost danpost

2015/5/2

#
How are you increasing the range if no leaf has been found within the range checked? If you keep checking within the same range, and none are in that range, you will keep checking forever.
You need to login to post a reply.
1
2