Final commit before bonus implementation
This commit is contained in:
parent
5a12b1c088
commit
d43c547709
|
|
@ -7,7 +7,8 @@ public abstract class Agent {
|
||||||
protected int x;
|
protected int x;
|
||||||
protected int y;
|
protected int y;
|
||||||
protected Color color;
|
protected Color color;
|
||||||
|
protected ArrayList<Agent> agents;
|
||||||
|
boolean deathMarker=false;
|
||||||
protected Agent(int x, int y, Color color) {
|
protected Agent(int x, int y, Color color) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
|
|
@ -30,6 +31,12 @@ public abstract class Agent {
|
||||||
return dist<radius;
|
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
|
// 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
|
||||||
|
|
@ -37,5 +44,8 @@ public abstract class Agent {
|
||||||
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
|
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
|
||||||
public abstract int getAgentType();
|
public abstract int getAgentType();
|
||||||
|
|
||||||
|
public void toggleDeathMarker() {
|
||||||
|
deathMarker=true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ public class Sheep extends Agent {
|
||||||
int hunger;
|
int hunger;
|
||||||
Random rand;
|
Random rand;
|
||||||
|
|
||||||
|
|
||||||
Sheep(int x,int y){
|
Sheep(int x,int y){
|
||||||
//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.
|
||||||
|
|
@ -30,14 +31,17 @@ public class Sheep 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(deathMarker) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if(world.getCell(x, y)==1) {
|
if(world.getCell(x, y)==1) {
|
||||||
world.setCell(x, y, 0);
|
world.setCell(x, y, 0);
|
||||||
hunger-=2;
|
hunger-=5;
|
||||||
} else {
|
} else {
|
||||||
hunger++;
|
hunger++;
|
||||||
}
|
}
|
||||||
this.moveRandom();
|
this.moveRandom();
|
||||||
return hunger<10;
|
return hunger<30;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveRandom() {
|
private void moveRandom() {
|
||||||
|
|
|
||||||
|
|
@ -29,8 +29,7 @@ public class Simulator extends Thread {
|
||||||
private boolean loopingBorder;
|
private boolean loopingBorder;
|
||||||
private int clickActionFlag;
|
private int clickActionFlag;
|
||||||
private int loopDelay = 150;
|
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>() ;
|
private ArrayList<Integer> ruleBirthCriteria=new ArrayList<Integer>() ;
|
||||||
//Rules rule = new Rules();
|
//Rules rule = new Rules();
|
||||||
//TODO : add missing attribute(s)
|
//TODO : add missing attribute(s)
|
||||||
|
|
@ -186,17 +185,18 @@ public class Simulator extends Thread {
|
||||||
// only modify if sure of what you do
|
// only modify if sure of what you do
|
||||||
// to modify agent behavior, see liveTurn method
|
// to modify agent behavior, see liveTurn method
|
||||||
// in agent classes
|
// in agent classes
|
||||||
|
|
||||||
ArrayList<Integer> arrayDeadAgent=new ArrayList<Integer>();
|
ArrayList<Integer> arrayDeadAgent=new ArrayList<Integer>();
|
||||||
for(Agent agent : agents) {
|
for(Agent agent : agents) {
|
||||||
|
|
||||||
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
|
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
|
||||||
if(!agent.liveTurn(neighbors,this)) {
|
if(!agent.liveTurn(neighbors,this)) {
|
||||||
//agents.remove(agent);
|
//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) {
|
for(Integer i : arrayDeadAgent) {//removing agent from agents taking into account the change in index
|
||||||
agents.remove(i.intValue()-arrayDeadAgent.indexOf(i));
|
agents.remove(i.intValue()-arrayDeadAgent.indexOf(i));//since the size of the array lowers at each iteration
|
||||||
}
|
}
|
||||||
|
|
||||||
//then evolution of the field
|
//then evolution of the field
|
||||||
|
|
@ -266,8 +266,10 @@ public class Simulator extends Thread {
|
||||||
*/
|
*/
|
||||||
public void clickCell(int x, int y) {
|
public void clickCell(int x, int y) {
|
||||||
//TODO : complete method
|
//TODO : complete method
|
||||||
//if clickActionFlag = true then create an agent
|
//if clickActionFlag = 0 then get the position
|
||||||
//else if flag = false then change value of cell
|
//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) {
|
switch (clickActionFlag) {
|
||||||
case 0 :
|
case 0 :
|
||||||
System.out.print(x );
|
System.out.print(x );
|
||||||
|
|
@ -281,7 +283,7 @@ public class Simulator extends Thread {
|
||||||
case 2 :
|
case 2 :
|
||||||
Sheep Shaun=new Sheep(x,y);
|
Sheep Shaun=new Sheep(x,y);
|
||||||
agents.add(Shaun);
|
agents.add(Shaun);
|
||||||
System.out.println(agents);
|
//System.out.println(agents);
|
||||||
break;
|
break;
|
||||||
case 3 :
|
case 3 :
|
||||||
Wolf agrou=new Wolf(x,y);
|
Wolf agrou=new Wolf(x,y);
|
||||||
|
|
@ -304,7 +306,7 @@ public class Simulator extends Thread {
|
||||||
return status;
|
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
|
//TODO : complete method with proper return
|
||||||
int status = newCells.get(x).get(y).getAlive();
|
int status = newCells.get(x).get(y).getAlive();
|
||||||
//int status = newCells.get(x).get(y);
|
//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) {
|
public void setCell(int x, int y, int status) {
|
||||||
cells.get(x).get(y).setAlive(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);
|
newCells.get(x).get(y).setAlive(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -360,10 +362,10 @@ public class Simulator extends Thread {
|
||||||
//We need to create an array of strings
|
//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
|
//each string should be the sum of all int transformed to strings in each Arrays of int
|
||||||
ArrayList<String> arrayLines=new ArrayList<String>();
|
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="";
|
String sumElementToLine="";
|
||||||
for(int y=0;y<getHeight();y++) {
|
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) {
|
if(y<getHeight()-1) {
|
||||||
sumElementToLine+=";";
|
sumElementToLine+=";";
|
||||||
}
|
}
|
||||||
|
|
@ -419,7 +421,7 @@ public class Simulator extends Thread {
|
||||||
Random ran= new Random();
|
Random ran= new Random();
|
||||||
for (int x=0;x<getWidth();x++){
|
for (int x=0;x<getWidth();x++){
|
||||||
for (int y=0;y<getHeight();y++) {
|
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);
|
setCell(x, y, 1);
|
||||||
}else {
|
}else {
|
||||||
setCell(x, y, 0);
|
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
|
//TODO : complete method
|
||||||
switch (clickActionFlag) {
|
switch (clickActionFlag) {
|
||||||
case 0:
|
case 0:
|
||||||
|
|
@ -497,7 +499,7 @@ public class Simulator extends Thread {
|
||||||
//TODO : complete method with proper return
|
//TODO : complete method with proper return
|
||||||
ArrayList<String> arrayline=new ArrayList<String>();
|
ArrayList<String> arrayline=new ArrayList<String>();
|
||||||
String lineOne="";
|
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));
|
lineOne+=Integer.toString(ruleSurviveCriteria.get(i));
|
||||||
if(i<ruleSurviveCriteria.size()-1) {
|
if(i<ruleSurviveCriteria.size()-1) {
|
||||||
lineOne+=";";
|
lineOne+=";";
|
||||||
|
|
@ -505,7 +507,7 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
arrayline.add(lineOne);
|
arrayline.add(lineOne);
|
||||||
String lineTwo="";
|
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));
|
lineTwo+=Integer.toString(ruleBirthCriteria.get(j));
|
||||||
if(j<ruleBirthCriteria.size()-1) {
|
if(j<ruleBirthCriteria.size()-1) {
|
||||||
lineTwo+=";";
|
lineTwo+=";";
|
||||||
|
|
@ -515,7 +517,7 @@ public class Simulator extends Thread {
|
||||||
return arrayline;
|
return arrayline;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadRule(ArrayList<String> lines) {
|
public void loadRule(ArrayList<String> lines) {//opposite of getRule
|
||||||
if(lines.size()<=0) {
|
if(lines.size()<=0) {
|
||||||
System.out.println("empty rule file");
|
System.out.println("empty rule file");
|
||||||
return;
|
return;
|
||||||
|
|
@ -608,7 +610,7 @@ public class Simulator extends Thread {
|
||||||
public String clickActionName() {
|
public String clickActionName() {
|
||||||
// TODO : initially return "sheep" or "cell"
|
// TODO : initially return "sheep" or "cell"
|
||||||
// depending on clickActionFlag
|
// depending on clickActionFlag
|
||||||
switch (clickActionFlag) {
|
switch (clickActionFlag) {//returns a string depending pn the value pf clickActionFlag
|
||||||
case 0:
|
case 0:
|
||||||
return "position";
|
return "position";
|
||||||
case 1:
|
case 1:
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,9 @@ import java.util.Random;
|
||||||
|
|
||||||
// example of basic animal.
|
// example of basic animal.
|
||||||
// do not hesitate to make it more complex
|
// do not hesitate to make it more complex
|
||||||
// and DO add at least another species that interact with it
|
//not very peculiar agent
|
||||||
// for example wolves that eat Wolf
|
//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 {
|
public class Wolf extends Agent {
|
||||||
|
|
||||||
int hunger;
|
int hunger;
|
||||||
|
|
@ -30,13 +31,43 @@ 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> inAreaAgents=new ArrayList<Agent>();
|
||||||
world.setCell(x, y, 0);
|
for(Agent agent : world.getAnimals()) {
|
||||||
} else {
|
if (this.getDistance(agent)<=1) {
|
||||||
hunger++;
|
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() {
|
private void moveRandom() {
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import javax.swing.event.ChangeListener;
|
||||||
|
|
||||||
import backend.Simulator;
|
import backend.Simulator;
|
||||||
|
|
||||||
|
import javax.imageio.ImageReader;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JFileChooser;
|
import javax.swing.JFileChooser;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
|
@ -155,6 +156,14 @@ public class MyInterface extends JFrame {
|
||||||
generateRandomBoard();
|
generateRandomBoard();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
/**panelRight.add(btnLoadImage);
|
||||||
|
|
||||||
|
JButton btnLoadImage = new JButton("Load Image");
|
||||||
|
btnLoadImage.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
clicLoadImage;
|
||||||
|
}
|
||||||
|
});*/
|
||||||
panelRight.add(btnRandGen);
|
panelRight.add(btnRandGen);
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -321,6 +330,25 @@ public class MyInterface extends JFrame {
|
||||||
this.repaint();
|
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() {
|
public void clicSaveToFileButton() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue