-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]);
}
}
我有點已經開始對這一計劃的評分方法。我將如何完成它來確定一個贏球手?如果我需要其他代碼,我會發布它。
謝謝!
Off topic:那裏的重複代碼的噸只是尖叫成功能。爲了你的排序,考慮節省一些時間和麻煩,並使用'std :: sort'。 – user4581301
關於主題,我很難找到你開始得分的位置,更不用說你是怎麼做的。 – user4581301
是的C++是迄今爲止我最不喜歡的類,我可能通過在某些區域採用不同的路線使代碼整體更清潔,但是我得到了這個工作,所以我要用它來大聲笑。 –