Results 1 to 3 of 3
  1. #1
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default Need gridworld help

    This is my current assignment:

    Write your own class called “MorphCritter” which changes the colors of any object around it to the color of the critter.
    If two “MorphCritter” are next to each other it will do nothing.

    Here is the code I have come up with so far.
    Java Code:
    package Project05;
    
    import info.gridworld.actor.Actor;
    import info.gridworld.actor.Critter;
    import info.gridworld.grid.Location;
    
    import java.util.ArrayList;
    
    public class MorphCritter extends Critter
    {
    
        @Override
        public void processActors(ArrayList<Actor> actors) {
            int n = actors.size();
            if (n == 0) {
                return;
            }
            int r = (int) (Math.random() * n);
    
            Actor other = actors.get(r);
            setColor(other.getColor());
        }
    
        @Override
        public void makeMove(Location loc) {
            setDirection(getLocation().getDirectionToward(loc));
            super.makeMove(loc);
        }
    }
    This is the code to open the grid and add the actors:
    Java Code:
    package Project05;
    
    import info.gridworld.actor.ActorWorld;
    import info.gridworld.actor.Rock;
    import info.gridworld.grid.Location;
    
    import java.awt.Color;
    
    public class MorphCritterRunner
    {
    
        public static void main(String[] args) {
            ActorWorld world = new ActorWorld();
    
            world.add(new Location(7, 8), new Rock());
            world.add(new Location(3, 3), new Rock());
            world.add(new Location(2, 8), new Rock(Color.BLUE));
            world.add(new Location(5, 5), new Rock(Color.PINK));
            world.add(new Location(1, 5), new Rock(Color.RED));
            world.add(new Location(7, 2), new Rock(Color.YELLOW));
            world.add(new Location(8, 2), new MorphCritter());
            world.add(new Location(8, 2), new MorphCritter());
    
            world.show();
        }
    }
    Now my problem with this code is the critter changes color of the object that it comes up to, instead of the object (like a rock) changing to the color of the critter :confused::confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This is in your morph critter's code:
    Java Code:
    setColor(other.getColor());
    So your code is just doing what you tell it to do. My guess is that you really want to loop through the things around this critter (the actors list perhaps?) with a for loop and change the color of the actors not the opposite which is what you're doing.
    Last edited by Fubarable; 05-03-2010 at 04:06 AM.

  3. #3
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    Not sure I really understand that, just started coding java 2 weeks ago :(

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •