-1

Array is 40 by 40 so [0 - 39][0 - 39].

is there a way that I can go through the array and select every item without selecting an item twice but do it randomly, so no a loop like for(i = 0; i < 40; i ++). sorry if my explanation is bad

Converted the array into a 1d Array

newArray = [];
for (var i = 0; i < gridsize; i ++) {
  for (var j = 0; j < gridsize; j ++) {
    newArray.push(grid[i][j])
  }
}
newArray = shuffle(newArray)

then I'm going through the array like so

for (var x = 0; x < newArray.length; x ++) {
  i = newArray[x].x
  j = newArray[x].y
    switch(grid[i][j].id) {

so that Because the array has been shuffled I can get a new random spot to search, is there a more efficient way than this?

  • 1
    Oh there are multiple ways, could you share with us what have you tried so we could help debug your code? As I'm sure you don't expect us to write the code for you :-) – Alexandre Elshobokshy Oct 04 '18 at 12:51
  • Standard approach would be to make arrays for each dimension containing the numbers 0 through 39, and then [shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) the arrays. – Pointy Oct 04 '18 at 12:52

1 Answers1

2

Have a 1D array that represents all 1600 elements. Shuffle the array How to randomize (shuffle) a JavaScript array?. Use that array to lookup the elements.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406