判断牌类型

来源:互联网 发布:高清影视软件 编辑:程序博客网 时间:2024/05/22 14:13
#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;}

0 0
原创粉丝点击