The problem is you are splicing the same array that you are iterating over. So in the second iteration of the loop when x=4, y=0
you have the following:
i=1, options[[2,0],[6,0],[4,-2],[4,2]]
After you splice the second option the array will change and you will have the following for the third iteration:
i=2, options[[2,0],[4,-2],[4,2]]
Notice that [4,-2]
gets skipped. The easiest fix for this would be to use $.grep
options = $.grep(options, function(option, index) {
return option[0]>=0 && option[0]<size && option[1]>=0 && option[1]<size;
});
This will iterate over the array and return an array of objects where grep returns true
. You should also do the same general thing for when you iterate over the already visited squares.