[英]How to find, count and show card pairs in C, where 2 or 3 of a kind is one pair, and 4 of a kind is two pairs?
我在掙扎,不知道如何確定和顯示對數
由於卡片組中有52張卡片,您將如何查找,計算和顯示對數?
碼:
#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]= {"Red","Black"};
int compareface(const void * c1, const void * c2);
int comparesuit(const void * c1, const void * c2);
void shuffle(int deck[52]);
pairs findpairs ( card *h ) ; /* finds any pairs in a hand */
int main()
{
int deck[52];
pairs numpairs[5], highest;
int s, c, a, i, j;
j = 0;
for(s = 0; s < 4; s++)//for filling a deck of 52 cards
{
for(c = 0; c < 13; c++)
{
deck[s * 13 + c] = j;
j++;
}
}
for(a = 0; a < 52; a++)
{
printf(" %s of", values[ deck[a] % 13 ]);
printf(" %s, is ", suits[ deck[a] / 13 ]);
printf("%s\n", colour[ deck[a] / 26 ]);
}
int hands[5][5],h,cd,winner,handssorted[5][5];
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++)
{
for ( i = 0; i < 5; i++) {
hands[h][i] = deck[j];
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:\n",h+1 );
for ( i = 0; i < 5; i++) {
printf(" %s of", values[ hands[h][i] % 13 ]);
printf(" %s, is ", suits[ hands[h][i] / 13 ]);
printf("%s\n", colour[ hands[h][i] / 26 ]);
}
printf("Number of pairs: \n");
}
// sort the cards by card value then card suit:
for (h=0;h<5;h++)
{
qsort(hands[h], 5, sizeof(int), comparesuit);
numpairs[h]= findpairs( handssorted[h] );
}
// print the hand:
for (h=0;h<5;h++)
{
for ( i = 0; i < 4; i++) {
for(j = i+1; j < 5; j++) {
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;
}
}
}
}
/* determine the winner and print it */
return 0;
}
void shuffle(int deck[52])
{
int i,rnd, 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;
}
pairs findpairs ( card *h )
{
pairs numpairs=0;
/* find the pairs here */
return numpairs;
}
卡片排序后,只需比較連續的卡片就可以找到對。 在此之前,您的代碼中還有另一個問題,當您進行排序時,您是在hands [h]中對其進行排序,而對於findpairs()來說,您是在給手排序數組。 因此,請先將這些值復制到手動排序后再進行排序。
之后,您只需比較findpairs調用中的連續值即可。 找到對后,將指針增加到第三張卡。
pairs findpairs ( card *h )
{
pairs numpairs=0;
int card1, card2;
/* find the pairs here */
for(int i = 0; i<4; i++){
/* Compare the card values here */
card1 = h[i];
card2 = h[i++]; /*extract values according to your encoding scheme*/
if(card1 == card2){
numpairs++;
i++; // skip the next card to avoid counting 3 of a kind, since it is just a pair
}
return numpairs;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.