Doubly Linked Lists
I have a programm which I want to analyse. The two function's splitDeck and mergeDeck are part of a card shuffeling programm that I dont know how works. The program prompts the user the number of shuffles required and number of hands required. I am mainly concerned with the two functions just mentioned. splitDeck splits the deck in two decks and then calls dealhands.
Below is the code for the two functions. Can anyone please help?
int main()
{
int numshuffle = -1;
struct deck *list[52];
// prompt the user
scanf("%d", &numshuffle);
while (numshuffle < 0){
if(numbershuffle < 0)
{
printf("Error: negative");
}
else
{
shuffleDeck(list[0], numshuffle); // calls splitDeck and mergeDeck
}
void splitDeck(struct deck **d, struct deck **d2)
{
int i, x;
d[0]->prev = d[25]; /* set the prev and next */
d[25]->next = d[0];
for(i=2;i<4;i++)
{
for(x=0;x<13;x++)
{
d2[(i*13)+x-26] = malloc(sizeof(struct deck));
d2[(i*13)+x-26]->suit = d[(i*13)+x]->suit;
d2[(i*13)+x-26]->value = d[(i*13)+x]->value;
d2[(i*13)+x-26]->prev = d2[((i*13)+x)-27]; /* reset the prev and next, the value and malloc the deck */
}
}
for(i=2;i<4;i++)
{
for(x=0;x<13;x++) /* the deck has four suits and each suit has 13 cards, this is the for loop to determine how each card is initialised */
{
d2[((i*13)+x)-26]->next = d2[((i*13)+x)-25];
}
}
d2[0]->prev = d2[25];
d2[25]->next = d2[0];
}
void mergeDeck(struct deck **d1, struct deck **d2)
{
struct deck *tem[26]; /* define tem as a temporary deck so it can be initialsied */
int i, x;
for(i=0;i<2 ;i++)
{
for(x=0;x<13;x++)
{
tem[(i*13)+x] = malloc(sizeof(struct deck)); /* set tem, malloc it, set the suit, value and the prev, get it ready to merge again */
tem[(i*13)+x]->suit = d1[(i*13)+x]->suit;
tem[(i*13)+x]->value = d1[(i*13)+x]->value;
tem[(i*13)+x]->prev = d1[((i*13)+x)]->prev;
tem[(i*13)+x]->next = d1[((i*13)+x)]->next;
}
}
x=0;
for(i=2;i<26;i++)
{
d1[x]->suit = tem[i]->suit; /* being the merging sequence, first the suits, then the value, then the prev, combining them all */
d1[x+1]->suit = d2[i]->suit;
d1[x]->value = tem[i]->value;
d1[x+1]->value = d2[i]->value;
d1[x]->prev = d1[x-1];
d1[x+1]->prev = d1[x];
x+=2;
}
for(i=0;i<4;i++)
{
for(x=0;x<13;x++)
{
d1[(i*13)+x]->next = d1[((i*13)+x)+1];
}
}
d1[0]->prev = d1[51]; /* set again the 51 cards */
d1[51]->next = d1[0];
}
void dealHands(struct deck **d, int numberhands)
{
int i, y = 0, z = 0;
struct deck *hands[52][6];
while(z * numberhands < 52) /* say that while the number is less than 52 (number of cards) do the function */
{
if(y < numberhands)
{
hands[z][y] = malloc( sizeof( struct deck ) );
hands[z][y]->suit = d[0]->suit;
hands[z][y]->value = d[0]->value;
hands[z][y]->next = hands[z+1][y]; /* set the hands, the suits and the values to print */
d[0] = d[0]->next;
y++;
}
else
{
y = 0;
z++;
}
}
for(i = 0; i < numberhands; i++)
{
printf("\nHand %d\n", i+1); /* print Hand and the hand number it is up to */
printf("[ ");
for(z = 0; z < ((52 / numberhands) + (52 % numberhands)); z++)
{
printf("%c%c%c ", valueName[(hands[z][i]->value)-1][3], /* print the hands and the cards that are part of it */
valueName[(hands[z][i]->value)-1][4], suitName[(hands[z][i]->suit)-1][2]);
}
printf(" ]");
}
printf("\n");
}
gbilios
__________________
gbilios
|