This site requires JavaScript, please enable it in your browser!
Greenfoot back
JasonZhu
JasonZhu wrote ...

2013/10/4

How do I Write a Method that can check to see if there are objects at adjacent cells surrounding the Object Giving Birth?

JasonZhu JasonZhu

2013/10/4

#
The teacher in class asked us to try to play around with mik's Foxes and Rabbit Scenario. He deleted a lot of code from the original and asked us to do I few task on it. So currently, I'm doing pretty well so far, but I've recently encountered a problem. Everytime a Rabbit gives birth, it places the new Rabbit at an Adjacent square. The problem with this is that eventually when a group of Rabbits get near a corner or edge, the new borns are so clumped that it mass spawns Rabbits to a point where the program lags out. I want to fix this by having a check to see if there are available squares around the rabbit before it allows it to give birth. Anyone know how to write this? Help is greatly appreciated and thanks for eveyone for helping!
danpost danpost

2013/10/4

#
First you will need to know how many adjacent squares surround the current square to begin with. Squares in the middle of the grid will have 4; squares along the edges will have 3; and corner squares will only have 2 (if you include diagonally adjacent squares, the numbers would be 8, 5 and 3).
int adjacentSquareCount = 4;
if (getX() == 0 || getX() == getWorld().getWidth()-1) adjacentSquareCount--;
if (getY==0 || getY() == getWorld().getHeight()-1) adjacentSquareCount--;
Now you can compare this value to the size of the list returned using 'getNeighbours(1, false, null)'. There is no available square to spawn a rabbit on if the two values are equal.
JasonZhu JasonZhu

2013/10/4

#
I considered what you wrote. However, I want to know if there is a better way to control the Rabbit population, as when I observe the graph, the Rabbit numbers spike up to the 20 thousands.
danpost danpost

2013/10/4

#
You could just put the spawning code inside an 'if' block using 'if (getWorld().getObjects(Rabbit.class).size() < 500)'. The 500 being a possible limit on the number of Rabbit object for the world to have.
You need to login to post a reply.