得分卡片组

时间:2016-04-21 00:18:02

标签: c++

所以我有这个特殊的问题,需要确定一个获胜的手。我在下面发布了我的代码。

#include <iostream>

using namespace std;

#include <stdlib.h>
#include <time.h>
#include <string.h>

#include "Card .h"

char * ValueNames[13] = {
    "Two",
    "Three",
    "Four",
    "Five",
    "Six",
    "Seven",
    "Eight",
    "Nine",
    "Ten",
    "Jack",
    "Queen",
    "King",
    "Ace"
};

void Display(const Card & C)
{
    cout << (char)C.Suit << ' ' << ValueNames[C.Value] << endl;
}

void InitDeck(Card Deck[])
{
    int     i;
    Suits   S;
    Values  V;

    srand(time(0)); 
    i = 0;
    for (S = Hearts; S <= Spades; S = (Suits)(S + 1))
        for (V = Two; V <= Ace; V = (Values)(V + 1))
        {
            Deck[i].Suit = S;
            Deck[i].Value = V;
            i++;
        }
}

void SortHand(Card Hand[])
{
    int     i;
    int     j;
    int     CardOne;
    int     CardTwo;
    Card    Temp;



    for (j = 0; j < 5; j++)
    {
        CardOne = 0;
        CardTwo = 1;
        for (i = 0; i <= 4; i++)
        {
            if (Hand[CardOne].Value > Hand[CardTwo].Value)
            {
                Temp = Hand[CardOne];
                Hand[CardOne] = Hand[CardTwo];
                Hand[CardTwo] = Temp;
            }
            else;

            CardOne++;
            CardTwo++;
            i++;
        }
    }
    if (Hand[3].Value > Hand[4].Value)
    {
        Temp = Hand[3];
        Hand[3] = Hand[4];
        Hand[4] = Temp;
    }
    if (Hand[2].Value > Hand[3].Value)
    {
        Temp = Hand[2];
        Hand[2] = Hand[3];
        Hand[3] = Temp;
    }

    if (Hand[1].Value > Hand[2].Value)
    {
        Temp = Hand[1];
        Hand[1] = Hand[2];
        Hand[2] = Temp;
    }
    if (Hand[0].Value > Hand[1].Value)
    {
        Temp = Hand[0];
        Hand[0] = Hand[1];
        Hand[1] = Temp;
    }

}

void ShowDeck(const Card Deck[])
{
    int     i;

    for (i = 0; i < NumCardsInDeck; i++)
        Display(Deck[i]);
}

void ShowHand(Card Deck[])
{
    int     i;
    for (i = 0; i < 5; i++)
        Display(Deck[i]);
}

void Shuffle(Card Deck[])
{
    int     CardOne;
    int     CardTwo;
    Card    Temp;

    for (CardOne = 0; CardOne <NumCardsInDeck; CardOne++)
    {
        CardTwo = rand() % NumCardsInDeck;
        Temp = Deck[CardOne];
        Deck[CardOne] = Deck[CardTwo];
        Deck[CardTwo] = Temp;
    }
}

void Deal(Card Deck[])
{
    int     CardOne;
    int     CardTwo;
    Card    Temp;

    for (CardOne = 0; CardOne < 5; CardOne++)
    {
        CardTwo = rand() % 5;
        Temp = Deck[CardOne];
        Deck[CardOne] = Deck[CardTwo];
        Deck[CardTwo] = Temp;
    }
}

bool    HighestCard(Card Hand[])
{
    if (Hand[0].Value != Hand[1].Value && Hand[0].Value != Hand[2].Value && Hand[0].Value != Hand[3].Value
        && Hand[0].Value != Hand[4].Value && Hand[1].Value != Hand[2].Value && Hand[1].Value != Hand[3].Value &&
        Hand[1].Value != Hand[4].Value && Hand[2].Value != Hand[3].Value && Hand[2].Value != Hand[4].Value && Hand[3].Value != Hand[4].Value)
    {
        return true;
        cout << "The High Card " << endl;
    }
    else
        return false;
}

