Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Flocking Birds Program

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Flocking Birds Program

    Hey, I am currently enrolled in a Computer Science course in my school. My teacher recently gave our class an assignment from the Advanced Placement Comp. Science curriculum and I am really struggling on it. I am still a beginner and I need help! The overall project is composed of 4 classes and uses Gridworld.jar as an archive.

    Here it is:

    (CLASS 1)

    /*
     * Bird.java
     */
     
    import info.gridworld.actor.Actor;
    import info.gridworld.grid.Grid;
    import info.gridworld.grid.Location;
    import java.util.*;
     
    public class Bird extends Actor implements Flockable {
     
     
        private static final double TURNPCT = 0.75;
        private Actor myLeader = null;
     
        /**
         *  Instructs this Bird on how to behave when it is told to move
         */
        public void move() {
            Grid<Actor> gr = getGrid();
            if (gr == null)
                return;
            Location loc = getLocation();
            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
     
        /**
         *  Overrides the act method in parent class Actor.  This method is
         *  called on each "step" when a World is created
         **/
        public void act() {
            if (isFollowing()) {
                turnToFace(myLeader.getLocation());
            } else if (Math.random() > TURNPCT) {
                turn();
            }
            if (canMove()) {
                move();
            }
        }
     
        /**
         *  Overrides method turn in class Actor.  Turn 45 degrees to the left.
         **/
        public void turn() {
            setDirection(getDirection() + Location.HALF_LEFT);
        }
     
        /** Given a Location, turn to face that Location
         * @param   loc The location this Predator should face
         **/
        private void turnToFace(Location loc) {
            if (loc.equals(this.getLocation())) return;
            int xDist = this.getLocation().getCol() - loc.getCol();
            int yDist = this.getLocation().getRow() - loc.getRow();
            double turn = Math.atan2(yDist, xDist);
            setDirection((int)Math.toDegrees(turn) - 90);
        }
     
        /**
         *  Return true iff this Bird can move
         * 
         * 
         * @return true if this Bird can move, false if it cannot
         */
        public boolean canMove() {
            Grid<Actor> gr = getGrid();
            if (gr == null)
                return false;
            Location loc = getLocation();
            Location next = loc.getAdjacentLocation(getDirection());
            if (!gr.isValid(next))
                return false;
            Actor neighbor = gr.get(next);
            return (neighbor == null);
     
        }
     
        /**
         *  Instruct this Flockable object to permanently  stop following its leader
         **/
        public void stopFollowing() {
            myLeader = null;
        }
     
        /**
         *  Return true if this Flockable has a leader that it is following
         *  @return true if this Flockable has a leader, false otherwise
         **/
        public boolean isFollowing() {
            return myLeader != null;
        }
     
        /**
         *  Provide this Flockable object with a leader that it can follow.
         *  Subsequent actions by this Flockable object should cause it to behave
         *  in a manner that causes it to follow flockLeader
         *  @param  flockLeader The Actor that this Flockable will follow
         **/
        public void setLeader(Actor leader) {
            myLeader = leader;
        }
    }

    (CLASS 2)


    /*
     * Flockable.java
     */
     
    import info.gridworld.actor.Actor;
     
    /**
     *
     */
    public interface Flockable {
        /**
         *  Provide this Flockable object with a leader that it can follow.
         *  Subsequent actions by this Flockable object should cause it to behave
         *  in a manner that causes it to follow flockLeader
         *  @param  flockLeader The Actor that this Flockable will follow
         **/
        public void setLeader(Actor flockLeader);
     
        /**
         *  Instruct this Flockable object to permanently  stop following its leader
         **/
        public void stopFollowing();
     
        /**
         *  Return true if this Flockable has a leader that it is following
         *  @return true if this Flockable has a leader, false otherwise
         **/
        public boolean isFollowing();    
    }

    (CLASS 3)

    /*
     * FlockRunner.java
     */
     
    import info.gridworld.actor.ActorWorld;
    import java.util.Iterator;
     
    public class FlockRunner {
        public static void main (String[] args) {
            Flock robins = new Flock();
            Flock jays = new Flock();
     
            ActorWorld world = new ActorWorld();
    //        ActorWorld world = new ActorWorld(new BoundedGrid<Actor>(30,30));
    //        ActorWorld world = new ActorWorld(new UnboundedGrid<Actor>());
     
            for (int r = 0 ; r < 5 ; r++) {
                Bird robin = new Bird();
                robin.setColor(java.awt.Color.ORANGE);
                robins.add(robin);
            }
            Bird robinLeader = new Bird();
            robinLeader.setColor(java.awt.Color.BLACK);
            robins.addLeader(robinLeader);
     
            for (int j = 0 ; j < 6 ; j++) {
                jays.add(new Bird());
            }
            Bird jayLeader = new Bird();
            jayLeader.setColor(java.awt.Color.GRAY);
            jays.addLeader(jayLeader);
     
            Iterator flockIter = robins.iterator();
            while (flockIter.hasNext()) {
                Bird b = (Bird)flockIter.next();
                world.add(world.getRandomEmptyLocation(), b);
            }
            world.add(world.getRandomEmptyLocation(), robinLeader);
     
            flockIter = jays.iterator();
            while (flockIter.hasNext()) {
                Bird b = (Bird)flockIter.next();
                world.add(world.getRandomEmptyLocation(), b);
            }
            world.add(world.getRandomEmptyLocation(), jayLeader);
     
            world.show();
        }
     
    }


    (CLASS 4)

    /*
     * Flock.java
     *
     */
     
    import info.gridworld.actor.Actor;
    import java.util.*;
     
    public class Flock {
        List <Flockable> theFlock = new ArrayList<Flockable>();
        Actor theLeader;
     
        /**
         *  Add f to the group of Flockables objects in this Flock
         *  @param  f   A Flockable object to be added to this Flock
         **/
        public void add(Flockable f) {
            theFlock.add(f);
        }
     
        /**
         *  Return an Iterator over the group of Flockable objects in this Flock
         **/
        public Iterator <Flockable> iterator() {
            return theFlock.iterator();
        }   
     
        /**
         *  Define the leader of this Flock.  Without a leader, each Flockable
         *  in this Flock will act of its own accord.  Given a leader, each 
         *  Flockable object should follow that leader
         *  @param  leader  The Actor to follow
         **/
        public void addLeader(Actor leader) {
            theLeader = leader;
            Iterator <Flockable> flockIter = iterator();
            while (flockIter.hasNext()) {
                Flockable nextFlockObject = flockIter.next();
                nextFlockObject.setLeader(leader);
            }
        }
     
        /**
         *  Remove the leader from this Flock.  All Flockable objects in this Flock
         *  should now act of their own accord
         **/
        public void removeLeader() {
            theLeader = null;
            Iterator <Flockable> flockIter = iterator();
            while (flockIter.hasNext()) {
                flockIter.next().stopFollowing();
            }
        }
    }


    Here's the question I'm stuck on:

    Observe that after running the FlockRunner sample for some time, the leader Bird inevitably becomes stuck in some position. This generally happens when the leader goes into a corner, and its flock traps it there. Come up with a solution to ensure that the leader will not become stuck in this manner. You may use one of these suggested methods, or come up with your own:

    1. After a certain # of steps, the Flock picks a new leader.

    2. After a certain # of steps, the whole Flock becomes "hungry" and stops following it's leader.

    3. After a certain # of steps without moving, the Flock picks a new leader within its Flock.

    4. The leader learns how to lead and avoids getting trapped in corners.

    I know it's a lot of code to look at, but I desperately need help for I have been trying to figure this out all week.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Flocking Birds Program

    So what is your question?

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Flocking Birds Program

    Quote Originally Posted by jps View Post
    So what is your question?
    I want to be able to have the Leader of both Flocks to be removed from the grid, then put back into the grid in a different location so it doesn't get stuck in any corners.

    Like after 5 steps (the leader takes), the leader is removed and put back because it tends to go to the corner and get trapped by it's "flockmates"

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Flocking Birds Program

    Where are you stuck?
    What is the nature of your problem?
    What were you working on that led you to the forum for help?

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Flocking Birds Program

    Quote Originally Posted by jps View Post
    Where are you stuck?
    What is the nature of your problem?
    What were you working on that led you to the forum for help?
    It's just small modifications I want to add to the Bird class.

    /**
         *  Instruct this Flockable object to permanently  stop following its leader
         **/
        public void stopFollowing() {
            myLeader = null;
        }

    I wanted to work on this little segment from the Bird class, but I'm just stuck on what to add.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Flocking Birds Program

    First you have to decide how to solve the problem.
    You said you want to remove and replace the leader of the flock to prevent being locked in a corner.
    Where are your list of steps to follow?
    Which step are you stuck on?

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Flocking Birds Program

    I don't have any steps to follow (to remove and replace the leader, it's just an idea of mine), this project was given so that I make a completely original solution (so no guidelines, etc).

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Flocking Birds Program

    Well moving the leader is your own idea.
    You have to figure out how to do it.
    Forget programming and write down steps to take if you were to walk across a field and do it yourself.

    1 Decide when to move the leader
    2 Locate the leader
    3 Pick up the leader
    4
    5
    6.... you get the idea

    Once you have your steps down, then you can start turning them to code.

Similar Threads

  1. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 06:16 AM
  2. Program goes into infinite compilation. University project - Library Program.
    By clarky2006 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: November 10th, 2012, 02:56 PM
  3. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  4. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM
  5. Program to launch and mirror another program
    By hayate in forum Java Theory & Questions
    Replies: 13
    Last Post: March 8th, 2012, 11:47 PM