![]() |
#2
lintaoyn2011-05-12 14:43
|
1.play.h

class Play
{
protected:
int CardAccount;
Card *CardArray;
void sort();
public:
Play();
Play(const Card arr[], int numcards);
int set(const Card arr[], int numcards);
int get(Card arr[])const;
int numCards()const;
bool sameSize(const Play& other)const;
int playType()const;
int compare(Card& A,Card& B);
};
2.Play.cpp{
protected:
int CardAccount;
Card *CardArray;
void sort();
public:
Play();
Play(const Card arr[], int numcards);
int set(const Card arr[], int numcards);
int get(Card arr[])const;
int numCards()const;
bool sameSize(const Play& other)const;
int playType()const;
int compare(Card& A,Card& B);
};

#include <new>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "card.h"
#include "play.h"
#include <algorithm>
#include <vector>
using namespace std;
//construct function
Play::Play()
{
for (int i=0;i<5;i++)
{
CardArray[i].CardFace[0]='\0';
CardArray[i].CardFace[1]='\0';
CardArray[i].CardSuit[0]='\0';
CardArray[i].CardSuit[1]='\0';
};
CardAccount=0;
}
//construct function
Play::Play(const Card arr[], int numcards)
{
int i=0;
if (numcards<=0)
{
numcards=0;
};
if (numcards>5)
{
numcards=5;
};
for(i=0;i<numcards;i++)
{
CardArray[i].CardFace[0]=arr[i].CardFace[0];
CardArray[i].CardFace[1]=arr[i].CardFace[1];
CardArray[i].CardSuit[0]=arr[i].CardSuit[0];
CardArray[i].CardSuit[1]=arr[i].CardSuit[1];
};
this->CardAccount=numcards;
}
//This function sets the Cards in the object to that of the array
//arr. If numcards is more than 5, store only the first 5 Cards.
//If numcards is 0 or less, this function does nothing. This function
//returns the number of Cards stored into the object.
int Play::set(const Card arr[],int numcards)
{
int i=0;
if (numcards<=0)
{
return 0;
};
if (numcards>5)
{
numcards=5;
};
for(i=0;i<numcards;i++)
{
CardArray[i].CardFace[0]=arr[i].CardFace[0];
CardArray[i].CardFace[1]=arr[i].CardFace[1];
CardArray[i].CardSuit[0]=arr[i].CardSuit[0];
CardArray[i].CardSuit[1]=arr[i].CardSuit[1];
};
this->CardAccount=numcards;
return this->CardAccount;
}
//This function copies the Cards stored in the object to the array
//arr and returns the number of Cards copied.
int Play::get(Card arr[]) const
{
int i=0;
for (i=0;i<this->CardAccount;i++)
{
arr[i].CardFace[0]=CardArray[i].CardFace[0];
arr[i].CardFace[1]=CardArray[i].CardFace[1];
arr[i].CardSuit[0]=CardArray[i].CardSuit[0];
arr[i].CardSuit[1]=CardArray[i].CardSuit[1];
};
return this->CardAccount;
}
//This function returns the number of Cards stored in the Play object.
int Play::numCards() const
{
return this->CardAccount;
}
//This function sorts the Cards in the Play object from small to big,
//according to the ranking of the Cards.
void Play::sort()
{
Card TmpCard;
int j,k=0;
int FaceJ=0,FaceK=0;
static char SuitJ,SuitK;
for (j=1;j<this->CardAccount;j++)
{
while(k<j)
{
FaceJ=CardArray[j].face();
FaceK=CardArray[k].face();
SuitJ=CardArray[j].suit();
SuitK=CardArray[k].suit();
if (CardArray[j].suit()=='X') {FaceJ=0;};
if (CardArray[k].suit()=='X') {FaceK=0;};
if (FaceJ<FaceK && FaceJ!=1)
{
TmpCard.CardFace[0]=CardArray[j].CardFace[0];
TmpCard.CardSuit[0]=CardArray[j].CardSuit[0];
CardArray[j].CardFace[0]=CardArray[k].CardFace[0];
CardArray[j].CardSuit[0]=CardArray[k].CardSuit[0];
CardArray[k].CardFace[0]=TmpCard.CardFace[0];
CardArray[k].CardSuit[0]=TmpCard.CardSuit[0];
};
if (FaceJ>FaceK && FaceK==1)
{
TmpCard.CardFace[0]=CardArray[j].CardFace[0];
TmpCard.CardSuit[0]=CardArray[j].CardSuit[0];
CardArray[j].CardFace[0]=CardArray[k].CardFace[0];
CardArray[j].CardSuit[0]=CardArray[k].CardSuit[0];
CardArray[k].CardFace[0]=TmpCard.CardFace[0];
CardArray[k].CardSuit[0]=TmpCard.CardSuit[0];
};
if (FaceJ==FaceK)
{
if(SuitJ=='D' && SuitK=='C')
{
TmpCard.CardFace[0]=CardArray[j].CardFace[0];
TmpCard.CardSuit[0]=CardArray[j].CardSuit[0];
CardArray[j].CardFace[0]=CardArray[k].CardFace[0];
CardArray[j].CardSuit[0]=CardArray[k].CardSuit[0];
CardArray[k].CardFace[0]=TmpCard.CardFace[0];
CardArray[k].CardSuit[0]=TmpCard.CardSuit[0];
}
else if(SuitJ=='C' && SuitK=='D')
{
}
else if((int)(SuitK)-(int)(SuitJ)>0)
{
TmpCard.CardFace[0]=CardArray[j].CardFace[0];
TmpCard.CardSuit[0]=CardArray[j].CardSuit[0];
CardArray[j].CardFace[0]=CardArray[k].CardFace[0];
CardArray[j].CardSuit[0]=CardArray[k].CardSuit[0];
CardArray[k].CardFace[0]=TmpCard.CardFace[0];
CardArray[k].CardSuit[0]=TmpCard.CardSuit[0];
};
};
k++;
};
k=0;
};
return;
}
//This function returns a true value if the number of cards in the current
//object and other is the same, false otherwise.
bool Play::sameSize(const Play& other) const
{
if (this->CardAccount==other.CardAccount) {return true;} else {return false;};
}
//This function returns a number representing the "Type of Play" stored in the
//object. The following chart shows the return values for the various play types
//and a description of what combination of cards results in that playtype. A more
//detailed descriptions of what these terms are is provided below the chart.
int Play::playType() const
{
int cardface[]={0,0,0,0,0};
int i=0,saveCount=0,myCount=0,countvar=0,countinv,facevar;
char cardsuit[]={' ',' ',' ',' ',' '};
vector<int>::iterator pointer;
for (i=0;i<CardAccount;i++)
{
cardface[i]=CardArray[i].face();
cardsuit[i]=CardArray[i].suit();
if (cardsuit[i]=='X') {cardface[i]=0;};
if (cardface[i]==0) {cardsuit[i]='X';};
};
for (i=0;i<CardAccount;i++)
{
countvar=CardArray[i].face();
if (countvar!=0)
{
vector<int> cardcontainer(cardface,cardface+5);
myCount=count(cardcontainer.begin(),cardcontainer.end(),countvar);
countinv=count(cardcontainer.begin(),cardcontainer.end(),0);
if (saveCount!=2 && saveCount!=3) {saveCount=myCount;};
if (countinv!=4 && myCount==1) {myCount=0;};
if (countinv!=3 && myCount==2)
{
if (myCount==2 && saveCount==3) {myCount=7;} else {myCount=0;}; //full house - exactly 5 cards where there is a three of a kind and a pair.
};
if (countinv!=2 && myCount==3)
{
if (myCount==3 && saveCount==2) {myCount=7;} else {myCount=0;}; //full house - exactly 5 cards where there is a three of a kind and a pair.
};
if (countinv!=1 && myCount==4) {myCount=0;};
};
};
if (CardAccount==5)
{
vector<int> cardcontainer(cardface,cardface+5);
//straight - exactly 5 cards in the Play object. 5 cards of any suit,
//where the face values of cards form a running sequence. For example:
//2,3,4,5,6 is a straight. 2, 3, 4, 5, 7 is not a straight. An Ace can
//be considered one higher than a King or one lower than a 2 but not both
//within the same Play. In other words, it is A,2,3,4,5 is a straight
//and 10,J,Q,K,A is a straight. However, Q,K,A,2,3 is NOT a straight.
stable_sort(cardcontainer.begin(),cardcontainer.end());
pointer=cardcontainer.begin();
if ((*(pointer+4))-(*(pointer+3))==1 && (*(pointer+3))-(*(pointer+2))==1 && (*(pointer+2))-(*(pointer+1))==1 && (*(pointer+1))-(*(pointer))==1) {myCount=5;};
if ((*(pointer+4))==13 && (*(pointer+3))==12 && (*(pointer+2))==11 && (*(pointer+1))==10 && (*(pointer))==1) {myCount=5;};
//flush or straight flush
if (cardsuit[4]==cardsuit[3] && cardsuit[3]==cardsuit[2] && cardsuit[2]==cardsuit[1] && cardsuit[1]==cardsuit[0])
{
if (myCount==5) {myCount=9;} //straight flush - exactly 5 cards where the cards are both a straight and a flush.
else
{
myCount=6; //flush - exactly 5 cards in the Play object. All cards must have exactly the same suit.
};
};
//poker four of a kind - exactly 5 cards. made of 4 cards with same face
//and any one other card.
vector<int> facecontainer(cardface,cardface+5);
saveCount=0;
for (i=0;i<CardAccount;i++)
{
facevar=CardArray[i].face();
saveCount=myCount;
myCount=count(cardcontainer.begin(),cardcontainer.end(),facevar);
if (myCount==4) {myCount=8;} else if (myCount!=8) {myCount=saveCount;};
};
};
return myCount;
}
//! operator
//returns true if the current object is invalid (invalid PlayType) false otherwise.
bool operator !(const Play& playvar)
{
int returnvar;
returnvar=playvar.playType();
if (returnvar==0)
{
return true;
}
else
{
return false;
};
}
//> operator
//returns true if the left operand is greater than the right operand.
bool operator >(Play& playvar1,Play& playvar2)
{
int intvar1,intvar2;
int num1=0,num2=0,comparint=0;
bool retvar=false;
intvar1=playvar1.playType();
intvar2=playvar2.playType();
//if both Plays are not valid, compare the highest card in the Play.
if (intvar1==0 && intvar2==0)
{
//If both plays are not valid but some cards are valid, compare the valid
//cards forthe highest. If both plays are not valid and no cards are valid,
//the play with thegreater number of cards is the greater play.
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[playvar1.CardAccount-1],playvar2.CardArray[playvar2.CardAccount-1]);
if (comparint==1) {retvar=true;};
//If both plays are not valid and no cards are valid, the play with thegreater
//number of cards is the greater play.
if (comparint==0)
{
if (playvar1.CardAccount>playvar2.CardAccount) {retvar=true;};
};
};
//if one of the Plays is not valid but the other is valid, then the valid one is bigger.
if (intvar1!=0 && intvar2==0) {retvar=true;};
if (intvar1!=0 && intvar2!=0)
{
//if both Plays are valid, the number of Cards in the Plays are different, then the Play with more cards is bigger.
num1=playvar1.numCards();
num2=playvar2.numCards();
if (num1>num2) {retvar=true;}
//if the number of cards in the Plays are the same, the following rules apply.
if (num1==num2)
{
if (num1==1) //if there is just one card in each Play, the bigger card is the card with the higher ranking.
{
comparint=(playvar1.CardArray[0],playvar2.CardArray[0]);
if (comparint==1){retvar=true;};
};
if (num1==2) //if there are two cards in each Play, the pair with the highest card is considered to be biggest.
{
if (intvar1==2 && intvar2!=2) {retvar=true;};
if (intvar1==2 && intvar2==2)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[1],playvar2.CardArray[1]);
if (comparint==1) {retvar=true;};
};
};
if (num1==3) //if there are three cards, the three of a kind with the highest card is considered to be bigger.
{
if (intvar1==3 && intvar2!=3) {retvar=true;};
if (intvar1==3 && intvar2==3)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[2],playvar2.CardArray[2]);
if (comparint==1) {retvar=true;};
};
};
if (num1==4) //if there are four cards, the four of a kind with the highest card is considered to be bigger.
{
if (intvar1==4 && intvar2!=4) {retvar=true;};
if (intvar1==4 && intvar2==4)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[3],playvar2.CardArray[3]);
if (comparint==1) {retvar=true;};
};
};
if (num1==5) //if there are five cards, the Play is ranked according to the following rules.
{
//All straights < all flushes < all full house < all poker four of a kind < all straight flushes.
if (intvar1>intvar2) {retvar=true;};
//if both are straights, the highest card in each Play determines which is higher. The only
//exception to this rule is when an Ace is used as part of a straight. If the Ace is used as
//the low card in the straight (eg A,2,3,4,5) the highest card is the five and not the Ace.
//If the Ace is used as the high card in the straight (10,J,Q,K,A) then the highest card is the
//Ace.
if (intvar1==5 && intvar2==5)
{
playvar1.sort();
playvar2.sort();
if (playvar1.CardArray[3].CardFace[0]=='5' && playvar1.CardArray[4].CardFace[0]=='1')
{
comparint=(playvar1.CardArray[3],playvar2.CardArray[4]);
}
else if (playvar2.CardArray[3].CardFace[0]=='5' && playvar2.CardArray[4].CardFace[0]=='1')
{
comparint=(playvar1.CardArray[4],playvar2.CardArray[3]);
}
else
{
comparint=(playvar1.CardArray[4],playvar2.CardArray[4]);
};
if (comparint==1) {retvar=true;};
};
//if both Plays are flushes, the highest card in each Play determines which Play is higher.
if (intvar1==6 && intvar2==6)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[4],playvar2.CardArray[4]);
if (comparint==1) {retvar=true;};
};
//if both Plays are full houses, the face value of the three of a kind determines which Play is higher.
if (intvar1==7 && intvar2==7)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[2],playvar2.CardArray[2]); //the face value of the three of a kind must in this cell.
if (comparint==1) {retvar=true;};
};
//if both Plays are poker four of a kinds, the face value of the four of a kind determine which Play is higher.
if (intvar1==8 && intvar2==8)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[2],playvar2.CardArray[2]); //the face value of the four of a kind must in this cell.
if (comparint==1) {retvar=true;};
};
//if both Plays are straight flushes, the same method of determining which Play is the highest straight applies.
if (intvar1==9 && intvar2==9)
{
playvar1.sort();
playvar2.sort();
if (playvar1.CardArray[3].CardFace[0]=='5' && playvar1.CardArray[4].CardFace[0]=='1')
{
comparint=(playvar1.CardArray[3],playvar2.CardArray[4]);
}
else if (playvar2.CardArray[3].CardFace[0]=='5' && playvar2.CardArray[4].CardFace[0]=='1')
{
comparint=(playvar1.CardArray[4],playvar2.CardArray[3]);
}
else
{
comparint=(playvar1.CardArray[4],playvar2.CardArray[4]);
};
if (comparint==1) {retvar=true;};
};
};
};
};
return retvar;
}
//< operator
//returns true if the left operand is less than the right operand.
bool operator<(Play& playvar1,Play& playvar2)
{
if (playvar2>playvar1){ return true;};
return false;
}
//int compare(const Card& A,const Card& B)
// If A is a higher rank card than B function returns 1
// If A is a lower rank card than B function returns -1
// If A is the same card as B function returns 0
// An invalid card has a rank lower than any valid card.
int Play::compare(Card& A,Card& B)
{
int CompRe=0;
int FaceA=0,FaceB=0;
FaceA=A.face();
FaceB=B.face();
if (A.suit()=='X' || FaceA==0)
{
if (B.suit()=='X' || FaceB==0)
{
CompRe=0;
}
else
{
CompRe=-1;
};
};
if (FaceA>FaceB && FaceB!=1 && A.suit()!='X' && CompRe==0)
{
CompRe=1;
}
else if (FaceA>FaceB && FaceB==1 && A.suit()!='X' && CompRe==0)
{
CompRe=-1;
};
if (FaceA<FaceB && FaceA!=1 && B.suit()!='X' && CompRe==0)
{
CompRe=-1;
}
else if (FaceA<FaceB && FaceA==1 && B.suit()!='X' && CompRe==0)
{
CompRe=1;
}
else if (FaceA<FaceB && FaceA!=1 && B.suit()=='X' && CompRe==0)
{
CompRe=1;
};
if (FaceA==FaceB && FaceB!=0 && CompRe==0)
{
if (B.suit()=='X')
{
CompRe=1;
};
int CardCmp;
static char SuitA[2],SuitB[2];
SuitA[0]=A.suit();
SuitB[0]=B.suit();
CardCmp=strcmp(SuitA,SuitB);
if (CardCmp>=0 && CompRe==0)
{
CompRe=CardCmp;
if (SuitA[0]=='D' && SuitB[0]=='C')
{
CompRe=-1;
};
}
else
{
if (SuitA[0]=='C' && SuitB[0]=='D' && CompRe==0)
{
CompRe=1;
};
if (CompRe==0)
{
CompRe=-1;
};
};
};
return CompRe;
}
3.card.h#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "card.h"
#include "play.h"
#include <algorithm>
#include <vector>
using namespace std;
//construct function
Play::Play()
{
for (int i=0;i<5;i++)
{
CardArray[i].CardFace[0]='\0';
CardArray[i].CardFace[1]='\0';
CardArray[i].CardSuit[0]='\0';
CardArray[i].CardSuit[1]='\0';
};
CardAccount=0;
}
//construct function
Play::Play(const Card arr[], int numcards)
{
int i=0;
if (numcards<=0)
{
numcards=0;
};
if (numcards>5)
{
numcards=5;
};
for(i=0;i<numcards;i++)
{
CardArray[i].CardFace[0]=arr[i].CardFace[0];
CardArray[i].CardFace[1]=arr[i].CardFace[1];
CardArray[i].CardSuit[0]=arr[i].CardSuit[0];
CardArray[i].CardSuit[1]=arr[i].CardSuit[1];
};
this->CardAccount=numcards;
}
//This function sets the Cards in the object to that of the array
//arr. If numcards is more than 5, store only the first 5 Cards.
//If numcards is 0 or less, this function does nothing. This function
//returns the number of Cards stored into the object.
int Play::set(const Card arr[],int numcards)
{
int i=0;
if (numcards<=0)
{
return 0;
};
if (numcards>5)
{
numcards=5;
};
for(i=0;i<numcards;i++)
{
CardArray[i].CardFace[0]=arr[i].CardFace[0];
CardArray[i].CardFace[1]=arr[i].CardFace[1];
CardArray[i].CardSuit[0]=arr[i].CardSuit[0];
CardArray[i].CardSuit[1]=arr[i].CardSuit[1];
};
this->CardAccount=numcards;
return this->CardAccount;
}
//This function copies the Cards stored in the object to the array
//arr and returns the number of Cards copied.
int Play::get(Card arr[]) const
{
int i=0;
for (i=0;i<this->CardAccount;i++)
{
arr[i].CardFace[0]=CardArray[i].CardFace[0];
arr[i].CardFace[1]=CardArray[i].CardFace[1];
arr[i].CardSuit[0]=CardArray[i].CardSuit[0];
arr[i].CardSuit[1]=CardArray[i].CardSuit[1];
};
return this->CardAccount;
}
//This function returns the number of Cards stored in the Play object.
int Play::numCards() const
{
return this->CardAccount;
}
//This function sorts the Cards in the Play object from small to big,
//according to the ranking of the Cards.
void Play::sort()
{
Card TmpCard;
int j,k=0;
int FaceJ=0,FaceK=0;
static char SuitJ,SuitK;
for (j=1;j<this->CardAccount;j++)
{
while(k<j)
{
FaceJ=CardArray[j].face();
FaceK=CardArray[k].face();
SuitJ=CardArray[j].suit();
SuitK=CardArray[k].suit();
if (CardArray[j].suit()=='X') {FaceJ=0;};
if (CardArray[k].suit()=='X') {FaceK=0;};
if (FaceJ<FaceK && FaceJ!=1)
{
TmpCard.CardFace[0]=CardArray[j].CardFace[0];
TmpCard.CardSuit[0]=CardArray[j].CardSuit[0];
CardArray[j].CardFace[0]=CardArray[k].CardFace[0];
CardArray[j].CardSuit[0]=CardArray[k].CardSuit[0];
CardArray[k].CardFace[0]=TmpCard.CardFace[0];
CardArray[k].CardSuit[0]=TmpCard.CardSuit[0];
};
if (FaceJ>FaceK && FaceK==1)
{
TmpCard.CardFace[0]=CardArray[j].CardFace[0];
TmpCard.CardSuit[0]=CardArray[j].CardSuit[0];
CardArray[j].CardFace[0]=CardArray[k].CardFace[0];
CardArray[j].CardSuit[0]=CardArray[k].CardSuit[0];
CardArray[k].CardFace[0]=TmpCard.CardFace[0];
CardArray[k].CardSuit[0]=TmpCard.CardSuit[0];
};
if (FaceJ==FaceK)
{
if(SuitJ=='D' && SuitK=='C')
{
TmpCard.CardFace[0]=CardArray[j].CardFace[0];
TmpCard.CardSuit[0]=CardArray[j].CardSuit[0];
CardArray[j].CardFace[0]=CardArray[k].CardFace[0];
CardArray[j].CardSuit[0]=CardArray[k].CardSuit[0];
CardArray[k].CardFace[0]=TmpCard.CardFace[0];
CardArray[k].CardSuit[0]=TmpCard.CardSuit[0];
}
else if(SuitJ=='C' && SuitK=='D')
{
}
else if((int)(SuitK)-(int)(SuitJ)>0)
{
TmpCard.CardFace[0]=CardArray[j].CardFace[0];
TmpCard.CardSuit[0]=CardArray[j].CardSuit[0];
CardArray[j].CardFace[0]=CardArray[k].CardFace[0];
CardArray[j].CardSuit[0]=CardArray[k].CardSuit[0];
CardArray[k].CardFace[0]=TmpCard.CardFace[0];
CardArray[k].CardSuit[0]=TmpCard.CardSuit[0];
};
};
k++;
};
k=0;
};
return;
}
//This function returns a true value if the number of cards in the current
//object and other is the same, false otherwise.
bool Play::sameSize(const Play& other) const
{
if (this->CardAccount==other.CardAccount) {return true;} else {return false;};
}
//This function returns a number representing the "Type of Play" stored in the
//object. The following chart shows the return values for the various play types
//and a description of what combination of cards results in that playtype. A more
//detailed descriptions of what these terms are is provided below the chart.
int Play::playType() const
{
int cardface[]={0,0,0,0,0};
int i=0,saveCount=0,myCount=0,countvar=0,countinv,facevar;
char cardsuit[]={' ',' ',' ',' ',' '};
vector<int>::iterator pointer;
for (i=0;i<CardAccount;i++)
{
cardface[i]=CardArray[i].face();
cardsuit[i]=CardArray[i].suit();
if (cardsuit[i]=='X') {cardface[i]=0;};
if (cardface[i]==0) {cardsuit[i]='X';};
};
for (i=0;i<CardAccount;i++)
{
countvar=CardArray[i].face();
if (countvar!=0)
{
vector<int> cardcontainer(cardface,cardface+5);
myCount=count(cardcontainer.begin(),cardcontainer.end(),countvar);
countinv=count(cardcontainer.begin(),cardcontainer.end(),0);
if (saveCount!=2 && saveCount!=3) {saveCount=myCount;};
if (countinv!=4 && myCount==1) {myCount=0;};
if (countinv!=3 && myCount==2)
{
if (myCount==2 && saveCount==3) {myCount=7;} else {myCount=0;}; //full house - exactly 5 cards where there is a three of a kind and a pair.
};
if (countinv!=2 && myCount==3)
{
if (myCount==3 && saveCount==2) {myCount=7;} else {myCount=0;}; //full house - exactly 5 cards where there is a three of a kind and a pair.
};
if (countinv!=1 && myCount==4) {myCount=0;};
};
};
if (CardAccount==5)
{
vector<int> cardcontainer(cardface,cardface+5);
//straight - exactly 5 cards in the Play object. 5 cards of any suit,
//where the face values of cards form a running sequence. For example:
//2,3,4,5,6 is a straight. 2, 3, 4, 5, 7 is not a straight. An Ace can
//be considered one higher than a King or one lower than a 2 but not both
//within the same Play. In other words, it is A,2,3,4,5 is a straight
//and 10,J,Q,K,A is a straight. However, Q,K,A,2,3 is NOT a straight.
stable_sort(cardcontainer.begin(),cardcontainer.end());
pointer=cardcontainer.begin();
if ((*(pointer+4))-(*(pointer+3))==1 && (*(pointer+3))-(*(pointer+2))==1 && (*(pointer+2))-(*(pointer+1))==1 && (*(pointer+1))-(*(pointer))==1) {myCount=5;};
if ((*(pointer+4))==13 && (*(pointer+3))==12 && (*(pointer+2))==11 && (*(pointer+1))==10 && (*(pointer))==1) {myCount=5;};
//flush or straight flush
if (cardsuit[4]==cardsuit[3] && cardsuit[3]==cardsuit[2] && cardsuit[2]==cardsuit[1] && cardsuit[1]==cardsuit[0])
{
if (myCount==5) {myCount=9;} //straight flush - exactly 5 cards where the cards are both a straight and a flush.
else
{
myCount=6; //flush - exactly 5 cards in the Play object. All cards must have exactly the same suit.
};
};
//poker four of a kind - exactly 5 cards. made of 4 cards with same face
//and any one other card.
vector<int> facecontainer(cardface,cardface+5);
saveCount=0;
for (i=0;i<CardAccount;i++)
{
facevar=CardArray[i].face();
saveCount=myCount;
myCount=count(cardcontainer.begin(),cardcontainer.end(),facevar);
if (myCount==4) {myCount=8;} else if (myCount!=8) {myCount=saveCount;};
};
};
return myCount;
}
//! operator
//returns true if the current object is invalid (invalid PlayType) false otherwise.
bool operator !(const Play& playvar)
{
int returnvar;
returnvar=playvar.playType();
if (returnvar==0)
{
return true;
}
else
{
return false;
};
}
//> operator
//returns true if the left operand is greater than the right operand.
bool operator >(Play& playvar1,Play& playvar2)
{
int intvar1,intvar2;
int num1=0,num2=0,comparint=0;
bool retvar=false;
intvar1=playvar1.playType();
intvar2=playvar2.playType();
//if both Plays are not valid, compare the highest card in the Play.
if (intvar1==0 && intvar2==0)
{
//If both plays are not valid but some cards are valid, compare the valid
//cards forthe highest. If both plays are not valid and no cards are valid,
//the play with thegreater number of cards is the greater play.
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[playvar1.CardAccount-1],playvar2.CardArray[playvar2.CardAccount-1]);
if (comparint==1) {retvar=true;};
//If both plays are not valid and no cards are valid, the play with thegreater
//number of cards is the greater play.
if (comparint==0)
{
if (playvar1.CardAccount>playvar2.CardAccount) {retvar=true;};
};
};
//if one of the Plays is not valid but the other is valid, then the valid one is bigger.
if (intvar1!=0 && intvar2==0) {retvar=true;};
if (intvar1!=0 && intvar2!=0)
{
//if both Plays are valid, the number of Cards in the Plays are different, then the Play with more cards is bigger.
num1=playvar1.numCards();
num2=playvar2.numCards();
if (num1>num2) {retvar=true;}
//if the number of cards in the Plays are the same, the following rules apply.
if (num1==num2)
{
if (num1==1) //if there is just one card in each Play, the bigger card is the card with the higher ranking.
{
comparint=(playvar1.CardArray[0],playvar2.CardArray[0]);
if (comparint==1){retvar=true;};
};
if (num1==2) //if there are two cards in each Play, the pair with the highest card is considered to be biggest.
{
if (intvar1==2 && intvar2!=2) {retvar=true;};
if (intvar1==2 && intvar2==2)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[1],playvar2.CardArray[1]);
if (comparint==1) {retvar=true;};
};
};
if (num1==3) //if there are three cards, the three of a kind with the highest card is considered to be bigger.
{
if (intvar1==3 && intvar2!=3) {retvar=true;};
if (intvar1==3 && intvar2==3)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[2],playvar2.CardArray[2]);
if (comparint==1) {retvar=true;};
};
};
if (num1==4) //if there are four cards, the four of a kind with the highest card is considered to be bigger.
{
if (intvar1==4 && intvar2!=4) {retvar=true;};
if (intvar1==4 && intvar2==4)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[3],playvar2.CardArray[3]);
if (comparint==1) {retvar=true;};
};
};
if (num1==5) //if there are five cards, the Play is ranked according to the following rules.
{
//All straights < all flushes < all full house < all poker four of a kind < all straight flushes.
if (intvar1>intvar2) {retvar=true;};
//if both are straights, the highest card in each Play determines which is higher. The only
//exception to this rule is when an Ace is used as part of a straight. If the Ace is used as
//the low card in the straight (eg A,2,3,4,5) the highest card is the five and not the Ace.
//If the Ace is used as the high card in the straight (10,J,Q,K,A) then the highest card is the
//Ace.
if (intvar1==5 && intvar2==5)
{
playvar1.sort();
playvar2.sort();
if (playvar1.CardArray[3].CardFace[0]=='5' && playvar1.CardArray[4].CardFace[0]=='1')
{
comparint=(playvar1.CardArray[3],playvar2.CardArray[4]);
}
else if (playvar2.CardArray[3].CardFace[0]=='5' && playvar2.CardArray[4].CardFace[0]=='1')
{
comparint=(playvar1.CardArray[4],playvar2.CardArray[3]);
}
else
{
comparint=(playvar1.CardArray[4],playvar2.CardArray[4]);
};
if (comparint==1) {retvar=true;};
};
//if both Plays are flushes, the highest card in each Play determines which Play is higher.
if (intvar1==6 && intvar2==6)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[4],playvar2.CardArray[4]);
if (comparint==1) {retvar=true;};
};
//if both Plays are full houses, the face value of the three of a kind determines which Play is higher.
if (intvar1==7 && intvar2==7)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[2],playvar2.CardArray[2]); //the face value of the three of a kind must in this cell.
if (comparint==1) {retvar=true;};
};
//if both Plays are poker four of a kinds, the face value of the four of a kind determine which Play is higher.
if (intvar1==8 && intvar2==8)
{
playvar1.sort();
playvar2.sort();
comparint=(playvar1.CardArray[2],playvar2.CardArray[2]); //the face value of the four of a kind must in this cell.
if (comparint==1) {retvar=true;};
};
//if both Plays are straight flushes, the same method of determining which Play is the highest straight applies.
if (intvar1==9 && intvar2==9)
{
playvar1.sort();
playvar2.sort();
if (playvar1.CardArray[3].CardFace[0]=='5' && playvar1.CardArray[4].CardFace[0]=='1')
{
comparint=(playvar1.CardArray[3],playvar2.CardArray[4]);
}
else if (playvar2.CardArray[3].CardFace[0]=='5' && playvar2.CardArray[4].CardFace[0]=='1')
{
comparint=(playvar1.CardArray[4],playvar2.CardArray[3]);
}
else
{
comparint=(playvar1.CardArray[4],playvar2.CardArray[4]);
};
if (comparint==1) {retvar=true;};
};
};
};
};
return retvar;
}
//< operator
//returns true if the left operand is less than the right operand.
bool operator<(Play& playvar1,Play& playvar2)
{
if (playvar2>playvar1){ return true;};
return false;
}
//int compare(const Card& A,const Card& B)
// If A is a higher rank card than B function returns 1
// If A is a lower rank card than B function returns -1
// If A is the same card as B function returns 0
// An invalid card has a rank lower than any valid card.
int Play::compare(Card& A,Card& B)
{
int CompRe=0;
int FaceA=0,FaceB=0;
FaceA=A.face();
FaceB=B.face();
if (A.suit()=='X' || FaceA==0)
{
if (B.suit()=='X' || FaceB==0)
{
CompRe=0;
}
else
{
CompRe=-1;
};
};
if (FaceA>FaceB && FaceB!=1 && A.suit()!='X' && CompRe==0)
{
CompRe=1;
}
else if (FaceA>FaceB && FaceB==1 && A.suit()!='X' && CompRe==0)
{
CompRe=-1;
};
if (FaceA<FaceB && FaceA!=1 && B.suit()!='X' && CompRe==0)
{
CompRe=-1;
}
else if (FaceA<FaceB && FaceA==1 && B.suit()!='X' && CompRe==0)
{
CompRe=1;
}
else if (FaceA<FaceB && FaceA!=1 && B.suit()=='X' && CompRe==0)
{
CompRe=1;
};
if (FaceA==FaceB && FaceB!=0 && CompRe==0)
{
if (B.suit()=='X')
{
CompRe=1;
};
int CardCmp;
static char SuitA[2],SuitB[2];
SuitA[0]=A.suit();
SuitB[0]=B.suit();
CardCmp=strcmp(SuitA,SuitB);
if (CardCmp>=0 && CompRe==0)
{
CompRe=CardCmp;
if (SuitA[0]=='D' && SuitB[0]=='C')
{
CompRe=-1;
};
}
else
{
if (SuitA[0]=='C' && SuitB[0]=='D' && CompRe==0)
{
CompRe=1;
};
if (CompRe==0)
{
CompRe=-1;
};
};
};
return CompRe;
}

class Card {
public:
char CardFace[2],CardSuit[2];
public:
Card();
Card(char cardvar[]);
~Card();
char suit() const;
int face() const;
void set(const char cardstr[]);
};
compile的时候它说我有run time error~~到底怎么回事?如果需要我可以把其他files也复制上来.public:
char CardFace[2],CardSuit[2];
public:
Card();
Card(char cardvar[]);
~Card();
char suit() const;
int face() const;
void set(const char cardstr[]);
};
我debug了好久,都找不到错在哪里,请大大指教.小弟在此谢过了~~