This commit is contained in:
Timéo 2024-05-30 12:00:52 +02:00
commit cf3646307b
3 changed files with 34 additions and 14 deletions

View File

@ -33,7 +33,7 @@ public abstract class Agent {
// Does whatever the agent does during a step
// then returns a boolean
// if false, agent dies at end of turn
// see step function in Simulator
// see step function in Simulator
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);

View File

@ -11,8 +11,8 @@ public class Simulator extends Thread {
private MyInterface mjf;
private int COL_NUM = 100;
private int LINE_NUM = 100;
private int COL_NUM = 10;
private int LINE_NUM = 10;
private final int LIFE_TYPE_NUM = 4;
//Conway Radius : 1
private final int LIFE_AREA_RADIUS = 1;
@ -81,7 +81,6 @@ public class Simulator extends Thread {
System.out.println("wrong file buddy, this one's empty");
}else if(row.size() <= 0) {
System.out.println("wrong file buddy, this one's does not have survival rules, won't work");
return;
}else {
try {
for (int x = 0; x < surviveCells.length; x++) {
@ -217,19 +216,25 @@ public class Simulator extends Thread {
// to modify agent behavior, see liveTurn method
// in agent classes
ArrayList<Agent> newAgents = new ArrayList<>();
for (int i = agents.size() - 1; i >= 0; i--) {
Agent agent = agents.get(i);
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
if (!agent.liveTurn(neighbors, this)) {
agents.remove(i);
}
}
/*ArrayList<Agent> newAgents = new ArrayList<>();
for(Agent agent : agents) {
ArrayList<Agent> neighbors =
this.getNeighboringAnimals(
agent.getX(),
agent.getY(),
ANIMAL_AREA_RADIUS);}
/*if(!agent.liveTurn(
if(!agent.liveTurn(
neighbors,
this)) {
agents.remove(agent);
{*/
}*/
// Apply Game of Life rules
@ -328,8 +333,8 @@ public class Simulator extends Thread {
} else if (clickActionFlag==2) { // wolf
ArrayList<Agent> nearby=getNeighboringAnimals(x,y,1);
if (nearby.isEmpty()) {
Sheep sheep = new Sheep(x,y);
agents.add(sheep);
Wolf wolf = new Wolf(x,y);
agents.add(wolf);
}else {
for (Agent animal:nearby) {
agents.remove(animal);
@ -373,7 +378,15 @@ public class Simulator extends Thread {
}
return inArea;
}
public String typeAnimals(int x , int y,ArrayList<Agent> neighbor) {
//ArrayList<Agent> neighbor =getNeighboringAnimals(x,y,1);
Agent agent = neighbor.get(0);
if (agent instanceof Wolf) {
return "Wolf";
} else {
return "Sheep";
}
}
/**
* set value of cell
* @param x coord of cell
@ -596,8 +609,10 @@ public class Simulator extends Thread {
public void startSimulation() {
if (isWorldEmpty()) {
if (isWorldEmpty() & getWidth()==100 & getHeight()==100) {
mjf.clicLoadFileButtonCSV("World/baseworld.csv");
}else {
mjf.generateRandomBoard();
}

View File

@ -26,8 +26,13 @@ public class Wolf extends Agent {
* as you wish
*/
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
if(world.getCell(x, y)==1) {
world.setCell(x, y, 0);
//ArrayList<Agent> nearby=world.getNeighboringAnimals(x,y,1); //look if the cell has an agent
if (!neighbors.isEmpty()) {
if(world.typeAnimals(x,y, neighbors)== "Sheep") {
Sheep sheep = new Sheep(x,y) ;
world.getAnimals().remove(sheep);
}
} else {
hunger++;
}