本文小编为大家详细介绍“C语言怎么实现发牌洗牌”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言怎么实现发牌洗牌”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。模拟发牌程序通过写两个函数模块来编写洗牌程序和打印
本文小编为大家详细介绍“C语言怎么实现发牌洗牌”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言怎么实现发牌洗牌”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
模拟发牌程序
通过写两个函数模块来编写洗牌程序和打印的发牌程序
编写洗牌程序
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();}
在主函数预定义和调用
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;}
最终程序
#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();}
1程序结果
2
读到这里,这篇“C语言怎么实现发牌洗牌”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网其他教程频道。
--结束END--
本文标题: C语言怎么实现发牌洗牌
本文链接: https://www.lsjlt.com/news/328570.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0