-
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
d1e3311
commit 5c13c19
Showing
11 changed files
with
286 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package simulation; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class AnimalBoard { | ||
private AbstractMap<Location, List<Animal>> locationAnimalMap = new TreeMap<>(); | ||
|
||
public List<Animal> getAll(){ | ||
List<Animal> allAnimals = new ArrayList<Animal>(); | ||
for (List<Animal> locationAnimals : locationAnimalMap.values()){ | ||
allAnimals = Stream.concat(allAnimals.stream(),locationAnimals.stream()).collect(Collectors.toList()); | ||
} | ||
return allAnimals; | ||
} | ||
public void insert(Animal animal){ | ||
List<Animal> locationAnimals = locationAnimalMap.get(animal.getLocation()); | ||
if(locationAnimals != null){ | ||
locationAnimals.add(animal); | ||
} | ||
else { | ||
locationAnimalMap.put(animal.getLocation(), new ArrayList<Animal>()); | ||
locationAnimalMap.get(animal.getLocation()).add(animal); | ||
} | ||
} | ||
public void remove(Animal animal) { | ||
Location locationToRemove = null; | ||
for (Map.Entry<Location, List<Animal>> entry : locationAnimalMap.entrySet()) { | ||
Location location = entry.getKey(); | ||
List<Animal> animals = entry.getValue(); | ||
if (animals.contains(animal)) { | ||
animals.remove(animal); | ||
if (animals.isEmpty()) | ||
locationToRemove = location; | ||
} | ||
} | ||
if (locationToRemove != null) { | ||
locationAnimalMap.remove(locationToRemove); | ||
} | ||
} | ||
public List<Animal> get(Location location){ | ||
List<Animal> animals = locationAnimalMap.get(location); | ||
if (animals == null) return new ArrayList<Animal>(); | ||
else return animals; | ||
} | ||
|
||
public List<Animal> get(int x, int y){ | ||
return get(new Location(x,y)); | ||
|
||
} | ||
public void update(){ | ||
List<Animal> animals = this.getAll(); | ||
locationAnimalMap = new TreeMap<>(); | ||
for (Animal animal : animals){ | ||
if (animal.isAlive()){ | ||
insert(animal); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
package simulation; | ||
|
||
public class InvalidRectangleException extends SimulationErrorException { | ||
public InvalidRectangleException(String errorMessage) { | ||
super(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package simulation; | ||
|
||
public class Jungle { | ||
private final int x1; | ||
private final int x2; | ||
private final int y1; | ||
private final int y2; | ||
Jungle(int boardWidth, int boardHeight, double jungleratio){ | ||
this.x1 = (int) Math.round((1.0 - jungleratio)*0.5*boardWidth); | ||
this.x2 = (int) Math.round((1.0 + jungleratio)*0.5*boardWidth); | ||
this.y1 = (int) Math.round((1.0 - jungleratio)*0.5*boardHeight); | ||
this.y2 = (int) Math.round((1.0 + jungleratio)*0.5*boardWidth); | ||
} | ||
|
||
Jungle(int x1, int y1, int x2, int y2) throws InvalidRectangleException{ | ||
if (x2 < x1 || y2 < y1) throw new InvalidRectangleException("Trying to create an invalid rectangle in jungle constructor: \n" | ||
+ "x1 = " + x1 | ||
+ ", y1 = " + y1 | ||
+ ", x2 = " + x2 | ||
+ ", y2 = " + y2 | ||
); | ||
this.x1 = x1; | ||
this.y1 = y1; | ||
this.x2 = x2; | ||
this.y2 = y2; | ||
} | ||
public boolean contains(int x, int y){ | ||
return x1 <= x && x <= x2 && y1 <= y && y <= y2; | ||
} | ||
|
||
public boolean contains(Location location){ | ||
return contains(location.getX(), location.getY()); | ||
} | ||
|
||
public Location getRandomLocation() throws InvalidRectangleException{ | ||
return Location.getRandom(x1,y1,x2+1,y2+1); | ||
} | ||
|
||
public int getX1() { | ||
return x1; | ||
} | ||
|
||
public int getX2() { | ||
return x2; | ||
} | ||
|
||
public int getY1() { | ||
return y1; | ||
} | ||
|
||
public int getY2() { | ||
return y2; | ||
} | ||
|
||
public Location getTopLeftCorner(){ | ||
return new Location(x1,y1); | ||
} | ||
|
||
public Location getBottomRightCorner(){ | ||
return new Location(x2,y2); | ||
} | ||
} |
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,41 @@ | ||
package simulation; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.TreeMap; | ||
|
||
public class PlantBoard { | ||
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); | ||
} | ||
else{ | ||
plantedSpots.put(location,1); | ||
} | ||
} | ||
public void unplant(int x, int y) throws UnplantingUnplantedLocationException{ | ||
if (! isPlanted(x,y)){ | ||
throw new UnplantingUnplantedLocationException("Trying to unplant an unplanted location" | ||
+ "x: " + x + "y: "+ y + "\n"); | ||
} | ||
else { | ||
Location location = new Location(x,y); | ||
int plantsNum = plantedSpots.get(location); | ||
plantedSpots.remove(location); | ||
if (plantsNum != 1){ | ||
plantedSpots.put(location,plantsNum+-1); | ||
} | ||
} | ||
} | ||
public boolean isPlanted(int x, int y){ | ||
return isPlanted(new Location(x,y)); | ||
} | ||
public boolean isPlanted(Location location){ | ||
return plantedSpots.containsKey(location); | ||
} | ||
} |
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,7 @@ | ||
package simulation; | ||
|
||
public class UnplantingUnplantedLocationException extends SimulationErrorException { | ||
public UnplantingUnplantedLocationException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
Oops, something went wrong.