笔者在大学二年级期间,做过的一次C++程序设计:扑克牌的洗牌发牌游戏。具体内容是:除去大王和小王,将52张扑克牌洗牌,并发出5张牌。然后判断这5张牌中有几张相同大小的牌,是否是一条链,有几个同花等。
创新互联专注于企业营销型网站建设、网站重做改版、晋城网站定制设计、自适应品牌网站建设、HTML5建站、商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为晋城等各大城市提供网站开发制作服务。
笔者在学习他人设计的基础上,完成了自己的程序设计。这里将源程序分享给大家。
[1] 文件“card.h”
#ifndef CARD_H #define CARD_H #include<string> using namespace std; class Card { public: static const int totalFaces=13; static const int totalSuits=4; Card(int,int); int getFace(); int getSuit(); string toString(); private: int face,suit; static const string faceNames[totalFaces]; static const string suitNames[totalSuits]; }; #endif // CARD_H
[2] 文件“card.cpp”
#include"card.h" Card::Card(int faceNumber,int suitNumber) { face=faceNumber; suit=suitNumber; } string Card::toString() { return faceNames[face]+" of "+suitNames[suit]; } int Card::getFace() {return face;} int Card::getSuit() {return suit;} const string Card::faceNames[totalFaces]= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; const string Card::suitNames[totalSuits]= {"heart","diamond","club","spade"};
[3] 文件“deckofcards.h”
#ifndef DECKOFCARDS_H #define DECKOFCARDS_H #include<vector> #include"card.h" using namespace std; class DeckOfCards { public: static const int fiveCards=5; static const int allCards=52; DeckOfCards(); void shuffle(); Card dealCard(int); void sortCards(); int duiziCards(int* sortFaces); //bool moreCards(); private: vector<Card> deck; int currentCard; }; #endif // DECKOFCARDS_H
[4] 文件“deckofcards.cpp”
#include"deckofcards.h" #include<cstdlib> #include<ctime> DeckOfCards::DeckOfCards() { //currentCard=0; for(int i=0;i<allCards;i++) { Card card(i%Card::totalFaces,i/Card::totalFaces); deck.push_back(card); } } void DeckOfCards::shuffle() { srand(time(0)); int swapRandom[allCards]; for(int i=0;i<allCards;i++) swapRandom[i]=int(rand())%allCards; for(int i=0;i<allCards;i++) { Card swapCard(0,0); swapCard=deck[i]; deck[i]=deck[swapRandom[i]]; deck[swapRandom[i]]=swapCard; } } Card DeckOfCards::dealCard(int how_many) { for(int i=0;i<how_many;i++) { cout <<deck[i].toString() <<endl; if((i+1)%13==0) cout <<endl; } } void DeckOfCards::sortCards() { int sortFaces[fiveCards]; int sortSuits[fiveCards]; for(int i=0;i<fiveCards;i++) { sortFaces[i]=deck[i].getFace(); sortSuits[i]=deck[i].getSuit(); } for(int i=fiveCards-1;i>=0;i--) for(int j=0;j<=i-1;j++) if(sortFaces[j]>sortFaces[j+1]) { int t; t=sortFaces[j]; sortFaces[j]=sortFaces[j+1]; sortFaces[j+1]=t; } if((sortFaces[0]==sortFaces[1]&&sortFaces[0]==sortFaces[2]&&sortFaces[0]==sortFaces[3])|| (sortFaces[1]==sortFaces[2]&&sortFaces[1]==sortFaces[3]&&sortFaces[1]==sortFaces[4])) cout <<"There are 4 same cards." <<endl; else if((sortFaces[0]==sortFaces[1]&&sortFaces[0]==sortFaces[2])|| (sortFaces[1]==sortFaces[2]&&sortFaces[1]==sortFaces[3])|| (sortFaces[2]==sortFaces[3]&&sortFaces[2]==sortFaces[4])) cout <<"There are 3 same cards." <<endl; else if(int i=duiziCards(sortFaces)) cout <<"There are " <<i <<" pairs." <<endl; else cout <<"There is no same cards." <<endl; if(sortFaces[0]+1==sortFaces[1]&&sortFaces[1]+1==sortFaces[2]&& sortFaces[2]+1==sortFaces[3]&&sortFaces[3]+1==sortFaces[4]) cout <<"The five cards is a straight." <<endl; else cout <<"The five cards is not a straight." <<endl; if(sortSuits[0]==sortSuits[1]&&sortSuits[0]==sortSuits[2]&& sortSuits[0]==sortSuits[3]&&sortSuits[0]==sortSuits[4]) cout <<"The five cards have same flower." <<endl; else cout <<"The five cards have different flower." <<endl; } int DeckOfCards::duiziCards(int* sortFaces) { int duiziNum=0; for(int i=0;i<fiveCards-1;i++) if(sortFaces[i]==sortFaces[i+1]) duiziNum++; return duiziNum; }
[5] main函数文件“main_FiveCards.cpp”
#include<iostream> #include"card.cpp" #include"deckofcards.cpp" using namespace std; int main() { DeckOfCards aDeck; cout <<"Deal all cards before shuffle." <<endl; aDeck.dealCard(aDeck.allCards); aDeck.shuffle(); cout <<"Deal five cards after shuffle." <<endl; aDeck.dealCard(aDeck.fiveCards); cout <<"\nThe result after sorting." <<endl; aDeck.sortCards(); return 0; }
将5个文件放在同一目录下,对文件“main_FiveCards.cpp”编译运行即可。以下是在CodeBlocks中的运行结果:
注:如有疑问或者高见,欢迎各位读者与笔者交流,以期共同学习进步。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
文章名称:C++扑克牌的洗牌发牌游戏设计
当前网址:https://www.cdcxhl.com/article46/pgdceg.html
成都网站建设公司_创新互联,为您提供微信公众号、网站导航、动态网站、商城网站、小程序开发、网站收录
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联