bool    OnePair(Card Hand[])
{
    if ((Hand[0].Value == Hand[1].Value && Hand[0].Value != Hand[2].Value) ||
        (Hand[1].Value == Hand[2].Value && Hand[1].Value != Hand[3].Value) ||
        (Hand[2].Value == Hand[3].Value && Hand[2].Value != Hand[4].Value) ||
        Hand[3].Value == Hand[4].Value)
    {
        return true;
        cout << "One Pair" << endl;
        //scoring = (scoring + 16);

    }
    else
        return false;
}

bool IsTwoPair(Card Hand[])
{
    if ((Hand[0].Value == Hand[1].Value && Hand[2].Value == Hand[3].Value) || (Hand[1].Value == Hand[2].Value && Hand[3].Value == Hand[4].Value) || (Hand[0].Value == Hand[1].Value && Hand[3].Value == Hand[4].Value))
    {
        return true;
        cout << "Two Pair" << endl;
        //scoring = (scoring + 17);
    }
    else
        return false;
}

bool IsThreeOfAKind(Card Hand[])
{
    if ((Hand[0].Value == Hand[2].Value) || (Hand[1].Value == Hand[3].Value) || (Hand[2].Value == Hand[4].Value))
    {
        return true;
        cout << "Three of a Kind" << endl;
        //scoring = (scoring + 18;
    }
    else
        return false;
}

bool IsFourOfAKind(Card Hand[])
{
    if ((Hand[0].Value == Hand[1].Value && Hand[0].Value == Hand[2].Value && Hand[0].Value == Hand[3].Value) || (Hand[1].Value == Hand[2].Value && Hand[1].Value == Hand[3].Value && Hand[1].Value == Hand[4].Value))
    {
        return true;
        cout << "Four of a Kind" << endl;
        //scoring = (scoring + 22);
    }
    else
        return false;
}

bool IsStraight(Card Hand[])
{
    if (Hand[1].Value == Hand[0].Value + 1 && Hand[2].Value == Hand[1].Value + 1 && Hand[3].Value == Hand[2].Value + 1 && Hand[4].Value == Hand[3].Value + 1)
    {
        return true;
        cout << "Straight" << endl;
        //scoring = (scoring + 19);
    }
    else
        return false;
}
bool IsFullHouse(Card Hand[])
{
    if ((Hand[0].Value == Hand[1].Value && Hand[0].Value == Hand[2].Value && Hand[3].Value == Hand[4].Value) || (Hand[0].Value == Hand[1].Value && Hand[2].Value == Hand[3].Value && Hand[2].Value == Hand[4].Value))

    {
        return true;
        cout << "Full House" << endl;
    }
    else
        return false;
}

bool IsRoyalFlush(Card Hand[])
{
    if (Hand[1].Value == Hand[0].Value + 1 && Hand[2].Value == Hand[1].Value + 1 && Hand[3].Value == Hand[2].Value + 1 && Hand[4].Value == Hand[3].Value + 1)
    {
        return true;
        cout << "Straight" << endl;
        //scoring = (scoring + 19);
    }
    else
        return false;
}

Main.cpp的

#include <iostream>

using namespace std;

#include "Card .h"

