#include <stdio.h> #include <time.h> #include <stdlib.h> #include <unistd.h> #define N 52 int hcard[4][14]; //四个玩家的牌 void initCard(int* card) //初始化一副顺序牌 { int i = 0; for(;i<N;i++) { card[i] = i+1; } } void orderCard(int* card) //打乱牌的顺序 { int a; int b; srand((unsigned)time(NULL)); int i = 0; for(;i<N;i++) { a = rand()%52; b = rand()%52; int temp = card[a]; card[a] = card[b]; card[b] = temp; } } void sortCard(int* ccard) //将玩家手上的牌按顺序排序 { int i = 1; for(;i<13;i++) { int temp = ccard[i]; int j = i-1; while(j>=0&&temp>ccard[j]) { ccard[j+1] = ccard[j]; j--; } ccard[j+1] = temp; } } void printCard(int data) //打印一张牌 { switch((data-1)/13) { case 3: printf("黑桃"); break; case 2: printf("红桃"); break; case 1: printf("梅花"); break; case 0: printf("方块"); break; default: printf("程序出错了!"); break; } int i = (data-1)%13; if(i>=0&&i<9) { printf("%d",i+1); } else if(i==9) { printf("10"); } else if(i==10) { printf("J"); } else if(i==11) { printf("Q"); } else if(i==12) { printf("K"); } } void displayCard(int* hcard,int j,int data) //打印玩家的牌 { printf("玩家%d得到的牌为:",j); printCard(data); printf("/n"); printf("玩家%d手上的牌为:/n",j ); int i = 0; while(hcard[i]!=0) { printCard(hcard[i]); printf(" "); i++; } printf("/n"); } int main() { int card[N]; initCard(card); orderCard(card); int i = 0; for(;i<13;i++) { int j = 0; for(;j<4;j++) { hcard[j][i] = card[4*i+j]; //给每个玩家发牌 sortCard(hcard[j]); displayCard(hcard[j],j+1,hcard[j][i]); printf("/n"); sleep(1); } } return 0; }