模拟扑克的洗发牌
1 #include<stdio.h> 2 #include<string.h> 3 #include<time.h> 4 5 typedef struct 6 { 7 char suit[10]; /*花色*/ 8 char face[10]; /*牌面*/ 9 }CARD; 10 11 void FillCard(CARD card[]);//保存一幅扑克牌 12 void DealCard(CARD card[],int result[]);//发牌 13 void InitializeCard(int result[]);//发牌 14 void shuffleCard(int result[]);//模拟洗牌 15 16 17 int main() 18 { 19 CARD card[52]; 20 int result[52]; 21 FillCard(card); 22 InitializeCard(result); 23 DealCard(card,result);//洗牌前 24 printf("After shuffle:\n"); 25 shuffleCard(result); 26 DealCard(card,result); 27 return 0; 28 } 29 30 31 /*保存一副扑克牌*/ 32 void FillCard(CARD card[]) 33 { 34 int i; 35 char *pSuit[]={"Spades","Hearts","Clubs","Diamonds"}; 36 char *pFace[]={"A","2","3","4","5","6","7","8","9","10","Jack","Queen","King"}; 37 for (i=0;i<52;++i) 38 { 39 strcpy(card[i].suit,pSuit[i/13]); 40 strcpy(card[i].face,pFace[i%13]); 41 } 42 } 43 44 /*顺序发牌*/ 45 void DealCard(CARD card[],int result[]) 46 { 47 int i; 48 for (i=0;i<52;++i) 49 { 50 printf("%10s%7s\n",card[result[i]].suit,card[result[i]].face); 51 } 52 } 53 54 55 void InitializeCard(int result[]) 56 { 57 int i; 58 for (i=0;i<52;++i) 59 { 60 result[i]=i; 61 } 62 } 63 64 /*洗牌*/ 65 void shuffleCard(int result[]) 66 { 67 int i,j,temp; 68 srand(time(NULL)); 69 for (i=0;i<52;++i) 70 { 71 j=rand()%52; 72 temp =result[j]; 73 result[j]=result[i]; 74 result[i]=temp; 75 } 76 77 }