Michael Rycroft
Michael Rycroft

Reputation: 21

Trying to find an algorithm or maybe math?

Take the pathway example below, it has 7 pathways, I create an array of 7 to hold a value for each pathway, the initial values of the array is as follows:

[1,2,3,4,5,6,7] (see picture: top row array values under the paths where start is)

if at random the user picks pathway 5 which is array index [4] the value returned will be 5.. So far so good.

Now it gets a little confusing, because the user has selected pathway 5. I need to change the array values for the next random choice, I always need the currently selected pathway (5 in this example) to now have an array value of 1 and count up to the right and up to the left, the array values should now look like this

[5,4,3,2,1,2,3]

If you look at the pathway example picture and follow down each step you will see this happening in the array values under the paths the red box highlights the selected path, hope that makes it clearer, and to the question, is there an algorithm that can do this?

Pathway Example 2

Upvotes: 1

Views: 123

Answers (1)

Fernando Aires
Fernando Aires

Reputation: 531

Every array cell contains the distance of the cell you've chosen plus one, is that it? If so, you can simply solve with a for statement:

for each index of array:
    array[index] = abs(index - selectedIndex)+1

where selectedIndex is the index that was picked, and abs(...) the modulus function.

Your solution of circular array is quicker, because you just need to show the numbers, and you can calculate the new limits based on the selectedIndex and the previous limits only. This solution provided just cost less memory.

Upvotes: 2

Related Questions