functioning wolf
This commit is contained in:
parent
e501b7feb6
commit
d47255953f
|
|
@ -262,14 +262,45 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(clickActionFlag == 3) { //wolf
|
else if(clickActionFlag == 3) { //Wolf
|
||||||
//Make agent sheep/wolf
|
int i=0;
|
||||||
return;
|
Agent wolf = new Wolf(x,y);
|
||||||
|
|
||||||
|
//if there are no agents, skip directly to adding one
|
||||||
|
boolean removal =false;
|
||||||
|
if (agents.size()>0 ){
|
||||||
|
|
||||||
|
//if an agent is in this area we iterate in the arraylist agents in order to find which one it is
|
||||||
|
|
||||||
|
for(i=0;i<=agents.size()-1;i++){
|
||||||
|
|
||||||
|
//if we proceed to find the agent on the coordinates of the click, we remove it
|
||||||
|
|
||||||
|
if (agents.get(i).getX() == x && agents.get(i).getY() == y ){
|
||||||
|
agents.remove(i);
|
||||||
|
System.out.println("Corresponding agent found, proceeding with removal");
|
||||||
|
removal = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(i==agents.size() && removal ==false){
|
||||||
|
//if we find no corresponding agent after the for loop, we add one
|
||||||
|
System.out.println("no agents to remove, proceeding with creation");
|
||||||
|
setWolf(x, y);
|
||||||
|
if (enableLogs) {
|
||||||
|
System.out.println("clickAgent Called, Agent created at: " + x + "," + y + "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (enableLogs) {
|
else{
|
||||||
System.out.println("clickCell Called, cell :" + x + "," + y + " is now" + newCellValue + "");
|
setWolf(x,y);
|
||||||
|
if (enableLogs) {
|
||||||
|
System.out.println("clickAgent Called, Agent created at: " + x + "," + y + "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//TODO-INPROGRESS : set agent (x y agent) load an agent to coordinates x,y
|
//TODO-INPROGRESS : set agent (x y agent) load an agent to coordinates x,y
|
||||||
public void setSheep(int x,int y){
|
public void setSheep(int x,int y){
|
||||||
|
|
@ -278,6 +309,12 @@ public class Simulator extends Thread {
|
||||||
|
|
||||||
agents.add(sheep);
|
agents.add(sheep);
|
||||||
}
|
}
|
||||||
|
public void setWolf(int x,int y){
|
||||||
|
|
||||||
|
Wolf wolf = new Wolf(x,y);
|
||||||
|
|
||||||
|
agents.add(wolf);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,23 +19,100 @@ public class Wolf extends Agent {
|
||||||
|
|
||||||
//we put a condition for the behavior of the wolf depending on the presence of a sheep in the detection range
|
//we put a condition for the behavior of the wolf depending on the presence of a sheep in the detection range
|
||||||
int detectionRange =3;
|
int detectionRange =3;
|
||||||
int eatingRange=1;
|
|
||||||
int i=0;
|
int i=0;
|
||||||
|
//Agent in detectionrange
|
||||||
if (isInArea(x, y,detectionRange)){
|
if (isInArea(x, y,detectionRange)){
|
||||||
|
|
||||||
|
//iterations for the length of the arraylist
|
||||||
for(i=0;i<=world.getArrayAgent().size()-1;i++){
|
for(i=0;i<=world.getArrayAgent().size()-1;i++){
|
||||||
|
|
||||||
//if we proceed to find the agent, we check if it is an instance of sheep
|
//if we proceed to find the agent, we check if it is an instance of sheep
|
||||||
if (world.getArrayAgent().get(i) instanceof Sheep ){
|
if (world.getArrayAgent().get(i) instanceof Sheep ){
|
||||||
Agent agent = new Wolf(x,y);
|
System.out.println("sheep detected");
|
||||||
|
Agent wolf = new Wolf(x,y);
|
||||||
//if the instance of sheep is detected to be inside the detection range it will
|
//if the instance of sheep is detected to be inside the detection range it will
|
||||||
if (world.getArrayAgent().get(i).getX()-Wolf.getX()<=detectionRange || world.getArrayAgent().get(i).getY()-Wolf.getY()<=detectionRange){
|
if (world.getArrayAgent().get(i).getX()-wolf.getX()<=detectionRange || world.getArrayAgent().get(i).getY()-wolf.getY()<=detectionRange){
|
||||||
|
System.out.println("sheep in range");
|
||||||
|
int xSpotted = world.getArrayAgent().get(i).getX()-wolf.getX();
|
||||||
|
int ySpotted = world.getArrayAgent().get(i).getY()-wolf.getY();
|
||||||
|
// here the values calculated can be over or under 0
|
||||||
|
//thus we need to further determine its meaning, a negative xSpotted means that the value of x for the wolf is
|
||||||
|
//greater than the sheep thus it means that the wolf will have to go in the negative x direction (to the left) in order to reach
|
||||||
|
//the sheep
|
||||||
|
//We also need to determine the different cases possible by splitting the movements in 8 directions
|
||||||
|
//such as the four mains directions and the 4 resulting diagonals
|
||||||
|
//ySpotted<0 means go to -y direction
|
||||||
|
//xSpotted<0 means to go in the -x direction
|
||||||
|
//if the distance in x is smaller than the distance in y then the wolf will start by reducing the distance in y
|
||||||
|
//and according to the sign of ySpotted
|
||||||
|
|
||||||
|
//taking care of the displacement on y if there is a dy
|
||||||
|
if (xSpotted < ySpotted && ySpotted<0){
|
||||||
|
y-=1;
|
||||||
|
}
|
||||||
|
if (xSpotted < ySpotted && ySpotted>0){
|
||||||
|
y+=1;
|
||||||
|
}
|
||||||
|
//displacement on x if there is no dy
|
||||||
|
if (xSpotted < ySpotted && ySpotted==0){
|
||||||
|
x-=1;
|
||||||
|
}
|
||||||
|
if (xSpotted > ySpotted && ySpotted==0){
|
||||||
|
x+=1;
|
||||||
|
}
|
||||||
|
//displacement in x when there is a dx
|
||||||
|
if (xSpotted>ySpotted && xSpotted<0){
|
||||||
|
x-=1;
|
||||||
|
}
|
||||||
|
if (xSpotted>ySpotted && xSpotted>0){
|
||||||
|
x+=1;
|
||||||
|
}
|
||||||
|
if (xSpotted < ySpotted && xSpotted==0){
|
||||||
|
y-=1;
|
||||||
|
}
|
||||||
|
if (xSpotted > ySpotted && xSpotted==0){
|
||||||
|
y+=1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (xSpotted==ySpotted && xSpotted>0){
|
||||||
|
x+=1;
|
||||||
|
y+=1;
|
||||||
|
}
|
||||||
|
if (xSpotted==ySpotted && xSpotted<0){
|
||||||
|
x-=1;
|
||||||
|
y-=1;
|
||||||
|
}
|
||||||
|
if (xSpotted == -ySpotted && xSpotted>0){
|
||||||
|
x+=1;
|
||||||
|
y-=1;
|
||||||
|
}
|
||||||
|
if (xSpotted == -ySpotted && xSpotted<0){
|
||||||
|
x-=1;
|
||||||
|
y+=1;
|
||||||
|
}
|
||||||
|
if(world.getArrayAgent().get(i).getX() == wolf.getX() && world.getArrayAgent().get(i).getY() == wolf.getY()){
|
||||||
|
world.getArrayAgent().remove(i);
|
||||||
|
hunger = 0;
|
||||||
|
System.out.println("sheep killed");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
hunger++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
this.moveRandom();
|
||||||
|
hunger++;
|
||||||
|
System.out.println("nothing detected");
|
||||||
|
|
||||||
|
}
|
||||||
|
return hunger<8;
|
||||||
}
|
}
|
||||||
//move randomly if no sheep in detection range
|
//move randomly if no sheep in detection range
|
||||||
private void moveRandom() {
|
private void moveRandom() {
|
||||||
|
|
@ -53,6 +130,6 @@ public class Wolf extends Agent {
|
||||||
y-=1;
|
y-=1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//implement a method that induce movement in the direction of the sheep in the detection range
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue