C语言之斗地主游戏

  1. 头文件和命名空间:代码包含了一些基本的头文件,如iostreamvectoralgorithm等,并使用了标准命名空间std

  2. 宏定义:定义了一些宏,如PLAYERCOUNT(玩家数量)、CARDSCOUNT(牌的总数)、VALUECOUNT(牌面值的数量)等。

  3. 颜色枚举:定义了一个COLOR枚举,表示牌的花色。

  4. Card:表示单张牌,包含牌面值、花色和牌的值。有一个print方法用于打印牌的信息。

  5. CardsType:用于表示牌型的类型,如单牌、对子、炸弹等。

  6. CardGroup:表示一组牌,可以是单张牌、对子、顺子等。包含牌的数组和牌型的类型。有方法来计算牌型、初始化牌组、判断是否能打过另一组牌等。

  7. LastCards:用于记录上一次出的牌,使用单例模式实现。

  8. Player:表示玩家,包含玩家的名字、手中的牌、出牌方法等。

  9. Landlords:是游戏的主类,包含玩家数组、牌数组、是否结束游戏等状态。有方法来初始化游戏、开始游戏、洗牌、发牌、抢地主、检查出牌是否合法等。

  10. main函数:程序的入口点,创建Landlords对象,循环进行游戏。

  11. 辅助函数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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LucianaiB

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值