rotate values in array

Blitz3D Forums/Blitz3D Programming/rotate values in array

Another way is to store a offset value...something like:


;some function calls
;each "rolls" the array, or at least gives the impression it does

fruitroll(3)
fruitroll(7)
fruitroll(1)



;now the function

FUNCTION fruitroll(offset%)


 FOR i = 0 TO 9

  temp = ABS( ( i + offset ) MOD 10 )
     
  value = fruit(temp)
   ;now you can print the value, display the graphic value..whatever

 NEXT

END FUNCTION


all you would need to do is keep track of a value indicateing the number of array spaces that have been rolled (note this number can be negative too...as long as it's an integer)

say you have:

 ;just setting a value here
rolls = 45829

 ;the exact array value at that location
value1 = fruit( ABS( rolls MOD 10 ) )

 ;the array value two spaces below it, in terms of a slot machine
value2 = fruit( ABS( ( rolls + 2 ) MOD 10 ) )


;if you wish you can stick the math stuff into a function

FUNCTION fruitindex(value%)
 
RETURN ABS( value MOD 10 )

END FUNCTION


 ;then its just a matter of calling it

value1 = fruit( fruitindex( rolls ) )

value3 = fruit( fruitindex( rolls - 37 ) )
 
 ;etc..


for such a small array, actually moveing the data around isn't such a big deal...but when you get into larger arrays, and wish to "roll" them very often it can slow things down considerably.