温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言怎么实现发牌洗牌

发布时间:2022-05-05 10:14:12 来源:亿速云 阅读:149 作者:iii 栏目:开发技术

本文小编为大家详细介绍“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<stdio.h>
#include<string.h>
#include<stdlib.h> 
#include<time.h>
void 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程序结果

C语言怎么实现发牌洗牌

6.2

C语言怎么实现发牌洗牌

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI