版本比较
扑克牌c++
正文
增加 745 行删除 461 行
3 行未改动4567889101112131415101112131415161718191720212223242526272824252627282930313233343536323334353738394041373839424344454647484950454647484950515251525354555657585960616263645465666757585960616869707172647374666775767778798069708182838485868788727374757677787980818289909192939495858687888996979899100101102103104105106107108109110929394111112113114115116969798991171181191201211011021031041051221231241251261271281291301311321331341351361371381391401411421071081091431441451111121461471481491501511151161521531541551561571191201581591601611621631231241641651661671681691271281701711721731741751311321761771781791801811351361821831841851861871391401881891901421431441451911921931941951961971981992002012021471481492032042052062071511521531541551561571581591602082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542551631641651661672562572582592602612621701711721731741751761771781791801811822632642652662672682692702712722731851861871881891901911922742752762772782792802812822832842852861961971981992002012872882892902912042052922932942952962072082092972982993002112122132143013023033043053063073083093103113123133143152162172182192202212222232242253163173183193203213223233243252282292302312322332342352363263273283293303313323333343352382392402412423363373383393403413423433442453453463472472482492503483493503513523533543553563573583593603613623633643652532542552563663673683693703713723733742582592603753763773783792623803812642652662672682692702713823833843853863873883893903913923933943952732742752762772782792802813963973983994002832842852862872882892904014024034044054064074084094104114124134144154164174182932942952962972982993003013024194204214224234244254264274284294304314324333043053063073083093103113123133143153163174344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914923213223233243253263273283293304934944954964974984995005015025035045055065075085095105115125135145155165175183323333343353363375195203393403413423433445215225233463473483493503513523533545245255265275285295305315325335345355365375385395405415425433565445455465475485495505515525535545555565575585595605615625635645655665675685695705715725733603615745755765775785793633643653665805815825835845855863693705875885893723733743755905915925935945955965975985993776006013793803813823833843853863876026036046056066076086096106116126136146156166176186196206216226236243893903913926253943953963973983994004014024036266276286296306316326336346354054064074084094106366376386396406414134144154166426436446456466476486494184194204214224236506516526536546554254264274286566576586596606616626634304314326644344356656666676684374384396696706716724414426736746754444454464474484494504514524534544556766776786796806816826836846856866876886894576906914594604616926936946956966976986997004634647017027034664674684694704714724734744754764774784794804814824834847047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357364877374894904914924934944954964977387397407417427437447457467475007487497507517527537545025035045055067557567577587597605085097617627637645115125135145155165175185197657667677687697707717727737747757767777787797807815217827835235245255265275285297847857867877887897907915325337927937947955365375385395405415425435445455465475487967977987998008018028038048055505515528068078088098108118128138145565575585595605618158168178185635645655665678198208218228238245695708258268278288295735745758308318328338345778358368378385795805815825838398408418428438448458468478488498508515875885895905915925935945958528538548558568578588598608615975985998628638648656016028668678686046056066076086096108698708718728738748758768778788798808818828838848858868878888898908918928938948958968973 行未改动#define CARDSCOUNT 54#define CURRENTPLAYER 0#define VALUECOUNT 17#define ERROR -1
using namespace std;const char toFigure[]="34567890JQKA 2YZ";enum COLOR{ //花色显示ASCII: 3~6 eHEART=3,//红桃 eDIAMOND,//方片 eCLUB, //草花 eSPADE //黑桃
const char toFigure[] = "34567890JQKA 2YZ";const string CARD_NAMES[] = {"3","4","5","6","7","8","9","10","J","Q","K","A","2","小王","大王"};
enum COLOR { eHEART = 3, eDIAMOND, eCLUB, eSPADE};
class Card;class CardsType;class CardGroup;class Player;class Landlords;class LastCards;
bool makeChoice(const string& tip);bool makeChoice(string tip);bool cmp(Card* a,Card* b); class Card{ public:bool cmp(Card* a, Card* b);string getColorChar(COLOR color);
class Card {public: char figure; COLOR color; int value; Card(char _figure,COLOR _color){ figure=_figure; color=_color; value=calValue(); Card(char _figure, COLOR _color) : figure(_figure), color(_color) { value = calValue(); } int calValue(){ for(int i=0;toFigure[i];i++){ if(toFigure[i]==figure){ int calValue() const { for(int i = 0; toFigure[i]; i++) { if(toFigure[i] == figure) { return i; } } return ERROR; } void print(){ assert(value!=ERROR); if(figure=='Z'){ cout<<"ZZ"; }else if(figure=='Y'){ cout<<"YY"; }else{ cout<<figure<<(char)color; void print() const { assert(value != ERROR); if(figure == 'Z') { cout << "大王"; } else if(figure == 'Y') { cout << "小王"; } else { if(figure == '0') { cout << "10"; } else { cout << figure; } cout << getColorChar(color); } cout<<' '; cout << ' '; }}; class CardsType{ public: //为了规范查找对应牌的方法 //统一为3个参数cnt1、isContinuous、cnt2
class CardsType {public: int typeId; string typeStr; int cnt1,cnt2; int cnt1, cnt2; bool isContinuous; CardsType(){ typeId=ERROR; CardsType() : typeId(ERROR), cnt1(0), cnt2(0), isContinuous(false) {} bool operator==(const CardsType& other) const { return typeId == other.typeId; } bool operator ==(const CardsType& other)const{ return this->typeId==other.typeId; void init(const string& _typeStr, int _typeId, int _cnt1, bool _isContinuous, int _cnt2) { typeStr = _typeStr; typeId = _typeId; cnt1 = _cnt1; isContinuous = _isContinuous; cnt2 = _cnt2; } 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:};
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); void calType() { int n = cards.size(); if(n == 0) { type.init("不出", 14, 0, false, 0); return; } // 王炸 if(n == 2) { bool hasKing = false, hasJoker = false; for(auto card : cards) { if(card->figure == 'Z') hasKing = true; if(card->figure == 'Y') hasJoker = true; } if(n==2&&cards[0]->value==15&&cards[1]->value==14){ type.init("王炸",0,0,0,0); return; if(hasKing && hasJoker) { type.init("王炸", 0, 0, false, 0); return; } } //统计同点数牌有多少张 int cntFlag[VALUECOUNT]={0}; for(i=0;i<n;i++){ cntFlag[cards[i]->value]++; // 统计点数 int cntFlag[VALUECOUNT] = {0}; for(auto card : cards) { cntFlag[card->value]++; } //统计点数最多和最少的牌 int maxCnt=0,minCnt=4; for(i=0;i<VALUECOUNT;i++){ if(maxCnt<cntFlag[i]){ maxCnt=cntFlag[i]; // 统计信息 int maxCnt = 0, minCnt = 4, tripleCount = 0, pairCount = 0; int maxValue = -1; vector<int> tripleValues, pairValues, singleValues; for(int i = 0; i < VALUECOUNT; i++) { if(cntFlag[i] > 0) { if(cntFlag[i] > maxCnt) { maxCnt = cntFlag[i]; maxValue = i; } if(cntFlag[i] < minCnt) minCnt = cntFlag[i]; if(cntFlag[i] == 3) { tripleCount++; tripleValues.push_back(i); } if(cntFlag[i] == 2) pairValues.push_back(i); if(cntFlag[i] == 1) singleValues.push_back(i); } if(cntFlag[i]&&minCnt>cntFlag[i]){ minCnt=cntFlag[i]; } } // 炸弹 if(n==4&&maxCnt==4){ type.init("炸dan",1,4,0,0); if(n == 4 && maxCnt == 4) { type.init("炸弹", 1, 4, false, 0); return; } // 单牌 if(n==1){ type.init("单牌",2,1,0,0); if(n == 1) { type.init("单牌", 2, 1, false, 0); return; } // 对子 if(n==2&&maxCnt==2){ type.init("对子",3,2,0,0); if(n == 2 && maxCnt == 2) { type.init("对子", 3, 2, false, 0); return; } // 三张 if(n==3&&maxCnt==3){ type.init("三张 ",4,3,0,0); if(n == 3 && maxCnt == 3) { type.init("三张", 4, 3, false, 0); return; } // 三带一 if(n==4&&maxCnt==3){ type.init("三带一",5,3,0,1); if(n == 4 && maxCnt == 3 && minCnt == 1) { type.init("三带一", 5, 3, false, 1); return; } // 三带一对 if(n==5&&maxCnt==3&&minCnt==2){ type.init("三带一对",6,3,0,2); if(n == 5 && maxCnt == 3 && minCnt == 2) { type.init("三带一对", 6, 3, false, 2); return; } // 四带二(单) if(n==6&&maxCnt==4){ type.init("四带二",7,4,0,1); if(n == 6 && maxCnt == 4) { type.init("四带二", 7, 4, false, 1); return; } // 四带二(对) if(n==8&&maxCnt==4&&minCnt==2){ type.init("四带二",8,4,0,2); if(n == 8 && maxCnt == 4) { type.init("四带二", 8, 4, false, 2); return; } if(n>=5&&maxCnt==1&&cards[0]->value==cards[n-1]->value+n-1){ type.init("顺子",9,1,1,0); return; } // 顺子(至少5张连续单牌) if(maxCnt == 1 && n >= 5) { sort(cards.begin(), cards.end(), cmp); bool isStraight = true; for(int i = 1; i < n; i++) { if(cards[i-1]->value != cards[i]->value + 1) { isStraight = false; break; } } if(n>=6&&maxCnt==2&&minCnt==2&&cards[0]->value==cards[n-1]->value+n/2-1){ type.init("连对",10,2,1,0); return; if(isStraight && cards[0]->value <= 12) { // A以下才能组成顺子 type.init("顺子", 9, 1, true, 0); return; } } int fjCnt;//统计连续且大于3三张的牌 for(i=0;i<VALUECOUNT &&cntFlag[i]<3;i++); for(fjCnt=0;i<VALUECOUNT &&cntFlag[i]>=3;i++,fjCnt++); if(fjCnt>1){ if(n==fjCnt*3) type.init("飞机",11,3,1,0); else if(n==fjCnt*4) type.init("飞机",12,3,1,1); else if(n==fjCnt*5&&minCnt==2) type.init("飞机",13,3,1,2); // 连对(至少3对连续对子) if(maxCnt == 2 && n >= 6 && n % 2 == 0) { sort(cards.begin(), cards.end(), cmp); bool isConsecutivePairs = true; for(int i = 0; i < n; i += 2) { if(i + 1 < n && cards[i]->value != cards[i+1]->value) { isConsecutivePairs = false; break; } if(i + 2 < n && cards[i]->value != cards[i+2]->value + 1) { isConsecutivePairs = false; break; } } if(isConsecutivePairs && n/2 >= 3) { type.init("连对", 10, 2, true, 0); return; } } // 飞机(连续三张) if(tripleCount >= 2) { sort(tripleValues.begin(), tripleValues.end(), greater<int>()); bool isConsecutiveTriples = true; for(int i = 1; i < tripleCount; i++) { if(tripleValues[i-1] != tripleValues[i] + 1) { isConsecutiveTriples = false; break; } } if(isConsecutiveTriples) { int expectedSize = tripleCount * 3; if(n == expectedSize) { type.init("飞机", 11, 3, true, 0); return; } else if(n == expectedSize + tripleCount) { type.init("飞机带单", 12, 3, true, 1); return; } else if(n == expectedSize + tripleCount * 2) { type.init("飞机带对", 13, 3, true, 2); return; } } } } void init(string inputStr, vector<Card*> &cardsHolded){ this->cards.clear(); //不出 if(inputStr=="N"){ this->calType(); void init(const string& inputStr, vector<Card*>& cardsHolded) { cards.clear(); if(inputStr == "N" || inputStr == "n") { calType(); return; } int i,j; //输入合法性判断 for(i=0;i<inputStr.size();i++){ bool find=false; for(j=0;toFigure[j];j++){ if(inputStr[i]==toFigure[j]){ find=true; break; } } if(find==false){ //输入字符不在toFigure中 return; // 解析输入 vector<char> figures; for(int i = 0; i < inputStr.size(); i++) { if(inputStr[i] == '1' && i+1 < inputStr.size() && inputStr[i+1] == '0') { figures.push_back('0'); i++; } else { figures.push_back(inputStr[i]); } } //查找手中有没有这些牌 int visitFlag[20]={0}; for(i=0;i<inputStr.size();i++){ Card *find=NULL; for(j=0;j<cardsHolded.size();j++){ if(!visitFlag[j]&&cardsHolded[j]->figure==inputStr[i]){ visitFlag[j]=1; find=cardsHolded[j]; // 查找对应牌 vector<bool> used(cardsHolded.size(), false); for(char fig : figures) { bool found = false; for(int j = 0; j < cardsHolded.size(); j++) { if(!used[j] && cardsHolded[j]->figure == fig) { used[j] = true; cards.push_back(cardsHolded[j]); found = true; break; } } if(find){ this->cards.push_back(find); }else{ cout<<inputStr[i]; cout<<"没有找到\t"; this->cards.clear(); if(!found) { cout << "错误:您没有牌 " << fig << endl; cards.clear(); return; } }//end for(i=0;i<inputStr.size();i++) this->arrange(); } arrange(); } void init(vector<Card*> newCards){ this->cards=newCards; this->arrange(); void init(const vector<Card*>& newCards) { cards = newCards; arrange(); } bool isCanBeat(CardGroup &cardGroup){ if(cardGroup.type.typeStr=="王炸"){ return false; }else if(this->type.typeStr=="王炸"){ bool canBeat(const CardGroup& other) const { // 空牌不能管牌 if(type.typeStr == "不出") return false; // 王炸最大 if(type.typeStr == "王炸") return true; if(other.type.typeStr == "王炸") return false; // 炸弹比较 if(type.typeStr == "炸弹") { if(other.type.typeStr == "炸弹") { return getMainValue() > other.getMainValue(); } return true; }else if(cardGroup.type==this->type &&this->type.typeStr=="炸dan"){ return value()>cardGroup.value(); }else if(cardGroup.type.typeStr=="炸dan"){ return false; }else if(this->type.typeStr=="炸dan"){ return true; }else if(cardGroup.type==this->type &&this->cards.size()==cardGroup.cards.size()){ return this->value()>cardGroup.value(); }else{ return false; } if(other.type.typeStr == "炸弹") return false; // 同类型比较 if(type.typeId == other.type.typeId && cards.size() == other.cards.size()) { return getMainValue() > other.getMainValue(); } return false; } int value(){ //计算牌组权值 int i; if(type.typeStr=="三带一"||type.typeStr=="三带一对"||type.typeStr=="飞机"){ for(i=2;i<cards.size();i++){ if(cards[i]->value==cards[i-2]->value){ return cards[i]->value; } } int getMainValue() const { // 获取主牌的点数(用于比较大小) if(cards.empty()) return 0; // 统计点数 int cntFlag[VALUECOUNT] = {0}; for(auto card : cards) { cntFlag[card->value]++; } if(type.typeStr=="四带二"){ for(i=3;i<cards.size();i++){ if(cards[i]->value==cards[i-3]->value){ return cards[i]->value; } // 找出现次数最多的点数 int maxCnt = 0, mainValue = 0; for(int i = 0; i < VALUECOUNT; i++) { if(cntFlag[i] > maxCnt) { maxCnt = cntFlag[i]; mainValue = i; } } return cards[0]->value; return mainValue; } void arrange(){ //整理:排序、计算类型 sort(this->cards.begin(),this->cards.end(),cmp); this->calType(); void arrange() { sort(cards.begin(), cards.end(), cmp); calType(); } void print() const { cout << type.typeStr << ": "; for(auto card : cards) { card->print(); } cout << endl; } bool isEmpty() const { return cards.empty(); }};class LastCards{ static LastCards *lastCards; public: Player *player;
class LastCards {private: static LastCards* instance; LastCards() : player(nullptr) {} public: Player* player; CardGroup cardGroup; static LastCards* inst(){//单例模式 if(lastCards==NULL){ lastCards=new LastCards(); static LastCards* getInstance() { if(!instance) { instance = new LastCards(); } return lastCards; return instance; } vector<Card*> findCanBeatFrom(vector<Card*> &cardsHolded){ //查找能打得过的牌 int i,j,k,n=cardsHolded.size(),m=cardGroup.cards.size(); string typeStr=cardGroup.type.typeStr; vector<Card*> ret; if(typeStr=="王炸"||n<m){ //打不过,返回空数组 return ret; void reset() { player = nullptr; cardGroup.cards.clear(); cardGroup.calType(); } vector<Card*> findCanBeatFrom(vector<Card*>& cardsHolded) { vector<Card*> result; // 没有上家牌或上家没出牌 if(!player || cardGroup.isEmpty()) { return result; } int value=cardGroup.value(); //统计各点牌出现的次数 int cntFlag[VALUECOUNT]={0}; for(i=0;i<n;i++){ cntFlag[cardsHolded[i]->value]++; } int continuousCount=1; if(cardGroup.type.isContinuous){ continuousCount=m/(cardGroup.type.cnt1+cardGroup.type.cnt2); // 王炸无法被击败 if(cardGroup.type.typeStr == "王炸") { return result; } bool findFirstFigure; //cout<<"continuousCount="<<continuousCount<<endl; for(i=value+1;i<VALUECOUNT;i++){ findFirstFigure=true; for(j=0;j<continuousCount;j++){ if(cntFlag[i-j]<cardGroup.type.cnt1){ findFirstFigure=false; break; int n = cardsHolded.size(); int targetValue = cardGroup.getMainValue(); int targetSize = cardGroup.cards.size(); // 先生成所有可能的牌型组合进行测试 vector<Card*> testCards; // 1. 查找炸弹(优先使用) if(cardGroup.type.typeStr != "炸弹") { vector<int> bombValues; for(int i = 0; i < VALUECOUNT; i++) { int cnt = 0; for(auto card : cardsHolded) { if(card->value == i) cnt++; } if(cnt >= 4) bombValues.push_back(i); } if(findFirstFigure){ ret.clear(); int firstFigure=i; //cout<<"查找"<<cardGroup.type.cnt1<<"个"<<firstFigure+3<<endl; for(k=0,j=0;k<cardsHolded.size() &&j<continuousCount;k++){ if(cardsHolded[k]->value==firstFigure-j){ for(int kk=0;j>=0&&kk<cardGroup.type.cnt1;kk++){ ret.push_back(cardsHolded[k+kk]); } j++; if(!bombValues.empty()) { // 找到最小的能管上的炸弹 sort(bombValues.begin(), bombValues.end()); for(int bombVal : bombValues) { if(bombVal > targetValue || cardGroup.type.typeStr == "炸弹") { result.clear(); for(auto card : cardsHolded) { if(card->value == bombVal) { result.push_back(card); if(result.size() == 4) break; } } return result; } } if(cardGroup.type.cnt2>0){ int SecondFigures[5]; int SecondCount=continuousCount; if(cardGroup.type.typeStr=="四带二") SecondCount=2; bool findSecondFigure=true; for(j=0,k=-1;j<SecondCount &&findSecondFigure;j++){ findSecondFigure=false; for(k++;k<VALUECOUNT;k++){ SecondFigures[j]=k; if(cntFlag[k]>=cardGroup.type.cnt2 &&cntFlag[k]<cardGroup.type.cnt1){ findSecondFigure=true; break; } } } // 2. 查找同类型更大的牌 if(cardGroup.type.typeStr != "炸弹" && cardGroup.type.typeStr != "王炸") { // 根据牌型查找 if(cardGroup.type.typeStr == "单牌") { for(int i = targetValue + 1; i < VALUECOUNT; i++) { for(auto card : cardsHolded) { if(card->value == i && find(result.begin(), result.end(), card) == result.end()) { result.push_back(card); return result; } } } } else if(cardGroup.type.typeStr == "对子") { for(int i = targetValue + 1; i < VALUECOUNT; i++) { result.clear(); for(auto card : cardsHolded) { if(card->value == i) { result.push_back(card); if(result.size() == 2) return result; } } } } else if(cardGroup.type.typeStr == "三张") { for(int i = targetValue + 1; i < VALUECOUNT; i++) { result.clear(); for(auto card : cardsHolded) { if(card->value == i) { result.push_back(card); if(result.size() == 3) return result; } } } } else if(cardGroup.type.typeStr == "三带一") { for(int i = targetValue + 1; i < VALUECOUNT; i++) { // 找三张 vector<Card*> triple; for(auto card : cardsHolded) { if(card->value == i) { triple.push_back(card); if(triple.size() == 3) break; } } if(triple.size() == 3) { // 找一张单牌 for(auto card : cardsHolded) { if(card->value != i && find(triple.begin(), triple.end(), card) == triple.end()) { result = triple; result.push_back(card); return result; } } } if(findSecondFigure){ //cout<<"查找SecondFigure "<<cardGroup.type.cnt2<<"个"<<SecondFigures[0]+3<<endl; //cout<<"SecondCount= "<<SecondCount<<endl; //for(i=0;i<SecondCount;i++)cout<<"SecondFigures["<<i<<"]="<<SecondFigures[i]<<endl; for(i=0;i<SecondCount;i++){ for(j=0;j<cardsHolded.size();){ if(cardsHolded[j]->value==SecondFigures[i]){ for(k=0;k<cardGroup.type.cnt2;k++){ //cout<<"添加"<<cardsHolded[j]->value+3<<endl; ret.push_back(cardsHolded[j+k]); } } else if(cardGroup.type.typeStr == "三带一对") { for(int i = targetValue + 1; i < VALUECOUNT; i++) { // 找三张 vector<Card*> triple; for(auto card : cardsHolded) { if(card->value == i) { triple.push_back(card); if(triple.size() == 3) break; } } if(triple.size() == 3) { // 找一对 for(int j = 0; j < VALUECOUNT; j++) { if(j != i) { vector<Card*> pair; for(auto card : cardsHolded) { if(card->value == j && find(triple.begin(), triple.end(), card) == triple.end()) { pair.push_back(card); if(pair.size() == 2) { result = triple; result.insert(result.end(), pair.begin(), pair.end()); return result; } } do{ j++; }while(j<cardsHolded.size()&&cardsHolded[j]->value==SecondFigures[i]); }else{ j++; } } } } return ret; }//if(findSecondFigure) }//end if(cardGroup.type.cnt2>0) else{ return ret; } } } }//end if(findFirstFigure) }//end for(i=value+1;i<VALUECOUNT;i++) ret.clear(); //没牌打得过时查找有没有炸dan if(typeStr!="炸dan"){ for(i=cardsHolded.size()-1;i>=3;i--){ if(cardsHolded[i]->value==cardsHolded[i-3]->value){ for(j=0;j<4;j++){ ret.push_back(cardsHolded[i-j]); } else if(cardGroup.type.typeStr == "顺子") { int len = targetSize; for(int start = targetValue + 1; start + len - 1 < VALUECOUNT; start++) { result.clear(); bool canForm = true; for(int k = 0; k < len; k++) { bool found = false; for(auto card : cardsHolded) { if(card->value == start + k && find(result.begin(), result.end(), card) == result.end()) { result.push_back(card); found = true; break; } } if(!found) { canForm = false; break; } } break; if(canForm && result.size() == len) { return result; } } } else if(cardGroup.type.typeStr == "连对") { int len = targetSize / 2; for(int start = targetValue + 1; start + len - 1 < VALUECOUNT; start++) { result.clear(); bool canForm = true; for(int k = 0; k < len; k++) { int pairCount = 0; for(auto card : cardsHolded) { if(card->value == start + k && find(result.begin(), result.end(), card) == result.end()) { result.push_back(card); pairCount++; if(pairCount == 2) break; } } if(pairCount < 2) { canForm = false; break; } } if(canForm && result.size() == targetSize) { return result; } } } } return ret; }//end vector<Card*> findCanBeatFrom() result.clear(); return result; }};
LastCards* LastCards::lastCards = NULL; class Player{ public:LastCards* LastCards::instance = nullptr;
class Player {public: string name; vector<Card*> cards; void arrange(){ sort(cards.begin(),cards.end(),cmp); void arrange() { sort(cards.begin(), cards.end(), cmp); } void print(){ cout<<this->name<<":\t"; for(int i=0;i<cards.size();i++){ cards[i]->print(); void print() const { cout << name << ":\t"; if(cards.empty()) { cout << "无牌"; } else { for(auto card : cards) { card->print(); } } cout<<"["<<cards.size()<<"]\n"; cout << "[" << cards.size() << "张]" << endl; } vector<Card*> tip(){ //提示功能,使自己最小一张连最长 CardGroup ret; string temp; int j,k,m=cards.size(); for(j=0;j<m;j++){ temp=""; for(k=j;k<m;k++){ temp+=cards[k]->figure; vector<Card*> tip() { // 简单的提示:出最小的单牌 if(cards.empty()) return vector<Card*>(); // 尝试出最小的单牌 Card* minCard = cards.back(); // 已排序,最后一张最小 return vector<Card*>{minCard}; } void playCards(CardGroup& cardGroup) { cout << name << ":\t"; if(cardGroup.type.typeStr == "不出") { cout << "不出" << endl; } else { cardGroup.print(); // 移除打出的牌 for(auto card : cardGroup.cards) { auto it = find(cards.begin(), cards.end(), card); if(it != cards.end()) { cards.erase(it); } } ret.init(temp,cards); if(ret.type.typeId!=ERROR){ return ret.cards; } } ret.cards.clear(); return ret.cards; } void chupai(CardGroup &cardGroup){ //出牌 cout<<this->name<<":\t"; cout<<cardGroup.type.typeStr<<' '; for(int i=0;i<cardGroup.cards.size();i++){ cardGroup.cards[i]->print(); this->cards.erase(find(this->cards.begin(),this->cards.end(),cardGroup.cards[i])); cout << "\t剩余[" << cards.size() << "张]" << endl; // 记录到LastCards if(cardGroup.type.typeStr != "不出") { LastCards::getInstance()->player = this; LastCards::getInstance()->cardGroup.init(cardGroup.cards); } else { // 不出牌也要记录是谁不出 LastCards::getInstance()->player = this; } cout<<"\t["<<this->cards.size()<<"]\n"; if(cardGroup.type.typeStr!="不出"){ //记录到 LastCards 中 LastCards::inst()->player=this; LastCards::inst()->cardGroup.init(cardGroup.cards); } } bool hasCards() const { return !cards.empty(); }}; class Landlords{ Player *player[PLAYERCOUNT]; bool finished,youWin,landlordWin;
class Landlords {private: Player* players[PLAYERCOUNT]; vector<Card*> allCards; bool finished; bool youWin; int landlordIndex; Card *cards[CARDSCOUNT]; public: Landlords(){ int i,j,k; for(i=0;i<PLAYERCOUNT;i++){ this->player[i]=new Player(); public: Landlords() { for(int i = 0; i < PLAYERCOUNT; i++) { players[i] = new Player(); } //54张牌初始化 for(k=i=0;i<14;i++){ if(toFigure[i]==' '){ continue; // 初始化54张牌 for(int i = 0; i < 15; i++) { if(toFigure[i] == ' ') continue; if(toFigure[i] == 'Y' || toFigure[i] == 'Z') continue; for(COLOR color = eHEART; color <= eSPADE; color = (COLOR)(color + 1)) { allCards.push_back(new Card(toFigure[i], color)); } for(COLOR color=eHEART;color<=eSPADE;color=(COLOR)(color+1)){ this->cards[k++]=new Card(toFigure[i],color); } } this->cards[k++]=new Card('Y',eSPADE); this->cards[k]=new Card('Z',eHEART); allCards.push_back(new Card('Y', eSPADE)); allCards.push_back(new Card('Z', eHEART)); } ~Landlords(){ for(int i=0;i<PLAYERCOUNT;i++){ delete this->player[i]; ~Landlords() { for(int i = 0; i < PLAYERCOUNT; i++) { delete players[i]; } for(int i=0;i<CARDSCOUNT;i++){ delete this->cards[i]; for(auto card : allCards) { delete card; } } void init(){ player[CURRENTPLAYER]->name="Bice"; player[1]->name="玩家2"; player[2]->name="玩家3"; finished=false; youWin=false; landlordWin=false; //抢地主 landlordIndex=ERROR; while(landlordIndex==ERROR){ srand((int)time(0)); } void init() { players[CURRENTPLAYER]->name = "【玩家】"; players[1]->name = "【电脑A】"; players[2]->name = "【电脑B】"; finished = false; youWin = false; // 抢地主 landlordIndex = ERROR; while(landlordIndex == ERROR) { srand(time(nullptr)); shuffle(); landlordIndex=chooseLandlord(); landlordIndex = chooseLandlord(); } cout<<player[landlordIndex]->name<<"\t成为地主\n\n"; this->add3Cards(); LastCards::inst()->player=player[landlordIndex]; cout << endl << "═══════════════════════════════════════" << endl; cout << " " << players[landlordIndex]->name << " 成为地主!" << endl; cout << "═══════════════════════════════════════" << endl << endl; addThreeCards(); LastCards::getInstance()->reset(); } void startGame(){ string inputSrt; void startGame() { string inputStr; CardGroup inputCards; for(int iTurns=landlordIndex;!finished;iTurns++){ if(iTurns>=PLAYERCOUNT){ iTurns=0; } if(iTurns==CURRENTPLAYER){ cout<<endl; player[iTurns]->print(); cout<<"输入提示:Z=大王 Y=小王 0=10 输入可无序 例如:JKQ0A9\n请出牌:\t"; do{ cin>>inputSrt; inputCards.init(inputSrt,player[iTurns]->cards); }while(check(&inputCards)==false); }else{ if(player[iTurns]==LastCards::inst()->player){ //若是上次出牌的是自己,启用提示功能 inputCards.init(player[iTurns]->tip()); }else{ //查找能打得过上家的牌 inputCards.init(LastCards::inst()->findCanBeatFrom(player[iTurns]->cards)); int turnCount = 0; for(int turn = landlordIndex; !finished; turn = (turn + 1) % PLAYERCOUNT, turnCount++) { if(turnCount > 100) break; // 防止死循环 cout << "\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" << endl; if(turn == CURRENTPLAYER) { cout << endl; players[turn]->print(); cout << "\n请出牌(例如:3334 或 10JQKA 或 N): "; do { cin >> inputStr; inputCards.init(inputStr, players[turn]->cards); if(inputCards.type.typeStr == "不出") break; } while(!isValidPlay(inputCards, turn)); } else { // 电脑出牌 if(LastCards::getInstance()->player == players[turn]) { // 上次是自己出的,出最小的牌 inputCards.init(players[turn]->tip()); } else { // 尝试管上家 auto beatCards = LastCards::getInstance()->findCanBeatFrom(players[turn]->cards); if(beatCards.empty()) { inputCards.init("N", players[turn]->cards); } else { inputCards.init(beatCards); } } } player[iTurns]->chupai(inputCards);//出牌 if(player[iTurns]->cards.size()==0){ //玩家手中没牌了,游戏结束 finished=true; landlordWin=iTurns==landlordIndex; if(landlordWin){ youWin=landlordIndex==CURRENTPLAYER; }else{ youWin=landlordIndex!=CURRENTPLAYER; } players[turn]->playCards(inputCards); // 检查游戏结束 if(!players[turn]->hasCards()) { finished = true; bool landlordWin = (turn == landlordIndex); youWin = (landlordWin && landlordIndex == CURRENTPLAYER) || (!landlordWin && landlordIndex != CURRENTPLAYER); } } cout<<"\n_________________________ "<<(youWin?"You Win!":"You Lose!")<<" _________________________\n\n"; cout << "\n═══════════════════════════════════════" << endl; cout << " " << (youWin ? "** 恭喜获胜! **" : "** 遗憾落败! **") << endl; cout << "═══════════════════════════════════════\n" << endl; } private: void add3Cards(){ cout<<"地主3张牌:\t"; for(int i=PLAYERCOUNT*17;i<CARDSCOUNT;i++){ this->cards[i]->print(); player[landlordIndex]->cards.push_back(cards[i]); void addThreeCards() { cout << "地主底牌: "; for(int i = PLAYERCOUNT * 17; i < CARDSCOUNT; i++) { allCards[i]->print(); players[landlordIndex]->cards.push_back(allCards[i]); } cout<<endl; player[landlordIndex]->arrange(); cout << endl; players[landlordIndex]->arrange(); cout << endl; } int chooseLandlord(){ cout<<"\n_________________________ 抢地主 _________________________\n\n"; int first=-1,last,cnt=0,i,j=rand()%PLAYERCOUNT; bool decision; for(i=0;i<PLAYERCOUNT;i++,j==2?j=0:j++){ if(j==CURRENTPLAYER){ decision=makeChoice("是否抢地主?(Y=抢/N=不抢):"); }else{ decision=rand()%2; int chooseLandlord() { cout << "\n════════════════ 抢地主 ════════════════\n" << endl; int first = -1, last = -1, cnt = 0; int start = rand() % PLAYERCOUNT; for(int i = 0; i < PLAYERCOUNT; i++) { int idx = (start + i) % PLAYERCOUNT; bool decision; if(idx == CURRENTPLAYER) { decision = makeChoice(players[idx]->name + " 是否抢地主?(Y/N): "); } else { decision = (rand() % 2 == 1); } if(decision){ if(decision) { cnt++; last=j; if(first==-1){ first=j; } cout<<this->player[j]->name<<"\t抢地主\n"; }else{ cout<<this->player[j]->name<<"\t没有抢\n"; last = idx; if(first == -1) first = idx; cout << players[idx]->name << " ** 抢地主!" << endl; } else { cout << players[idx]->name << " * 不抢" << endl; } } if(cnt==0){ cout<<"没人抢,重新发牌\n"; if(cnt == 0) { cout << "\n无人抢地主,重新发牌..." << endl; return ERROR; } if(cnt==1){ //第一轮只有一人抢地主 return first; } else{ //最后一次争抢 if(first==CURRENTPLAYER){ decision=makeChoice("是否抢地主?(Y=抢/N=不抢):"); }else{ decision=rand()%2; } if(decision){ cout<<this->player[first]->name<<"\t抢地主\n"; if(cnt == 1) return first; // 二次抢地主 cout << "\n────── 决胜局 ──────" << endl; if(first == CURRENTPLAYER) { bool decision = makeChoice(players[first]->name + " 是否抢地主?(Y/N): "); if(decision) { cout << players[first]->name << " ** 抢地主!" << endl; return first; }else{ cout<<this->player[first]->name<<"\t没有抢\n"; return last; } } else if(rand() % 2 == 1) { cout << players[first]->name << " ** 抢地主!" << endl; return first; } cout << players[first]->name << " * 放弃" << endl; return last; } void shuffle(){ int i,j,k; //洗牌 for(i=0;i<CARDSCOUNT;i++){ swap(this->cards[i],this->cards[rand()%CARDSCOUNT]); } void shuffle() { random_shuffle(allCards.begin(), allCards.end()); //分牌 for(k=i=0;i<PLAYERCOUNT;i++){ this->player[i]->cards.clear(); for(j=0;j<17;j++){ this->player[i]->cards.push_back(this->cards[k++]); int k = 0; for(int i = 0; i < PLAYERCOUNT; i++) { players[i]->cards.clear(); for(int j = 0; j < 17; j++) { players[i]->cards.push_back(allCards[k++]); } this->player[i]->arrange();//整理 this->player[i]->print(); players[i]->arrange(); players[i]->print(); } cout << endl; } bool check(CardGroup *cardGroup){ if(cardGroup->type.typeId==ERROR){ cout<<"出牌错误,重新输入\n"; bool isValidPlay(CardGroup& cardGroup, int playerIdx) { if(cardGroup.type.typeId == ERROR) { cout << "* 无效牌型,请重新出牌!" << endl; return false; }else if(cardGroup->type.typeStr=="不出"){ } if(cardGroup.type.typeStr == "不出") { return true; }else if(LastCards::inst()->player!=player[CURRENTPLAYER]&&!cardGroup->isCanBeat(LastCards::inst()->cardGroup)){ cout<<"打不过,重新输入\n"; return false; }else{ return true; } LastCards* last = LastCards::getInstance(); if(last->player && last->player != players[playerIdx] && !last->cardGroup.isEmpty()) { if(!cardGroup.canBeat(last->cardGroup)) { cout << "* 管不上,请重新出牌!" << endl; return false; } } return true; }}; int main(){ Landlords *landlords=new Landlords(); do{ landlords->init();//发牌、抢地主 landlords->startGame();//游戏开始 }while(makeChoice("\n是否继续游戏?(Y=继续/N=结束): ")); delete landlords; return 0;
string getColorChar(COLOR color) { switch(color) { case eHEART: return "红心"; case eDIAMOND: return "黑桃"; case eCLUB: return "梅花"; case eSPADE: return "方块"; default: return ""; }} bool makeChoice(string tip){ cout<<tip;
bool makeChoice(const string& tip) { cout << tip; string input; cin>>input; return input=="Y"||input=="y"; cin >> input; return input == "Y" || input == "y";} bool cmp(Card* a,Card* b){ //比较两张牌大小 if(a->value==b->value){ return a->color>b->color; }else{ return a->value>b->value;
bool cmp(Card* a, Card* b) { if(a->value == b->value) { return a->color > b->color; } return a->value > b->value;}
#ifdef _WIN32#include <windows.h>#else#include <unistd.h>#define Sleep(x) usleep((x)*1000)#endif
int main() { srand(time(nullptr)); Landlords* game = new Landlords(); do { game->init(); game->startGame(); } while(makeChoice("\n是否继续游戏?(Y/N): ")); delete game; cout << "\n感谢游玩!" << endl; return 0;}```