I've spent hours today trying to find a way to do this "right". All I want to do is "shuffle" a list of strings within an array without affecting other arrays.
For example:
Code:
def arrayOriginal = ["blue","red","green","yellow","orange,"pink"]
def arrayExample = arrayOriginal
First, I read about "Collections.shuffle" online but that seems to behave unexpectedly for me:
Code:
def arrayOriginal = ["blue","red","green","yellow","orange,"pink"]
def arrayExample = arrayOriginal
Collections.shuffle(arrayExample)
... seems to shuffle both arrayExample *AND* arrayOriginal, though I have no idea why!
So I tried (based on a Farkel script snippet that seemed to do what I wanted)...
Code:
def shuffleArray = {
array ->
for(int i=0; i<array.size(); i++){
int s = i+(int)(getRandom(array.size()-i))
def temp = array[s]
array[s] = array[i]
array[i] = temp
}
}
def arrayOriginal = ["blue","red","green","yellow","orange,"pink"]
def arrayExample = arrayOriginal
shuffleArray(arrayExample)
... but that ALSO still seems to shuffle arrayOriginal too!
What am I missing/screwing up here?
I've "fixed" my script by simply moving the shuffle function *after* the array is show to the player (rather than before) and this works, but I would still like to know how to do this without having to work around things if possible, to learn better coding.