I converted my 2d array to a 1d array. For example: (starts with 0, not 1); 00 01 02 03 04
05 06 07 08 09
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
was converted to a 1d array. [0, 1, 2, 3, 4 ..... 23, 24].
Now I'm trying to create a function that finds every spot that is “connected” or next to a specific element of the array. This includes elements that are diagonal from it. Therefore, using the 2d array above, if I want an array of elements associated with 0, I expect the function to return an array [1, 5, 6].
The problem I am facing is finding the diagonals. This is my JS code for an array to be returned.
var poss = [Number(num+1),Number(num-1),Number(num+col),Number(num-col),Number((num+col) + 1),Number((num+col) - 1),Number((num-col) + 1),Number((num-col) - 1)];
This returns [1, 5, 6, 4]. I have code that excludes negative numbers. However, 4 should not be. I understand that this is because it is a boundary case, and it is not registered as a transcendental one, because it is not a negative number. Is there any formula that will find the elements associated with it diagonally? Remember that I use a 1d array. This program also runs regardless of the size of the array. Thus, this will also work for 4x4 or 5x4 boards. Therefore, the use of field strings and numbers is ideal.