//Tasha Wallage //Period 7 import java.util.HashMap; import java.util.ArrayList; import java.util.Iterator; public class World { private HashMap specLocs; private HashMap defVals; private HashMap aggVals; private double defAve, aggAve; private int counter, numFood, numOrg, numPred, worldSize; public World() { defAve = aggAve = 0.0; counter =0; specLocs = new HashMap(); } //returns the HashMap that contains all the Species in the World public HashMap getHMS() { return specLocs; } public int getF() { return numFood; } public int getO() { return numOrg; } public int getP() { return numPred; } //adds a Species to the World and updates population count public void add(Species s) { String t = s.getName(); //System.out.println(t); if(t.equals("Food")) numFood++; else if(t.equals("Organism")) numOrg++; else if(t.equals("Predator")) numPred++; else System.out.println("Problem adding: **"+t+"**"); specLocs.put(s.getLoc(), s); } //returns the average aggresion values of the Predators in the World public double aggAve() { if(aggVals==null) return 0.0; double sum = 0; double ave = 0; Iterator it = aggVals.keySet().iterator(); while(it.hasNext()) { int d = Integer.parseInt((it.next().toString())); int t = aggVals.get(d); sum+=t; ave+=d*t; } double temp = ave/sum; if(temp > 0 && temp!=aggAve){ System.out.println("**********AGGRESSION VALUE CHANGE*********"); System.out.println("Iteration: "+counter); System.out.println("Average AGGRESSION: "+(ave/sum)); aggAve = temp; } return temp; } //returns the average defense values of the Organisms in the World public double defAve() { if(defVals == null) return 0.0; double sum = 0; double ave = 0; Iterator it = defVals.keySet().iterator(); while(it.hasNext()) { int d = Integer.parseInt((it.next().toString())); int t = defVals.get(d); //System.out.println(d+": "+t); sum+=t; ave+=d*t; } double temp = ave/sum; if(temp>0 && temp!=defAve){ System.out.println("**********DEFENSE VALUE CHANGE*********"); System.out.println("Iteration: "+counter); System.out.println("Average Defense: "+(ave/sum)); defAve = temp; } return temp; } //if the HashMap contains a certain Location, it will return that Location public Location contains(String name, Location loc) { Iterator it = specLocs.keySet().iterator(); while(it.hasNext()) { Location l = (Location) it.next(); if(l.equals(loc) && specLocs.get(l).getName().equals(name)) return l; } return null; } //calls act on all of the Species and keeps track of the number of iterations (time) public void step() { counter++; Iterator it = specLocs.keySet().iterator(); ArrayList a = new ArrayList(); defVals = new HashMap(); aggVals = new HashMap(); while(it.hasNext()){ Location temp = (Location) it.next(); Species s = (Species)specLocs.get(temp); a.add(s); String t = s.getClass().getName(); if(t.equals("Organism")) { int def = ((Organism)s).getDefense(); if(defVals.containsKey(def)){ int w = defVals.get(def); defVals.put(def, w+1); } else defVals.put(def, 1); } else if(t.equals("Predator")) { int agg = ((Predator)s).getAggression(); if(aggVals.containsKey(agg)){ int q = aggVals.get(agg); aggVals.put(agg, q+1); } else aggVals.put(agg, 1); } } aggAve(); defAve(); for(int k=0; k al = getNeighbors(s1.getLoc()); for(int k=0; k al = getNeighbors(loc); for(int k=0; k worldSize || y > worldSize) return false; return true; } //returns all the neighbors of a given Location public ArrayList getNeighbors(Location loc) { int x = loc.getX(); int x1 = x+1; int x2 = x-11; int y = loc.getY(); int y1 = y+1; int y2 = y-1; ArrayList list = new ArrayList(); Location a = new Location(x2, y2); list.add(a); Location b = new Location(x, y2); list.add(b); Location c = new Location(x1, y2); list.add(c); Location d = new Location(x2, y); list.add(d); Location e = new Location(x1, y); list.add(e); Location f = new Location(x2, y1); list.add(f); Location g = new Location(x, y1); list.add(g); Location h = new Location(x1, y1); list.add(h); return list; } //removes the given Species and updates the population count public boolean isNum(String s) { if(s==null || s=="" || s=="NaN") return false; for(int k=0; k