LackeyCCG

LackeyCCG Forum => General Discussion Forum => Topic started by: 3XXXDDD on July 17, 2013, 07:05:32 PM

Title: Shuffling Algorithim
Post by: 3XXXDDD on July 17, 2013, 07:05:32 PM
Just curious what the shuffling algorithm is in LackeyCCG, is it something simple like List_Shuffle or is it more than that like Fisher-Yates?
Title: Re: Shuffling Algorithim
Post by: Trevor on July 17, 2013, 07:41:13 PM
Here it is:
      int *PositionsArray = new int [GetNumEntries()];
if(PositionsArray==NULL)
return;

for(int i=0; i<GetNumEntries(); i++)
PositionsArray[i]=i;

RandomizeArray(GetNumEntries(),PositionsArray);

ItemClass *TempEntries = new ItemClass[GetNumEntries()];
for(int i=0; i<GetNumEntries(); i++)
TempEntries[i].Set(Entries[i]);

for(int i=0; i<GetNumEntries(); i++)
Entries[i].Set(TempEntries[PositionsArray[i]]);

if(TempEntries)
delete [] TempEntries;
TempEntries=NULL;
if(PositionsArray!=NULL)
delete [] PositionsArray;
PositionsArray=NULL;


void RandomizeArray(int ArrayLength,int Array[])
{
if(ArrayLength==0)
return;
int *TempData = new int [ArrayLength];
if(TempData==NULL)
return;
int CurrentLength=0;
for(int i=0; i<ArrayLength; i++) // COPY ARRAY to TEMPARRAY
TempData[i]=Array[i];
int Pos=0;
CurrentLength=ArrayLength;
for(int i=0; i<ArrayLength; i++)
{
Pos=GenerateRandomNumber(0,CurrentLength-1); // randomly choose a position in those that remain
Array[i]=TempData[Pos]; //  copy it
for(int j=Pos; j<CurrentLength-1; j++)
TempData[j]=TempData[j+1];
CurrentLength--;  // Then remove the one you just picked from the array
}
DeleteTempData
}

int GenerateRandomNumber(int LowerInt, int UpperInt)
{
if(LowerInt==UpperInt)//If there is only one option, just return that
return LowerInt;
if(LowerInt>UpperInt)//This section assures that lower number is indeed lower
{
int TempInt=LowerInt;
LowerInt=UpperInt;
UpperInt=TempInt;
}
long int TempResult=rand();
int Result = TempResult % (UpperInt - LowerInt + 1) + LowerInt;
return Result;
}

I wrote it a while ago, and I might do things differently now, but I believe it works as intended.
Title: Re: Shuffling Algorithim
Post by: Alastair on July 17, 2013, 10:37:43 PM
For future reference Trevor or anybody really, if you need to show code in the forum don't use quote, use the code button (looks like a # symbol). It ignores forum shortcuts.