I've created a 2 dimensional 'grid' for a game world I'm creating in Java. I'm having a problem with the 'wander' mode algorithm I've created.
I noticed a problem that the objects would seem to favor the bottom right corner of the grid. I changed the algorithm and thought it was fixed.
Today, while stress testing, I noticed that the problem was not fixed, and now wandering objects favor the top left corner of the grid, but it takes longer for them to wander in that area.
The algorithm works by: 1. Getting the current values of the person's position 2. Putting all the squares in 3 block radius into a linked list 3. Randomize the list 4. Choose a random value from the list 5. Setting that values point as the next target
Here's a code snippet:
Point personAt = this.getTopLeftPoint();
personAt = Game.getGame().screenToGame(personAt);
LinkedList<Point> thisSet = new LinkedList<Point>();
for (int x = personAt.x - 2; x < personAt.x + 3; x++) {
for (int y = personAt.y - 2; y < personAt.y + 3; y++) {
if (!(x == personAt.x && y == personAt.y)) {
//TODO: Clean up the next line of code.
if (x > 0 && y > 0 && !Board.getBoard().getSquare(x, y).getSquareType().equals(SquareType.path)) {
if (!Game.getGame().getMap().blocked(x, y)) {
thisSet.add(new Point(x, y));
}
}
}
}
}
Collections.shuffle(thisSet);
//Random random = new Random();
//Integer randomNo = random.nextInt(thisSet.size());
setNextTarget(thisSet.get(0));
Is there anything I'm missing here?
I am confused as to why they still end up within the top left quarter of the grid.
EDIT: I completely removed the Random object, as suggested by Don. Still end up with the same result.
EDIT: Updated code to fix one part of the issue, so now only the square the person is currently on is ignored, rather that all squares on the current X or Y co ordinate. As suggested by rsp.