抓牌哦
今日碰见一难题(老师留的期末作业,无奈我一可怜文科生,不得要领),百思不解,请大家帮帮我!编写一个模拟发牌程序,分5步:
(1)共52张牌,先初始化.所用函数(Void InitialDack(int deck[],int n))
(2)洗牌,每人13张(Void shuffle(int deck[],int n))
(3)抓牌(Void handin(int deck[],int hand[],int n))
(4)对手上的牌排序(Void SortHand(int hand[],int n))
(5)按指定格式显示1-A 11-J 12-Q 13-K(Void ShowHang(int hand[],int n))
感激不尽!
void InitialDeck(int deck[],int n) { int i,j; for(i=1;i<=4;i++) { for(j=1;j<=13;j++) deck[(i-1)*13+j-1]=i*100+j; } for(i=0;i<52;i++) printf("%4d",deck[i]); printf("\n"); } void Shuffle(int deck[], int n) { int lh, rh; for (lh = 0; lh < n; lh++) { rh = RandomInteger(lh, n - 1); SwapElements(deck, lh, rh); } } void Handin(int deck[],int hand[],int n) { int i; for(i=0;i<n;i++) hand[i]=deck[i+4]; printf("The card in the hand is following:\n"); for(i=0;i<n;i++) printf("%4d",hand[i]); printf("\n"); } void SortHand(int hand[],int n) { int i,j,flag,s ; for(i=0;i<n-1;i++) { j=FindMax(hand,i,n-1); SwapElements(hand,i,j); } for(i=0;i<n;i++) {flag=hand[i]%100; if(flag<10) if (flag==1){s=hand[i]/100; Insert(hand,s,i); } } void ShowHand(int hand[],int n) { int i,rank,grade; char crank,grank; for(i=0;i<n;i++) { rank=hand[i]/100; grade=hand[i]%100; switch(rank) { case 1: crank='C';break; case 2: crank='D';break; case 3: crank='H';break; case 4: crank='S';break; } } if(grade==1) { grank='A'; printf("%c%c ",grank,crank); } else if(grade>10) { switch(grade) { case 11:grank='J';break; case 12:grank='Q';break; case 13:grank='K';break; } printf("%c%c ",grank,crank); } else printf("%2d%c ",grade,crank); } printf("\n"); } |