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.