正文
增加 745 行删除 461 行
3 行未改动4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126133 行未改动#define CARDSCOUNT 54#define CURRENTPLAYER 0#define VALUECOUNT 17#define ERROR -1using namespace std;const char toFigure[]="34567890JQKA 2YZ";enum COLOR{ //花色显示ASCII: 3~6eHEART=3,//红桃eDIAMOND,//方片eCLUB, //草花eSPADE //黑桃};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);if(figure=='Z'){cout<<"ZZ";}else if(figure=='Y'){cout<<"YY";}else{cout<<figure<<(char)color;}cout<<' ';}};class CardsType{public://为了规范查找对应牌的方法//统一为3个参数cnt1、isContinuous、cnt2int 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];}if(cntFlag[i]&&minCnt>cntFlag[i]){minCnt=cntFlag[i];}}if(n==4&&maxCnt==4){type.init("炸dan",1,4,0,0);return;}if(n==1){type.init("单牌",2,1,0,0);return;}if(n==2&&maxCnt==2){type.init("对子",3,2,0,0);return;}if(n==3&&maxCnt==3){type.init("三张 ",4,3,0,0);return;}if(n==4&&maxCnt==3){type.init("三带一",5,3,0,1);return;}if(n==5&&maxCnt==3&&minCnt==2){type.init("三带一对",6,3,0,2);return;}if(n==6&&maxCnt==4){type.init("四带二",7,4,0,1);return;}if(n==8&&maxCnt==4&&minCnt==2){type.init("四带二",8,4,0,2);return;}if(n>=5&&maxCnt==1&&cards[0]->value==cards[n-1]->value+n-1){type.init("顺子",9,1,1,0);return;}if(n>=6&&maxCnt==2&&minCnt==2&&cards[0]->value==cards[n-1]->value+n/2-1){type.init("连对",10,2,1,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);}}void init(string inputStr, vector<Card*> &cardsHolded){this->cards.clear();//不出if(inputStr=="N"){this->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;}}//查找手中有没有这些牌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];break;}}if(find){this->cards.push_back(find);}else{cout<<inputStr[i];cout<<"没有找到\t";this->cards.clear();return;}}//end for(i=0;i<inputStr.size();i++)this->arrange();}void init(vector<Card*> newCards){this->cards=newCards;this->arrange();}bool isCanBeat(CardGroup &cardGroup){if(cardGroup.type.typeStr=="王炸"){return false;}else if(this->type.typeStr=="王炸"){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;}}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;}}}if(type.typeStr=="四带二"){for(i=3;i<cards.size();i++){if(cards[i]->value==cards[i-3]->value){return cards[i]->value;}}}return cards[0]->value;}void arrange(){//整理:排序、计算类型sort(this->cards.begin(),this->cards.end(),cmp);this->calType();}};class LastCards{static LastCards *lastCards;public:Player *player;CardGroup cardGroup;static LastCards* inst(){//单例模式if(lastCards==NULL){lastCards=new LastCards();}return lastCards;}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;}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);}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;}}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(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;}}}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]);}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();//没牌打得过时查找有没有炸danif(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]);}break;}}}return ret;}//end vector<Card*> findCanBeatFrom()};LastCards* LastCards::lastCards = NULL;class Player{public:string name;vector<Card*> cards;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();}cout<<"["<<cards.size()<<"]\n";}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;}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["<<this->cards.size()<<"]\n";if(cardGroup.type.typeStr!="不出"){//记录到 LastCards 中LastCards::inst()->player=this;LastCards::inst()->cardGroup.init(cardGroup.cards);}}};class Landlords{Player *player[PLAYERCOUNT];bool finished,youWin,landlordWin;int landlordIndex;Card *cards[CARDSCOUNT];public:Landlords(){int i,j,k;for(i=0;i<PLAYERCOUNT;i++){this->player[i]=new Player();}//54张牌初始化for(k=i=0;i<14;i++){if(toFigure[i]==' '){continue;}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);}~Landlords(){for(int i=0;i<PLAYERCOUNT;i++){delete this->player[i];}for(int i=0;i<CARDSCOUNT;i++){delete this->cards[i];}}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));shuffle();landlordIndex=chooseLandlord();}cout<<player[landlordIndex]->name<<"\t成为地主\n\n";this->add3Cards();LastCards::inst()->player=player[landlordIndex];}void startGame(){string inputSrt;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));}}player[iTurns]->chupai(inputCards);//出牌if(player[iTurns]->cards.size()==0){//玩家手中没牌了,游戏结束finished=true;landlordWin=iTurns==landlordIndex;if(landlordWin){youWin=landlordIndex==CURRENTPLAYER;}else{youWin=landlordIndex!=CURRENTPLAYER;}}}cout<<"\n_________________________ "<<(youWin?"You Win!":"You Lose!")<<" _________________________\n\n";}void add3Cards(){cout<<"地主3张牌:\t";for(int i=PLAYERCOUNT*17;i<CARDSCOUNT;i++){this->cards[i]->print();player[landlordIndex]->cards.push_back(cards[i]);}cout<<endl;player[landlordIndex]->arrange();}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;}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";}}if(cnt==0){cout<<"没人抢,重新发牌\n";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";return first;}else{cout<<this->player[first]->name<<"\t没有抢\n";return last;}}}void shuffle(){int i,j,k;//洗牌for(i=0;i<CARDSCOUNT;i++){swap(this->cards[i],this->cards[rand()%CARDSCOUNT]);}//分牌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++]);}this->player[i]->arrange();//整理this->player[i]->print();}}bool check(CardGroup *cardGroup){if(cardGroup->type.typeId==ERROR){cout<<"出牌错误,重新输入\n";return false;}else 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;}}};int main(){Landlords *landlords=new Landlords();do{landlords->init();//发牌、抢地主landlords->startGame();//游戏开始}while(makeChoice("\n是否继续游戏?(Y=继续/N=结束): "));delete landlords;return 0;}bool makeChoice(string tip){cout<<tip;string input;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;}}```3 行未改动4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968973 行未改动#define CARDSCOUNT 54#define CURRENTPLAYER 0#define VALUECOUNT 17#define ERROR -1using namespace std;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 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();}int calValue() const {for(int i = 0; toFigure[i]; i++) {if(toFigure[i] == figure) {return i;}}return ERROR;}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 << ' ';}};class CardsType {public:int typeId;string typeStr;int cnt1, cnt2;bool isContinuous;CardsType() : typeId(ERROR), cnt1(0), cnt2(0), isContinuous(false) {}bool operator==(const CardsType& other) const {return 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;}};class CardGroup {public:vector<Card*> cards;CardsType type;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(hasKing && hasJoker) {type.init("王炸", 0, 0, false, 0);return;}}// 统计点数int cntFlag[VALUECOUNT] = {0};for(auto card : cards) {cntFlag[card->value]++;}// 统计信息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(n == 4 && maxCnt == 4) {type.init("炸弹", 1, 4, false, 0);return;}// 单牌if(n == 1) {type.init("单牌", 2, 1, false, 0);return;}// 对子if(n == 2 && maxCnt == 2) {type.init("对子", 3, 2, false, 0);return;}// 三张if(n == 3 && maxCnt == 3) {type.init("三张", 4, 3, false, 0);return;}// 三带一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, false, 2);return;}// 四带二(单)if(n == 6 && maxCnt == 4) {type.init("四带二", 7, 4, false, 1);return;}// 四带二(对)if(n == 8 && maxCnt == 4) {type.init("四带二", 8, 4, false, 2);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(isStraight && cards[0]->value <= 12) { // A以下才能组成顺子type.init("顺子", 9, 1, true, 0);return;}}// 连对(至少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(const string& inputStr, vector<Card*>& cardsHolded) {cards.clear();if(inputStr == "N" || inputStr == "n") {calType();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]);}}// 查找对应牌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(!found) {cout << "错误:您没有牌 " << fig << endl;cards.clear();return;}}arrange();}void init(const vector<Card*>& newCards) {cards = newCards;arrange();}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;}if(other.type.typeStr == "炸弹") return false;// 同类型比较if(type.typeId == other.type.typeId && cards.size() == other.cards.size()) {return getMainValue() > other.getMainValue();}return false;}int getMainValue() const {// 获取主牌的点数(用于比较大小)if(cards.empty()) return 0;// 统计点数int cntFlag[VALUECOUNT] = {0};for(auto card : cards) {cntFlag[card->value]++;}// 找出现次数最多的点数int maxCnt = 0, mainValue = 0;for(int i = 0; i < VALUECOUNT; i++) {if(cntFlag[i] > maxCnt) {maxCnt = cntFlag[i];mainValue = i;}}return mainValue;}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 {private:static LastCards* instance;LastCards() : player(nullptr) {}public:Player* player;CardGroup cardGroup;static LastCards* getInstance() {if(!instance) {instance = new LastCards();}return instance;}void reset() {player = nullptr;cardGroup.cards.clear();cardGroup.calType();}vector<Card*> findCanBeatFrom(vector<Card*>& cardsHolded) {vector<Card*> result;// 没有上家牌或上家没出牌if(!player || cardGroup.isEmpty()) {return result;}// 王炸无法被击败if(cardGroup.type.typeStr == "王炸") {return result;}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(!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;}}}}// 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;}}}}}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;}}}}}}}}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;}}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;}}}}result.clear();return result;}};LastCards* LastCards::instance = nullptr;class Player {public:string name;vector<Card*> cards;void arrange() {sort(cards.begin(), cards.end(), cmp);}void print() const {cout << name << ":\t";if(cards.empty()) {cout << "无牌";} else {for(auto card : cards) {card->print();}}cout << "[" << cards.size() << "张]" << endl;}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);}}}cout << "\t剩余[" << cards.size() << "张]" << endl;// 记录到LastCardsif(cardGroup.type.typeStr != "不出") {LastCards::getInstance()->player = this;LastCards::getInstance()->cardGroup.init(cardGroup.cards);} else {// 不出牌也要记录是谁不出LastCards::getInstance()->player = this;}}bool hasCards() const {return !cards.empty();}};class Landlords {private:Player* players[PLAYERCOUNT];vector<Card*> allCards;bool finished;bool youWin;int landlordIndex;public:Landlords() {for(int i = 0; i < PLAYERCOUNT; i++) {players[i] = new Player();}// 初始化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));}}allCards.push_back(new Card('Y', eSPADE));allCards.push_back(new Card('Z', eHEART));}~Landlords() {for(int i = 0; i < PLAYERCOUNT; i++) {delete players[i];}for(auto card : allCards) {delete card;}}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();}cout << endl << "═══════════════════════════════════════" << endl;cout << " " << players[landlordIndex]->name << " 成为地主!" << endl;cout << "═══════════════════════════════════════" << endl << endl;addThreeCards();LastCards::getInstance()->reset();}void startGame() {string inputStr;CardGroup inputCards;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);}}}players[turn]->playCards(inputCards);// 检查游戏结束if(!players[turn]->hasCards()) {finished = true;bool landlordWin = (turn == landlordIndex);youWin = (landlordWin && landlordIndex == CURRENTPLAYER) ||(!landlordWin && landlordIndex != CURRENTPLAYER);}}cout << "\n═══════════════════════════════════════" << endl;cout << " " << (youWin ? "** 恭喜获胜! **" : "** 遗憾落败! **") << endl;cout << "═══════════════════════════════════════\n" << endl;}private:void addThreeCards() {cout << "地主底牌: ";for(int i = PLAYERCOUNT * 17; i < CARDSCOUNT; i++) {allCards[i]->print();players[landlordIndex]->cards.push_back(allCards[i]);}cout << endl;players[landlordIndex]->arrange();cout << endl;}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) {cnt++;last = idx;if(first == -1) first = idx;cout << players[idx]->name << " ** 抢地主!" << endl;} else {cout << players[idx]->name << " * 不抢" << endl;}}if(cnt == 0) {cout << "\n无人抢地主,重新发牌..." << endl;return ERROR;}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 if(rand() % 2 == 1) {cout << players[first]->name << " ** 抢地主!" << endl;return first;}cout << players[first]->name << " * 放弃" << endl;return last;}void shuffle() {random_shuffle(allCards.begin(), allCards.end());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++]);}players[i]->arrange();players[i]->print();}cout << endl;}bool isValidPlay(CardGroup& cardGroup, int playerIdx) {if(cardGroup.type.typeId == ERROR) {cout << "* 无效牌型,请重新出牌!" << endl;return false;}if(cardGroup.type.typeStr == "不出") {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;}};string getColorChar(COLOR color) {switch(color) {case eHEART: return "红心";case eDIAMOND: return "黑桃";case eCLUB: return "梅花";case eSPADE: return "方块";default: return "";}}bool makeChoice(const string& tip) {cout << tip;string input;cin >> input;return input == "Y" || input == "y";}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)#endifint 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;}```