-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c13c19
commit 9b80542
Showing
12 changed files
with
179 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package simulation; | ||
|
||
import java.util.Comparator; | ||
|
||
public class AnimalEnergyComparator implements Comparator<Animal> { | ||
|
||
@Override | ||
public int compare(Animal a1, Animal a2) { | ||
return Double.compare(a1.getEnergy(), a2.getEnergy()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,54 @@ | ||
package simulation; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.List; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
|
||
public class PlantBoard { | ||
AbstractMap<Location,Integer> plantedSpots = new TreeMap<Location,Integer>(); | ||
private AbstractMap<Location,Integer> plantedSpots = new TreeMap<Location,Integer>(); | ||
public void plant(int x, int y){ | ||
plant(new Location(x,y)); | ||
} | ||
public void plant(Location location){ | ||
if (isPlanted(location)){ | ||
int plantsNum = plantedSpots.get(location); | ||
plantedSpots.remove(location); | ||
plantedSpots.put(location,plantsNum+1); | ||
int plantsNum = getPlantedSpots().get(location); | ||
getPlantedSpots().remove(location); | ||
getPlantedSpots().put(location,plantsNum+1); | ||
} | ||
else{ | ||
plantedSpots.put(location,1); | ||
getPlantedSpots().put(location,1); | ||
} | ||
} | ||
public void unplant(int x, int y) throws UnplantingUnplantedLocationException{ | ||
if (! isPlanted(x,y)){ | ||
public void unplant(Location location) throws UnplantingUnplantedLocationException{ | ||
if (! isPlanted(location)){ | ||
throw new UnplantingUnplantedLocationException("Trying to unplant an unplanted location" | ||
+ "x: " + x + "y: "+ y + "\n"); | ||
+ "x: " + location.getX() + "y: "+ location.getY() + "\n"); | ||
} | ||
else { | ||
Location location = new Location(x,y); | ||
int plantsNum = plantedSpots.get(location); | ||
plantedSpots.remove(location); | ||
int plantsNum = getPlantedSpots().get(location); | ||
getPlantedSpots().remove(location); | ||
if (plantsNum != 1){ | ||
plantedSpots.put(location,plantsNum+-1); | ||
getPlantedSpots().put(location,plantsNum+-1); | ||
} | ||
} | ||
} | ||
|
||
public void unplant(int x, int y) throws UnplantingUnplantedLocationException{ | ||
unplant(new Location(x,y)); | ||
} | ||
public boolean isPlanted(int x, int y){ | ||
return isPlanted(new Location(x,y)); | ||
} | ||
public boolean isPlanted(Location location){ | ||
return plantedSpots.containsKey(location); | ||
return getPlantedSpots().containsKey(location); | ||
} | ||
|
||
public List<Location> getPlantedLocations() { | ||
return plantedSpots.keySet().stream().collect(Collectors.toList()); | ||
} | ||
|
||
public AbstractMap<Location, Integer> getPlantedSpots() { | ||
return plantedSpots; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package simulation; | ||
|
||
public class SimulationErrorException extends Exception{ | ||
public SimulationErrorException(String errorMessage) { | ||
super(errorMessage); | ||
public SimulationErrorException(String errorMessage, Simulation simulation) { | ||
super("Error in simulation " + simulation.getName() + errorMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters