fill,shuffle and deal cards from a deck

Trying to make a card dealing program

Page 1 of 1

14 Replies - 7867 Views - Last Post: 08 August 2010 - 11:51 PM Rate Topic: -----

#1 Guest_wilson*


Reputation:

fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 03:09 AM

I am working on the program below which is supposed to fill adeck, shuffle it and then deal the card.I am to determine the winer.{group with highest pairs}Before that, am still stuck with making the correct display, attach the colour to each card.
NB: my place holder in printf %c to hold the color cos the right one S is just crashing the program.

My expected results are just like:
Hand 1:
Six of Spades, is Black
Seven of Diamonds, is Red
Eight of Spades, is Black
Ten of Hearts, is Red
Queen of Spades, is Black
Number of pairs: 0

Hand 2:
Three of Spades, is Black
Five of Diamonds, is Red
Five of Clubs, is Black
Nine of Diamonds, is Red
Queen of Diamonds, is Red
Number of pairs: 1
Highest pair is: Five


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct card { 
 const char *face;
 const char *suit;
 const char *color;
};

typedef struct card Card;
typedef unsigned char pairs;

void fillDeck( Card * const, const char *[], const char *[] ,const char *[]);
void shuffle( Card * const );
void print( const Card * const );
pairs findpairs(card *hand);  /* finds any pairs in a hand */

int main()
{
int hand,cd,winner;
card hands[5][5],handssorted[5][5];
pairs numpairs[5],highest;
Card deck[52];
           const char *face[] = { "Ace", "Deuce", "Three", "Four", "Five","Six", "Seven",
                                  "Eight", "Nine", "Ten","Jack", "Queen", "King"};
		   const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
		   const char *color[]= {"Black","Red"};
		   srand( time( NULL ) );
		   fillDeck( deck, face, suit, color );
		   print( deck );
		   printf("\n ----------------------------------------------------------\n");
		   shuffle( deck );
		   print( deck );
		   for(cd=0;cd<5;cd++)
  {
     
  }

  for(hand=0;hand<5;hand++)
  {
     /* sort the hands here */
     numpairs[hand]=findpairs(handssorted[hand]);
     printf("Hand %i:\n",hand+1);
    //for hands  */
    //for pairs
  }

  /* determine the winner and print it */
system("pause");
return 0;

}
//------------------------------------------------------------------------------------------------------
void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[], const char * wColor[])
{
		   int i;
		   for ( i = 0; i <= 51; i++ ) { 
		      wDeck[i].face  = wFace[ i % 13 ];
		      wDeck[i].suit  = wSuit[ i / 13 ];
		      wDeck[i].color = wColor[i%2];
		  //    if ()
//		      wDeck[i].suit = wSuit[ i / 13 ];
        }
}
//-----------------------------------------------------------------------------------------------------
void shuffle( Card * const wDeck )
{
		   int i, j;
		   Card temp;
		   for ( i = 0; i <= 51; i++ ) { 
		      j = rand() % 52;
		      temp = wDeck[ i ];
		      wDeck[ i ] = wDeck[ j ];
		      wDeck[ j ] = temp;
}
}
//-----------------------------------------------------------------------------------------------------------
void print( const Card * const wDeck )
{
           int i;
		   for ( i = 0; i <= 51; i++ ){
		      printf( "\t%s\t of \t%-8s is \t% \n \t", wDeck[i].face, 
		              wDeck[i].suit,wDeck[i].color,
		             ( i + 1 ) % 2 ? '\t' : '\n' );}
}
//------------------------------------------------------------------------------------------------------------
pairs findpairs(card *hand)
{
   pairs numpairs=0;  
  //pairs will delt from here 
   
   return numpairs;

}




Is This A Good Question/Topic? 0

Replies To: fill,shuffle and deal cards from a deck

#2 Guest_wilson*


Reputation:

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 03:38 AM

Some how I got it working with the colour. I am now having an issue with pairing the cards.
- I want to place 5 card in each hand. Which I did with my hand array.
-Pair them(Pairs are cards with the same value.)e.g Ace of Hearts & Ace of Spades make a pair.
I then count those pairs. hand with highest pairs wins.

