Game Development Community

Rough List Shuffler Snippet

by Rodney Rindels - Torqued · in Torque Game Builder · 06/11/2006 (6:54 pm) · 0 replies

Given a "List" (Space Seperated Group of Something), this rough shuffler will shuffle the values and give you back a roughly sorted list, this is not good enough for things like "Real Card Games where people loose real money" where you want to implement more of a Fisher-Yates shuffle. But it seems to work well for shuffling decks, dominoes, etc currently this has a 1000 item limit (Although I wouldn't try more than say 52 items)

function shuffle(%list)
{
   //Rough Shuffler, *Do not use for Accuracy needed Games.
   %objCount = getWordCount(%list);
   for (%i=0;%i<%objCount;%i++)
   {
      %r=getRandom(1000);
      while(%temp[%r])
      {
         %r=getRandom(1000);         
      }
      %temp[%r]=getWord(%list,%i);
   }
   for(%i=0;%i<1000;%i++)
   {
         if(%temp[%i])
         {    
              if(%sorted)
              {
               %sorted=%sorted SPC %temp[%i];            
              } else {
               %sorted=%temp[%i];
              }
         }
   }
   return %sorted;
}
I should note the lists I'm passing this are lists of sceneObject Id's. But should work with vectors, sentences, anything space seperated.

Enjoy!

Rodney