From 6664b8766c2aafdcabaee84d629aaeb827f20098 Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Wed, 29 May 2024 16:34:20 +0200 Subject: [PATCH] rules for sheep and looping border for sheeps --- ressources/Rule/grassForSheepsRule.json | 17 ++++++++++++++++ src/backend/Sheep.java | 27 ++++++++++--------------- 2 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 ressources/Rule/grassForSheepsRule.json diff --git a/ressources/Rule/grassForSheepsRule.json b/ressources/Rule/grassForSheepsRule.json new file mode 100644 index 0000000..0fcae70 --- /dev/null +++ b/ressources/Rule/grassForSheepsRule.json @@ -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 +}} +] \ No newline at end of file diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index 9420f05..6801fc5 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -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)); } } - - }