Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 15, 2020
0 parents commit 2712c08
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
out/*
.idea/*
*.iml
4 changes: 4 additions & 0 deletions src/sample/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package sample;

public class Controller {
}
23 changes: 23 additions & 0 deletions src/sample/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}


public static void main(String[] args) {
launch(args);
}
}
8 changes: 8 additions & 0 deletions src/sample/sample.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
</GridPane>
85 changes: 85 additions & 0 deletions src/simulation/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package simulation;

import java.util.ArrayList;
import java.util.List;

public class Animal{
private final int birthDay;
private int deathDay;
private final Genome genome;
private Simulation simulation;

private Location location;
private Direction direction;
private int energy;


private List<AnimalStateAfterDay> stateAfterEachDay = new ArrayList<>();

Animal(Animal parent1, Animal parent2, int energy){
this.simulation = parent1.simulation;
this.genome = new Genome(parent1.getGenome(), parent2.getGenome());
this.energy = energy;
this.birthDay = simulation.getCurrentDay();
}

public void kill() throws AnimalStateException {
if (!this.isAlive())
throw new AnimalStateException("Trying to kill a dead animal: " + this.toString());
this.energy = 0;
this.deathDay = simulation.getCurrentDay();
}
public Direction shift() {
return direction = direction.shiftedBy(genome.pickRandomGene());
}

public Location move() throws AnimalStateException {
if (!this.isAlive())
throw new AnimalStateException("Trying to move a dead animal: " + this.toString());
if (this.energy < simulation.getParams().getMoveEnergy())
this.kill();
this.energy -= simulation.getParams().getMoveEnergy();
int boardWidth = simulation.getParams().getWidth();
int boardHeight = simulation.getParams().getHeight();
int newX = (location.getX() + direction.getX() + boardWidth) % boardWidth;
int newY = (location.getY() + direction.getY() + boardHeight) % boardHeight;
return location = new Location(newX,newY);
}

private void saveStateAfterDay(){

}


public int getBirthDay() {
return birthDay;
}

public int getDeathDay() {
return deathDay;
}

public Genome getGenome() {
return genome;
}

public Location getLocation() {
return location;
}

public Direction getDirection() {
return direction;
}

public boolean isAlive() {
return energy > 0;
}

public int getEnergy() {
return energy;
}

public List<AnimalStateAfterDay> getStateAfterEachDay() {
return stateAfterEachDay;
}
}
17 changes: 17 additions & 0 deletions src/simulation/AnimalStateAfterDay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package simulation;

import java.util.ArrayList;

public class AnimalStateAfterDay {
public final Location location;
public final Direction direction;
public final int energy;
public final ArrayList<Animal> childrenBornOnThisDay;

public AnimalStateAfterDay(Location location, Direction direction, int energy, ArrayList<Animal> childrenBornOnThisDay) {
this.location = location;
this.direction = direction;
this.energy = energy;
this.childrenBornOnThisDay = childrenBornOnThisDay;
}
}
7 changes: 7 additions & 0 deletions src/simulation/AnimalStateException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package simulation;

public class AnimalStateException extends SimulationErrorException {
public AnimalStateException(String errorMessage) {
super(errorMessage);
}
}
9 changes: 9 additions & 0 deletions src/simulation/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package simulation;

import java.util.*;

public class Board {
private AbstractSet<Location> plantedSpots = new TreeSet<>();
private AbstractMap<Location, List<Animal>> animalLocations = new TreeMap<>();
private AbstractMap<Genome,Integer> dominantGenomes = new TreeMap<>();
}
43 changes: 43 additions & 0 deletions src/simulation/BoardParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package simulation;

public class BoardParams {
private final int width;
private final int height;
private final double jungleRatio;
private final double moveEnergy;
private final double plantEnergy;
private final int initialAnimalsNum;

public BoardParams(int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum) {
this.width = width;
this.height = height;
this.jungleRatio = jungleRatio;
this.moveEnergy = moveEnergy;
this.plantEnergy = plantEnergy;
this.initialAnimalsNum = initialAnimalsNum;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public double getJungleRatio() {
return jungleRatio;
}

public double getMoveEnergy() {
return moveEnergy;
}

public double getPlantEnergy() {
return plantEnergy;
}

public int getInitialAnimalsNum() {
return initialAnimalsNum;
}
}
9 changes: 9 additions & 0 deletions src/simulation/Day.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package simulation;

import java.util.*;

public class Day{

Board board;

}
31 changes: 31 additions & 0 deletions src/simulation/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package simulation;

public enum Direction {
NORTH(0, 1),
NORTHEAST(1, 1),
EAST(1, 0),
SOUTHEAST(1, -1),
SOUTH(0, -1),
SOUTHWEST(-1, -1),
WEST(-1, 0),
NORTHWEST(-1, 1);
private final int x;
private final int y;

Direction(int x, int y) {
this.x = x;
this.y = y;
}

public Direction shiftedBy(int step){
return Direction.values()[(this.ordinal()+step)%8];
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
44 changes: 44 additions & 0 deletions src/simulation/Genome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package simulation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Genome {
private final List<Integer> genes;
private int length;

Genome(int length){
Random rng = new Random();
List<Integer> genes;
do {
genes = new ArrayList<>();
for (int i = 0; i < length; i++) {
genes.add(rng.nextInt(8));
}
} while (! this.isValid());
this.genes = (List<Integer>) genes.stream().sorted();
this.length = length;
};

Genome(Genome g1, Genome g2){
Random rng = new Random();
List<Integer> genes = new ArrayList<>();
int breakpt1 = rng.nextInt(31);
int breakpt2 = breakpt1 + 1 + rng.nextInt(31-breakpt1);

this.genes = genes;
};

public int pickRandomGene(){
int i = new Random().nextInt(genes.size());
return genes.get(i);
}

public boolean isValid(){
// checks if the genome contains all 8 genes
int bitMask = 0;
for (int gene : genes) bitMask |= (1 << gene);
return bitMask == (2^8 - 1);
}
}
25 changes: 25 additions & 0 deletions src/simulation/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package simulation;

public class Location implements Comparable<Location>{
private final int x;
private final int y;

Location(int x,int y){
this.x = x;
this.y = y;
}

@Override
public int compareTo(Location o) {
if (this.getX() == o.getX()) return this.getY() - o.getY();
else return this.getX() - o.getX();
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
32 changes: 32 additions & 0 deletions src/simulation/Simulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package simulation;

import java.util.ArrayList;
import java.util.List;

public class Simulation {

private List<Day> day = new ArrayList<Day>();
private final BoardParams params;
private int currentDay = 0;
private final String name;


public Simulation(String name, BoardParams params) {
this.name = name;
this.params = params;
}



public int getCurrentDay() {
return currentDay;
}

public String getName() {
return name;
}

public BoardParams getParams() {
return this.params;
}
}
7 changes: 7 additions & 0 deletions src/simulation/SimulationErrorException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package simulation;

public class SimulationErrorException extends Exception{
public SimulationErrorException(String errorMessage) {
super(errorMessage);
}
}
33 changes: 33 additions & 0 deletions test/simulation/LocationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package simulation;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class LocationTest {
Location l1 = new Location(1,4);
Location l2 = new Location(1,2);
Location l3 = new Location(3,5);
Location l4 = new Location(3,0);
@Test
void compareTo() {
assertTrue(l1.compareTo(l2) > 0);
assertTrue(l1.compareTo(l3) < 0);
assertTrue(l1.compareTo(l4) < 0);
assertTrue(l2.compareTo(l1) < 0);
assertTrue(l2.compareTo(l3) < 0);
assertTrue(l2.compareTo(l4) < 0);
assertTrue(l3.compareTo(l4) > 0);
assertTrue(l2.compareTo(l1) < 0);
assertTrue(l3.compareTo(l1) > 0);
assertTrue(l4.compareTo(l1) > 0);
assertTrue(l1.compareTo(l2) > 0);
assertTrue(l3.compareTo(l2) > 0);
assertTrue(l4.compareTo(l2) > 0);
assertTrue(l4.compareTo(l3) < 0);
assertTrue(l1.compareTo(l1) == 0);
assertTrue(l2.compareTo(l2) == 0);
assertTrue(l3.compareTo(l3) == 0);
assertTrue(l4.compareTo(l4) == 0);
}
}

0 comments on commit 2712c08

Please sign in to comment.