260 likes | 413 Views
Marine Biology Case Study. AP Computer Science. Once Upon A Time…. There was an Environment. In this Environment lived…. A Locatable object. (Anything that knows where it is). Some Environments are Bounded Environments. And COLUMNS. 0. 1. 2. 3. 0. With ROWS. 1. 2. 3.
E N D
Marine BiologyCase Study AP Computer Science
Once Upon A Time… There was an Environment
In this Environment lived… A Locatable object (Anything that knows where it is)
Some Environments areBounded Environments And COLUMNS 0 1 2 3 0 With ROWS 1 2 3
It turns out thatFISHknow where they are. I’m Locatable
So FISH can live inBounded Environments 0 1 2 3 0 1 2 3
FISH know… Direction is a Class.It is similar to Color: Direction.EAST Direction.WEST Direction.NORTH Direction.SOUTH MY VARIABLES privateEnvironmenttheEnv;privateintmyId;privateLocationmyLoc;privateDirectionmyDir;privateColormyColor; My EnvironmentMy ID NumberMy LocationThe Direction I’m FacingMy Color Location is a Class that has already been written.You can make a new Location like so:Location loc = new Location(2, 3);
Bounded Environments Keep track of HOW MANY objects are living in the environment:private intobjectCount; 0 1 2 3 0 1 objectCount 2 0 3 Keep all their Locatable objects in a 2 Dimensional array.
Bounded Environments 0 1 2 3 0 null null null null 1 null null null null objectCount 2 null null null null 0 3 null null null null If NOBODY lives at a certain spot,NULL lives there.
Bounded Environments 0 1 2 3 0 null null null null 1 null null null null objectCount 2 null null null null 0 1 2 3 4 3 null null null null Locatable objects can be ADDED to the Environment.
Playing with an Environment Row, Column Let’s make an Environment: BoundedEnv env = new BoundedEnv(4,5);Location loc1 = new Location(3,4);Location loc2 = new Location(3,3);Location loc3 = new Location(2,4);Direction dir1 = env.getDirection(loc1, loc2);Direction dir2 = env.getDirection(loc3, loc1);Direction dir3 = dir2.reverse(); System.out.println(dir1);System.out.println(dir2);System.out.println(dir3); Parameters = Rows, Columns • Create a new class called EnvironmentTest. (give it a main) Prints:WestSouthNorth
Building A Fish A Fish memorizes its Location and its Environment. Then it HAS to tell its Environment to put it in the right place! All Fish constructors utilize an initialize function to do the real work! private voidinitialize(Environmentenv,Locationloc,Directiondir,Colorcol){theEnv=env;myId=nextAvailableID;nextAvailableID++;myLoc=loc;myDir=dir;myColor=col;theEnv.add(this);}
Building a Fish Let’s Build Some Fish Fishf1=newFish(env,loc2);Fishf2=newFish(env,newLocation(0,4));System.out.println(env.objectAt(loc2));System.out.println(env.isEmpty(f2.location()));System.out.println(env.objectAt(loc1));env.remove(f1);System.out.println(env.objectAt(f1.location())); OUTPUT: 1(3, 3)South false null null
Act Like a Fish This simulation is constantly asking the Fish Objects to act( ) Let’s see how a Fish acts… public voidact(){if(isInEnv())move();} The Fish asks his Environment if he is in there.If so, the Fish moves.
Move Like a Fish • How a Fish Moves: • Decide my next location • Check to see if it is the same as current location. • Change location • Ask my environment what direction I went • Turn to face that direction
Move Like a Fish ?????????? protected voidmove(){ LocationnextLoc=nextLocation();if(!nextLoc.equals(location())){LocationoldLoc=location();changeLocation(nextLoc); DirectionnewDir=environment().getDirection(oldLoc,nextLoc);changeDirection(newDir); } else Debug.println(" Does not move."); }
Where Am I Going? • How a Fish Decides on a Next Location: • Look up, down, left, and right for empty spaces next to me. (These are the potential candidates) • Eliminate the Location behind me as a candidate(It’s hard to swim backwards) • Randomly select one of the remaining candidates • If the list is empty, I stay put.
Where Am I Going? protectedLocationnextLocation(){ArrayListemptyNbrs=emptyNeighbors();DirectionoppositeDir=direction().reverse(); LocationlocationBehind= environment().getNeighbor(location(),oppositeDir); emptyNbrs.remove(locationBehind);if(emptyNbrs.size()==0)returnlocation(); RandomrandNumGen=RandNumGenerator.getInstance();intrandNum=randNumGen.nextInt(emptyNbrs.size());return(Location)emptyNbrs.get(randNum);} Now let’s get back on the move( )…
Move Like a Fish OK ?????????? protected voidmove(){ LocationnextLoc=nextLocation();if(!nextLoc.equals(location())){LocationoldLoc=location();changeLocation(nextLoc); DirectionnewDir=environment().getDirection(oldLoc,nextLoc);changeDirection(newDir); } else Debug.println(" Does not move."); }
Change Location Change my Location variable. Tell my Environment to: Place me at my Location in the grid Set my old Location in the grid to NULL protected voidchangeLocation(Location newLoc){ LocationoldLoc=location();myLoc=newLoc;environment().recordMove(this,oldLoc);}
Act More Like a Fish • Fish do more than just move. • This is how a real Fish acts: • Either Breed(1 in 7 chance) • Birth children into ALL empty neighboring spaces. • OrMove • Business as usual • ThenmaybeDie(1 in 5 chance) • Get out of the environment.
Breeding Fish Assign values in initialize( ) function probOfBreeding = 1.0/7;probOfDying = 1.0/5; • Let’s add a breed( ) method to FISH: • protected: only the Fish should be able to cause breeding to happen. • boolean: returns true if children were created. (so the Fish knows if it should try to move) • FIRST: add two member variables: private doubleprobOfBreeding;private doubleprobOfDying;
Breeding Fish • How Fish Breed: • Decide if it is time to breed (random num) • Get all the empty neighboring Locations • Create new Fish in each of these Locations • Return true or false
Breeding Fish protected booleanbreed(){if(Math.random()<probOfBreeding){ ArrayListemptyNbrs=emptyNeighbors();if(emptyNbrs.size()==0)return false;for(inti=0;i<emptyNbrs.size();i++){ Locationloc=(Location)emptyNbrs.get(i);newFish(environment(),loc, theEnv.randomDirection(), myColor); }return true; }else return false;}
Dying Fish Make a die( ) function protected void die(){ theEnv.remove(this);} • Fish “DIE” when they are no longer in the Environment. • They will still remember what Environmentthey WERE in even though it no longer contains them.
Updating act() Original Act public voidact(){if(isInEnv())move(); } public voidact(){if(!isInEnv())return;if(!breed())move();if(Math.random()<probOfDying)die();}