在內存中模擬出一副牌,然後模擬洗牌,發牌等動作。
流程是這樣的:構建一副牌保存到一個數組中—洗牌—創建玩家—向玩家發牌–輸出每個玩家的牌。
#include <stdio.h> #include <stdlib.h> #include <time.h> //定義撲克的花色 enum Suit{ heart, spade, diamond, club, joker1, joker2 }; //一副牌的數量 #define CARD_COUNT 54 //定義撲克 typedef struct Card{ int value;//牌的點數從1開始 enum Suit suit;//花色 }Card; //定義玩家 typedef struct Player{ char name[64];//玩家的名字 Card ** cards;//玩家分到的牌。每項是一個指針,指向原始一副牌數組中的一項,這樣可以節省空間 int cardsCount;//玩家分到的牌的數量 }Player; //分牌完成後調用的函數的類型 typedef int (*COMPARE)(Card* ,Card*); //函數聲明們 char* getCardName(const Card*); Card** shuffle(const Card*); void dispatchCards(Player** ,int ,const Card** ); void sort(Card**,int,COMPARE); int compare1(Card* ,Card*); int compare2(Card* ,Card*); void initOnePack(); //原始一副牌所在的數組 Card pokers[CARD_COUNT]; //入口函數 int main(void) { //初始化一副牌 initOnePack(); //洗牌,shuffledPokers保存洗後的牌們 Card** shuffledPokers = shuffle(pokers); //構建三個玩家 Player player1; strcpy(player1.name,"隔壁老王"); player1.cards=NULL; player1.cardsCount=0; Player player2; strcpy(player2.name,"小明"); player2.cards=NULL; player2.cardsCount=0; Player player3; strcpy(player3.name,"田中龜孫"); player3.cards=NULL; player3.cardsCount=0; //把三放到一個數組中,以傳入發牌函數中 Player* players[]={&player1,&player2,&player3}; //發牌 dispatchCards(players,sizeof(players)/sizeof(Player*),shuffledPokers); //洗後的牌用完了,釋放之 free(shuffledPokers); int i; //打印出每個玩家手中的牌 for(i=0;i<sizeof(players) -="">name); //需要對玩家手中的牌排序 sort(players[i]->cards,players[i]->cardsCount,compare1); //打印玩家手中所有的牌 int j; for(j=0;j<players[i]->cardsCount;j++){ char * name = getCardName(players[i]->cards[j]); printf("%s ",name); free(name); } //每個玩家都需要換一次行 printf("\n"); } //釋放玩家手中牌的數組 for(i=0;i<sizeof(players) -="">cards); } return 0; } //構造一副牌 void initOnePack(){ int i=0; //前52張 for(;i<card_count-2;i++){ .value="i/4+2;" .suit="joker2;" joker1="" joker2="" const="" int="" retpokers="malloc(CARD_COUNT*sizeof(Card*));" pokers2="malloc(CARD_COUNT*sizeof(Card*));" i="0;i<CARD_COUNT;i++){" unsigned="" index="rand()%CARD_COUNT;" return="" void="" numbercards="CARD_COUNT/playerCount+1;" cards="malloc(numberCards*sizeof(Card*));" -="">cards = cards; } //輪流向每個玩家發牌 for(i=0;i<card_count;i++){ player="" curplayer="players[i%playerCount];" curplayer-="">cards[curPlayer->cardsCount] = shuffledCards[i]; //玩家手中實際的牌數增加 curPlayer->cardsCount++; } } //排序函數 //cards是要排序的牌,每一項是牌的指針 //cardsCount是牌的數量 //compare_func是比較函數 void sort(Card** cards,int cardsCount,COMPARE compare_func){ int i; for(i=0;i<cardscount-1;i++){ int="" j="0;j<cardsCount-i-1;j++){" tmp="cards[j];" a-="">value > b->value){ return 1; }else if(a->value < b->value){ return 0; }else{ if(a->suit > b->suit) return 1; else return 0; } } //比較函數,先比較點數再比較花色 int compare2(Card* a,Card* b){ if(a->value > b->value){ return 0; }else if(a->value < b->value){ return 1; }else{ if(a->suit > b->suit) return 0; else return 1; } } //獲取牌的名字 //返回牌的名字字符串,調用者用完後需要free()之。 char* getCardName(const Card* card){ //存放花色名字 char suitStr[16]={0};//0=='\0' switch (card->suit) { case heart: strcpy(suitStr,"紅桃"); break; case spade: strcpy(suitStr,"黑桃"); break; case diamond: strcpy(suitStr,"方塊"); break; case club: strcpy(suitStr,"梅花"); break; } //存放點數名字 char valueStr[16]; switch(card->value){ case 1: strcpy(valueStr,"A"); break; case 11: strcpy(valueStr,"J"); break; case 12: strcpy(valueStr,"Q"); break; case 13: strcpy(valueStr,"K"); break; case 14: strcpy(valueStr,"小王"); break; case 15: strcpy(valueStr,"大王"); break; default: sprintf(valueStr,"%d",card->value); break; } //動態分配足夠的空間 char * ret = malloc(16); //將兩個名字合並到ret中 sprintf(ret,"%s%s",suitStr,valueStr); return ret; }
上一篇:成為C++高手之回調函數