rules for sheep and looping border for sheeps

This commit is contained in:
Guillaume BONABAU 2024-05-29 16:34:20 +02:00
parent 4cc9220bd9
commit e157d8e991
2 changed files with 28 additions and 16 deletions

View File

@ -0,0 +1,17 @@
[{"cell": {
"value" : 1,
"color" : [0,102,0],
"conditionCountNear" : [3,4,5,6,7,8],
"conditionHighestNear" : [],
"ifValue" : 1,
"elseValue" : 0
}},
{"cell": {
"value" : 0,
"color" : [102,51,0],
"conditionCountNear" : [2,3,4,5,6,7,8],
"conditionHighestNear" : [],
"ifValue" : 1,
"elseValue" : 0
}}
]

View File

@ -37,25 +37,20 @@ public class Sheep extends Agent {
} else {
hunger++;
}
this.moveRandom();
this.moveRandom(world);
return hunger<10; //condition to be alive
}
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;
private void moveRandom(Simulator world) {
//check is looping border is activated
if(world.isLoopingBorder()) {
//if looping border is activated we can move in any direction
x = (x+rand.nextInt(3)-1+world.getWidth())%world.getWidth();
y = (y+rand.nextInt(3)-1+world.getHeight())%world.getHeight();
} else {
//if looping border is not activated we can only move in the world
x = Math.max(0, Math.min(world.getWidth()-1, x+rand.nextInt(3)-1));
y = Math.max(0, Math.min(world.getHeight()-1, y+rand.nextInt(3)-1));
}
}
}