Final commit before bonus implementation

This commit is contained in:
l.dupuis-burtin 2024-06-01 00:02:17 +02:00
parent 5a12b1c088
commit d43c547709
5 changed files with 105 additions and 30 deletions

View File

@ -7,7 +7,8 @@ public abstract class Agent {
protected int x;
protected int y;
protected Color color;
protected ArrayList<Agent> agents;
boolean deathMarker=false;
protected Agent(int x, int y, Color color) {
this.x = x;
this.y = y;
@ -30,6 +31,12 @@ public abstract class Agent {
return dist<radius;
}
public int getDistance(Agent animal) {
int diffX = this.x-animal.x;
int diffY = this.y-animal.y;
int dist = (int) Math.floor(Math.sqrt(diffX*diffX+diffY*diffY));
return dist;
}
// Does whatever the agent does during a step
// then returns a boolean
// if false, agent dies at end of turn
@ -37,5 +44,8 @@ public abstract class Agent {
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
public abstract int getAgentType();
public void toggleDeathMarker() {
deathMarker=true;
}
}

View File

@ -13,6 +13,7 @@ public class Sheep extends Agent {
int hunger;
Random rand;
Sheep(int x,int y){
//first we call the constructor of the superClass(Animal)
//with the values we want.
@ -30,14 +31,17 @@ public class Sheep extends Agent {
* as you wish
*/
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
if(deathMarker) {
return false;
}
if(world.getCell(x, y)==1) {
world.setCell(x, y, 0);
hunger-=2;
hunger-=5;
} else {
hunger++;
}
this.moveRandom();
return hunger<10;
return hunger<30;
}
private void moveRandom() {
@ -58,5 +62,5 @@ public class Sheep extends Agent {
public int getAgentType() {
return 1;
}
}

View File

@ -29,8 +29,7 @@ public class Simulator extends Thread {
private boolean loopingBorder;
private int clickActionFlag;
private int loopDelay = 150;
private ArrayList<Integer> ruleSurviveCriteria= new ArrayList<Integer>();
private ArrayList<Integer> ruleSurviveCriteria= new ArrayList<Integer>();//array lists used to compute the Rules
private ArrayList<Integer> ruleBirthCriteria=new ArrayList<Integer>() ;
//Rules rule = new Rules();
//TODO : add missing attribute(s)
@ -186,17 +185,18 @@ public class Simulator extends Thread {
// only modify if sure of what you do
// to modify agent behavior, see liveTurn method
// in agent classes
ArrayList<Integer> arrayDeadAgent=new ArrayList<Integer>();
for(Agent agent : agents) {
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
if(!agent.liveTurn(neighbors,this)) {
//agents.remove(agent);
arrayDeadAgent.add(agents.indexOf(agent));
arrayDeadAgent.add(agents.indexOf(agent));//if an agent doesn't fulfill its condition to live it will be listed as dead before being removed
}
}
for(Integer i : arrayDeadAgent) {
agents.remove(i.intValue()-arrayDeadAgent.indexOf(i));
for(Integer i : arrayDeadAgent) {//removing agent from agents taking into account the change in index
agents.remove(i.intValue()-arrayDeadAgent.indexOf(i));//since the size of the array lowers at each iteration
}
//then evolution of the field
@ -266,8 +266,10 @@ public class Simulator extends Thread {
*/
public void clickCell(int x, int y) {
//TODO : complete method
//if clickActionFlag = true then create an agent
//else if flag = false then change value of cell
//if clickActionFlag = 0 then get the position
//else if flag = 1 then change value of cell
//else if flag=2 then set a sheep
//else if flag=3 then set a wolf
switch (clickActionFlag) {
case 0 :
System.out.print(x );
@ -281,7 +283,7 @@ public class Simulator extends Thread {
case 2 :
Sheep Shaun=new Sheep(x,y);
agents.add(Shaun);
System.out.println(agents);
//System.out.println(agents);
break;
case 3 :
Wolf agrou=new Wolf(x,y);
@ -304,7 +306,7 @@ public class Simulator extends Thread {
return status;
}
public int getNewCell(int x, int y) {
public int getNewCell(int x, int y) {//getCell for the new array of cells for the next step
//TODO : complete method with proper return
int status = newCells.get(x).get(y).getAlive();
//int status = newCells.get(x).get(y);
@ -346,7 +348,7 @@ public class Simulator extends Thread {
public void setCell(int x, int y, int status) {
cells.get(x).get(y).setAlive(status);
}
public void setNewCell(int x, int y, int status) {
public void setNewCell(int x, int y, int status) {//same but for the new cells array
newCells.get(x).get(y).setAlive(status);
}
@ -360,10 +362,10 @@ public class Simulator extends Thread {
//We need to create an array of strings
//each string should be the sum of all int transformed to strings in each Arrays of int
ArrayList<String> arrayLines=new ArrayList<String>();
for(int x=0;x<getWidth();x++) {
for(int x=0;x<getWidth();x++) {//create an array of Strings where each line is the sum of the values of all the cells in the x row separated by a semicolon
String sumElementToLine="";
for(int y=0;y<getHeight();y++) {
sumElementToLine+=(Integer.toString(getCell(y, x)));
sumElementToLine+=(Integer.toString(getCell(y, x)));//create each line by computing all the cells and concatenate them into a single string
if(y<getHeight()-1) {
sumElementToLine+=";";
}
@ -419,7 +421,7 @@ public class Simulator extends Thread {
Random ran= new Random();
for (int x=0;x<getWidth();x++){
for (int y=0;y<getHeight();y++) {
if (ran.nextFloat()<=chanceOfLife) {
if (ran.nextFloat()<=chanceOfLife) {// since the ran.nextFloat is uniformly distributed we can obtain a grid with each cell having as a probability to live chanceOfLife
setCell(x, y, 1);
}else {
setCell(x, y, 0);
@ -464,7 +466,7 @@ public class Simulator extends Thread {
// }
}
public void toggleClickAction() {
public void toggleClickAction() {//make clickActionFlag do an endless loop when called
//TODO : complete method
switch (clickActionFlag) {
case 0:
@ -497,7 +499,7 @@ public class Simulator extends Thread {
//TODO : complete method with proper return
ArrayList<String> arrayline=new ArrayList<String>();
String lineOne="";
for(int i=0;i<ruleSurviveCriteria.size();i++) {
for(int i=0;i<ruleSurviveCriteria.size();i++) {//create an array of Strings where each line is the sum of the values of surviveCriteria of the in the x row separated by a semicolon
lineOne+=Integer.toString(ruleSurviveCriteria.get(i));
if(i<ruleSurviveCriteria.size()-1) {
lineOne+=";";
@ -505,7 +507,7 @@ public class Simulator extends Thread {
}
arrayline.add(lineOne);
String lineTwo="";
for(int j=0;j<ruleBirthCriteria.size();j++) {
for(int j=0;j<ruleBirthCriteria.size();j++) {//create an array of Strings where each line is the sum of the values of birthCriteria of the in the x row separated by a semicolon
lineTwo+=Integer.toString(ruleBirthCriteria.get(j));
if(j<ruleBirthCriteria.size()-1) {
lineTwo+=";";
@ -515,7 +517,7 @@ public class Simulator extends Thread {
return arrayline;
}
public void loadRule(ArrayList<String> lines) {
public void loadRule(ArrayList<String> lines) {//opposite of getRule
if(lines.size()<=0) {
System.out.println("empty rule file");
return;
@ -608,7 +610,7 @@ public class Simulator extends Thread {
public String clickActionName() {
// TODO : initially return "sheep" or "cell"
// depending on clickActionFlag
switch (clickActionFlag) {
switch (clickActionFlag) {//returns a string depending pn the value pf clickActionFlag
case 0:
return "position";
case 1:

View File

@ -6,8 +6,9 @@ import java.util.Random;
// example of basic animal.
// do not hesitate to make it more complex
// and DO add at least another species that interact with it
// for example wolves that eat Wolf
//not very peculiar agent
//this class creates wolves that kill the agents that are of the Sheep subclass
//the wolves objects are moving towards any agent, making their behavior as a pack and going towards Sheep object to eat them
public class Wolf extends Agent {
int hunger;
@ -30,13 +31,43 @@ 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);
} else {
hunger++;
ArrayList<Agent> inAreaAgents=new ArrayList<Agent>();
for(Agent agent : world.getAnimals()) {
if (this.getDistance(agent)<=1) {
agent.toggleDeathMarker();//if a wolf is near a sheep(radius of 1) then the sheep will die the following liveTurn
hunger-=50;
}
else if (agent.isInArea(agent.getX(), agent.getY(), 5)) {//look for agents in area and add them to the array list inAreaAgents
inAreaAgents.add(agent);
}
}//gets the agent with the smallest distance with this wolf
if (inAreaAgents.size()>0) {
Agent smallestDistanceAgent=inAreaAgents.get(0);
for(Agent agentNear:inAreaAgents) {
if(this.getDistance(agentNear)<this.getDistance(smallestDistanceAgent)) {
smallestDistanceAgent=agentNear;
}
}
int smallX=smallestDistanceAgent.getX();
int smallY=smallestDistanceAgent.getY();
//decrease the distance between this wolf and the target
if((x-smallX)>0) {
x-=1;
}else if ((x-smallX)<0) {
x+=1;
}
if((y-smallY)>0) {
y-=1;
}else if ((y-smallY)<0) {
y+=1;
}
}else {
this.moveRandom();
}
this.moveRandom();
return hunger>10;
return hunger<50;
}
private void moveRandom() {

View File

@ -13,6 +13,7 @@ import javax.swing.event.ChangeListener;
import backend.Simulator;
import javax.imageio.ImageReader;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
@ -155,6 +156,14 @@ public class MyInterface extends JFrame {
generateRandomBoard();
}
});
/**panelRight.add(btnLoadImage);
JButton btnLoadImage = new JButton("Load Image");
btnLoadImage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clicLoadImage;
}
});*/
panelRight.add(btnRandGen);
@ -321,6 +330,25 @@ public class MyInterface extends JFrame {
this.repaint();
}
}
/**public void clicLoadImage() {
String fileName=SelectFile();
//ArrayList<String> stringArray = new ArrayList<String>();
if (fileName.length()>0) {
try {
ImageReader fileContent = new ImageReader(new FileReader(fileName));
String line = fileContent.readLine();
while (line != null) {
stringArray.add(line);
line = fileContent.readLine();
}
fileContent.close();
} catch (Exception e) {
e.printStackTrace();
}
mySimu.loadAgents(stringArray);
this.repaint();
}
}*/
public void clicSaveToFileButton() {