can load agents and and the wolf agent

This commit is contained in:
Laure BEL 2024-05-29 14:10:51 +02:00
parent e8f2d84147
commit a426e87da4
2 changed files with 63 additions and 5 deletions

View File

@ -1,4 +1,5 @@
package backend; package backend;
import java.awt.Color;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.util.ArrayList; import java.util.ArrayList;
@ -224,12 +225,14 @@ public class Simulator extends Thread {
* method called when clicking on a cell in the interface * method called when clicking on a cell in the interface
*/ */
public void clickCell(int x, int y) { public void clickCell(int x, int y) {
if (clickActionFlag==0) { if (clickActionFlag==0) { // cell
world.setCell(x, y, getCell(x, y) == 1 ? 0 : 1); world.setCell(x, y, getCell(x, y) == 1 ? 0 : 1);
} else if (clickActionFlag==1) { } else if (clickActionFlag==1) { // sheep
Sheep sheep = new Sheep(x,y);
} else if (clickActionFlag==2) { agents.add(sheep);
} else if (clickActionFlag==2) { // wolf
Wolf wolf = new Wolf(x,y);
agents.add(wolf);
} }
} }

55
src/backend/Wolf.java Normal file
View File

@ -0,0 +1,55 @@
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.orange);
// 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, World world) {
if(world.getCell(x, y)==1) {
world.setCell(x, y, 0);
} else {
hunger++;
}
this.moveRandom();
return hunger>10;
}
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;
}
}
}