写个单机版斗地主程序,复习c++面向对象

博主用两天时间完成了一个斗地主程序的初版,随后决定进行重构,以提升代码规范性和可读性。通过严格遵循面向对象原则,尽管重构过程花费了比预期更长的时间,但代码质量得到了一定改善。博主反思大学时学到的面向对象知识过于基础,强调面向对象思维的重要性。现公开代码,期待批评指正。
摘要由CSDN通过智能技术生成

2天时间敲了一个斗地主代码,觉得自己代码极不规范,决定重构,目标是用严格的面向对象思维、规范的代码风格、规范的命名方式重写一份。原以为这只是1小时的工作,终却断断续续花了好几天查重写后的bug。虽然代码还是很挫,但相比之前已经有了一点点可读性。大学时学了c++就以为懂了面向对象,现在回想学到的都是些渣渣,面向对象重要的是思维而不是语言规则。为提高自己代码质量不断努力。



 

#include <iostream>
#include <vector>
#define PLAYERCOUNT 3
#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   //黑桃 
};

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、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];
            }
            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&
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值