diff --git a/src/backend/Agent.java b/src/backend/Agent.java index bc71b36..9a8cc1c 100644 --- a/src/backend/Agent.java +++ b/src/backend/Agent.java @@ -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 neighbors, Simulator world); diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index e77f48c..4adbe69 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -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 newAgents = new ArrayList<>(); + for (int i = agents.size() - 1; i >= 0; i--) { + Agent agent = agents.get(i); + ArrayList neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS); + if (!agent.liveTurn(neighbors, this)) { + agents.remove(i); + } + } + /*ArrayList newAgents = new ArrayList<>(); for(Agent agent : agents) { ArrayList 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 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 neighbor) { + //ArrayList 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(); } diff --git a/src/backend/Wolf.java b/src/backend/Wolf.java index 584f4b0..b8013fa 100644 --- a/src/backend/Wolf.java +++ b/src/backend/Wolf.java @@ -26,8 +26,13 @@ public class Wolf extends Agent { * as you wish */ public boolean liveTurn(ArrayList neighbors, Simulator world) { - if(world.getCell(x, y)==1) { - world.setCell(x, y, 0); + //ArrayList 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++; }