This was what I was trying for the pairing but.. am still rellyy stuck with how I start the comparison to make the pairing.
pairs findpairs(card *hand)
{
   pairs numpairs=0;  
   for ( i = 0; i <= 51; i++ ){
       if (hand[i].face == )
       
       }
     
   return numpairs;

}


Was This Post Helpful? 0

#3 Guest_wilson*


Reputation:

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 02:51 PM

Can anyone help me how I can get this program working:

Error 122 no match for 'operator&' in 'cd1 & 60'

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct card { 
 const char *face;
 const char *suit;
 const char *color;
};

typedef struct card Card;
typedef unsigned char pairs;

void fillDeck( Card * const, const char *[], const char *[] ,const char *[]);
void shuffle( Card * const );
void print( const Card * const );
pairs findpairs(card *hand);  /* finds any pairs in a hand */

int main()
{
int hand,cd,winner;
card hands[5][5],handssorted[5][5];
pairs numpairs[5],highest;
Card deck[52];
           const char *face[] = { "Ace", "Two", "Three", "Four", "Five","Six", "Seven",
                                  "Eight", "Nine", "Ten","Jack", "Queen", "King"};
		   const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
		   const char *color[]= {"Black","Red"};
		   srand( time( NULL ) );
		   fillDeck( deck, face, suit, color );
		   print( deck );
		   printf("\n ----------------------------------------------------------\n");
		   shuffle( deck );
		   print( deck );
     int i=0;
for(cd=0;cd<5;cd++)
  {
     for(hand=0;hand<5;hand++)
     {
          hands[hand][cd]=deck[i];
     i++;
        /* deal the hands here */
     }
  }

for(hand=0;hand<5;hand++)
  {
     /* sort the hands here */
     //qsort(hands[hand],5,sizeof(card),compareface());
     numpairs[hand]=findpairs(handssorted[hand]);
     printf("Hand %i:\n",hand+1);
     /* print the hands here */
     /* print the number and value of any pairs here */
  }

  /* determine the winner and print it */
system("pause");
return 0;

}
//------------------------------------------------------------------------------------------------------
void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[], const char * wColor[])
{
		   int i;
		   for ( i = 0; i <= 51; i++ ) { 
		      wDeck[i].face  = wFace[ i % 13 ];
		      wDeck[i].suit  = wSuit[ i / 13 ];
		      wDeck[i].color = wColor[i%2];
		  //    if ()
//		      wDeck[i].suit = wSuit[ i / 13 ];
        }
}
//-----------------------------------------------------------------------------------------------------
void shuffle( Card * const wDeck )
{
		   int i, j;
		   Card temp;
		   for ( i = 0; i <= 51; i++ ) { 
		      j = rand() % 52;
		      temp = wDeck[ i ];
		      wDeck[ i ] = wDeck[ j ];
		      wDeck[ j ] = temp;
}
}
//-----------------------------------------------------------------------------------------------------------
void print( const Card * const wDeck )
{
           int i;
		   for ( i = 0; i <= 51; i++ ){
		      printf( "\t%s\t of \t%-8s is \t%s \n \t", wDeck[i].face, 
		              wDeck[i].suit,wDeck[i].color,
		             ( i + 1 ) % 2 ? '\t' : '\n' );}
}
//------------------------------------------------------------------------------------------------------------
pairs findpairs(card *hand)
{
   pairs numpairs=0;  
   for ( int i = 0; i <= 5; i++ ){
       //if (hand[i].face == )
       
       
       }
     
   return numpairs;

}
//-----------------------------------------------------------------------------
int compareface(const void* c1, const void *c2)
{
/* This function extracts the two cards face values
   and returns 1 if cd1 > cd2, 0 if cd1 == cd2, and
   -1 otherwise. The weird argument types are for
   compatibility with qsort(), the first two lines
   decode the arguments back into "card".
   */
    card cd1,cd2;
    
    cd1=*((card*) c1);
    cd2=*((card*) c2);
    
    cd1= (cd1&0x3c)>>2;
    cd2= (cd2&0x3c)>>2;
    
    if(cd1>cd2)
      return 1;
    if(cd1==cd2)
      return 0;
      
    return -1;
}



