Skip to content

Commit

Permalink
with console output
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 19, 2020
1 parent 9b80542 commit dac82b4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/sample/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void start(Stage primaryStage) throws Exception{


public static void main(String[] args) {
Simulation simulation = new Simulation("Sysad",100,122,0.3,10,40,22, 100);
Simulation simulation = new Simulation("Sysad",3,3,0.3,5,100,8, 100);
simulation.start();
//launch(args);
}
Expand Down
16 changes: 14 additions & 2 deletions src/simulation/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,42 @@

public class Animal{
private final int birthDay;
private int deathDay;
private int deathDay = -1;
private final Genome genome;
private Direction direction;
private double energy;
private Location location;

Animal(double energy, int birthDay, Location location){

this.birthDay = birthDay;
this.energy = energy;
this.genome = new Genome();
this.location = location;
this.direction = Direction.values()[(new Random(System.nanoTime()).nextInt(8))];
System.out.println("Spawning "+this.toString()+" at "+location.toString());
};

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;
this.direction = Direction.values()[(new Random(System.nanoTime()).nextInt(8))];
System.out.println("Born "+this.toString()+", child of "+parent1.toString()+" and "+parent2.toString()+", at "+location);
}

public void die(int deathDay) throws AnimalStateException {
System.out.println(this.toString()+" dies at "+this.location);
if (!this.isAlive())
throw new AnimalStateException("Trying to kill a dead animal: " + this.toString());
this.energy = 0;
this.deathDay = deathDay;
}
public void shift() {
direction = direction.shiftedBy(genome.pickRandomGene());
System.out.println(this.toString() + " shifts to " + this.direction);
}

public int getBirthDay() {
Expand All @@ -54,7 +61,7 @@ public Direction getDirection() {
}

public boolean isAlive() {
return energy > 0;
return this.deathDay == -1;
}

public double getEnergy() {
Expand All @@ -75,4 +82,9 @@ public void setEnergy(double energy) {
this.energy = energy;
}

@Override
public String toString() {
return genome.toString() +
"-" + birthDay;
}
}
8 changes: 8 additions & 0 deletions src/simulation/Genome.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ public boolean isValid(){
public List<Integer> getGenes(){
return List.copyOf(genes);
}

public String toString(){
StringBuilder sb = new StringBuilder();
for (int gene : genes){
sb.append(gene);
}
return sb.toString();
}
}
7 changes: 3 additions & 4 deletions src/simulation/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ public Location stepTo(Direction direction){

@Override
public String toString() {
return "Location{" +
"x=" + x +
", y=" + y +
'}';
return "("+ x + " "
+ y +
')';
}
}
5 changes: 3 additions & 2 deletions src/simulation/PlantBoard.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simulation;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -29,7 +30,7 @@ public void unplant(Location location) throws UnplantingUnplantedLocationExcepti
int plantsNum = getPlantedSpots().get(location);
getPlantedSpots().remove(location);
if (plantsNum != 1){
getPlantedSpots().put(location,plantsNum+-1);
getPlantedSpots().put(location,plantsNum-1);
}
}
}
Expand All @@ -45,7 +46,7 @@ public boolean isPlanted(Location location){
}

public List<Location> getPlantedLocations() {
return plantedSpots.keySet().stream().collect(Collectors.toList());
return new ArrayList<>(plantedSpots.keySet());
}

public AbstractMap<Location, Integer> getPlantedSpots() {
Expand Down
38 changes: 17 additions & 21 deletions src/simulation/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ private void addPlants(){
Location location = Location.getRandom(width,height);
if(jungle.contains(location) && !plantedInJungle){
plantBoard.plant(location);
System.out.println("Added in-jungle plant at "+location.toString());
plantedInJungle = true;
}
else if(!jungle.contains(location) && !plantedOutsideJungle){
plantBoard.plant(location);
System.out.println("Added outside-jungle plant at "+location.toString());
plantedOutsideJungle = true;
}
}
Expand All @@ -55,7 +57,8 @@ private void moveAnimals() throws AnimalStateException {
if (animal.getEnergy() < 0) {
animal.die(currentDay);
} else {
System.out.println("Moving animal from " + animal.getLocation() +" to "+toBoardLimits(animal.getLocation().stepTo(animal.getDirection())));
System.out.println("Moving "+animal.toString()+" from " + animal.getLocation() +" to "+toBoardLimits(animal.getLocation().stepTo(animal.getDirection())));
animal.shift();
animal.setLocation(toBoardLimits(animal.getLocation().stepTo(animal.getDirection())));
}
animalBoard.update();
Expand All @@ -69,18 +72,19 @@ public void eatPlants() throws UnplantingUnplantedLocationException{
for (Location location : plantBoard.getPlantedLocations()) {
plantBoard.unplant(location);
List<Animal> animals = animalBoard.get(location);
if (animals != null) {
if (!animals.isEmpty()) {
double highestEnergy = animals.stream()
.max(new AnimalEnergyComparator())
.get()
.getEnergy();
List<Animal> highestEnergyAnimals = animals.stream()
.filter((animal -> animal.getEnergy() == highestEnergy))
.filter((animal -> Math.abs(animal.getEnergy() - highestEnergy) < 1e-4))
.collect(Collectors.toList());
int n = highestEnergyAnimals.size();
for (Animal animal : highestEnergyAnimals) {
animal.addEnergy(plantEnergy / n);
}
System.out.println("Plant eaten at " + location.toString() + " by " + animals.get(0).toString());
}
}
}
Expand Down Expand Up @@ -133,35 +137,25 @@ public void addInitialAnimals(){
public void start(){
addInitialAnimals();
try {
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
simulateOneDay();
for (int i = 0; i < 25; i++)
simulateOneDay();
} catch (SimulationErrorException e){
System.out.println(e.toString());
}
}

public void simulateOneDay() throws SimulationErrorException{
currentDay++;
System.out.println("Starting day "+ currentDay);
System.out.println("\nSTARTING DAY"+ currentDay + "\n=======================================");
try {
System.out.println("Moving animals");

System.out.println("\nMoving animals\n=======================================");
moveAnimals();
System.out.println("Eating plants");
System.out.println("\nEating plants\n=======================================");
eatPlants();
System.out.println("Reproducing animals");
System.out.println("\nReproducing animals\n=======================================");
reproduceAnimals();
System.out.println("Adding plants");
System.out.println("\nAdding plants\n=======================================");
addPlants();
} catch(AnimalStateException e){
throw new SimulationErrorException("AnimalStateException: "+ e.toString(),this);
Expand All @@ -170,6 +164,8 @@ public void simulateOneDay() throws SimulationErrorException{
}
}



public int getCurrentDay() {
return currentDay;
}
Expand Down

0 comments on commit dac82b4

Please sign in to comment.