int main()
{
    Card    Deck[52];
    Card    Hands[4][5];
    int     i, j;
    const int StartingRank = 2;
    const int NumRanks = 13;


    InitDeck(Deck);
    cout << "\t The deck starts as " << endl;
    ShowDeck(Deck);
    Shuffle(Deck);
    cout << "\t After shuffling it is" << endl;
    ShowDeck(Deck);


    j = 0;

    for (i = 0; i < 5; i++)
    {
        Hands[0][i] = Deck[j];
        j++;
    }
    SortHand(Hands[0]);
    cout << "Hand 1 is: " << endl;
    if (HighestCard(Hands[0]))
        cout << "\tHighest Card" << endl;
    if (OnePair(Hands[0]))
        cout << "\tOne Pair" << endl;
    if (IsThreeOfAKind(Hands[0]))
        cout << "\tThree of a kind" << endl;
    if (IsTwoPair(Hands[0]))
        cout << "\tTwo Pair" << endl;
    if (IsFourOfAKind(Hands[0]))
        cout << "\tFour of a Kind" << endl;
    if (IsFullHouse(Hands[0]))
        cout << "\tFull House" << endl;
    if (IsStraight(Hands[0]))
        cout << " \tStraight" << endl;

        for (i = 0; i < 5; i++)
    {
        Display(Hands[0][i]);
    }



    for (i = 0; i < 5; i++)
    {
        Hands[1][i] = Deck[j];
        j++;
    }
    SortHand(Hands[1]);
    cout << "Hand 2 is: " << endl;
    if (HighestCard(Hands[1]))
        cout << "\tHighest Card" << endl;
    if (OnePair(Hands[1]))
        cout << "\tOne Pair" << endl;
    if (IsThreeOfAKind(Hands[1]))
        cout << "\tThree of a kind" << endl;
    if (IsTwoPair(Hands[1]))
        cout << "\tTwo Pair" << endl;
    if (IsFourOfAKind(Hands[1]))
        cout << "\tFour of a Kind" << endl;
    if (IsFullHouse(Hands[1]))
        cout << "\tFull House" << endl;
    if (IsStraight(Hands[1]))
        cout << " \tStraight" << endl;
    for (i = 0; i < 5; i++)
    {
        Display(Hands[1][i]);
    }

    for (i = 0; i < 5; i++)
    {
        Hands[2][i] = Deck[j];
        j++;
    }
    SortHand(Hands[2]);
    cout << "Hand 3 is: " << endl;
    if (HighestCard(Hands[2]))
        cout << "\tHighest Card" << endl;
    if (OnePair(Hands[2]))
        cout << "\tOne Pair" << endl;
    if (IsThreeOfAKind(Hands[2]))
        cout << "\tThree of a kind" << endl;
    if (IsTwoPair(Hands[2]))
        cout << "\tTwo Pair" << endl;
    if (IsFourOfAKind(Hands[2]))
        cout << "\tFour of a Kind" << endl;
    if (IsFullHouse(Hands[2]))
        cout << "\tFull House" << endl;
    if (IsStraight(Hands[2]))
        cout << " \tStraight" << endl;
    for (i = 0; i < 5; i++)
    {
        Display(Hands[2][i]);
    }


    for (i = 0; i < 5; i++)
    {
        Hands[3][i] = Deck[j];
        j++;
    }
    SortHand(Hands[3]);
    cout << "Hand 4 is: " << endl;
    if (HighestCard(Hands[3]))
        cout << "\tHighest Card" << endl;
    if (OnePair(Hands[3]))
        cout << "\tOne Pair" << endl;
    if (IsThreeOfAKind(Hands[3]))
        cout << "\tThree of a kind" << endl;
    if (IsTwoPair(Hands[3]))
        cout << "\tTwo Pair" << endl;
    if (IsFourOfAKind(Hands[3]))
        cout << "\tFour of a Kind" << endl;
    if (IsFullHouse(Hands[3]))
        cout << "\tFull House" << endl;
    if (IsStraight(Hands[4]))
            cout << " \tStraight" << endl;
    for (i = 0; i < 5; i++)
    {
        Display(Hands[3][i]);
    }

}

我已经为这个程序启动了一个评分方法。我如何完成它以确定获胜的手牌?如果需要我的其余代码,我会发布它。

谢谢!

1 个答案:

答案 0 :(得分:1)

  1. 根据订单的顺序为每个组合定义 int 值。 HighestCard - 0,OnePair - 1,......
  2. 找出每只手的组合。你已经使用if语句完成了它。只需将相应的值存储在某处。
  3. 按照他们的等级对每张牌中的牌进行排序。由于已经定义了所有组合,因此不再需要西装。
  4. 排序手。首先比较手部组合值。如果它们相等,则从最高的一个开始比较卡片。
  5. 获胜的手牌是分类容器中的第一个(最后一个)。
  6. 另外,查看this code以获得优化的组合定义可能会很有用。