Was This Post Helpful? 0

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 03:14 PM

What do you expect this line to do?

cd1= (cd1&0x3c)>>2;

Was This Post Helpful? 0
  • +
  • -

#5 Guest_wilson*


Reputation:

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 03:18 PM

I was trying to use it to extracts the cards face values so that I can compare them.
Was This Post Helpful? 0

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 03:22 PM

So tell us how what you wrote does the above. Explain cd1&0x3c to us.

I don't think you can. That's because you copy pasted someone else's code. Whose code was broken, by the way, but still, this isn't your code.
Was This Post Helpful? 1
  • +
  • -

#7 Guest_wilson*


Reputation:

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 03:27 PM

The honest fact is that , the code was provided in a class so that we fix it. Have done my best for the last to days to fill the deck and shuffle it.That part of the cord was already given.Theoretically I understand that it supposed to extract card values .I am trying to learn how it all works from first principles.
Was This Post Helpful? 0

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 03:50 PM

View Postwilson, on 08 August 2010 - 02:27 PM, said:

The honest fact is that , the code was provided in a class so that we fix it. Have done my best for the last to days to fill the deck and shuffle it.That part of the cord was already given.Theoretically I understand that it supposed to extract card values .I am trying to learn how it all works from first principles.

Ok...

What is cd1?
Was This Post Helpful? 0
  • +
  • -

#9 Guest_wilson*


Reputation:

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 04:02 PM

Quote

What is cd1?


cd1 is a card object.
Was This Post Helpful? 0

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 04:23 PM

View Postwilson, on 08 August 2010 - 03:02 PM, said:

Quote

What is cd1?


cd1 is a card object.

how do you access the card's values?
Was This Post Helpful? 0
  • +
  • -

#11 Guest_wilson*


Reputation:

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 04:36 PM

I really not so sure
Was This Post Helpful? 0

#12 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 05:27 PM

Research structure member access.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_wilson*


Reputation:

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 06:10 PM

Thanks
Was This Post Helpful? 0

#14 Guest_wilson*


Reputation:

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 08:30 PM

I seem to have failed matching the cards in the deck.Can anyone give me a hand. My latest code, now executes and deals the cards. but I cant do the pairing, let alone counting them to declare a winner.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#pragma warn -aus

struct card { 
 const char *face;
 const char *suit;
 const char *color;
};

typedef struct card Card;
typedef unsigned char pairs;

int compareface(const void *c1,const void *c2);
void fillDeck( Card * const, const char *[], const char *[] ,const char *[]);
void shuffle( Card * const );
void print( const Card * const );
void deal(card cards[52], card hands[5][5]);
pairs findpairs(card *hand);  /* finds any pairs in a hand */


      const char *face[] = { "Ace", "Two", "Three", "Four", "Five","Six", "Seven",
                                  "Eight", "Nine", "Ten","Jack", "Queen", "King"};
      const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
      const char *color[]= {"Black","Red"};

