Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: LUniqueDan on Sat 16/07/2011 19:47:42

Title: Fisher–Yates shuffle or any other kind of shuffle [SOLVED]
Post by: LUniqueDan on Sat 16/07/2011 19:47:42
Hi guys !

This is probably a school-case question for programmer to-be. Can someone script me a function who randomly shuffle 4 numbers (817, 818, 819, 820)
and store the result to A[0], A[1], A[2], A[3].

I can't. Really, I just can't. I was about to manually script all possible combinations but it's... kinda long... and messy... and ... you know I'm... kinda... lazy.

Thanks !
Title: Re: Fisher–Yates shuffle or any other kind of shuffle
Post by: Khris on Sat 16/07/2011 19:54:32
int A[4], s[4];

void Shuffle(int n1, n2, n3, n4) {
  s[0] = n1; s[1] = n2; s[2] = n3; s[3] = n4;

  int i;
  while (i < 4) { A[i] = -1; i++; }

  i = 0;
  bool exit;
  while (i < 4) {
    exit = false;
    while (!exit) {
      rr = Random(3);
      if (A[rr] == -1) exit = true;
    }
    A[rr] = s[i];
    i++;
  }
}


Now call:   Shuffle(817, 818, 819, 820);

This should do it. Not tested!
Title: Re: Fisher–Yates shuffle or any other kind of shuffle
Post by: LUniqueDan on Sat 16/07/2011 21:00:26
YEAH!  :D As usual, thanks a lot  Khrismuc ! Fast- Fine - Reliable. Everything works as predicted! (You just forgot to define rr... but you know...)

Here how I implement it for further reference :


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function SET_DOORS()
{
int A[4], s[4];

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++ DEFINE SPRITE NUMBERS +++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
s[0] = 816;
s[1] = 817;
s[2] = 818;
s[3] = 819;

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++ KHRISMUC'S COOL SHUFFLE ROUTINE +++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

int i;
int rr; // KHRIS FORGET THIS :P

while (i < 4) { A[i] = -1; i++; }

i = 0;
bool exit;
while (i < 4)
  {
  exit = false;
  while (!exit)
      {
      rr = Random(3);
      if (A[rr] == -1) exit = true;
      }
  A[rr] = s[i];
  i++;
  }
 
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
// +++ SET DOORS SPRITES +++   
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
oDoor1.Graphic = A[0];   
oDoor2.Graphic = A[1]; 
oDoor3.Graphic = A[2]; 
oDoor4.Graphic = A[3];   

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Cheerz!

Dan