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