德州撲克

 

一個快速計算德州撲克牌型的方法. 

只有生成圖那個地方是關鍵, 看懂了就全懂了

在車上寫的, 只寫了 同花和 順子,葫蘆, 似乎沒什麼問題

牌用16進製表示 , 高4位 是顏色位, 低4位是數值位

 

/*
  黑桃從 0x02開始,
  紅心從 0x12 開始
  
  16進制的高位 表示顏色 , 低位表示牌值
*/

enum CardColor{
    enum_spade = 0x0, // 黑桃
    enum_heart = 0x10 , //紅心
    enum_diamond = 0x20 , //方塊
    enum_club = 0x30 //梅花
};


struct card_map{
    enum {enum_CardWithoutColor = 15,  enum_CardWithColor = 4};
    card_map(){}
    void clear(){
        memset(__mapWithColor,0,enum_CardWithColor * enum_CardWithoutColor * sizeof(char));
        memset(__mapWithoutColor,0 , enum_CardWithoutColor);
    }

    static int getCardColor(char card){
        return card >> 4 & 0x0f;
    }
    static int getCardValue(char card){
        return card & 0x0f;
    }

    bool genMap(char * inCards , int iLen){
        if(NULL == inCards || iLen == 0)
            return false;
        clear();
        char card = 0;
        for(int i = 0 ; i < iLen ; ++i){
            card = inCards[i];
            // 牌沒有 0  ,從0x02開始
            if(card == 0)
                continue;

            int iColorIndex = getCardColor(card);
            int iValue = getCardValue(card);

            __mapWithoutColor[iValue]++;
            __mapWithColor[iColorIndex][0]++;
            __mapWithColor[iColorIndex][iValue]++;
        }
        return true;
    }

    void show(){
        cout << "no color:" << endl;
        for(int i =  0 ; i < enum_CardWithoutColor; ++i)
            cout <<std::hex <<(int)__mapWithoutColor[i] << " ";
        cout << endl;

        cout << "color:" << endl;
        for(int i = 0; i < enum_CardWithColor;++i){
            for(int j = 0; j < enum_CardWithoutColor; ++j){
                cout << std::hex  <<(int)__mapWithColor[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }

    char __mapWithoutColor[enum_CardWithoutColor];
    char __mapWithColor[enum_CardWithColor][enum_CardWithoutColor];
};


// 同花
bool isFlush(card_map & map , char * outCards , int iLen){

    bool bFound = false;
    int iColorIndex = 0;
    for( int i = 0 ; i < card_map::enum_CardWithColor; ++i){
        if(map.__mapWithColor[i][0] >= 5){
            bFound = true;
            iColorIndex = i;
            break;
        }
    }

    if(bFound && NULL != outCards && iLen >= 5){
        int iIndex = 0;
        int iCount = 0;
        for(int i = card_map::enum_CardWithoutColor-1;  i > 1 && iLen != 0 ; --i){
             iCount = map.__mapWithColor[iColorIndex][i];

             for(int j = 0 ; j < iCount  && iLen != 0; ++j){
                 outCards[iIndex++] = (iColorIndex << 4) | i;
                 --iLen;
             }
        }
    }


    return bFound;
}

//順子
bool isStraight(card_map & map, char * outCards, int iLen){
    bool bFound = false;
    int iFoundIndex = 0;
    for(int i =card_map::enum_CardWithoutColor-1 ; i  > 5 ;  --i){
        if(map.__mapWithoutColor[i] <= 0)
            continue;

        int iCount = 0;
        int iStartIndex = i;
        for(int j = 0 ; j < 5; ++j){
            if(map.__mapWithoutColor[iStartIndex--] > 0){
                ++iCount;
            }
            else
                break;
        }

        if(iCount == 5){
            bFound = true;
            iFoundIndex = i;
            break;
        }
    }

    cout << "index:" << iFoundIndex << endl;

    if(bFound && NULL != outCards && iLen >= 5){

        int iCount = 0;
        int iIndex  = 0;

        for(int j = 0 ; j < 5; ++j){
            for(int i = card_map::enum_CardWithColor -1 ; i >= 0 ; --i){
                if(map.__mapWithColor[i][iFoundIndex] > 0){
                    outCards[iIndex++] = (i<<4) | iFoundIndex;
                    --iFoundIndex;
                    break;
                }
            }
        }
    }


    return bFound;
}


//葫蘆 3帶2
bool isFullHouse(card_map  &map , char * out,  int iLen){

    bool bFound = false;
    int iFirstIndex=  0;
    int iSecondIndex = 0;
    bool bFirstFound = false;
    bool bSecondFound = false;


    for(int i = card_map::enum_CardWithoutColor - 1; i > 1; --i){
        if(map.__mapWithoutColor[i] == 3){
            bFirstFound = true;
            iFirstIndex = i;
            break;
        }
    }

    if(!bFirstFound)
        return false;

    for(int i =card_map::enum_CardWithoutColor - 1; i > 1; --i){
        if(i == iFirstIndex)
            continue;

        if(map.__mapWithoutColor[i] >= 2){
            bSecondFound = true;
            iSecondIndex = i;
            break;
        }
    }

    if(!bSecondFound)
        return false;

    if(bFirstFound && bSecondFound)
        bFound = true;


    int iIndex = 0;
    if(bFound && out != NULL && iLen >= 5){

        int iCount = 0;
        for(int i= card_map::enum_CardWithColor - 1; i >= 0 && iLen != 0 ; --i){
             iCount = map.__mapWithColor[i][iFirstIndex];

             while(iCount != 0 && iLen != 0){
                 out[iIndex++] = (i<<4) | iFirstIndex;
                 --iCount;
                 --iLen;
             }
        }

        for(int i = card_map::enum_CardWithColor - 1; i >= 0 && iLen != 0; --i){
            iCount = map.__mapWithColor[i][iSecondIndex];

            while(iCount != 0 && iLen != 0){
                out[iIndex++] = (i << 4) | iSecondIndex;
                --iCount;
                --iLen;
            }
        }

    }


    return bFound;
}

void showBuffer( char * arr ,int iLen){

    for(int i =0 ; i< iLen ; ++i){
        cout << std::hex << (int)arr[i] << " ";
    }
    cout << endl;
}

int main(int argc, char* argv[])
{
    char arr1[7] = {0x23,0x13,0x33,0x16,0x02,0x06,0x26};
    card_map map;
    map.genMap(arr1,7);
    map.show();
    char buffer[5] = {0};

// 同花
    bool bIsFlush = isFlush(map,buffer, 5);
    if(bIsFlush){
        cout << "flush:" << endl;
        showBuffer(buffer, 5);
    }

// 葫蘆

    bool bIsFullHouse = isFullHouse(map,buffer, 5);
    if(bIsFullHouse){
        cout << "fullhouse" << endl;
        showBuffer(buffer, 5);
    }

// 順子

    bool bIsStraight = isStraight(map,buffer,5);
    if(bIsStraight){
        cout << "straight" << endl;
        showBuffer(buffer,5);
    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章