我想从每只手中分类5张牌,首先按照牌值排序(从Ace到King),然后是卡牌套装(来自Hearts,然后是Diamonds,然后是Clubs,最后是黑桃),但它没有'工作。你怎么能成功地做到这一点?
代码:
#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"};
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 a 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; a < 52; a++)
{
printf("%s\n", deck[a]);
}
int hand,cd,winner;
int irand;
srand(time(NULL)); /* seed the random number generator */
for (hand=0;hand<5;hand++)
{
printf("Hand %i:\n",hand+1 );
for ( i = 0; i < 5; i++) {
irand = (rand() % 52);
// qsort(deck, 52, sizeof(int), compareface);
if ( (irand >= 0) && (irand <26))
printf(" %s, is Red.\n ", deck[irand]);
else
if ( (irand >= 26) && (irand <52))
printf(" %s, is Black.\n ", deck[irand]);
}
}
/* determine the winner and print it */
return 0;
}
void shuffle(card deck[52])
{
int i,rnd;
card c;
for(i=0;i<52;i++)
{
/* generate a 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)
{
/* 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;
}
答案 0 :(得分:2)
如果您为每张卡提供一个数值并将此值保存在卡座阵列中会更容易。 现在,一种方法是: 1 =黑桃王牌,2 =黑桃王牌...... 13 =黑桃王,14 =红心王牌等等,但是订购起来比较难。
然而,另一种方式是: 0 =两个钻石,1 =两个球杆,2 =两个心脏,3 =两个黑桃, 4 =三颗钻石等等。
如果您使用这种方式,(card number) / 4
会为您提供值(带偏移量),(card number) % 4
会为您提供诉讼。
此外,return (card1 number) - (card2 number)
为您提供了所需的比较功能。
希望我帮助
答案 1 :(得分:0)
正如Shay Gold所说,使用int
作为牌的价值更为简单。编辑:他的方法在这篇文章的最后描述,他的方法似乎自动在一个值排序中进行西装排序。
您可以定义0到51之间的值:
价值卡&lt; 26是黑色,其他是红色,您可以使用value / 26
获得颜色。
价值卡&lt; 13是心,值> = 13且&lt; 26是钻石,其值> = 26且&lt; 39是俱乐部,其值> = 39且&lt; 52是黑桃。你可以通过value / 13
获得诉讼。
可以使用value % 13
获取卡片的价值(从0到12)。
如果你希望Ace高于King,只需将它移到第13位的values
表中。
您可以查看以下代码:
#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"};
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; a < 52; a++) //for filling a 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 the random number generator */
// shuffle the deck before to get the hands:
shuffle(deck);
j = 0;
for (h=0;h<5;h++)
{
printf("\nHand %d:",h+1 );
for ( i = 0; i < 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 the cards by card value:
for (h=0;h<5;h++)
{
qsort(hands[h], 5, sizeof(int), compareface);
}
// print the hands:
for (h=0;h<5;h++)
{
printf("\nHand %d:",h+1 );
for ( i = 0; i < 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 the cards by card suit:
for (h=0;h<5;h++)
{
qsort(hands[h], 5, sizeof(int), comparesuit);
}
// print the hands:
for (h=0;h<5;h++)
{
printf("\nHand %d:",h+1 );
for ( i = 0; i < 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 the winner and print it */
return 0;
}
void shuffle(int deck[52])
{
int i,rnd;
int c;
for(i=0;i<52;i++)
{
/* generate a 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;
}
关于排序问题,您是否希望有两种不同的排序,或者在另一种排序中排序?例如,如果你想在一个值排序中有一个西装排序,你可以用先前代码替换第二个qsort调用,我不确定这个算法,但它似乎有效:
for (h=0;h<5;h++)
{
for ( i = 0; i < 4; i++) {
for(j = i+1; j < 5; j++) {
// if the cards have the same values but a suit higher than the following card,
// then exchange the 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;
}
}
}
}
如果您希望在诉讼类别中对值进行排序:
for (h=0;h<5;h++)
{
qsort(hands[h], 5, sizeof(int), comparesuit);
}
for (h=0;h<5;h++)
{
for ( i = 0; i < 4; i++) {
for(j = i+1; j < 5; j++) {
// if it is the same suit and the card have a higher value
// then exchange the 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方法:
#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[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; a < 52; a++) //for filling a 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 the random number generator */
// shuffle the deck before to get the hands:
shuffle(deck);
j = 0;
for (h=0;h<5;h++)
{
printf("\nHand %d:",h+1 );
for ( i = 0; i < 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 the cards by card value:
for (h=0;h<5;h++)
{
qsort(hands[h], 5, sizeof(int), compareface);
}
// print the hand:
for (h=0;h<5;h++)
{
printf("\nHand %d:",h+1 );
for ( i = 0; i < 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 the winner and print it */
return 0;
}
void shuffle(int deck[52])
{
int i,rnd;
int c;
for(i=0;i<52;i++)
{
/* generate a 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;
}