Thread: Help with deal cards program.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    Help with deal cards program.

    Hi I'm making a poker game program and I need to deal 5 hands of cards(each hand has 5 cards each) in the program, each hand needs to contain different cards and we need to then display the number of pairs in each hands. I'm currently stuck in the dealing part.
    This is what I have so far for dealing card:
    Code:
    void deal(card cards[52], card hands[5][5]) {
      int i, j, cd, hd;
      int value, hi, ohi, lo;
      card shuff;
      
    for(cd=0, i=0; cd<5; cd++) {
        for(hd=0;hd<5;hd++) {
          hands[hd][cd] = cards[i];
          ++i;
        }
      }
    for(i=0;i<5;i++) {  
        ohi = 5;
        lo = 1;
    for(; lo < ohi; lo++)  {
          shuff = hands[i][lo];    
          hi = lo - 1;
          while(hands[i][hi].face > shuff.face)  {
            hands[i][hi + 1] = hands[i][hi];
            --hi;
            if(hi < 0) break;
          } 
          hands[i][hi+1] = shuff;
        }
      }
      for(i = 0; i < 5; i++) {
        printf("\nHand #%d:\n\n", i+1);
        for(j = 0; j < 5; j++) {
          printf("\t%s of %s, is %s \n ",cards[j%13].face,cards[j%13].suit,cards[j%26].color);
        }  
      }
    }
    however, the compiled result shows all the hands has the exactly same cards. I'm not sure what Ive done wrong, can anyone help pls?

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Code:
    for(cd=0, i=0; cd<5; cd++) {
        for(hd=0;hd<5;hd++) {
          hands[hd][cd] = cards[i];
          ++i;
        }
      }
    In this fragment you're putting the same cards into each hand. Did you intend them to be different at this point? EDIT: I was wrong... looking at it more now, but please still consider what I said below:

    Also, please consider choosing better names for "hd", "cd", "hi", "lo", "ohi", and maybe revise your indentation (why is "for" at the beginning of the line in 3 places?).

    I think I see the problem. When you print each hand, you're outputting from the cards array, not from each hand:

    Code:
    printf("\t%s of %s, is %s \n ",cards[j%13].face,cards[j%13].suit,cards[j%26].color);
    Last edited by Mozza314; 03-28-2011 at 11:41 PM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Quote Originally Posted by Mozza314 View Post
    Code:
    for(cd=0, i=0; cd<5; cd++) {
        for(hd=0;hd<5;hd++) {
          hands[hd][cd] = cards[i];
          ++i;
        }
      }
    In this fragment you're putting the same cards into each hand. Did you intend them to be different at this point? EDIT: I was wrong... looking at it more now, but please still consider what I said below:

    Also, please consider choosing better names for "hd", "cd", "hi", "lo", "ohi", and maybe revise your indentation (why is "for" at the beginning of the line in 3 places?).

    I think I see the problem. When you print each hand, you're outputting from the cards array, not from each hand:

    Code:
    printf("\t%s of %s, is %s \n ",cards[j%13].face,cards[j%13].suit,cards[j%26].color);
    HI thanks for the reply. Can you give me an example on how to outputting from each hand instead of from the array? Sorry Im beginner at coding and really confused now.
    Thanks again.

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by Izzy123 View Post
    HI thanks for the reply. Can you give me an example on how to outputting from each hand instead of from the array? Sorry Im beginner at coding and really confused now.
    Thanks again.
    One way to do it would be to make a function void PrintCard(card c); and do:

    Code:
    for (int i = 0; i != 5; ++i)
    {
        printf("Hand %d:\n", i + 1);
        for (int j = 0; j != 5; ++j)
            PrintCard(hands[i][j]);
    }

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Quote Originally Posted by Mozza314 View Post
    One way to do it would be to make a function void PrintCard(card c); and do:

    Code:
    for (int i = 0; i != 5; ++i)
    {
        printf("Hand %d:\n", i + 1);
        for (int j = 0; j != 5; ++j)
            PrintCard(hands[i][j]);
    }
    I have a print deck function, I try to put the function in but it gives error
    Code:
    void printdeck( const Card * const Deck )
    {
               int i;
    		   for ( i = 0; i <= 51; i++ ){
    		      printf("\t%s of %s, is %s \n ",Deck[i].face,Deck[i].suit,Deck[i].color,
    		             ( i + 1 ) % 2 ? '\t' : '\n' );}
    }
    The error says "cannot convert 'card' to 'const Card*' for argument '1' to 'void printdeck[const Card*]'
    Do I need to make another function instead of using the existing one?

    Don't worry Ive got it fixed. Working on the pairing bit now
    thanks for your help!
    Last edited by Izzy123; 03-29-2011 at 01:22 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did you intend for this to be C?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 03:22 PM
  2. Designing a program to deal out seven card stud poker hands
    By killsthehorse in forum C++ Programming
    Replies: 29
    Last Post: 12-08-2008, 04:03 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM