Exemple #1
0
QList<card> Method::PlayBeatHand(Hand hand)
{
    // 先固定住最优顺子,从余下牌中打出
    QList<card>  left = m_cards;
    QList<QList<card> > cardlll=Method(m_player,left).PickOptimalSeqSingles();
    for(int i=0;i<cardlll.size();i++)
        for(int j=0;j<cardlll[i].size();j++ )
            left.removeOne(cardlll[i][j]);

    //left.Remove(Method(m_player, left).PickOptimalSeqSingles());

    if (hand.getHandType() == Hand_Single)	// 如果压单牌,尽量从单张牌中挑
    {
        QList<QList<card> > singleArray = Method(m_player, left).FindCardsByCount(1);
        for (int i = 0; i < singleArray.size(); i++)
        {
            if (Hand(singleArray[i]).Defeat(hand))
            {
                return singleArray[i];
            }
        }
    }
    else if (hand.getHandType() == Hand_Pair)	// 如果压双牌,尽量从双牌中挑
    {
        QList<QList<card> > pairArray = Method(m_player, left).FindCardsByCount(2);
        for (int i = 0; i < pairArray.size(); i++)
        {
            if (Hand(pairArray[i]).Defeat(hand))
            {
                return pairArray[i];
            }
        }
    }

    Player* nextPlayer = m_player->getNextPlayer();
    QList<QList<card> > beatCardsArray = Method(m_player, left).FindHand(hand, true);
    if (!beatCardsArray.isEmpty())
    {
        if (m_player->getRole() != nextPlayer->getRole() &&
                nextPlayer->getCards().size() <= 2)
        {
            return beatCardsArray.back();
        }
        else
        {
            return beatCardsArray.front();
        }
    }
    else	// 余下牌没法打时,只好从顺子中挑牌
    {
        beatCardsArray = Method(m_player, m_cards).FindHand(hand, true);
        if (!beatCardsArray.isEmpty())
        {
            if (m_player->getRole() != nextPlayer->getRole() &&
                    nextPlayer->getCards().size() <= 2)
            {
                return beatCardsArray.back();
            }
            else
            {
                return beatCardsArray.front();
            }
        }
    }

    // 对家剩牌小于3张,有炸则炸
    Player* hitPlayer = m_player->getHitPlayer();
    if (m_player->getRole() != hitPlayer->getRole())
    {
        if (hitPlayer->getCards().size() <= 3)
        {
            QList<QList<card> > bombs = FindCardsByCount(4);
            if (!bombs.isEmpty())
            {
                return bombs[0];
            }
        }
    }

    QList<card>  empty;
    empty.clear();
    return empty;
}
Exemple #2
0
QList<QList<card> > Method::FindHand(Hand hand, bool beat)
{
    HandType handType = hand.getHandType();
    CardPoint basePoint = CardPoint(hand.getBasePoint());
    int extra = hand.getExtra();

    if (handType == Hand_Pass)
    {}
    else if (handType == Hand_Single)
    {
        QList<QList<card> > findCardsArray;

        CardPoint beginPoint = beat ? CardPoint(basePoint + 1) : CardPoint(Card_Begin + 1);
        for (CardPoint point = beginPoint; point < Card_End; point = CardPoint(point + 1))
        {
            QList<card> findCards = FindSamePointCards(point, 1);
            if (!findCards.isEmpty())
            {
                findCardsArray.append(findCards);
            }
        }
        return findCardsArray;
    }
    else if (handType == Hand_Pair)
    {
        QList<QList<card> > findCardsArray;

        CardPoint beginPoint = beat ? CardPoint(basePoint + 1) : CardPoint(Card_Begin + 1);
        for (CardPoint point = beginPoint; point < Card_End; point = CardPoint(point + 1))
        {
            QList<card> findCards = FindSamePointCards(point, 2);
            if (!findCards.isEmpty())
            {
                findCardsArray.append(findCards);
            }
        }

        return findCardsArray;
    }
    else if (handType == Hand_Triple)
    {
        QList<QList<card> > findCardsArray;

        CardPoint beginPoint = beat ? CardPoint(basePoint + 1) : CardPoint(Card_Begin + 1);
        for (CardPoint point = beginPoint; point < Card_End; point = CardPoint(point + 1))
        {
            QList<card> findCards = FindSamePointCards(point, 3);
            if (!findCards.isEmpty())
            {
                findCardsArray.append(findCards);
            }
        }

        return findCardsArray;
    }
    else if (handType == Hand_Triple_Single)
    {
        QList<QList<card> > findCardsArray;

        CardPoint beginPoint = beat ? CardPoint(basePoint + 1) : CardPoint(Card_Begin + 1);
        for (CardPoint point = beginPoint; point < Card_End; point = CardPoint(point + 1))
        {
            QList<card> findCards = FindSamePointCards(point, 3);
            if (!findCards.isEmpty())
            {
                findCardsArray.append(findCards);
            }
        }

        if (!findCardsArray.isEmpty())
        {
            QList<card> remainCards = m_cards;
            for(int i=0;i<findCardsArray.size();i++)
            {
                for(int j=0;j<findCardsArray[i].size();j++)
                    remainCards.removeOne((findCardsArray[i])[j]);
            }
            //remainCards.Remove(findCardsArray);

            Method st(m_player, remainCards);
            QList<QList<card> > oneCardsArray = st.FindHand(Hand(Hand_Single, Card_Begin, 0), false);
            if (!oneCardsArray.isEmpty())
            {
                for (int i = 0; i < findCardsArray.size(); i++)
                {
                    findCardsArray[i] .append(oneCardsArray[0]);
                }
            }
            else
            {
                findCardsArray.clear();
            }
        }

        return findCardsArray;
    }
    else if (handType == Hand_Triple_Pair)
    {
        QList<QList<card> > findCardsArray;

        CardPoint beginPoint = beat ? CardPoint(basePoint + 1) : CardPoint(Card_Begin + 1);
        for (CardPoint point = beginPoint; point < Card_End; point = CardPoint(point + 1))
        {
            QList<card>  findCards = FindSamePointCards(point, 3);
            if (!findCards.isEmpty())
            {
                findCardsArray << findCards;
            }
        }

        if (!findCardsArray.isEmpty())
        {
            QList<card>  remainCards = m_cards;
            for(int i=0;i<findCardsArray.size();i++)
                for(int j=0;j<findCardsArray[i].size();j++)
                    remainCards.removeOne(findCardsArray[i][j]);

            //            remainCards.Remove(findCardsArray);

            Method st(m_player, remainCards);
            QList<QList<card> > pairCardsArray = st.FindHand(Hand(Hand_Pair, Card_Begin, 0), false);
            if (!pairCardsArray.isEmpty())
            {
                for (int i = 0; i < findCardsArray.size(); i++)
                {
                    findCardsArray[i].append(pairCardsArray[0]);
                }
            }
            else
            {
                findCardsArray.clear();
            }
        }

        return findCardsArray;
    }
    else if (handType == Hand_Plane)
    {
        QList<QList<card> > findCardsArray;

        CardPoint beginPoint = beat ? CardPoint(basePoint + 1) : CardPoint(Card_Begin + 1);
        for (CardPoint point = beginPoint; point <= Card_K; point = CardPoint(point + 1))
        {
            QList<card>  prevCards = FindSamePointCards(point, 3);
            QList<card>  nextCards = FindSamePointCards(CardPoint(point+1), 3);
            if (!prevCards.isEmpty() && !nextCards.isEmpty())
            {
                findCardsArray.append(prevCards);
                findCardsArray.append(nextCards);
            }
        }
    }
    else if (handType == Hand_Plane_Two_Single)
    {
        QList<QList<card> > findCardsArray;

        CardPoint beginPoint = beat ? CardPoint(basePoint + 1) : CardPoint(Card_Begin + 1);
        for (CardPoint point = beginPoint; point <= Card_K; point = CardPoint(point + 1))
        {
            QList<card>  prevCards = FindSamePointCards(point, 3);
            QList<card>  nextCards = FindSamePointCards(CardPoint(point+1), 3);
            if (!prevCards.isEmpty() && !nextCards.isEmpty())
            {
                QList<card>  findCards;

                findCards.append(prevCards);
                findCards.append(nextCards);
                findCardsArray.append(findCards);
            }
        }

        if (!findCardsArray.isEmpty())
        {
            QList<card>  remainCards = m_cards;
            for(int i=0;i<findCardsArray.size();i++)
            {
                for(int j=0;j<findCardsArray[i].size();j++)
                    remainCards.removeOne((findCardsArray[i])[j]);
            }

            Method st(m_player, remainCards);
            QList<QList<card> > oneCardsArray = st.FindHand(Hand(Hand_Single, Card_Begin, 0), false);
            if (oneCardsArray.size() >= 2)
            {
                for (int i = 0; i < findCardsArray.size(); i++)
                {
                    QList<card>  oneCards;
                    oneCards.append(oneCardsArray[0]);
                    oneCards.append(oneCardsArray[1]);
                    findCardsArray[i] << oneCards;
                }
            }
            else
            {
                findCardsArray.clear();
            }
        }

        return findCardsArray;
    }
    else if (handType == Hand_Plane_Two_Pair)
    {
        QList<QList<card> > findCardsArray;

        CardPoint beginPoint = beat ? CardPoint(basePoint + 1) : CardPoint(Card_Begin + 1);
        for (CardPoint point = beginPoint; point <= Card_K; point = CardPoint(point + 1))
        {
            QList<card>  prevCards = FindSamePointCards(point, 3);
            QList<card>  nextCards = FindSamePointCards(CardPoint(point+1), 3);
            if (!prevCards.isEmpty() && !nextCards.isEmpty())
            {
                QList<card>  findCards;
                findCards << prevCards << nextCards;
                findCardsArray << findCards;
            }
        }

        if (!findCardsArray.isEmpty())
        {
            QList<card>  remainCards = m_cards;
            for(int i=0;i<findCardsArray.size();i++)
            {
                for(int j=0;j<findCardsArray[i].size();j++)
                    remainCards.removeOne((findCardsArray[i])[j]);
            }

            Method st(m_player, remainCards);
            QList<QList<card> > pairCardsArray = st.FindHand(Hand(Hand_Pair, Card_Begin, 0), false);
            if (pairCardsArray.size() >= 2)
            {
                for (int i = 0; i < findCardsArray.size(); i++)
                {
                    QList<card>  pairCards;
                    pairCards << pairCardsArray[0] << pairCardsArray[1];
                    findCardsArray[i] << pairCards;
                }
            }
            else
            {
                findCardsArray.clear();
            }
        }

        return findCardsArray;
    }
    else if (handType == Hand_Seq_Pair)
    {
        if (beat)
        {
            QList<QList<card> > findCardsArray;
            for (CardPoint point = CardPoint(basePoint + 1); point <= Card_Q; point = CardPoint(point + 1))
            {
                bool seqPairFound = true;
                QList<card>  seqPair;

                for (int i = 0; i < extra; i++)
                {
                    QList<card>  cards = FindSamePointCards(CardPoint(point + i), 2);
                    if (cards.isEmpty() || (point + i >= Card_2))	// 连对中断,或顶到2了
                    {
                        seqPairFound = false;
                        seqPair.clear();
                        break;
                    }
                    else
                    {
                        seqPair << cards;
                    }
                }

                if (seqPairFound)
                {
                    findCardsArray << seqPair;
                    return findCardsArray;
                }
            }

            return findCardsArray;
        }
        else
        {
            QList<QList<card> > findCardsArray;

            for (CardPoint point = Card_3; point <= Card_Q; point = CardPoint(point + 1))
            {
                QList<card>  cards0 = FindSamePointCards(point, 2);
                QList<card>  cards1 = FindSamePointCards(CardPoint(point + 1), 2);
                QList<card>  cards2 = FindSamePointCards(CardPoint(point + 2), 2);

                if (cards0.isEmpty() || cards1.isEmpty() || cards2.isEmpty()) continue;

                QList<card>  baseSeq;
                baseSeq << cards0 << cards1 << cards2;
                findCardsArray << baseSeq;

                int followed = 3;
                QList<card>  alreadyFollowedCards;

                while (true)
                {
                    CardPoint followedPoint = CardPoint(point + followed);
                    if (followedPoint >= Card_2)
                    {
                        break;
                    }

                    QList<card>  followedCards = FindSamePointCards(followedPoint, 2);
                    if (followedCards.isEmpty())
                    {
                        break;
                    }
                    else
                    {
                        alreadyFollowedCards << followedCards;
                        QList<card>  newSeq = baseSeq;
                        newSeq << alreadyFollowedCards;

                        findCardsArray << newSeq;
                        followed++;
                    }
                }
            }

            return findCardsArray;
        }
    }
    else if (handType == Hand_Seq_Single)
    {
        if (beat)
        {
            QList<QList<card> > findCardsArray;
            for (CardPoint point = CardPoint(basePoint + 1); point <= Card_10; point = CardPoint(point + 1))
            {
                bool seqSingleFound = true;
                QList<card>  seqSingle;

                for (int i = 0; i < extra; i++)
                {
                    QList<card>  cards = FindSamePointCards(CardPoint(point + i), 1);
                    if (cards.isEmpty() || (point + extra >= Card_2))
                    {
                        seqSingleFound = false;
                        seqSingle.clear();
                        break;
                    }
                    else
                    {
                        seqSingle << cards;
                    }
                }

                if (seqSingleFound)
                {
                    findCardsArray << seqSingle;
                    return findCardsArray;
                }
            }
        }
        else
        {
            QList<QList<card> > findCardsArray;

            for (CardPoint point = Card_3; point <= Card_10; point = CardPoint(point + 1))
            {
                QList<card>  cards0 = FindSamePointCards(point, 1);
                QList<card>  cards1 = FindSamePointCards(CardPoint(point + 1), 1);
                QList<card>  cards2 = FindSamePointCards(CardPoint(point + 2), 1);
                QList<card>  cards3 = FindSamePointCards(CardPoint(point + 3), 1);
                QList<card>  cards4 = FindSamePointCards(CardPoint(point + 4), 1);

                if (cards0.isEmpty() || cards1.isEmpty() || cards2.isEmpty()
                        || cards3.isEmpty() || cards4.isEmpty())
                {
                    continue;
                }

                QList<card>  baseSeq;
                baseSeq << cards0 << cards1 << cards2 << cards3 << cards4;
                findCardsArray << baseSeq;

                int followed = 5;
                QList<card>  alreadyFollowedCards;

                while (true)
                {
                    CardPoint followedPoint = CardPoint(point + followed);
                    if (followedPoint >= Card_2)
                    {
                        break;
                    }

                    QList<card>  followedCards = FindSamePointCards(followedPoint, 1);
                    if (followedCards.isEmpty())
                    {
                        break;
                    }
                    else
                    {
                        alreadyFollowedCards << followedCards;
                        QList<card>  newSeq = baseSeq;
                        newSeq << alreadyFollowedCards;

                        findCardsArray << newSeq;
                        followed++;
                    }
                }
            }

            return findCardsArray;
        }
    }
    else if (handType == Hand_Bomb)
    {
        QList<QList<card> > findCardsArray;

        CardPoint beginPoint = beat ? CardPoint(basePoint + 1) : CardPoint(Card_Begin + 1);
        for (CardPoint point = beginPoint; point < Card_End; point = CardPoint(point + 1))
        {
            QList<card>  findCards = FindSamePointCards(point, 4);
            if (!findCards.isEmpty())
            {
                findCardsArray << findCards;
            }
        }

        return findCardsArray;
    }

    QList<QList<card> > c;
    c.clear();
    return c;
}