判断牌类型

原创 2016年06月01日 02:43:21
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;

#define CREATEVARIABLE(Type, Name)\
protected:\
    Type Name;\
public:\
    void set##Name(Type value){\
        Name = value;\
    }\
    Type get##Name()const{\
        return Name;\
    }

struct CardsTypeCount {
    int One;
    int Two;
    int Three;
    int Four;
    vector<int> OneArray;
    vector<int> TwoArray;
    vector<int> ThreeArray;
    vector<int> FourArray;
};
class Cards
{
    CREATEVARIABLE(vector<int>, obj1)
        CREATEVARIABLE(vector<int>, obj2)
        CREATEVARIABLE(vector<int>, obj3)

    enum CARDSTYPE {
        TypeError = 0,
        Single,	                //1
        DanShunzi,		//单顺    1*n(n>=5)
        Double,			//2
        Rocket,			//火箭    双王
        DoubleShunZi,		//双顺    2*n(n>=3)
        Three,			//3
        ThreeBandOne,		//3+1
        ThreeBandTwo,		//3+2
        ThreeShunZi,	        //三顺    3*n(n>=2)
        AircraftWithWingsOne,	//3*n+n
        AircraftWithWingsTwo,   //3*n+2*n(n>=2)
        Bomb,			//炸弹    4*1
        FourBandOne,		//4+1
        FourBandTwo		//4+2*2
    };
public:
    void shuffleCards();
    CARDSTYPE judge( vector<int> &data);
    vector<int> sort(vector<int> &data);

public:
    CardsTypeCount identical(const vector<int> &data);
    CARDSTYPE switchOne(const vector<int> &data, const CardsTypeCount &TypeCount);
    CARDSTYPE switchTwo(const vector<int> &data, const CardsTypeCount &TypeCount);
    CARDSTYPE switchThree(const vector<int> &data, const CardsTypeCount &TypeCount);
    CARDSTYPE switchFour(const vector<int> &data, const CardsTypeCount &TypeCount);
    void put(vector<int>&data,int count);
};

void Cards::shuffleCards()
{
    vector<int> init;
    for (int i = 0; i < 54; i++)
        init.push_back(i);

    random_shuffle(init.begin(), init.end());
    for(int i=0;i<13;i++)
        cout <<setw(3)<<i+3;

    for(int i=0;i<54;i++)
    {
        if(!(i%13))
            cout <<endl<<endl;
        cout << setw(3)<<i;

    }
cout <<endl<<endl;
    obj1.clear();
    obj2.clear();
    obj3.clear();
    for (int i = 0; i < 17; i++)
        obj1.push_back(init[i]);
    for (int i = 17; i < 34; i++)
        obj2.push_back(init[i]);
    for (int i = 34; i < 51; i++)
        obj3.push_back(init[i]);
}

Cards::CARDSTYPE Cards::judge( vector<int> &data)
{
    CARDSTYPE Type = CARDSTYPE::TypeError;
    const CardsTypeCount &TypeCount = identical(data);
    int Count;
    if (TypeCount.OneArray.size() ? data[0] == TypeCount.OneArray[0] : 0)
        Count = 1;
    else if (TypeCount.TwoArray.size() ? data[0] == TypeCount.TwoArray[0] : 0)
        Count = 2;
    else if (TypeCount.ThreeArray.size() ? data[0] == TypeCount.ThreeArray[0] : 0)
        Count = 3;
    else if(TypeCount.FourArray.size() ? data[0] == TypeCount.FourArray[0] : 0)
        Count = 4;

    const vector<int>&Tmp = sort(data);

    switch (Count)
    {
    case 1:Type = switchOne(Tmp, TypeCount); break;
    case 2:Type = switchTwo(Tmp, TypeCount); break;
    case 3:Type = switchThree(Tmp, TypeCount); break;
    case 4:Type = switchFour(Tmp, TypeCount);
    }
    return Type;
}

vector<int> Cards::sort(vector<int>& data)
{
    int Array[4][15] = {0};
    for (int i = 0; i < data.size(); i++)
    {
        if ((data[i] == 52) || (data[i] == 53))
        {
            if(data[i] == 52)
            Array[0][13] = 52;
            else
                Array[0][14] = 53;
            continue;
        }
        for (int j = 0; j < 4; j++)
            if (Array[j][data[i]%13] == 0)
            {
                Array[j][data[i]%13] = data[i];
                break;
            }
    }
    data.clear();
    vector<int>Return;
    for (int i = 3; i >= 0;i--)
    {
        for (int j = 14; j >= 0; j--)
        {
            if (Array[i][j] != 0)
            {
                for (int k = i; k >= 0; k--)
                {
                    data.push_back(Array[k][j]);
                    Return.push_back(Array[k][j] % 13);
                    Array[k][j] = 0;
                }
            }
        }
    }
    return Return;
}

