sorting - How to sort the cards in each hand from the card values first and then the suits in C? -
i want sort 5 cards each hand, sorting card value first (from ace king) , card suit (from hearts, diamonds, clubs , lastly spades), doesn't work. how able successfully?
code:
#include <stdio.h> #include <stdlib.h> #include <time.h> /* handy typedefs */ typedef unsigned char card; typedef unsigned char pairs; /* arrays 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"}; int compareface(const void* c1,const void *c2); int main() { card deck[52][24],*deckp; int s, c, a; for(s = 0; s < 4; s++)//for filling deck of 52 cards { for(c = 0; c < 13; c++) { if (s== 0 || s== 1) sprintf(deck[s * 13 + c], "%s of %s", values[c], suits[s]); else if (s== 2 || s== 3) sprintf(deck[s * 13 + c], "%s of %s", values[c], suits[s]); } } for(a = 0; < 52; a++) { printf("%s\n", deck[a]); } int hand,cd,winner; int irand; srand(time(null)); /* seed random number generator */ (hand=0;hand<5;hand++) { printf("hand %i:\n",hand+1 ); ( = 0; < 5; i++) { irand = (rand() % 52); // qsort(deck, 52, sizeof(int), compareface); if ( (irand >= 0) && (irand <26)) printf(" %s, red.\n ", deck[irand]); else if ( (irand >= 26) && (irand <52)) printf(" %s, black.\n ", deck[irand]); } } /* determine winner , print */ return 0; } void shuffle(card deck[52]) { int i,rnd; card c; for(i=0;i<52;i++) { /* generate random number between 0 & 51 */ rnd=rand() * 52.0 / rand_max; c = deck[i]; deck[i] = deck[rnd]; deck[rnd] = c; } } int compareface(const void* c1, const void *c2) { /* function extracts 2 cards face values , returns 1 if cd1 > cd2, 0 if cd1 == cd2, , -1 otherwise. weird argument types compatibility qsort(), first 2 lines decode arguments "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; }
as shay gold said, simpler use int
value of cards. edit: method described @ end of post, method seems automatically make suit sort inside value sort.
you can define values 0 51:
cards of value < 26 black , others red, can colour
value / 26
.cards of value < 13 hearts, value >= 13 , < 26 diamonds, value >= 26 , < 39 clubs, value >= 39 , < 52 spades. can suit
value / 13
.the value(from 0 12) of card can got
value % 13
.
if want ace higher king, move in table values
@ 13th place.
you can have @ following code:
#include <stdio.h> #include <stdlib.h> #include <time.h> /* handy typedefs */ typedef unsigned char card; typedef unsigned char pairs; /* arrays 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"}; int compareface(const void * c1, const void * c2); int comparesuit(const void * c1, const void * c2); void shuffle(int deck[52]); int main() { int deck[52]; int s, c, a, i, j; for(a = 0; < 52; a++) //for filling deck of 52 cards { deck[a] = a; printf("\n%s:", colour[ deck[a] / 26 ]); printf(" %s of", values[ deck[a] % 13 ]); printf(" %s", suits[ deck[a] / 13 ]); } int hands[5][5],h,cd,winner; int irand; srand(time(null)); /* seed random number generator */ // shuffle deck before hands: shuffle(deck); j = 0; (h=0;h<5;h++) { printf("\nhand %d:",h+1 ); ( = 0; < 5; i++) { hands[h][i] = deck[j]; printf("\n (%s)", colour[ hands[h][i] / 26 ]); printf(" %s of", values[ hands[h][i] % 13 ]); printf(" %s", suits[ hands[h][i] / 13 ]); j++; } } // sort cards card value: (h=0;h<5;h++) { qsort(hands[h], 5, sizeof(int), compareface); } // print hands: (h=0;h<5;h++) { printf("\nhand %d:",h+1 ); ( = 0; < 5; i++) { printf("\n (%s)", colour[ hands[h][i] / 26 ]); printf(" %s of", values[ hands[h][i] % 13 ]); printf(" %s", suits[ hands[h][i] / 13 ]); } } // sort cards card suit: (h=0;h<5;h++) { qsort(hands[h], 5, sizeof(int), comparesuit); } // print hands: (h=0;h<5;h++) { printf("\nhand %d:",h+1 ); ( = 0; < 5; i++) { printf("\n (%s)", colour[ hands[h][i] / 26 ]); printf(" %s of", values[ hands[h][i] % 13 ]); printf(" %s", suits[ hands[h][i] / 13 ]); } } /* determine winner , print */ return 0; } void shuffle(int deck[52]) { int i,rnd; int c; for(i=0;i<52;i++) { /* generate random number between 0 & 51 */ rnd=rand() * 52.0 / rand_max; c = deck[i]; deck[i] = deck[rnd]; deck[rnd] = c; } } int compareface(const void * c1, const void * c2) { const int cd1 = *(const int*)c1; const int cd2 = *(const int*)c2; if(cd1 % 13 > cd2 % 13) return 1; if(cd1 % 13 == cd2 % 13) return 0; return -1; } int comparesuit(const void * c1, const void * c2) { const int cd1 = *(const int*)c1; const int cd2 = *(const int*)c2; if(cd1 / 13 > cd2 / 13) return 1; if(cd1 / 13 == cd2 / 13) return 0; return -1; }
about sorting problem, want have 2 differents sorts, or have sort inside sort ? example, if want have suit sort inside value sort, can replace second qsort call in precedent code with, i'am not sure of algorithm, seems work:
for (h=0;h<5;h++) { ( = 0; < 4; i++) { for(j = i+1; j < 5; j++) { // if cards have same values suit higher following card, // exchange cards: if( (hands[h][i] / 13 > hands[h][j] / 13) && (hands[h][i] % 13 == hands[h][j] % 13) ) { c = hands[h][i]; hands[h][i] = hands[h][j]; hands[h][j] = c; } } } }
if want values sort inside suit sort:
for (h=0;h<5;h++) { qsort(hands[h], 5, sizeof(int), comparesuit); } (h=0;h<5;h++) { ( = 0; < 4; i++) { for(j = i+1; j < 5; j++) { // if same suit , card have higher value // exchange cards: if( (hands[h][i] % 13 > hands[h][j] % 13) && (hands[h][i] / 13 == hands[h][j] / 13) ) { c = hands[h][i]; hands[h][i] = hands[h][j]; hands[h][j] = c; } } } }
shay gold method:
#include <stdio.h> #include <stdlib.h> #include <time.h> /* handy typedefs */ typedef unsigned char card; typedef unsigned char pairs; /* arrays 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[4]= {"red","red","black","black"}; int compareface(const void * c1, const void * c2); void shuffle(int deck[52]); int main() { int deck[52]; int s, c, a, i, j; for(a = 0; < 52; a++) //for filling deck of 52 cards { deck[a] = a; printf("\n%s:", colour[ deck[a] % 4 ]); printf(" %s of", values[ deck[a] / 4 ]); printf(" %s", suits[ deck[a] % 4 ]); } int hands[5][5],h,cd,winner; int irand; srand(time(null)); /* seed random number generator */ // shuffle deck before hands: shuffle(deck); j = 0; (h=0;h<5;h++) { printf("\nhand %d:",h+1 ); ( = 0; < 5; i++) { hands[h][i] = deck[j]; printf("\n (%s)", colour[ hands[h][i] % 4 ]); printf(" %s of", values[ hands[h][i] / 4 ]); printf(" %s", suits[ hands[h][i] % 4 ]); j++; } } // sort cards card value: (h=0;h<5;h++) { qsort(hands[h], 5, sizeof(int), compareface); } // print hand: (h=0;h<5;h++) { printf("\nhand %d:",h+1 ); ( = 0; < 5; i++) { printf("\n (%s)", colour[ hands[h][i] % 4 ]); printf(" %s of", values[ hands[h][i] / 4 ]); printf(" %s", suits[ hands[h][i] % 4 ]); } } /* determine winner , print */ return 0; } void shuffle(int deck[52]) { int i,rnd; int c; for(i=0;i<52;i++) { /* generate random number between 0 & 51 */ rnd=rand() * 52.0 / rand_max; c = deck[i]; deck[i] = deck[rnd]; deck[rnd] = c; } } int compareface(const void * c1, const void * c2) { const int cd1 = *(const int*)c1; const int cd2 = *(const int*)c2; return cd1 - cd2; }
Comments
Post a Comment