int main()
{
int hand,cd,winner;
card hands[5][5],handssorted[5][5];
pairs numpairs[5],highest;
Card deck[52];
		   srand( time( NULL ) );
		   fillDeck( deck, face, suit, color );
		   print(deck);
		   printf("\n ---------------------- Shuffled Cards -------------------------\n\n");
		   shuffle(deck);
		   print(deck);
     int i=0;
printf("\n ---------------------- delt hands -------------------------\n\n");

deal(deck,handssorted);

//for(cd=0;cd<5;cd++)
//  {}
//     for(hand=0;hand<5;hand++)
//     {
//          hands[hand][cd]=deck[i];
//          //i++;
//        /* deal the hands here */
//         /* sort the hands here */
//        qsort(hands[hand],5,sizeof(card),compareface);
//        numpairs[hand]=findpairs(handssorted[hand]);
//        printf("Hand %i:\n",hand+1);
//     /* print the number and value of any pairs here */
//     }
//        
//  
//
//for(hand=0;hand<5;hand++)
//  {
//     /* sort the hands here */
//     //qsort(hands[hand],5,sizeof(card),compareface());
//     //numpairs[hand]=findpairs(handssorted[hand]);
//     //printf("Hand %i:\n",hand+1);
//     /* print the hands here */
//     /* print the number and value of any pairs here */
//  }
//
//  /* determine the winner and print it */
system("pause");
return 0;

}
//------------------------------------------------------------------------------------------------------
void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[], const char * wColor[])
{
		   int i;
		   for ( i = 0; i <= 51; i++ ) { 
		      wDeck[i].face  = wFace[ i % 13 ];
		      wDeck[i].suit  = wSuit[ i / 13 ];
		      wDeck[i].color = wColor[i%2];
		      
	      // for(wface=0;wface<13;wface++)
//             {
//             wDeck[i]=suit|(face<<2);/* combine suit and value here */                        
//             if(suit<2)
//               deck[i]|=0x40;
//                
//                /*card is red, so do red stuff */                         

        }
}
//-----------------------------------------------------------------------------------------------------
void shuffle( Card * const wDeck )
{
		   int i, j;
		   Card temp;
		   for ( i = 0; i <= 51; i++ ) { 
		      j = rand() % 52;
		      temp = wDeck[ i ];
		      wDeck[ i ] = wDeck[ j ];
		      wDeck[ j ] = temp;
}
}
//-----------------------------------------------------------------------------------------------------------
void print( const Card * const wDeck )
{
           int i;
		   for ( i = 0; i <= 51; i++ ){
		      printf("%s\t of  %-8s is %s \n ",wDeck[i].face,wDeck[i].suit,wDeck[i].color,
		             ( i + 1 ) % 2 ? '\t' : '\n' );}
}
//------------------------------------------------------------------------------------------------------------
pairs findpairs(card *hand)
{
   pairs numpairs=0;  
   for ( int i = 0; i <= 5; i++ ){
       //if (hand[i].face == )
       
       
       }
     
   return numpairs;

}
//-----------------------------------------------------------------------------

int compareface(const void* c1, const void *c2)
{
/* This function extracts the two cards face values
and returns 1 if cd1 > cd2, 0 if cd1 == cd2, and
-1 otherwise. The weird argument types are for
compatibility with qsort(), the first two lines
decode the arguments back into "card".
*/
card cd1,cd2;

cd1=*((card*) c1);
cd2=*((card*) c2);

//cd1.face= (cd1&0x3c)>>2;
//cd2= (cd2&0x3c)>>2;
//
//if(cd1>cd2)
//return 1;
//if(cd1==cd2)
//return 0;

return -1;
}
//-----------------------dealing the card------------------------------------------
void deal(card cards[52], card hands[5][5]) {
  int i, j, cd, hd;
  int value, hi, ohi, lo;
  card temp;
  

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++) {   /* sort the hands here */
    ohi = 5;
    lo = 1;
for(; lo < ohi; lo++)  {
      temp = hands[i][lo];     //value = A[lo];
      hi = lo - 1;
      while(hands[i][hi].face > temp.face)  {
        hands[i][hi + 1] = hands[i][hi];
        --hi;
        if(hi < 0) break;
      } 
      //A[hi + 1] = value;
      hands[i][hi+1] = temp;
    }
  }
//printf("\nHow's the sorting?\n");
  for(i = 0; i < 5; i++) {
    printf("\nHand #%d:\n\n", i+1);
    for(j = 0; j < 5; j++) {
      printf("%s\t of  %-8s is %s \n ",cards[j%13].face,cards[j%13].suit,cards[j%13].color);
    }  
    printf("\nNumber of Pairs : %d\n\n", i);
  }
}



Was This Post Helpful? 0

#15 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: fill,shuffle and deal cards from a deck

Posted 08 August 2010 - 11:51 PM

Could you point out trouble spots in the code because frankly it looks like crap.

It looks like you are using Visual Studio or a variant of it (the pragma), why not place a breakpoint at the beginning and step through the code line at a time until you figure out exactly where something is going wrong.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1