Skip to content

Commit

Permalink
review at classes
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 16, 2020
1 parent d1e3311 commit 5c13c19
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 33 deletions.
21 changes: 17 additions & 4 deletions src/simulation/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ public class Animal{
private int deathDay;
private final Genome genome;
private Direction direction;
private int energy;
private double energy;
private Location location;

Animal(int energy, int birthDay){
Animal(double energy, int birthDay, Location location){
this.birthDay = birthDay;
this.energy = energy;
this.genome = new Genome();
this.location = location;
};

Animal(Animal parent1, Animal parent2, int birthDay){
Animal(Animal parent1, Animal parent2, int birthDay, Location location){
this.genome = new Genome(parent1.getGenome(), parent2.getGenome());
this.energy = parent1.energy / 4 + parent2.energy / 4;
this.birthDay = birthDay;
this.location = location;
}

public void die(int deathDay) throws AnimalStateException {
Expand Down Expand Up @@ -52,7 +55,17 @@ public boolean isAlive() {
return energy > 0;
}

public int getEnergy() {
public double getEnergy() {
return energy;
}

public Location getLocation() { return location; }

public void setLocation(Location location) {
this.location = location;
}

public void setEnergy(double energy) {
this.energy = energy;
}
}
61 changes: 61 additions & 0 deletions src/simulation/AnimalBoard.java
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);
}
}
}
}
23 changes: 0 additions & 23 deletions src/simulation/Board.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/simulation/GenomeWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;

public class GenomeWatcher{
Board board;
AnimalBoard animalBoard;
private List<Genome> genomeFrequency = new ArrayList<Genome>();

public void update(){};
Expand Down
7 changes: 7 additions & 0 deletions src/simulation/InvalidRectangleException.java
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);
}
}
62 changes: 62 additions & 0 deletions src/simulation/Jungle.java
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);
}
}
27 changes: 27 additions & 0 deletions src/simulation/Location.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package simulation;

import java.util.Random;

public class Location implements Comparable<Location>{
private final int x;
private final int y;
Expand All @@ -22,4 +24,29 @@ public int getX() {
public int getY() {
return y;
}

public static Location getRandom(int xBound, int yBound){
Random rng = new Random(System.nanoTime());
return new Location(rng.nextInt(xBound),rng.nextInt(yBound));
}
public static Location getRandom(int xLowerBound, int yLowerBound, int xUpperBound, int yUpperBound) throws InvalidRectangleException {
if (xLowerBound >= xUpperBound || yLowerBound >= yUpperBound){
throw new InvalidRectangleException(
"Trying to generate a random location from invalid parameters: \n"
+ "xLowerBound = " + xLowerBound
+ ", yLowerBound = " + yLowerBound
+ ", xUpperBound = " + xUpperBound
+ ", yUpperBound = " + yUpperBound
);
}

Random rng = new Random(System.nanoTime());
int x = xLowerBound + rng.nextInt(xUpperBound-xLowerBound);
int y = yLowerBound + rng.nextInt(yUpperBound-yLowerBound);
return new Location(x,y);
}

public Location stepTo(Direction direction){
return new Location(x+ direction.getX(),y+direction.getY());
}
}
41 changes: 41 additions & 0 deletions src/simulation/PlantBoard.java
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);
}
}
55 changes: 50 additions & 5 deletions src/simulation/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,78 @@
package simulation;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class Simulation {

private int currentDay = 0;
public final String name;
public final Board board = new Board();
public final AnimalBoard animalBoard = new AnimalBoard();
public final PlantBoard plantBoard = new PlantBoard();
private final int width;
private final int height;
private final double jungleRatio;
private final Jungle jungle;
private final double moveEnergy;
private final double plantEnergy;
private final int initialAnimalsNum;
private AbstractMap<Genome,Integer> dominantGenomes = new TreeMap<>();

public Simulation(String name, int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum) {
this.name = name;
this.width = width;
this.height = height;
this.jungleRatio = jungleRatio;
this.jungle = new Jungle(width,height,jungleRatio);
this.moveEnergy = moveEnergy;
this.plantEnergy = plantEnergy;
this.initialAnimalsNum = initialAnimalsNum;
}

private void addPlants(){
boolean plantedInJungle = false;
boolean plantedOutsideJungle = false;
while (!plantedInJungle || !plantedOutsideJungle){
Location location = Location.getRandom(width,height);
if(jungle.contains(location) && !plantedInJungle){
plantBoard.plant(location);
plantedInJungle = true;
}
else if(!jungle.contains(location) && !plantedOutsideJungle){
plantBoard.plant(location);
plantedOutsideJungle = true;
}
}
}

private void moveAnimals() throws AnimalStateException {
for (Animal animal : animalBoard.getAll()) {
animal.setEnergy(animal.getEnergy()-moveEnergy);
if (animal.getEnergy() < 0) {
animal.die(currentDay);
} else {
animal.setLocation(correctOverlap(animal.getLocation().stepTo(animal.getDirection())));
}
animalBoard.update();
}
}

public Location correctOverlap(Location location){
return new Location(location.getX()%width, location.getY()%height);
}

public void start(){
}



public void simulateOneDay() throws SimulationErrorException{
try {
moveAnimals();
addPlants();
}catch(AnimalStateException e){
throw new SimulationErrorException(e.toString());
}
}

public int getCurrentDay() {
return currentDay;
}
Expand Down
7 changes: 7 additions & 0 deletions src/simulation/UnplantingUnplantedLocationException.java
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);
}
}
Loading

0 comments on commit 5c13c19

Please sign in to comment.