Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 13th, 2004, 08:09 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default 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
Reply With Quote
  #2 (permalink)  
Old May 13th, 2004, 07:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, there's obviously some code missing. You don't have enough of your main() shown -- the curly braces don't match up before you define (implement) your deck functions.

Also, it appears you're programming in C, not C++. If you were programming in C++, you should have a Deck object that makes use of some sort of List container object for holding your Card objects. On to business:


You are mixing up variable names with 'numshuffle' and 'numbershuffle'.

This code makes little sense:
Quote:
quote:
   while (numshuffle < 0)
   {
      if(numbershuffle < 0)
      {
         printf("Error: negative");
      }
      else
      {
         shuffleDeck(list[0], numshuffle); // calls splitDeck and mergeDeck
      }
   }
... for a couple reasons. First of all, if numshuffle is negative, then it the while loop will NOT execute, so your if() statement is clearly never going to be executed. It's impossible. Next, you don't ever modify numshuffle, so your loop will iterate infinitely.


Now let's take a look at your deck manipulation algorithms.

Splitting a deck should be a simple matter of identifying the first card of each half of the deck. It shouldn't really require a lot of mathematics. After shuffling the deck, your first card shouldn't change. (That's why dealers have to cut the deck, by the way.) Cutting the deck splits the deck at some random place, and puts the top half on the bottom. Note that this deck split is different than what's required for a perfect shuffle -- a perfect shuffle should divide the deck into equal halves.


If you look through your code, you malloc memory all over the place, and you do it inside of loops (so you do it a lot). You never, however FREE this memory.

You're always dealing with the same deck, so why are you continually allocating memory for new cards? You don't ever create new cards, you only reorganize the ones that already exist. There should be NO malloc involved.


Is it your assignment to learn how this program works, or to write your own??


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #3 (permalink)  
Old May 13th, 2004, 10:49 PM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default


The code was written in C by someone else. Its not my homework
I just dont know how the functions split and merge deck work.

In splitDeck there has to code to traverse the list, that is, get to the midpoint in the list.
Once i am 26 in the list, then I am at the *end* of one half, and the 27th is the *start* of the second half.

Since a pointer to the list is the first element of it, store the 27th as a new list.

Also remember that the last element of a list should have next == NULL and the first should have prev == NULL.

The trouble is how to do it

If you look in the splitDeck() function, he writes
d[0]->prev = d[25];
d[25]->next = d[0];
//Trying to get the first half
I have done a little coding in my splitDeck function

struct deck *splitDeck(struct deck *deck)
{
  struct deck *deck2;
  enum suiteType s;
  enum valueName v;

..
  for(s = Heart; s <= Club; s++)
  {
    for(v = Ace; v < Two; v++)
    {
     ....
    }
  }
return ..&&..;
}

gbilios
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Lists rickyoung Pro Visual Basic 2005 0 November 24th, 2006 09:04 AM
Linked Dropdown Lists contagiouss_blue Beginning VB 6 1 June 8th, 2005 10:35 AM
Linked Drop Down Lists contagiouss_blue Excel VBA 6 June 8th, 2005 09:02 AM
linked lists vs collections jfancy Pro VB.NET 2002/2003 2 June 29th, 2004 10:04 PM
Lists Martyn Forum and Wrox.com Feedback 9 June 6th, 2003 11:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.