首页 > 编程 > C++ > 正文

C++扑克牌的洗牌发牌游戏设计

2020-05-23 13:26:27
字体:
来源:转载
供稿:网友

笔者在大学二年级期间,做过的一次C++程序设计:扑克牌的洗牌发牌游戏。具体内容是:除去大王和小王,将52张扑克牌洗牌,并发出5张牌。然后判断这5张牌中有几张相同大小的牌,是否是一条链,有几个同花等。

笔者在学习他人设计的基础上,完成了自己的程序设计。这里将源程序分享给大家。 

[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++,扑克牌,游戏设计

注:如有疑问或者高见,欢迎各位读者与笔者交流,以期共同学习进步。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表