成為C++高手之實戰項目

https://blog.csdn.net/niu_gao/article/details/51458721

在內存中模擬出一副牌,然后模擬洗牌,發牌等動作。

流程是這樣的:構建一副牌保存到一個數組中—洗牌—創建玩家—向玩家發牌–輸出每個玩家的牌。

#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)/sizeof(Player*);i++){//先打印玩家的名字printf("%s\n",players[i]->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)/sizeof(Player*);i++){free(players[i]->cards);}return 0;
}
//構造一副牌
void initOnePack(){int i=0;//前52張for(;i<CARD_COUNT-2;i++){pokers[i].value=i/4+1;pokers[i].suit = i%4;}//剩下的兩張:大王和小王//joker1pokers[i].value=i/4+1;pokers[i].suit=joker1;//joker2pokers[i+1].value=i/4+2;pokers[i+1].suit=joker2;
}
//洗牌,參數是原始的一副牌,返回洗完后的牌
Card** shuffle(const Card* pokers){int i;//分牌返回牌數組的內存空間Card** retPokers = malloc(CARD_COUNT*sizeof(Card*));//為了不改動原始的一副牌,另建一個數組,保存原始牌的指針(注意每項不是牌,而是牌的指針)Card** pokers2 = malloc(CARD_COUNT*sizeof(Card*));for(i=0;i<CARD_COUNT;i++){pokers2[i] = &pokers[i];}//種下隨機種子。種子取的是當前時間,//所以保證了每次運行程序時,產生的隨機數序列不同srand(time(NULL));//取得隨機序號,從pokers2取出序號所指的項,把它依次加到retPokers中。for(i=0;i<CARD_COUNT;i++){unsigned int index = rand()%CARD_COUNT;if(pokers2[index] != NULL){retPokers[i] = pokers2[index];pokers2[index]=NULL;}else{i--;}}free(pokers2);//返回洗完后的數組return retPokers;
}
//發牌
//players是玩家數組
//playerCount是玩家數量
//shuffledCards是洗完后的一副牌
void dispatchCards(Player** players,int playerCount,const Card** shuffledCards){//計算每個玩家手中牌的數組的容量,如果每個玩家手中的牌不一樣,//最多就差一張,加1是為了保證數組分配的空間足夠容納分到的牌。int numberCards = CARD_COUNT/playerCount+1;//為每個玩家的牌數組分配空間int i;for(i=0;i<playerCount;i++){Card* cards = malloc(numberCards*sizeof(Card*));players[i]->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;for(j=0;j<cardsCount-i-1;j++){if(compare_func(cards[j],cards[j+1])){int tmp=cards[j];cards[j]=cards[j+1];cards[j+1]=tmp;}}}
}
//比較函數,先比較點數再比較花色
int compare1(Card* a,Card* b){if(a->value > b->value){return 1;}else if(a->value < b->value){return 0;}else{if(a->suit > b->suit)return 1;elsereturn 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;elsereturn 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;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383741.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383741.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383741.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++中String類的實現

https://www.cnblogs.com/zhizhan/p/4876093.html原文&#xff1a;http://noalgo.info/382.html String是C中的重要類型&#xff0c;程序員在C面試中經常會遇到關于String的細節問題&#xff0c;甚至要求當場實現這個類。只是由于時間關系&#xff0c;可能只要求實現構造函數、…

Ubuntu軟件更新失敗

剛安裝好Ubuntu以后需要將系統的軟件都更新一下&#xff0c;但是遇到一個問題就是下載倉庫信息失敗&#xff0c;大概是這個樣子的錯誤&#xff1a; 經國遇到這樣的問題可以試一下下面這個命令&#xff1a; sudo rm -rf /var/lib/apt/lists/* sudo apt-get update參考網址&…

getsockname函數與getpeername函數的使用

https://www.tuicool.com/articles/V3Aveygetsockname和getpeername函數 getsockname函數用于獲取與某個套接字關聯的本地協議地址 getpeername函數用于獲取與某個套接字關聯的外地協議地址 定義如下&#xff1a;[cpp] view plaincopy#include<sys/socket.h> int gets…

Ubuntu根目錄空間不足

自己在固態硬盤上安裝的Ubuntu&#xff0c;結果只用了一天就顯示磁盤空間不足。查看空間以后發現Ubuntu自己安裝的時候默認給根目錄分配的是10GB,然而我們下載的軟件以及環境等一般都安裝在根目錄空間下&#xff0c;尤其是/usr目錄所占的空間很大。 不得已我在網上查找了如何給…

Linux命令【一】基本命令

shell命令和bash命令相同&#xff0c;指的是命令解析器 快捷鍵 history 所有的歷史命令ctrl P 向上滾動命令 ctrl N 向下滾動命令 ctrlB將光標向前移動 ctrlF將光標向后移動 ctrlA移動到命令行頭部 ctrlE移動到命令行尾部 光標刪除操作&#xff1a;刪除光標前面字符ctrlh或…

The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

http://www.jb51.net/article/119654.htmThe MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 意思貌似MYSQL還運行在 --skip-grant-tables模式&#xff0c;如何讓他回到原來的模式 第一種方法&#xff1a;原來在mysql.ini文…

解決Ubuntu“下載額外數據文件失敗 ttf-mscorefonts-installer”的問題

參考博客&#xff1a;傳送門 下載[ttf-mscorefonts-installer.zip](https://pan.baidu.com/s/1i5rLfMH) 密碼: h76g 然后解壓到下載的目錄&#xff0c;在當前目錄執行命令&#xff1a; sudo dpkg-reconfigure ttf-mscorefonts-installer這條命令手動指定文件夾的位置,重新配置…

【C語言】單鏈表的相關熱點面試題(包括:從尾到頭打印,逆置,冒泡,尋找中間節點,倒數k節點)

https://blog.csdn.net/hanjing_1995/article/details/51539599從尾到頭打印單鏈表[cpp] view plaincopyvoid FromTailToHeadPrint(SListNode*& head) { stack<SListNode*> s; SListNode* cur head; while (cur) { s.push(cur); …

Linux命令【二】終端+Vim

需要先安裝net-tools ifconfig eth0 網卡&#xff0c;硬件地址為MAC 地址&#xff0c;網卡編號&#xff0c;絕對不會重復 lo 回環地址 測試兩臺主機之間能否通信&#xff1a;ping IP或域名 [-c 4//回饋四條信息 -i//每隔多少秒回饋一次] 得到域名對應的IPnslookup 域名得到域…

Linux如何將文件中內容放到粘貼板上

沒有找到如何在vim中將內容復制到粘貼板上&#xff0c;只找到了使用另一個軟件進行操作。 首先安裝xsel sudo apt-get install xsel # 將剪切板中的內容輸出到文件 echo $(xsel --clipboard) >> a.txt# 將文件的內容復制到剪切板 cat a.txt | xsel --clipboard

【C語言】str類與men庫函數的實現(如:strcpy,strcmp,strstr,strcat,memmove,memcpy)

https://blog.csdn.net/hanjing_1995/article/details/51539583strcpy拷貝源字符串到子字符串&#xff0c;包括‘\0’。代碼實現&#xff1a;[cpp] view plaincopychar* strcpy(char* dst,const char* src) { assert(src); char* ret dst; while (*src) …

【筆試常考】C語言:深度剖析strlen,sizeof

https://blog.csdn.net/hanjing_1995/article/details/51539532在之前的博客中&#xff0c;我也探索過strlen,sizeof區別&#xff0c;詳情可見博客http://10740184.blog.51cto.com/10730184/1705820。關于strlen,sizeof均可求字符串長度&#xff0c;這兩者是筆試面試常考的知識…

vim環境配置 +vimplus配置

vim配置 參考網站&#xff1a;傳送門 這個網站詳細說明了vim配置的命令&#xff0c;我挑選了我想要用的部分&#xff0c;自己配置了一下。 配置vim的文件有兩個&#xff0c;一個是/etc/vim/vimrc 這個是系統配置文件&#xff0c;修改這個文件將會修改所有用戶的vim環境&…

劍指offer面試題:替換空格

https://blog.csdn.net/yanxiaolx/article/details/52235212題目&#xff1a;請實現一個函數&#xff0c;把字符串中的每個空格替換成“%20”。例如輸入“We are happy.”&#xff0c;則輸出“We%20are%20happy.”。解析&#xff1a;時間復雜度為O(n)的解法。完整代碼及測試用例…

數據庫原理及應用【一】引言

什么是數據庫&#xff1a;一個大規模的集成的數據集合 作用&#xff1a;描述現實世界的實體(entities)以及實體之間的關系 管理數據庫的系統軟件&#xff1a;DBMS 文件是一個平滑的字符流&#xff0c;無法完成信息的檢索和管理 數據&#xff08;data&#xff09;:用來描述現…

Linux命令【三】gcc編譯+靜態庫+動態庫+makefile+gdb調試

用C編譯器編譯源文件&#xff1a;gcc 源文件 -o 可執行文件名 詳細步驟&#xff1a; gcc -E a.c -o a.i預處理器將頭文件展開&#xff0c;宏替換&#xff0c;去掉注釋gcc -S a.i -o a.s編譯器將C文件變成匯編文件gcc -c a.s -o a.o匯編器將會變文件變成二進制文件gcc a.o -o a…

用c++模擬實現一個學生成績管理系統

https://blog.csdn.net/yanxiaolx/article/details/53393437題目&#xff1a;用c模擬實現一個學生成績的信息管理系統&#xff0c;要求能添加、刪除、修改、查看和保存學生的信息等功能 源代碼如下:[cpp] view plaincopy#define _CRT_SECURE_NO_WARNINGS #include<iostr…

Linux命令【四】文件+虛擬內存+常用系統函數

File*其實是一個結構體 文件描述符FD&#xff1a;索引到對應的磁盤文件文件讀寫位置指針FP_POS&#xff0c;如果同時讀寫需要注意文件指針的位置I/O緩沖區BUFFER&#xff1a;保存內存指針&#xff0c;默認大小是8kb&#xff0c;用于減小我們對硬盤操作的次數。因為我們對硬盤的…

Python3列表

操作&#xff1a;索引、切片、加、乘、檢查成員、確定序列長度、確定最大最小元素 定義&#xff1a; 列表名 [元素]下標列表名[x] 截取:列表名[x:y] 更新&#xff1a; list[x]y 或者使用append()方法添加列表項刪除&#xff1a; del list[x]常用操作&#xff1a; 截取與…

Linux驚群效應詳解(最詳細的了吧)

https://blog.csdn.net/lyztyycode/article/details/78648798?locationNum6&fps1 linux驚群效應詳細的介紹什么是驚群&#xff0c;驚群在線程和進程中的具體表現&#xff0c;驚群的系統消耗和驚群的處理方法。1、驚群效應是什么&#xff1f;驚群效應也有人叫做雷鳴群體效應…