C语言实现发牌洗牌
时间:2022-12-04 12:56:36|栏目: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程序结果
6.2