CardsTypeCount Cards::identical(const vector<int>& data)
{
    vector<int>Count;
    vector<int>Type;
    bool off;
    for (auto value : data)
    {
        off = true;
        for (int i = 0; i < Type.size();i++)
        {
            if (value == Type[i])
            {
                Count[i]++;
                off = false;
            }
        }
        if (off)
        {
            Type.push_back(value);
            Count.push_back(1);
        }
    }

    CardsTypeCount TypeCount;
    TypeCount.One = TypeCount.Two = TypeCount.Three = TypeCount.Four = 0;

    for (int i = 0; i < Type.size();i++)
    {
        switch (Count[i])
        {
        case 1:
            TypeCount.One++;
            TypeCount.OneArray.push_back(Type[i]);	break;
        case 2:
            TypeCount.Two++;
            TypeCount.TwoArray.push_back(Type[i]);	break;
        case 3:
            TypeCount.Three++;
            TypeCount.ThreeArray.push_back(Type[i]);	break;
        case 4:
            TypeCount.Four++;
            TypeCount.FourArray.push_back(Type[i]);
        }
    }
    return TypeCount;
}

Cards::CARDSTYPE Cards::switchOne(const vector<int> &data, const CardsTypeCount &TypeCount)
{
    const int &Size = data.size();
    if (Size == 1)
        return CARDSTYPE::Single;
    else if ((Size == 2) && (data[0] == 53) && (data[1] == 52))
        return CARDSTYPE::Rocket;
    else if(Size >=5 )
    {
        for (int i = 0; i < Size-1; i++)
        {
            if (data[i] - 1 != data[i + 1])
                break;
            else
            {
                if (i == Size - 2)
                    return CARDSTYPE::DanShunzi;
            }
        }
    }
    return CARDSTYPE::TypeError;
}

Cards::CARDSTYPE Cards::switchTwo(const vector<int> &data, const CardsTypeCount &TypeCount)
{
    const int &Size = data.size();
    if(!(Size%2))
    {
        if (Size == 2)
            return CARDSTYPE::Double;
        else if (Size >=6)
            if(TypeCount.Two == Size / 2.0)
                return CARDSTYPE::DoubleShunZi;
    }

    return CARDSTYPE::TypeError;
}

Cards::CARDSTYPE Cards::switchThree(const vector<int> &data, const CardsTypeCount &TypeCount)
{
    const int &Size = data.size();
    if (Size == 3)
        return CARDSTYPE::Three;
    else if (Size == 4)
        return CARDSTYPE::ThreeBandOne;
    else if ((TypeCount.Three == 1) && (TypeCount.Two == 1))
        return CARDSTYPE::ThreeBandTwo;
    else if (Size >= 6)
    {
        if (TypeCount.Three == Size / 3.0)
            return CARDSTYPE::ThreeShunZi;
        else if (TypeCount.Three == TypeCount.One)
            return CARDSTYPE::AircraftWithWingsOne;
        else if (TypeCount.Three == TypeCount.Two)
            return CARDSTYPE::AircraftWithWingsTwo;
    }
    return CARDSTYPE::TypeError;
}

Cards::CARDSTYPE Cards::switchFour(const vector<int> &data, const CardsTypeCount &TypeCount)
{
    const int &Size = data.size();
    switch (Size)
    {
    case 4:
        return CARDSTYPE::Bomb;
    case 5:
        return CARDSTYPE::FourBandOne;
    case 6:
        if (TypeCount.Two == 1)
            return CARDSTYPE::FourBandTwo;
    }

    return CARDSTYPE::TypeError;
}

void Cards::put(vector<int>& data, int count)
{
    cout << endl << "count:" << count << endl;
    for (auto value : data)
        cout << setw(3) << value;
}

int main()
{
    Cards cards;
    srand(unsigned(time(nullptr)));
    while(1)
    {
        cards.shuffleCards();
        for (auto value : cards.getobj1())
        {
            cout << setw(3) << value;
        }
        cout << endl << endl;
        cards.sort(cards.getobj1());
        getchar();
    }

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

斗地主算法的设计与实现(二)--如何判断一手牌的类型(单,对子,三不带,三带一,四代二等)

牌的类型共有10种: 1. 单 2.对子 3.3不带 4.3带1 5.炸弹 6.顺子 7.4带2 8.连队 9.飞机 10.对王 单牌:任意一张单牌。 对牌:任意两张点数相同的牌。 三张:任意三张点...

判断三角形的类型

  • 2014年03月21日 19:09
  • 481B
  • 下载

jquery1.9判断浏览器类型和版本

  • 2015年05月29日 15:56
  • 1KB
  • 下载

通过JS判断浏览器类型,详细区分IE各版本浏览器

今天用到JS判断浏览器类型,于是就系统整理了一下,便于后期使用。 /* * 描述:判断浏览器信息 * 编写:LittleQiang_w * 日期:2016.1...

判断磁盘类型的代码-getdrivetype

  • 2011年03月02日 10:43
  • 696B
  • 下载

asp.net 判断控件类型源码

  • 2009年11月16日 20:47
  • 1KB
  • 下载

通过js判断打开页面的手机浏览器类型

转载的这篇文章很不错,主要是介绍通过js来判断访问页面的浏览器类型,可以做不同的处理。自己借鉴完之后,也再补充一点,现在项目中,还要注意,通过复制链接访问的浏览器方式和通过app应用访问的内置浏览器方...

判断文件类型,测试文件长度

  • 2011年12月29日 22:47
  • 2KB
  • 下载

js文件_判断浏览器类型

  • 2013年10月30日 22:35
  • 214B
  • 下载

Java实例instanceof类型判断

instanceof是Java的一个二元操作符,和==,>,   String s = "I AM an Object!";   boolean isObject = s instanceof O...
  • Mydwr
  • Mydwr
  • 2013年07月15日 14:55
  • 14919
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:判断牌类型
举报原因:
原因补充:

(最多只允许输入30个字)