2016-02-28 66 views
0

我試圖打印出C中5張卡片的每一手中找到的對的數量,但其中1對= 2到3個相同的卡片值,並且2對= 4個相同的卡片值。我應該怎麼做才能做到這一點?如何在C中的5張牌的手中匹配相同的2-3張牌值?

代碼:

#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]); 
pairs findpairs(card *hand); /* finds any pairs in a hand */ 

int main() { 
    int deck[52]; 
    int s, c, a, i, j; 
    pairs numpairs[5], highest; 
    int hand; 

    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:\n", h + 1); 
     for (i = 0; i < 5; i++) { 
      hands[h][i] = deck[j]; 
      printf("%s of", values[hands[h][i]/4]); 
      printf(" %s", suits[hands[h][i] % 4]); 
      printf(" is %s. \n", colour[hands[h][i] % 4]); 
      j++; 
     } 
     printf("Number of pairs: %i \n", findpairs(numpairs)); 
    } 
    // sort the cards by card value: 
    for (h = 0; h < 5; h++) { 
     qsort(hands[h], 5, sizeof(int), compareface); 
    } 

    /* determine the winner and print it */ 
    return 0; 
} 

pairs findpairs(card *hand) { 
    int values[13] = { 0 }; 
    int i, numpairs = 0; 

    for (i = 0; i < 5; i++) { 
     values[hand[i] % 13] += 1; 
    } 

    for (i = 0; i < 13; i++) { 
     numpairs += values[i]/2; 
     } 
return numpairs; 
} 

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; 
    int c = 0; //c is used to count card value pairs 
    if (cd1 == cd2) { 
     c = c + 1; 
     if (c == 4) 
      findpairs(cd1 * 2); 
     else 
      findpairs(cd1 * c); 
    }  
    return cd1 - cd2; 
} 
+3

提示:如果你的代碼格式正確,人們更可能花時間閱讀並嘗試幫助你。 – szczurcio

+1

你能舉一個不起作用的例子嗎?你應該把這個例子放到你的代碼中,而不是'shuffle'函數。這樣所有人都會(希望)具有相同的確定性程序行爲。 –

回答

0

你的代碼中有幾個問題:

  • compareface通過卡號(0-51)相比,它並不能幫助找到對,您應該通過卡值排序(0-12)。
  • compareface試圖通過調用findpairs來做一些更新,但忽略返回值。這是不適合尋找配對的地方。
  • findpairs沒有做任何有用的事情:numpairs = card * hand;是語法錯誤。無論如何,返回值都會被忽略。
  • printf("Number of pairs: %i \n", numpairs);嘗試打印int,但給出指向數組numpairs的指針,這是一種類型不匹配,它調用未定義的行爲。

你並不需要手中排序來計算對數,使用for循環計數重複的數量爲每面值從結果來確定對數:

pairs findpairs(card *hand) { 
    int values[13] = { 0 }; 
    int i, numpairs = 0; 
    for (i = 0; i < 5; i++) { 
     values[hand[i] % 13] += 1; 
    } 
    for (i = 0; i < 13; i++) { 
     numpairs += values[i]/2; 
    } 
    return numpairs; 
} 
+0

@ user4728257:這個答案是否幫助你理解問題並獲得正確的值? – chqrlie

+0

我無法正確地獲得匹配對功能。 – user4728257

相關問題