首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C中匹配相同的2-3张牌值作为一对5张牌的牌值?

如何在C中匹配相同的2-3张牌值作为一对5张牌的牌值?
EN

Stack Overflow用户
提问于 2016-02-28 18:55:46
回答 1查看 160关注 0票数 0

我试图打印出在C中每手5张牌中找到的对数,其中1对=2到3张相同的卡片值,2对=4张相同的卡片值。我应该怎么做才能让它工作呢?

代码:

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2016-02-28 19:22:26

您的代码有几个问题:

  • compareface按卡号(0-51)比较,定位对没有帮助,您应该按卡值(0-12)排序。instead.
  • compareface尝试通过调用findpairs进行一些更新,但忽略返回值。这不是寻找pairs.
  • findpairs没有任何用处的正确地方:numpairs = card * hand;是一个语法错误。返回值被忽略anyway.
  • printf("Number of pairs: %i \n", numpairs);尝试打印int,但提供了指向数组numpairs的指针,这是类型不匹配,它会调用未定义的行为。

您不需要对指针进行排序来计算配对的数量,而是使用for循环来计算每个面值的重复数量并根据结果确定配对的数量:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35681579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档