您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“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
讀到這里,這篇“C語言怎么實現發牌洗牌”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。