千家信息网

C语言怎么实现发牌洗牌

发表于:2025-01-18 作者:千家信息网编辑
千家信息网最后更新 2025年01月18日,本文小编为大家详细介绍"C语言怎么实现发牌洗牌",内容详细,步骤清晰,细节处理妥当,希望这篇"C语言怎么实现发牌洗牌"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。1.模拟发
千家信息网最后更新 2025年01月18日C语言怎么实现发牌洗牌

本文小编为大家详细介绍"C语言怎么实现发牌洗牌",内容详细,步骤清晰,细节处理妥当,希望这篇"C语言怎么实现发牌洗牌"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

1.模拟发牌程序

通过写两个函数模块来编写洗牌程序和打印的发牌程序

2.编写洗牌程序

void shuffle(char cards[][10],int n){    int i=54;    char ca,cb,ct[10];    srand((unsigned)time(NULL));        while(i--){        ca=rand()%54;        cb=rand()%54;                strcpy(ct,cards[ca]);        strcpy(cards[ca],cards[cb]);        strcpy(cards[cb],ct);            }        }

3.编写打印的发牌程序

void printCard(char cards[][10],int n){    int i,rn=0;    printf("第一个人:\n");    for(i=0;i<17;i++){        if(rn%5==0)            printf("\n");        rn++;        printf("%s      ",cards[i]);            }    printf("\n\n第二个人:\n");    rn=0;    for(i=17;i<33;i++){        if(rn%5==0)            printf("\n");        rn++;        printf("%s      ",cards[i]);            }    printf("\n\n第三个人:\n");    rn=0;    for(i=33;i<50;i++){        if(rn%5==0)            printf("\n");        rn++;            printf("%s      ",cards[i]);        }    printf("\n\n底牌:\n");    for(i=51;i<54;i++){        printf("%s      ",cards[i]);            }    printf("\n");    getchar();}

4.在主函数预定义和调用

main(){    system("color 0f");    char cards[][10]={    "红桃 2","红桃 3","红桃 4","红桃 5","红桃 6","红桃 7","红桃 8","红桃 9","红桃 10","红桃 J","红桃 Q","红桃 K","红桃 A",    "方块 2","方块 3","方块 4","方块 5","方块 6","方块 7","方块 8","方块 9","方块 10","方块 J","方块 Q","方块 K","方块 A",    "梅花 2","梅花 3","梅花 4","梅花 5","梅花 6","梅花 7","梅花 8","梅花 9","梅花 10","梅花 J","梅花 Q","梅花 K","梅花 A",            "黑桃 2","黑桃 3","黑桃 4","黑桃 5","黑桃 6","黑桃 7","黑桃 8","黑桃 9","黑桃 10","黑桃 J","黑桃 Q","黑桃 K","黑桃 A",    "大王","小王"     };    int n = 54;     shuffle(cards,n);    printCard( cards, n);        return 0;}

5.最终程序

#include#include#include #includevoid shuffle(char cards[][10],int n);void printCard(char cards[][10],int n);main(){    system("color 0f");    char cards[][10]={    "红桃 2","红桃 3","红桃 4","红桃 5","红桃 6","红桃 7","红桃 8","红桃 9","红桃 10","红桃 J","红桃 Q","红桃 K","红桃 A",    "方块 2","方块 3","方块 4","方块 5","方块 6","方块 7","方块 8","方块 9","方块 10","方块 J","方块 Q","方块 K","方块 A",    "梅花 2","梅花 3","梅花 4","梅花 5","梅花 6","梅花 7","梅花 8","梅花 9","梅花 10","梅花 J","梅花 Q","梅花 K","梅花 A",            "黑桃 2","黑桃 3","黑桃 4","黑桃 5","黑桃 6","黑桃 7","黑桃 8","黑桃 9","黑桃 10","黑桃 J","黑桃 Q","黑桃 K","黑桃 A",    "大王","小王"     };    int n = 54;     shuffle(cards,n);    printCard( cards, n);        return 0;}void shuffle(char cards[][10],int n){    int i=54;    char ca,cb,ct[10];    srand((unsigned)time(NULL));        while(i--){        ca=rand()%54;        cb=rand()%54;                strcpy(ct,cards[ca]);        strcpy(cards[ca],cards[cb]);        strcpy(cards[cb],ct);            }        }void printCard(char cards[][10],int n){    int i,rn=0;    printf("第一个人:\n");    for(i=0;i<17;i++){        if(rn%5==0)            printf("\n");        rn++;        printf("%s      ",cards[i]);            }    printf("\n\n第二个人:\n");    rn=0;    for(i=17;i<33;i++){        if(rn%5==0)            printf("\n");        rn++;        printf("%s      ",cards[i]);            }    printf("\n\n第三个人:\n");    rn=0;    for(i=33;i<50;i++){        if(rn%5==0)            printf("\n");        rn++;            printf("%s      ",cards[i]);        }    printf("\n\n底牌:\n");    for(i=51;i<54;i++){        printf("%s      ",cards[i]);            }    printf("\n");    getchar();}

6.1程序结果

6.2

读到这里,这篇"C语言怎么实现发牌洗牌"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。

0