该程序应该对卡片进行排序,并将它们分组为5个组,称为手,但代码完全是这样做的,但由于某种原因我不太确定,我无法确定该winer。优胜者应该是在双人中显示最高的价值。如果手牌有最高的国王,而手牌3有最高的7副,那么手牌应该被宣布为赢家。使用qsort对数组进行排序
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <windows.h>
typedef unsigned char card;
typedef unsigned char pairs;
static char *suits[] = {"Hearts","Diamonds","Clubs","Spades"};
static char *values[]= {"Ace","Two","Three","Four","Five","Six",\
"Seven","Eight","Nine","Ten","Jack",\
"Queen","King"};
static char *colour[]= {"Black","Red"};
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);
pairs findpairs(card *hand); /* finds any pairs in a hand */
int findwinner(card *hand);
int main()
{
card deck[52],*deckp,*temp3,max;
card hands[5][5],handssorted[5][5];
pairs numpairs[5],highest;
int hand,cd,winner=0,temp2;
char loop;
{
printf("\n(__)(__)(__)(__) CARD GAME(__)(__)(__)(__)(__)\n\n");
printf("\n(__)(__)(__)(__)(__)(__)(__)(__)(__)(__)(__)(__)\n\n");
printf("\n\t ====== Before Shuffling =====\n\n");
srand(time(NULL)); /* seed the random number generator */
filldeck(deck);//printdeck(deck);
printdeck(deck);
printf("\n\t ========== Shuffled Cards ========\n\n");
shuffle(deck);//printdeck(deck);
printdeck(deck);
int i=0;
for(cd=0;cd<5;cd++)
{
for(hand=0;hand<5;hand++){
hands[hand][cd]=deck[i];
i++;
}
}
printf("\n\t ============= Hands ===========\n\n");
for(hand=0;hand<5;hand++)
{
qsort(hands[hand],5,sizeof(card),compareface);;
int kok=0;
printf("\tHand %i:\n",hand+1);
for(cd=0;cd<5;cd++)
{
printcard(hands[hand][cd]);
}
numpairs[hand]=findpairs(hands[hand]);
printf("\n\tNumber of pairs:%i\n\n",numpairs[hand]);
temp2=0;
if(numpairs!=0)
temp2=findwinner(hands[cd]);
}
pairs findpairs(card *hand)
{ int cd=0,dd=0;
card temp;
pairs numpairs=0;
for (cd=0;cd<4;cd++)
{
if (values[(hand[cd]&0x3c)>>2] == values[(hand[cd+1]&0x3c)>>2])
{
dd++;
temp=hand[cd];
}
switch (dd)
{
case 0:numpairs=0;
break;
case 1:numpairs=1;
break;
case 2:
if ((values[(hand[1]&0x3c)>>2] !=values[(hand[3]&0x3c)>>2])&&
(values [(hand[0]&0x3c)>>2]\
!=values[(hand[2]&0x3c)>>2])&&(values[(hand[2]&0x3c)>>2] !=values
[(hand[4]&0x3c)>>2]))numpairs=2;
else
numpairs=1;
break;
case 3:numpairs=2;
break;
case 4:numpairs=2;
break;
}}
printf("\tHighest pair is:");
if (numpairs!=0)
printf("\t%s",values[(temp&0x3c)>>2]);
return numpairs;
}
int findwinner(card *hand){
int cd=0,temp2=0,winner=0;
for (cd=0;cd<4;cd++)
{
if (values[(hand[cd]&0x3c)>>2] == values[(hand[cd+1]&0x3c)>>2])
{
// temp2=cd;
winner=cd;
}}
return winner;
}
void filldeck(card deck[52])
{
card suit,value;
int i=0;
for(suit=0;suit<4;suit++)
{
for(value=0;value<13;value++)
{
deck[i]=suit|(value<<2);
if(suit<2)
deck[i]|=0x40; /*card is red, so do red stuff */
i++;
}
}
return;
}
void printdeck(card deck[52])
{
int i;
for(i=0;i<52;i++)
printcard(deck[i]);
return;
}
void printcard(card c)
{
printf("\t%s\t of %-8s is %s\n",values[(c&0x3c)>>2],suits[c&0x3],
colour[(c&0x40) >>6]);
return;
}
void shuffle(card deck[52])
{
int i,rnd;
card c;
for(i=0;i<52;i++)
{
rnd=rand() * 52.0/RAND_MAX;
c=deck[rnd];
deck[rnd]=deck[i];
deck[i]=c;
}
return;
}
int compareface(const void* c1, const void *c2)
{
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;
}
是你通常如何格式化你的C代码? – 2010-08-15 04:36:00
也许你可以添加声明'手','甲板'以及'卡'的代码? – 2010-08-15 04:43:10