News:

A forum for users of LackeyCCG

Main Menu

Shuffling Algorithim

Started by 3XXXDDD, July 17, 2013, 07:05:32 PM

Previous topic - Next topic

3XXXDDD

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?

Trevor

#1
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.

Alastair

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.