2016-04-21 49 views
-3

所以我有這個特殊的問題,需要確定一個贏手。我已經在下面發佈了我的代碼。得分牌1

#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

Off topic:那裏的重複代碼的噸只是尖叫成功能。爲了你的排序,考慮節省一些時間和麻煩,並使用'std :: sort'。 – user4581301

+1

關於主題,我很難找到你開始得分的位置,更不用說你是怎麼做的。 – user4581301

+0

是的C++是迄今爲止我最不喜歡的類,我可能通過在某些區域採用不同的路線使代碼整體更清潔,但是我得到了這個工作,所以我要用它來大聲笑。 –

回答

1
  1. 根據它們的順序爲每個組合定義一個int值。 HighestCard - 0,OnePair - 1,...
  2. 找出每隻手有哪些組合。你已經用你的if語句做了。只需在某處存儲相應的值。
  3. 按照他們的等級對每張牌進行分類。由於所有組合都已經定義,因此不再需要套裝。
  4. 分類的手。首先比較手組合值。如果它們相同,則比較從最高一個一個開始的卡。
  5. 贏手是排序容器中的第一個(最後一個)。

另外,查看this code可能會對組合的優化定義有用。