This commit is contained in:
Guillaume BONABAU 2024-05-29 11:24:25 +02:00
commit 3639e340f9
5 changed files with 89 additions and 20 deletions

View File

@ -23,6 +23,17 @@ public abstract class Agent {
public int getY() { public int getY() {
return y; return y;
} }
//presence of an agent at those coordinates
public boolean agentPresence(int x, int y){
if (this.getX() == x && this.getY() == y) {
return true;
}
else {
return false;
}
}
//presenec of an agent in an area
public boolean isInArea(int x, int y, int radius) { public boolean isInArea(int x, int y, int radius) {
int diffX = this.x-x; int diffX = this.x-x;
int diffY = this.y-y; int diffY = this.y-y;

View File

@ -3,7 +3,6 @@ package backend;
public class Cell { public class Cell {
private int value; private int value;
public Cell(int value) { public Cell(int value) {
this.value = value; this.value = value;
} }
@ -12,12 +11,7 @@ public class Cell {
return value; return value;
} }
public void setValue(int value) { public void setValue(int value) {
this.value = value; this.value = value;
} }
}
}

View File

@ -17,13 +17,15 @@ public class Sheep extends Agent {
//first we call the constructor of the superClass(Animal) //first we call the constructor of the superClass(Animal)
//with the values we want. //with the values we want.
// here we decide that a Sheep is initially white using this constructor // here we decide that a Sheep is initially white using this constructor
super(x,y,Color.white); super(x,y,Color.pink);
// we give our sheep a hunger value of zero at birth // we give our sheep a hunger value of zero at birth
hunger = 0; hunger = 0;
//we initialize the random number generator we will use to move randomly //we initialize the random number generator we will use to move randomly
rand = new Random(); rand = new Random();
} }
/** /**
* action of the animal * action of the animal
* it can interact with the cells or with other animals * it can interact with the cells or with other animals

View File

@ -1,5 +1,6 @@
package backend; package backend;
import java.awt.Color;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
@ -19,8 +20,8 @@ public class Simulator extends Thread {
private MyInterface mjf; private MyInterface mjf;
private final int COL_NUM = 100; private final int COL_NUM = 10;
private final int LINE_NUM = 100; private final int LINE_NUM = 10;
private final int LIFE_TYPE_NUM = 4; private final int LIFE_TYPE_NUM = 4;
//Conway Radius : 1 //Conway Radius : 1
private final int LIFE_AREA_RADIUS = 1; private final int LIFE_AREA_RADIUS = 1;
@ -28,15 +29,13 @@ public class Simulator extends Thread {
private final int ANIMAL_AREA_RADIUS = 2; private final int ANIMAL_AREA_RADIUS = 2;
private ArrayList<Integer> fieldSurviveValues; private ArrayList<Integer> fieldSurviveValues;
private ArrayList<Integer> fieldBirthValues; private ArrayList<Integer> fieldBirthValues;
private ArrayList<Agent> agents; private ArrayList<Agent> agents;
private boolean stopFlag; private boolean stopFlag;
private boolean pauseFlag; private boolean pauseFlag;
private boolean loopingBorder; private boolean loopingBorder;
private boolean clickActionFlag; private boolean clickActionFlag;
private int loopDelay = 150; private int loopDelay = 150;
//TODO : add missing attribute(s) //TODO : add missing attribute(s)
private double randomDansitySlider = 0.5; private double randomDansitySlider = 0.5;
private int width; private int width;
@ -85,9 +84,6 @@ public class Simulator extends Thread {
//TODO-COMPLETE : replace with proper return //TODO-COMPLETE : replace with proper return
return this.height; return this.height;
} }
public ArrayList<ArrayList<Integer>> getColorArrayList() {
return colorArrayList;
}
//Should probably stay as is //Should probably stay as is
public void run() { public void run() {
@ -206,11 +202,76 @@ public class Simulator extends Thread {
} }
this.setCell(x, y, newCellValue); this.setCell(x, y, newCellValue);
} else {
return;
}
else {
int i=0;
Agent agent = new Sheep(x,y);
//if there are no agents, skip directly to adding one
boolean removal =false;
if (agents.size()>0 ){
//if an agent is in this area we iterate in the arraylist agents in order to find which one it is
for(i=0;i<=agents.size()-1;i++){
//if we proceed to find the agent on the coordinates of the click, we remove it
if (agents.get(i).getX() == x && agents.get(i).getY() == y ){
agents.remove(i);
System.out.println("Corresponding agent found, proceeding with removal");
System.out.println(agents.size());
removal = true;
}
}
if(i==agents.size() && removal ==false){
//if we find no corresponding agent after the for loop, we add one
System.out.println("no agents to remove, proceeding with creation");
setSheep(x, y);
}
}
else{
System.out.println("1st iteration");
setSheep(x,y);
}
if (enableLogs) {
System.out.println("clickAgent Called, Agent created at: " + x + "," + y + "");
}
} }
} }
//TODO-INPROGRESS : set agent (x y agent) load an agent to coordinates x,y
public void setSheep(int x,int y){
Sheep sheep = new Sheep(x,y);
agents.add(sheep);
}
public void printAgents() {
for (Agent Agent : agents) {
String agentType = Agent.getClass().getSimpleName();
int x = Agent.getX();
int y = Agent.getY();
Color color = Agent.getDisplayColor();
System.out.println("Agent Type: " + agentType);
System.out.println("Position: (" + x + ", " + y + ")");
System.out.println("Color: " + color);
System.out.println();
}
}
/** /**
* get cell value in simulated world * get cell value in simulated world
* @param x coordinate of cell * @param x coordinate of cell
@ -513,7 +574,7 @@ public class Simulator extends Thread {
* @return String representation of click action * @return String representation of click action
*/ */
public String clickActionName() { public String clickActionName() {
// TODO : initially return "sheep" or "cell" // TODO-COMPLETE : initially return "sheep" or "cell"
// depending on clickActionFlag // depending on clickActionFlag
if (clickActionFlag){ if (clickActionFlag){
return "cell"; return "cell";
@ -540,4 +601,6 @@ public class Simulator extends Thread {
} }
} }
} }

View File

@ -124,7 +124,6 @@ public class Table {
} }
//TODO : set agent (x y agent) load an agent to coordinates x,y
//TODO : set random (density) create a random table of determined density //TODO : set random (density) create a random table of determined density
public void setRandom(double density) { public void setRandom(double density) {