-
头文件和命名空间:代码包含了一些基本的头文件,如
iostream
、vector
、algorithm
等,并使用了标准命名空间std
。 -
宏定义:定义了一些宏,如
PLAYERCOUNT
(玩家数量)、CARDSCOUNT
(牌的总数)、VALUECOUNT
(牌面值的数量)等。 -
颜色枚举:定义了一个
COLOR
枚举,表示牌的花色。 -
Card
类:表示单张牌,包含牌面值、花色和牌的值。有一个print
方法用于打印牌的信息。 -
CardsType
类:用于表示牌型的类型,如单牌、对子、炸弹等。 -
CardGroup
类:表示一组牌,可以是单张牌、对子、顺子等。包含牌的数组和牌型的类型。有方法来计算牌型、初始化牌组、判断是否能打过另一组牌等。 -
LastCards
类:用于记录上一次出的牌,使用单例模式实现。 -
Player
类:表示玩家,包含玩家的名字、手中的牌、出牌方法等。 -
Landlords
类:是游戏的主类,包含玩家数组、牌数组、是否结束游戏等状态。有方法来初始化游戏、开始游戏、洗牌、发牌、抢地主、检查出牌是否合法等。 -
main
函数:程序的入口点,创建Landlords
对象,循环进行游戏。 -
辅助函数:
makeChoice
用于获取用户的选择,cmp
用于比较两张牌的大小。
#include <iostream> #include<vector> #include<assert.h> #include<string> #include<stdio.h> #include<algorithm> #include<time.h> using namespace std; #define PLAYERCOUNT 3 #define CARDSCOUNT 54 #define CURRENTPLAYER 0 #define VALUECOUNT 17 #define ERROR -1 using namespace std; int scnt=0; const char toFigure[]="34567890JQKA 2YZ"; enum COLOR{ //花色显示ASCII: 3~6 eHEART=3,//红桃 eDIAMOND=4,//方片 eCLUB=5, //草花 eSPADE=6 //黑桃 }; class Card; class CardsType; class CardGroup; class Player; class Landlords; class LastCards; bool makeChoice(string tip); bool cmp(Card* a,Card* b); class Card{ public: char figure; COLOR color; int value; Card(char _figure,COLOR _color){ figure=_figure; color=_color; value=calValue(); } int calValue(){ for(int i=0;toFigure[i];i++){ if(toFigure[i]==figure){ return i; } } return ERROR; } void print() { assert(value != ERROR); switch (color) { case eHEART: cout << "♥"; // 红桃 break; case eDIAMOND: cout << "♦"; // 方片 break; case eCLUB: cout << "♣"; // 草花 break; case eSPADE: cout << "♠"; // 黑桃 break; default: cout << "?"; // 未知花色 break; } cout << figure << ' '; } }; class CardsType{ public: //为了规范查找对应牌的方法 //统一为3个参数cnt1、isContinuous、cnt2 int typeId; string typeStr; int cnt1,cnt2; bool isContinuous; CardsType(){ typeId=ERROR; } bool operator ==(const CardsType& other)const{ return this->typeId==other.typeId; } void init(char* _typeStr,int _typeId,int _cnt1,bool _isContinuous,int _cnt2){ cnt1=_cnt1; isContinuous=_isContinuous; cnt2=_cnt2; typeStr=_typeStr; typeId=_typeId; } }; class CardGroup{ public: vector<Card*> cards; CardsType type; void calType(){ int i,n=cards.size(); //init(typeStr,typeId,cnt1,isContinuous,cnt2) if(n==0){ type.init("不出",14,0,0,0); return; } if(n==2&&cards[0]->value==15&&cards[1]->value==14){ type.init("王炸",0,0,0,0); return; } //统计同点数牌有多少张 int cntFlag[VALUECOUNT]={0}; for(i=0;i<n;i++){ cntFlag[cards[i]->value]++; } //统计点数最多和最少的牌 int maxCnt=0,minCnt=4; for(i=0;i<VALUECOUNT;i++){ if(maxCnt<cntFlag[i]){ maxCnt=cntFlag[i]