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 // Does whatever the agent does during a step
// then returns a boolean // then returns a boolean
// if false, agent dies at end of turn // 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); public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);

View File

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

View File

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