Finished thank you
This commit is contained in:
parent
5c08a08b46
commit
5b424800fe
|
|
@ -22,10 +22,10 @@ public class Simulator extends Thread {
|
||||||
private boolean stopFlag;
|
private boolean stopFlag;
|
||||||
private boolean pauseFlag;
|
private boolean pauseFlag;
|
||||||
private boolean loopingBorder;
|
private boolean loopingBorder;
|
||||||
private int clickActionFlag;
|
private int clickActionFlag; // change from boolean to int
|
||||||
private int loopDelay = 150;
|
private int loopDelay = 150;
|
||||||
|
|
||||||
World currentWorld = new World(getWidth(),getHeight());
|
World currentWorld = new World(getWidth(),getHeight()); // create the instance of class World
|
||||||
|
|
||||||
//TODO : add missing attribute(s)
|
//TODO : add missing attribute(s)
|
||||||
|
|
||||||
|
|
@ -34,7 +34,7 @@ public class Simulator extends Thread {
|
||||||
stopFlag=false;
|
stopFlag=false;
|
||||||
pauseFlag=false;
|
pauseFlag=false;
|
||||||
loopingBorder=false;
|
loopingBorder=false;
|
||||||
clickActionFlag=0;
|
clickActionFlag=0; //initallize the clickactionflag by 0
|
||||||
|
|
||||||
agents = new ArrayList<Agent>();
|
agents = new ArrayList<Agent>();
|
||||||
fieldBirthValues = new ArrayList<Integer>();
|
fieldBirthValues = new ArrayList<Integer>();
|
||||||
|
|
@ -52,7 +52,7 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
//TODO : replace with proper return
|
//TODO replace with proper return
|
||||||
return COL_NUM;
|
return COL_NUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -190,6 +190,9 @@ public class Simulator extends Thread {
|
||||||
else if(clickActionFlag ==2) {
|
else if(clickActionFlag ==2) {
|
||||||
agents.add(new Sheep(x,y));
|
agents.add(new Sheep(x,y));
|
||||||
}
|
}
|
||||||
|
else if (clickActionFlag == 3) {
|
||||||
|
agents.add(new Wolf(x,y));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -412,6 +415,9 @@ public class Simulator extends Thread {
|
||||||
agents.add(new Cow(agentValues[0],agentValues[1]));
|
agents.add(new Cow(agentValues[0],agentValues[1]));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
if(agentValues[2]==3) {
|
||||||
|
agents.add(new Wolf(agentValues[0],agentValues[1]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -423,14 +429,25 @@ public class Simulator extends Thread {
|
||||||
// TODO : initially return "sheep" or "cell"
|
// TODO : initially return "sheep" or "cell"
|
||||||
// depending on clickActionFlag
|
// depending on clickActionFlag
|
||||||
if(clickActionFlag == 1) {
|
if(clickActionFlag == 1) {
|
||||||
return "Cow";
|
return "cow";
|
||||||
}
|
}
|
||||||
else if (clickActionFlag == 0) {
|
else if (clickActionFlag == 0) {
|
||||||
return "cell";
|
return "cell";
|
||||||
}
|
}
|
||||||
|
else if(clickActionFlag == 2){
|
||||||
|
return "wolf";
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
return "sheep";
|
return "sheep";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public String typeAnimals(int x , int y,ArrayList<Agent> neighbor) {
|
||||||
|
Agent agent = neighbor.get(0);
|
||||||
|
if (agent instanceof Wolf) {
|
||||||
|
return "Wolf";
|
||||||
|
} else {
|
||||||
|
return "Sheep";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,67 @@
|
||||||
|
package backend;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class Wolf extends Agent {
|
||||||
|
|
||||||
|
int hunger;
|
||||||
|
Random rand;
|
||||||
|
|
||||||
|
Wolf(int x,int y){
|
||||||
|
//first we call the constructor of the superClass(Animal)
|
||||||
|
//with the values we want.
|
||||||
|
// here we decide that a Sheep is initially white using this constructor
|
||||||
|
super(x,y,Color.red);
|
||||||
|
// we give our sheep a hunger value of zero at birth
|
||||||
|
hunger = 0;
|
||||||
|
//we initialize the random number generator we will use to move randomly
|
||||||
|
rand = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* action of the animal
|
||||||
|
* it can interact with the cells or with other animals
|
||||||
|
* as you wish
|
||||||
|
*/
|
||||||
|
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
||||||
|
//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);
|
||||||
|
hunger=hunger-1;
|
||||||
|
}else {
|
||||||
|
hunger++;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
hunger++;
|
||||||
|
}
|
||||||
|
this.moveRandom();
|
||||||
|
if (hunger<=10) {
|
||||||
|
return true;
|
||||||
|
}else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void moveRandom() {
|
||||||
|
int direction = rand.nextInt(4);
|
||||||
|
if(direction == 0) {
|
||||||
|
x+=1;
|
||||||
|
}
|
||||||
|
if(direction == 1) {
|
||||||
|
y+=1;
|
||||||
|
}
|
||||||
|
if(direction == 2) {
|
||||||
|
x-=1;
|
||||||
|
}
|
||||||
|
if(direction == 3) {
|
||||||
|
y-=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue