I have a grid based game 8 squares by 8 squares giving 64 pieces in total, these pieces are stored in an array. I’m having a problem where certain grid squares are being populated twice so I need to check the array for duplicate co-ordinates.
Below code gives the x, y grid co-ordinates of each piece – testX and testY, I’m not sure how I would go about running through this array to remove duplicates. If there are duplicates pieces I need to keep the first encountered and remove any subsequent duplicates. I’m using jQuery if that helps.
function checkGrid() {
var x;
for (x = 0; x < grid.length; x++) {
var testY= grid[x].getY();
var testX = grid[x].getX();
}
}
You could consider using an object instead of an array:
Something like this. Then if you change the value of a grid location, you don’t need to check for duplicates.
EDIT.
Since you can’t change to object, you should find an existing item when you insert them. You didn’t post the code where you add items to the grid, but can you do something like this: