4 Replies - 910 Views - Last Post: 20 March 2013 - 10:48 PM Rate Topic: -----

#1 larrylive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-May 11

Can anyone tell me how to sort a card c programming

Posted 20 March 2013 - 09:54 AM

Need help with sorting a card deck of 52, and finding the winner with the highest pair.

Hand 1:
Three of Clubs, is Black
Five of Hearts, is Red
Seven of Spades, is Black
Queen of Clubs, is Black
King of Spades, is Black
Number of pairs: 0
Hand 2:
Three of Diamonds, is Red
Five of Diamonds, is Red
Seven of Hearts, is Red
Queen of Diamonds, is Red
King of Hearts, is Red
Number of pairs: 0
Hand 3:
Ace of Hearts, is Red
Two of Clubs, is Black
Seven of Diamonds, is Red
Nine of Spades, is Black
Jack of Clubs, is Black
Number of pairs: 0
Hand 4:
Ace of Clubs, is Black
Five of Clubs, is Black
Nine of Diamonds, is Red
Jack of Diamonds, is Red
King of Clubs, is Black
Number of pairs: 0
Hand 5:
Three of Hearts, is Red
Four of Hearts, is Red
Ten of Hearts, is Red
Queen of Hearts, is Red
King of Diamonds, is Red
Number of pairs: 0
Drawn game

here is what i have done so far

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

/* handy typedefs */
typedef unsigned char card;
typedef unsigned char pairs;


/* arrays for the names of things */
static char *suits[4] = {"Hearts","Diamonds","Clubs","Spades"};
static char *values[13]= {"Ace","Two","Three","Four","Five","Six",\
                        "Seven","Eight","Nine","Ten","Jack",\
                        "Queen","King"};
static char *colour[2]= {"Black","Red"};

/* function prototypes */
void printcard(card c); /* Displays the value of a card*/

void printdeck(card deck[52]); /* prints an entire deck of cards*/

void filldeck(card deck[52]); /* Populates a deck of cards */

void shuffle(card deck[52]); /* Randomizes the order of cards */

int compareface(const void* c1,const void *c2); 
/* compares the face value of 2 cards, suitable to pass to qsort
   as the fourth argument */

pairs findpairs(card *hand); /* finds any pairs in a hand */

int main()
{

  card deck[52],*deckp;   /* = {64, 39, 112} */
  card hands[5][5],handssorted[5][5];
  pairs numpairs[5],highest;
  int hand,i,cd,winner;
  
  srand(time(NULL));       /* seed the random number generator */

  /* remove this to get your program to work */
 /*  printf("You forgot to remove the stub to mask the fact\n");
  printf("that the example code does nothing!!\n");
  exit(0); */

	
  /*populate and shuffle the deck */
  filldeck(deck);
  printdeck(deck);
  shuffle(deck);
  printdeck(deck);
  i=0;
  
  for(cd=0; cd<5; cd++)
  {
     for(hand=0;hand<5;hand++)
     { 
	 /* deal the hands here */
     	hands[hand][cd] = deck[i];
     	i++;
     }
     
     /*printf("\n");*/
  }

  for(hand=0;hand<5;hand++)
  {
  	
  /* sort the hands here */
  int num=0, x=0, y=0;
  
     numpairs[hand]=findpairs(handssorted[hand]);

     printf("\n Hand %i: \n",hand+1);
     /* print the hands here */
 
     	for(y = 0; (y < 5); y++)
     	{
     		printcard(handssorted[hand][y]);
     	}
     	
    /* for(int i = 0; i != 5, i++){
     	printf("hand %d: \n", i + 1);
     }*/
     
     /* print the number and value of any pairs here */
  }
   getch();
  /* determine the winner and print it */
  return 0;
}

pairs findpairs(card *hand)
{
   pairs numpairs=0;
   int num;
   int y=0, x=0;
   
   
   for(x=0; x<5; x++)
   {
   	for(y=0; y<4; y++)
   	{
   		num=compareface(&hand [x], &hand [y]);
   		if(num = 0)
   		{
   			numpairs++;
   		}
   	}
   }  
  
   return numpairs;

}


void filldeck(card deck[52])
{
/* populate the deck here */

printf("Non-Suffled Deck \n");
printf("---------------- \n");

	int ctr= 0;
	int i=0;
	
       for ( i =0; i<13; i++)  
	   {
	   	 // heart is red
	   	deck[ctr] = ((i<<1)|0x01) | 0x01;
	   	
	   	ctr++;
	   }
	   
	    for ( i =0; i<13; i++)  
	   {
	   	 // diamond is red
	   	deck[ctr] = ((i<<1)|0x01) | 0x21;
	   	
	   	ctr++;
	   }
	   
	     for ( i =0; i<13; i++)  
	   {
	   	 // club is black
	   	deck[ctr] = (i<<1) | 0x40;
	   	
	   	ctr++;
	   }
	   
	    for ( i =0; i<13; i++)  
	   {
	   	 // spade is black
	   	deck[ctr] = (i<<1) | 0x60;
	   	
	   	ctr++;
	   }
	   return;
}	    
	   
void printdeck(card deck[52])
{
   int i;
   for(i=0;i<52;i++)
      printcard(deck[i]);
   return;
}

void printcard(card c)
	
{
	/* print the value of the card here */
	printf("%s of %s, is %s \n", values[(c&0x1e)>>1], suits[(c&0x60)>>5], colour[(c&0x1)] );
   
   return;
}

void shuffle(card deck[52])
{
	printf("\nShuffled Deck \n");
	printf("--------------\n");
   int i,rnd;
   card c;
   
   for(i=0;i<52;i++)
   {
      /* generate a random number between 0 & 51 */
      rnd=rand() * 52.0 / RAND_MAX;
	  /* finish shuffling the deck here */
	  c=deck[i]; 
	  deck[i]=deck[rnd];
	  deck[rnd]=c;
   }
   
   return;
}

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&0x1e)>>1;
    cd2= (cd2&0x1e)>>1;
    
    if(cd1>cd2)
      return 1;
    if(cd1==cd2)
      return 0;
      
    return -1;
}
 



Is This A Good Question/Topic? 0
  • +

Replies To: Can anyone tell me how to sort a card c programming

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Can anyone tell me how to sort a card c programming

Posted 20 March 2013 - 10:17 AM

If you wrote all that code, you probably wouldn't need help sorting...

Of course, if you copied it from somewhere: http://units.scm.uws.../labA.example.c

Step 1, write some code to compare a pair. The code you ripped off already has an example of a comparison function, so you could work from there.
Was This Post Helpful? 0
  • +
  • -

#3 larrylive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-May 11

Re: Can anyone tell me how to sort a card c programming

Posted 20 March 2013 - 10:22 AM

View Postbaavgai, on 20 March 2013 - 10:17 AM, said:

If you wrote all that code, you probably wouldn't need help sorting...

Of course, if you copied it from somewhere: http://units.scm.uws.../labA.example.c

Step 1, write some code to compare a pair. The code you ripped off already has an example of a comparison function, so you could work from there.

That's just the code guide.We are asked to fix it, and i have done a fair bit.There is a lot of difference in it if you compare both codes.Just confused on how to sort out the hand, and get the winner
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Can anyone tell me how to sort a card c programming

Posted 20 March 2013 - 03:55 PM

Again, how do you figure out which pair is less than another pair. You need only answer that one question, really.
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,507
  • Joined: 19-February 09

Re: Can anyone tell me how to sort a card c programming

Posted 20 March 2013 - 10:48 PM

Looks like you copy the hand to the handsorted array, and then use the C library function qsort.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1