Vous n'êtes pas identifié(e). Pour pouvoir écrire ou répondre à un message, vous devez vous connecter ou vous créer un compte sur JAWA.

#1 25-11-2018 13:15:26

spook1
Membre
Inscription : 12-02-2018

randomize array

Does anyone know a way / script to randomize an array?

I want to place cards in a puzzle game in a random order.
I have the numbers of the cards in an array.

Is there an easy way to randomize the array?

Hors ligne

#2 28-11-2018 09:07:16

jawa
Admin

Re : randomize array

Sorry I have no answer for that right now, shuffling an array is a complex thing to do !
A solution could be to create several preset distributions in a separate 2D arrays and assign one of them to your current distribution based on a  random number :

for instance :

possible_shuffles =
1|2|3~
3|1|2~
3|2|1~
2|1|3~
...
(at variable initialization)

in a "draw cards" script :

variable:temp:[_rand:0:5]  (5 = if there is 6 different shufflings presets)

variable:my_shuffling:{{possible_shuffling$*$TEMP}}

=> assign to my_shuffling array the randomized line number TEMP of the possible preset shuffles

(tell me if it works !)

#3 28-11-2018 10:54:11

spook1
Membre
Inscription : 12-02-2018

Re : randomize array

Great idea, it works.

I implemented it as follows, to shuffle four cards.

variable:DRAW:[_rand:0:5]

updateobject:25827:x:[186*{{SHUFFLE$0$DRAW}}]:0
updateobject:25811:x:[186*{{SHUFFLE$1$DRAW}}]:0
updateobject:25826:x:[186*{{SHUFFLE$2$DRAW}}]:0
updateobject:25825:x:[186*{{SHUFFLE$3$DRAW}}]:0

Hors ligne

#4 29-11-2018 14:08:41

jawa
Admin

Re : randomize array

cool

Pied de page des forums