C++分享笔记:扑克牌的洗牌发牌游戏设计

  笔者在大学二年级期间,做过的一次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=;
static const int totalSuits=; 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","","","","","","","","","","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=;
static const int allCards=;
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=;i<allCards;i++)
{
Card card(i%Card::totalFaces,i/Card::totalFaces);
deck.push_back(card);
}
} void DeckOfCards::shuffle()
{
srand(time()); int swapRandom[allCards]; for(int i=;i<allCards;i++)
swapRandom[i]=int(rand())%allCards; for(int i=;i<allCards;i++)
{
Card swapCard(,);
swapCard=deck[i];
deck[i]=deck[swapRandom[i]];
deck[swapRandom[i]]=swapCard;
} } Card DeckOfCards::dealCard(int how_many)
{
for(int i=;i<how_many;i++)
{
cout <<deck[i].toString() <<endl;
if((i+)%==) cout <<endl;
}
} void DeckOfCards::sortCards()
{
int sortFaces[fiveCards];
int sortSuits[fiveCards];
for(int i=;i<fiveCards;i++)
{
sortFaces[i]=deck[i].getFace();
sortSuits[i]=deck[i].getSuit();
} for(int i=fiveCards-;i>=;i--)
for(int j=;j<=i-;j++)
if(sortFaces[j]>sortFaces[j+])
{
int t;
t=sortFaces[j];
sortFaces[j]=sortFaces[j+];
sortFaces[j+]=t;
} if((sortFaces[]==sortFaces[]&&sortFaces[]==sortFaces[]&&sortFaces[]==sortFaces[])||
(sortFaces[]==sortFaces[]&&sortFaces[]==sortFaces[]&&sortFaces[]==sortFaces[]))
cout <<"There are 4 same cards." <<endl; else if((sortFaces[]==sortFaces[]&&sortFaces[]==sortFaces[])||
(sortFaces[]==sortFaces[]&&sortFaces[]==sortFaces[])||
(sortFaces[]==sortFaces[]&&sortFaces[]==sortFaces[]))
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[]+==sortFaces[]&&sortFaces[]+==sortFaces[]&&
sortFaces[]+==sortFaces[]&&sortFaces[]+==sortFaces[])
cout <<"The five cards is a straight." <<endl;
else
cout <<"The five cards is not a straight." <<endl; if(sortSuits[]==sortSuits[]&&sortSuits[]==sortSuits[]&&
sortSuits[]==sortSuits[]&&sortSuits[]==sortSuits[])
cout <<"The five cards have same flower." <<endl;
else
cout <<"The five cards have different flower." <<endl; } int DeckOfCards::duiziCards(int* sortFaces)
{
int duiziNum=; for(int i=;i<fiveCards-;i++)
if(sortFaces[i]==sortFaces[i+]) 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 ;
}

  将5个文件放在同一目录下,对文件“main_FiveCards.cpp”编译运行即可。以下是在CodeBlocks中的运行结果:

  C++分享笔记:扑克牌的洗牌发牌游戏设计

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

上一篇:python之simplejson,Python版的简单、 快速、 可扩展 JSON 编码器/解码器


下一篇:Java 面向对象编程——第一章 初识Java