| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2636 人关注过本帖, 1 人收藏
标题:C++的锄大D,请大大帮忙看看我的code哪里出现了问题。
只看楼主 加入收藏
smsslin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-5-12
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:16 
C++的锄大D,请大大帮忙看看我的code哪里出现了问题。
以下是三个出现问题的files
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
程序代码:
#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
程序代码:
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也复制上来.
我debug了好久,都找不到错在哪里,请大大指教.小弟在此谢过了~~

搜索更多相关主题的帖子: 看看 
2011-05-12 14:37
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:7 
把全部代码都贴上一来,我来看看

迭代的是人,递归的是神。
2011-05-12 14:43
smsslin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-5-12
收藏
得分:0 
main.cpp
程序代码:
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include "card.h"
#include "play.h"
#include "player.h"

#define ON 1
#define OFF 0
#define SHOWALL OFF   //if you want to see what the cards held are after
                      //each round of play change OFF to ON

const char SUITS[4]={'D','C','H','S'};//the suits, ordered by their ranking

const char FACES[13]={'2','3','4','5','6','7','8',
                 '9','T','J','Q','K','1'};    //the face values, also ordered by 
                                      //their ranking test to make sure 
                                      //you can construct all the cards

void CreateDeck(Card deck[],int nc){
  char cardstr[3];
  cardstr[2]='\0';
  for(int i=0;i<nc;i++){
    cardstr[0]=SUITS[i%4];
    cardstr[1]=FACES[i/4];
    deck[i].set(cardstr);
  }
}

void TestShuffle(Card deck[],int whichone){
  Card mydeck[52];
  int pick[4][52]={
  {39, 22, 37, 43, 1, 27, 38, 16, 33, 2, 3, 26, 51, 14, 48, 10, 36, 24,
   7, 8, 29, 42, 18, 23, 9, 5, 49, 44, 35, 50, 4, 52, 28, 6, 32, 41,
   11, 40, 17, 30, 13, 12, 15, 31, 20, 46, 34, 21, 45, 19, 25, 47},
  {5, 43, 36, 12, 11, 13, 40, 51, 35, 10, 41, 42, 15, 49, 17, 8, 19, 24,
   52, 28, 27, 7, 33, 34, 16, 2, 4, 29, 45, 31, 6, 3, 22, 48, 23, 21,
   46, 32, 30, 26, 14, 44, 18, 38, 1, 20, 39, 50, 37, 9, 25, 47},
  {8, 33, 9, 14, 46, 47, 44, 50, 24, 41, 26, 20, 37, 4, 34, 38, 40, 45,
   7, 25, 17, 2, 31, 18, 36, 32, 6, 1, 15, 51, 49, 52, 12, 11, 29, 5,
   48, 30, 16, 35, 39, 3, 28, 13, 21, 23, 42, 27, 19, 10, 43, 22},
  {8, 47, 17, 31, 49, 9, 46, 35, 3, 13, 45, 32, 30, 34, 42, 21, 29, 52,
   48, 28, 50, 1, 22, 15, 24, 4, 7, 11, 27, 20, 25, 51, 26, 5, 44, 41,
   14, 43, 38, 6, 23, 19, 39, 18, 37, 36, 33, 10, 40, 12, 2, 16}};
  CreateDeck(mydeck,52);
  for(int i=0;i<52;i++){
    deck[i]=mydeck[pick[whichone%4][i]-1];
  }

}

void Print(const Player& p);

//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 compare(const Card& A,const Card& B);


int main(void){
    Card deck[52];
    HumanPlayer* hp;
    Player* p[3];  //3 computer player pointers
    char name[10]="Player # ";
    int iswin=0;
    Play oldplay, currplay;
    Card lowest1,lowest2,lowest3,lowest4;
    int lowidx,lowidx1;
    bool allpass=false;
    int i,passcount=0;

    #if (SHOWALL==ON)
        int round=1;
    #endif

    TestShuffle(deck,0);
    //not playing with full deck just 13 cards each
    cout << "Initially, here are the cards for each player: " << endl;
    
    name[8]='0'+1;
    hp=new HumanPlayer(name,deck,13);
    Print(*hp);

    for(i=1;i<4;i++)
    {
        name[8]='0'+(i+1);
        p[i-1]=new Player(name,deck+(i*13),13);
        Print(*p[i-1]);
    }

    hp->getLowest(lowest1);
    p[0]->getLowest(lowest2);
    p[1]->getLowest(lowest3);
    p[2]->getLowest(lowest4);
    lowidx=(compare(lowest1, lowest2)==-1)?0:1;
    if (lowidx==0)
    {lowidx1=(compare(lowest1, lowest3)==-1)?0:2;}
    else
    {lowidx1=(compare(lowest2, lowest3)==-1)?1:2;};

    lowidx=lowidx1;

    switch(lowidx)
    {
    case 0:
      lowidx1=(compare(lowest1, lowest4)==-1)?0:3;
      break;
    case 1:
      lowidx1=(compare(lowest2, lowest4)==-1)?1:3;
      break;
    case 2:
      lowidx1=(compare(lowest3, lowest4)==-1)?2:3;
      break;
    };

    lowidx=lowidx1;

    if (lowidx==0)
    {
        cout << hp->name() << " goes first" << endl;
    }
    else
    {
        cout << p[lowidx-1]->name() << " goes first" << endl;
    }
    cout << "Here we go!" << endl << endl;

    if (lowidx==0)
    {
        hp->getPlay(oldplay);
        cout << hp->name() << "throws out: " << endl;
    }
    else
    {
        p[lowidx-1]->getPlay(oldplay);
        cout << p[lowidx-1]->name() << "throws out: " << endl;
    }
    cout <<  oldplay << endl;

  for(i=(lowidx)%4;!iswin;i=(i+1)%4){
#if SHOWALL==ON
    if(i==lowidx){
      cout << "After " << round++ << " round" << endl;
      for(int j=0;j<2;j++){
        Print(*p[j]);
      }
    }
#endif
        if (i==3) 
        {
            if(hp->getPlay(oldplay,currplay,allpass)){
              cout << hp->name() << " throws out: " << endl;
              cout <<  currplay << endl;
              allpass=0;
              oldplay=currplay;
              if (passcount!=0) {passcount=0;};
            }
            else{
              cout << hp->name() << " passes" << endl;
              passcount++;
              if (passcount==3)
              {
                allpass=1;
                passcount=0;
              };
            }
            if(hp->numCards()==0)
            {
              iswin=i+1;
            };
        }
        else
        {
            if(p[i]->getPlay(oldplay,currplay,allpass)){
              cout << p[i]->name() << " throws out: " << endl;
              cout <<  currplay << endl;
              allpass=0;
              oldplay=currplay;
              if (passcount!=0) {passcount=0;};
            }
            else{
              cout << p[i]->name() << " passes" << endl;
              passcount++;
              if (passcount==3)
              {
                allpass=1;
                passcount=0;
              };
            }
            if(p[i]->numCards()==0)
            {
              iswin=i+1;
            };
        };
  }
  if ((iswin-1)==3)
  {
      cout << "Player # 1 has won the game!" << endl;
  }
  else
  {
    cout << p[iswin-1]->name() << " has won the game!" << endl;
  }
  delete [] hp;
  for(i=0;i<3;i++){
    delete p[i];
  }
  
  //system("pause");
  return 0;
}


void Print(const Player& p){
  Card cards[52];
  int nc,i=0;
  nc=p.getCards(cards,52);

  cout << "Cards held by " << p.name() << endl;
  for (i=0;i<nc;i++){
    cout << ' ' << cards[i].suit() << ' ';
  }
  cout << endl;
  for(i=0;i<nc;i++){
    switch(cards[i].face()){
      case 11: cout << " J "; break;
      case 12: cout << " Q "; break;
      case 13: cout << " K "; break;
      case 1:  cout << " A "; break;
      default: {
        cout.width(2);
        cout << cards[i].face() << ' ';
      }
    }
  }
  cout << endl << endl;
}


//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 compare(const Card& A,const Card& B)
{
    int CompRe=0;
    int FaceA=0,FaceB=0;

    FaceA=A.face();
    FaceB=B.face();

    //if (A.CardFace[0]=='1'){FaceA=1;};
    //if (B.CardFace[0]=='1'){FaceB=1;};

    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;
}
2011-05-12 14:50
smsslin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-5-12
收藏
得分:0 
player.h
程序代码:

class Player
{
protected:
    char playername[100];
    Card *playercard;
    int cardnumber;
    void sort();
public:
    Player();
    Player(Card plycard[],int plynumber);
    Player(const char plyname[],Card plycard[],int plynumber);
    void deal(const Card cardsdealt[],int numcards);
    void getLowest(Card& c);
    int numCards() const;
    int getCards(Card cardsheld[], int maxsize) const;
    const char* name() const;
    void print();
    virtual bool getPlay(const Play& oldplay,Play& currplay,bool allpass);
    virtual bool getPlay(Play& firsthand);
};

class HumanPlayer:public Player
{
private:
    void delecard(Card c);
    //void allt(char& userc,int clen);
public:
    HumanPlayer();
    HumanPlayer(Card Humcard[],int Humnumber);
    HumanPlayer(const char Humname[],Card Humcard[],int Humnumber);
    bool getPlay(Play& currplay);
    bool getPlay(const Play& oldplay,Play& currplay,bool allpass);
};
    
2011-05-12 14:51
smsslin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-5-12
收藏
得分:0 
player.cpp
程序代码:
#include <new>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "card.h"
#include "play.h"
#include "player.h"

using namespace std;

//construct function
Player::Player()
{
    strcpy(playername,"unknown\0");
    playercard=NULL;
    cardnumber=0;
}

//construct function
Player::Player(Card plycard[],int plynumber)
{
    int i;

    strcpy(playername,"unknown\0");

    if (playercard==NULL) delete [] playercard;
    playercard=new Card[plynumber];
    for (i=0;i<plynumber;i++)
    {
        playercard[i].CardFace[0]=plycard[i].CardFace[0];
        playercard[i].CardSuit[0]=plycard[i].CardSuit[0];
    };

    cardnumber=plynumber;
}

//construct function
Player::Player(const char plyname[],Card plycard[],int plynumber)
{
    int i;

    strcpy(playername,plyname);

    if (playercard==NULL) delete [] playercard;
    playercard=new Card[plynumber];
    for (i=0;i<plynumber;i++)
    {
        playercard[i].CardFace[0]=plycard[i].CardFace[0];
        playercard[i].CardSuit[0]=plycard[i].CardSuit[0];
    };

    cardnumber=plynumber;
}

//This function receives an array representing the cards dealt to the Player and
//an integer containing the number of cards dealt.  This function replaces whatever cards
//are currently held by the Player with the cards represented by the cardsdealt array
void Player::deal(const Card cardsdealt[],int numcards)
{
    int i;
    if (playercard==NULL) delete [] playercard;
    playercard=new Card[numcards];
    for (i=0;i<numcards;i++)
    {
        playercard[i].CardFace[0]=cardsdealt[i].CardFace[0];
        playercard[i].CardSuit[0]=cardsdealt[i].CardSuit[0];
    };
    cardnumber=numcards;
}

//This function finds the lowest Card held by the Player and passes that back through c
void Player::getLowest(Card& c)
{
    this->sort();    //sort the card,from low to high.
    strcpy(c.CardFace,playercard[0].CardFace);
    strcpy(c.CardSuit,playercard[0].CardSuit);
}

//Returns the number of cards held by the player
int Player::numCards() const
{
    return cardnumber;
}

//This function copies the cards from the Player into cardsheld.  If maxsize is less than
//the number of cards held by the Player, only copy maxsize cards.  Function returns number
//of cards copied into the cardsheld array.
int Player::getCards(Card cardsheld[], int maxsize) const
{
    int i,j;

    j=cardnumber;

    if (maxsize<cardnumber){j=maxsize;};    //If maxsize is less than the number of cards held by the Player, only copy maxsize cards.

    for (i=0;i<maxsize;i++)
    {
        strcpy(cardsheld[i].CardFace,playercard[i].CardFace);
        strcpy(cardsheld[i].CardSuit,playercard[i].CardSuit);
    };
    return i;
}

//This function returns the name of the Player.
const char* Player::name() const
{
    return playername;
}

//Prints the cards held by Player.  The cards must be printed in a SORTED manner from low to high.
void Player::print()
{
    sort();
    int i=0;
    for (i=0;i<cardnumber;i++)
    {
        cout<<playercard[i].CardSuit[0]<<"   ";
    };
    cout<<endl;

    for (i=0;i<cardnumber;i++)
    {
        cout<<playercard[i].CardFace[0]<<"   ";
    };
    cout<<endl;
}

//This function sorts the Cards in the Player object from small to big,
//according to the ranking of the Cards.
void Player::sort()
{
    Card TmpCard;
    int j,k=0;
    int FaceJ=0,FaceK=0;
    static char SuitJ,SuitK;

    for (j=1;j<cardnumber;j++)
    {
        while(k<j)
        {
            FaceJ=playercard[j].face();
            FaceK=playercard[k].face();

            SuitJ=playercard[j].suit();
            SuitK=playercard[k].suit();
            
            if (playercard[j].suit()=='X') {FaceJ=0;};
            if (playercard[k].suit()=='X') {FaceK=0;};

            if (FaceJ<FaceK && FaceJ!=1)
            {
                TmpCard.CardFace[0]=playercard[j].CardFace[0];
                TmpCard.CardSuit[0]=playercard[j].CardSuit[0];

                playercard[j].CardFace[0]=playercard[k].CardFace[0];
                playercard[j].CardSuit[0]=playercard[k].CardSuit[0];

                playercard[k].CardFace[0]=TmpCard.CardFace[0];
                playercard[k].CardSuit[0]=TmpCard.CardSuit[0];
            };

            if (FaceJ>FaceK && FaceK==1)
            {
                TmpCard.CardFace[0]=playercard[j].CardFace[0];
                TmpCard.CardSuit[0]=playercard[j].CardSuit[0];

                playercard[j].CardFace[0]=playercard[k].CardFace[0];
                playercard[j].CardSuit[0]=playercard[k].CardSuit[0];

                playercard[k].CardFace[0]=TmpCard.CardFace[0];
                playercard[k].CardSuit[0]=TmpCard.CardSuit[0];
            };

            if (FaceJ==FaceK)
            {
                if(SuitJ=='D' && SuitK=='C')
                {
                    TmpCard.CardFace[0]=playercard[j].CardFace[0];
                    TmpCard.CardSuit[0]=playercard[j].CardSuit[0];

                    playercard[j].CardFace[0]=playercard[k].CardFace[0];
                    playercard[j].CardSuit[0]=playercard[k].CardSuit[0];

                    playercard[k].CardFace[0]=TmpCard.CardFace[0];
                    playercard[k].CardSuit[0]=TmpCard.CardSuit[0];
                }
                else if(SuitJ=='C' && SuitK=='D')
                {
                }
                else if((int)(SuitK)-(int)(SuitJ)>0)
                {
                    TmpCard.CardFace[0]=playercard[j].CardFace[0];
                    TmpCard.CardSuit[0]=playercard[j].CardSuit[0];

                    playercard[j].CardFace[0]=playercard[k].CardFace[0];
                    playercard[j].CardSuit[0]=playercard[k].CardSuit[0];

                    playercard[k].CardFace[0]=TmpCard.CardFace[0];
                    playercard[k].CardSuit[0]=TmpCard.CardSuit[0];
                };
            };

            k++;
        };
        k=0;
    };

    return;
}

bool HumanPlayer::getPlay(Play& currplay)
{
    int m=0,n=0,i=0,j=0,playtype=0;
    bool testvar;
    char *userchoose,*searchpass,*searchsmallest;
    Play currentplay;
    Card *SeleCard;

    testvar=true;
    userchoose=NULL;
    searchpass=NULL;
    searchsmallest=NULL;
    SeleCard=NULL;

    Player::sort();
    
    //To label each card with with a unique letter starting from the letter A.
    m=Player::numCards();
    for (i=65;i<(65+m);i++)
    {
        cout<<(char)i<<"   ";
    };
    cout<<endl;
    for (i=65;i<(64+m);i++)
    {
        cout<<"====";
    };
    cout<<"="<<endl;
    for (i=0;i<m;i++)
    {
        cout<<playercard[i].CardSuit[0]<<"   ";
    };
    cout<<endl;
        for (i=0;i<m;i++)
    {
        cout<<playercard[i].CardFace[0]<<"   ";
    };
    cout<<endl;

    //end label each card with with a unique letter starting from the letter A.

    if (m>5)
    {
        userchoose=new char[5];
    }
    else
    {
        userchoose=new char[m];
    };

    while(testvar)
    {
        //To prompt the user to choose the cards for one Play or to hit 0 (zero) to pass.
        cout<<"Please choose the cards for one Play or to hit 0 (zero) to pass:"<<endl;
        cin>>userchoose;

        //To read in the cards that the user has chosen to throw out (form a Play with) in
        //the form of a single string.
        j=strlen(userchoose);

        //convert lowercase to uppercase.
        for (i=0;i<j;i++)
        {
            if ((int)userchoose[i]>=97 && (int)userchoose[i]<(97+m)) {userchoose[i]=(char)((int)userchoose[i]-32);};
        };

        i=0;

        //To check that the user's input is valid and if it is not valid, to ask for input
        //again until valid data is received.
        if (j==0) {testvar=true;} else {testvar=false;};
        while(i<j)
        {
            if (userchoose[i]!='\0')
            {
                if ((int)userchoose[i]<65 || (int)userchoose[i]>=(65+m)) {testvar=true;};
            };

            i++;
        };

        //hit 0 (zero) to pass.
        searchpass=strchr(userchoose,'0');
        if (searchpass!=NULL) {testvar=false;};

        //The first Play thrown out in the game must contain the smallest card held by the player.
        searchsmallest=strchr(userchoose,'A');
        if (searchsmallest==NULL && searchpass==NULL)
        {
            cout<<"The first Play thrown out in the game must contain the smallest card held by the player"<<endl;
            testvar=true;
        };

        SeleCard=new Card[j];
        for (i=0;i<j;i++)
        {
            n=(int)userchoose[i]-65;

            SeleCard[i].CardFace[0]=playercard[n].CardFace[0];
            SeleCard[i].CardFace[1]=playercard[n].CardFace[1];
            SeleCard[i].CardSuit[0]=playercard[n].CardSuit[0];
            SeleCard[i].CardSuit[1]=playercard[n].CardSuit[1];
        };

        currentplay.set(SeleCard,j);
        playtype=currentplay.playType();
        if (playtype==0) {testvar=true;};
    };

    //To place the cards chosen into the currplay argument that is passed into the getPlay() function.
    if (testvar==false)
    {
        if (searchpass!=NULL)
        {
            //To return a true value if a valid Play was successfully found, false if the user passed.
            cout<<"user has passed."<<endl;

            delete [] SeleCard;
            delete [] userchoose;
            return false;
        }
        else
        {
            currplay.set(SeleCard,j);
        };
    };

    //To modify the HumanPlayer object so that the cards currently held no longer include the cards that were
    //just chosen for the Play object.
    if (j<=5) {n=j;} else {n=5;};
    for (i=0;i<n;i++)
    {
        delecard(SeleCard[i]);
    };

    delete [] SeleCard;
    delete [] userchoose;
    return true;
};

bool HumanPlayer::getPlay(const Play& oldplay,Play& currplay,bool allpass)
{
    int m=0,n=0,i=0,j=0,playtype=0,oldlen=0;
    bool testvar;
    char *userchoose,*searchpass,*searchsmallest;
    Play currentplay;
    Card *SeleCard;

    testvar=true;
    userchoose=NULL;
    searchpass=NULL;
    searchsmallest=NULL;
    SeleCard=NULL;
    oldlen=oldplay.numCards();

    Player::sort();
    
    //To label each card with with a unique letter starting from the letter A.
    m=Player::numCards();
    for (i=65;i<(65+m);i++)
    {
        cout<<(char)i<<"   ";
    };
    cout<<endl;
    for (i=65;i<(64+m);i++)
    {
        cout<<"====";
    };
    cout<<"="<<endl;
    for (i=0;i<m;i++)
    {
        cout<<playercard[i].CardSuit[0]<<"   ";
    };
    cout<<endl;
    for (i=0;i<m;i++)
    {
        cout<<playercard[i].CardFace[0]<<"   ";
    };
    cout<<endl;

    //end label each card with with a unique letter starting from the letter A.

    if (m>5)
    {
        userchoose=new char[5];
    }
    else
    {
        userchoose=new char[m];
    };

    while(testvar)
    {
        //To prompt the user to choose the cards for one Play or to hit 0 (zero) to pass.
        cout<<"Please choose the cards for one Play or to hit 0 (zero) to pass:"<<endl;
        cin>>userchoose;

        //To read in the cards that the user has chosen to throw out (form a Play with) in
        //the form of a single string.
        j=strlen(userchoose);

        //convert lowercase to uppercase.
        for (i=0;i<j;i++)
        {
            if ((int)userchoose[i]>=97 && (int)userchoose[i]<(97+m)) {userchoose[i]=(char)((int)userchoose[i]-32);};
        };

        i=0;

        //To check that the user's input is valid and if it is not valid, to ask for input
        //again until valid data is received.
        if (j==0) {testvar=true;} else {testvar=false;};
        while(i<j)
        {
            if (userchoose[i]!='\0')
            {
                if ((int)userchoose[i]<65 || (int)userchoose[i]>=(65+m)) {testvar=true;};
            };

            i++;
        };

        //If allpass is true, the user MUST throw out a valid Play (according to the playType() function).
        //The user will not be allowed to pass as the user will always be able to create a valid Play.
        if (allpass==false)
        {
            //hit 0 (zero) to pass.
            //If allpass is false, the user may choose to pass, in which case the function simply returns
            //false.
            searchpass=strchr(userchoose,'0');
            if (searchpass!=NULL) {testvar=false;};
        };

        if (searchpass==NULL)
        {
            SeleCard=new Card[j];
            for (i=0;i<j;i++)
            {
                n=(int)userchoose[i]-65;

                SeleCard[i].CardFace[0]=playercard[n].CardFace[0];
                SeleCard[i].CardFace[1]=playercard[n].CardFace[1];
                SeleCard[i].CardSuit[0]=playercard[n].CardSuit[0];
                SeleCard[i].CardSuit[1]=playercard[n].CardSuit[1];
            };//end for

            currentplay.set(SeleCard,j);
            playtype=currentplay.playType();
            if (playtype==0) {testvar=true;};//end if

            //Alternative, if allpass is false and the user chooses to create a Play, 
            //the play must meet the following requirements:
            //1.has the same number of cards as the oldplay.
            if (j!=oldlen && allpass==false)
            {
                testvar=true;
                cout<<"You must throws out "<<oldlen<<" cards!"<<endl;
            }
            else
            {
                testvar=false;
            };//end if
        
            if (testvar==false)
            {
                currplay.set(SeleCard,j);
                //Alternative, if allpass is false and the user chooses to create a Play, 
                //the play must meet the following requirements:
                //2.is "bigger" than oldplay (as determined by the Play's > operator).
                currentplay=oldplay;
                if (currplay>currentplay)
                {
                    testvar=false;
                }
                else
                {
                    if (allpass==false)
                    {
                        testvar=true;
                        cout<<"You throws out must be bigger than the Play of the 'active Play'."<<endl;
                    };
                };//end if
            };//end if
        };
    };

    //To place the cards chosen into the currplay argument that is passed into the getPlay() function.
    if (testvar==false)
    {
        //If allpass is true, the user MUST throw out a valid Play (according to the playType() function).
        //The user will not be allowed to pass as the user will always be able to create a valid Play.
        if (searchpass!=NULL && allpass==false)
        {
            //If allpass is false, the user may choose to pass, in which case the function simply returns
            //false.
            //cout<<"user has passed."<<endl;

            delete [] SeleCard;
            delete [] userchoose;
            return false;
        }
    };

    //To modify the HumanPlayer object so that the cards currently held no longer include the cards that were
    //just chosen for the Play object.
    if (j<=5) {n=j;} else {n=5;};
    if (n==1 && (m-n)==0)
    {
        Player::playercard=NULL;
        Player::cardnumber=0;
    }
    else
    {
        for (i=0;i<n;i++)
        {
            delecard(SeleCard[i]);
        };
    };

    delete [] SeleCard;
    //delete [] userchoose;
    return true;
};

void HumanPlayer::delecard(Card c)
{
    Card *card;
    int i,j=0,m;

    m=numCards()-1;
    card=NULL;
    if (card==NULL) {delete [] card;};
    card=new Card[m];
    for(i=0;i<=m;i++)
    {
        if (playercard[i].CardSuit[0]==c.CardSuit[0] && playercard[i].CardFace[0]==c.CardFace[0])
        {}
        else
        {
            card[j].CardFace[0]=playercard[i].CardFace[0];
            card[j].CardSuit[0]=playercard[i].CardSuit[0];
            j++;
        };
    };

    Player::deal(card,m);
    
    return;
};

/*void HumanPlayer::allt(char& userc,int clen)
{
    int i,j,k=0;
    char *chr;
    chr=NULL;

    chr=new char(clen);

    for (j=0;j<clen;j++)
    {
        if (userc[j]!=' ')
        {
            chr[k]=userc[j];
        };
    };

    i=strlen(chr);

    strcpy(userc,chr);


    return;
};*/

HumanPlayer::HumanPlayer():Player()
{

};

HumanPlayer::HumanPlayer(Card Humcard[],int Humnumber):Player(Humcard,Humnumber)
{

};

HumanPlayer::HumanPlayer(const char Humname[],Card Humcard[],int Humnumber):Player(Humname,Humcard,Humnumber)
{

};
2011-05-12 14:51
smsslin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-5-12
收藏
得分:0 
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);

};


    
2011-05-12 14:52
smsslin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-5-12
收藏
得分:0 
play.cpp
程序代码:
#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;
}
2011-05-12 14:52
smsslin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-5-12
收藏
得分:0 
gp.cpp
程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <new>
#include "card.h"
#include "play.h"
#include "player.h"
#define RRRR(N) (N/4)
#define SSSS(N) (N%4)

/*namespaces are useful when a program is written by more than one person.
  The non-member function code below is in the namespace ComputerPlayerCode.
  Even if you coded a function of exactly the same name and signature, the
  compiler will be able to distinguish between them and use the appropriate
  function in the getPlay() functions below*/
namespace ComputerPlayerCode{

   /*Many of the functions below represent the cards using their ranks as identifiers
     The reason for doing this is that it simplifies some fo the coding involved.
     Thus arrays of cards get converted to arrays of ints and back again.  Another
     approach would be to add appropriate operators to the card class so that this
     would not be necessary.*/

  /*this function is used to set up a play that is less than 5 cards*/
  int PickLess5(const Play& oldhand,int cards[],int num,int where[],
                                                            int allpass);
  /* this function puts the correct cards back into the player after
     a play has been made been removed from*/
  void Redeal(int oldhand[],int numold, int newhand[],
                                                int pick[],int numpick);

  /*function returns the index of key in arr if key is not found, function returns -1*/
  int Search(int key,int arr[],int num);

  /*this function sorts an integer array arr of size num*/
  void Sort(int arr[],int num);

  /*function takes the suit char returned by the suit function and returns 1 for diamonds
    2 for clubs, 3 for hearts, 4 for spades*/
  int changesuit2int(char suit);

  /*function takes the integer changesuit2int() would have returned and converts it
    back to a suit char(D,C,H or S)*/
  char changeint2suit(int v);

  /*changes the return value of face() function to a character*/
  char changeint2face(int v);

  /*converts a card into its integer representation*/
  int Convert(Card c);

  /*returns the number of cards in the cards array that have the same face value
    as rank.  The index of where they are located are passed back through where*/
  int SameRank(int rank,int cards[],int numcards,int where[]);
  
  /*returns the number of cards in the cards array that have the same suit value
    as rank.  The index of where they are located are passed back through where*/
  int SameSuit(int suit,int cards[],int numcards,int where[]);

  /* this function is used to pick the smallest non-5 card hand it can*/
  int PickLess5(int musthave,int cards[],int num,int where[]);

  /*setsup the Cards*/
  void SetCards(Card setcards[],int cards[],int pick[],int np);

  /*retuns true if there is a straight in the cards array*/
  int HasStraight(int start,int cards[],int numcards,int where[]);

  /*retuns true if there is a flush in the cards array*/
  int HasFlush(int start,int cards[],int numcards,int where[]);

  /*retuns true if there is a straight flush in the cards array*/  
  int HasSFlush(int start,int cards[],int numcards,int where[]);

  /*retuns true if there is a fullhouse in the cards array*/
  int HasFull(int start,int cards[],int numcards,int where[]);

  /*retuns true if the cards in the position indicated by the where
    array has the same suit*/
  int allsamesuit(int cards[],int where[]);

  /*returns true if the card array can create a poker 4 of a kind*/
  int HasPFour(int start,int cards[],int numcards,int where[]);

  /*used to figure out what to throw out given the play currently on the table
    and whether or not everyone has passed*/
  int Pick5(const Play& oldhand,int*& cards,int num,int where[],int allpass);
  int Pick5(int musthave,int cards[],int num,int where[]);

  void Redeal(int oldhand[],int numold, Card newhand[],
                                                int pick[],int numpick){
    int i,j,k;
    Sort(pick,numpick);
    char cardstr[3];
    cardstr[2]='\0';
    for(i=0,j=0,k=0;i<numold && j<numpick;i++){
      if(i!=pick[j]){
        cardstr[0]=changeint2suit(SSSS(oldhand[i]));
        cardstr[1]=changeint2face(oldhand[i]);
        newhand[k++].set(cardstr);
      }
      else
        j++;
    }
    for(;i<numold;i++){
      cardstr[0]=changeint2suit(SSSS(oldhand[i]));
      cardstr[1]=changeint2face(oldhand[i]);
      newhand[k++].set(cardstr);
    }
  }
  int Search(int key,int arr[],int num){
    int found=-1;
    int i;
    for(i=0;i<num && found==-1;i++){
      if(arr[i]==key){
        found=i;
      }
    }
    return found;
  }
  void Sort(int arr[],int num){
    int curr;
    int i, j;
    for(i=1;i<num;i++){
      curr=arr[i];
      for(j=i;j>0 && arr[j-1] > curr;j--){
        arr[j]=arr[j-1];
      }
      arr[j]=curr;
    }
  }

  int HasSFlush(int start,int cards[],int numcards,int where[]){
    int next;
    int found;
    int i=1;
    found=Search(start,cards,numcards);
    where[0]=found;
    next=start-4;
    if(next >=0){
      found=Search(next,cards,numcards);
      while(i<5 && next >=0 && found!=-1){
        where[i++]=found;
        next=next-4;
        found=Search(next,cards,numcards);
      }
    }
    next=start+4;
    if(next<=51){
      found=Search(next,cards,numcards);
      while(i<5 && next <=51 && found!=-1){
        where[i++]=found;
        next=next+4;
        found=Search(next,cards,numcards);
      }
    }
    Sort(where,i);
    return (i==5);
  }
  int changesuit2int(char suit){
    int result;
    switch(suit){
      case 'D':  result=0; break;
      case 'C':  result=1; break;
      case 'H':  result=2; break;
      case 'S':  result=3; break;
    }
    return result;
  }
  char changeint2suit(int v){
    int result;
    switch(SSSS(v)){
      case 0:  result='D'; break;
      case 1:  result='C'; break;
      case 2:  result='H'; break;
      case 3:  result='S'; break;
    }
    return result;
  }

  char changeint2face(int v){
    int result;
    int rank=RRRR(v);
    const char faces[13]={'2','3','4','5','6','7','8',
                   '9','T','J','Q','K','1'};    
    result=faces[rank];
    return result;
  }
  int Convert(Card c){
    int result;
    int face=c.face();
    char suit=c.suit();
    if(face==1)
      face=14;
    result=(face-2)*4;
    result+=changesuit2int(suit);
    return result;
  }

  int SameRank(int rank,int cards[],int numcards,int where[]){
    int count=0;
    for(int i=0;i<numcards && RRRR(cards[i])<=rank;i++){
      if(RRRR(cards[i])==rank){
        where[count]=i;
        count++;
      }
    }
    return count;
  }
  int SameSuit(int suit,int cards[],int numcards,int where[]){
    int count=0;
    for(int i=0;i<numcards;i++){
      if(SSSS(cards[i])==suit){
        where[count]=i;
        count++;
      }
    }
    return count;
  }

  int HasFull(int start,int cards[],int numcards,int where[]){
    int loc1[4];
    int loc2[4];
    int begin=RRRR(start);
    int numsame=SameRank(begin,cards,numcards,loc1);
    int found=0;
    int tmp;
    int i,j;
    int retval=0;
    if(numsame>=2){
      for(i=0;i<=12 && !found;i++){
        if(i!=begin){
          tmp=SameRank(i,cards,numcards,loc2);
          if(tmp>=3 || (tmp==2 && numsame>=3)){
            found=1;
          }
        }
      }//for
      if(found){
        for(i=0,j=0;i<2;i++){
          where[j++]=loc1[i];
          where[j++]=loc2[i];
        }
        if(tmp==2)
          where[j]=loc1[2];
        else{
          if(numsame==2)
            where[j]=loc2[2];
          else{
            if(loc1[2] > loc2[2])
              where[j]=loc2[2];
            else
              where[j]=loc1[2];
          }
        }
      }
      retval=found;
    }
    return retval;
  }
  int Pick5(int musthave,int cards[],int num,int where[]){
    int idx=0;
    int retval=0;
    if(num>=5){
      Sort(cards,num);
      retval=HasStraight(musthave,cards,num,where);
      if(!retval){
        retval=HasFlush(musthave,cards,num,where);
        if(!retval){
          retval=HasFull(musthave,cards,num,where);
          if(!retval){
            retval=HasPFour(musthave,cards,num,where);
            if(!retval)
              retval=HasSFlush(musthave,cards,num,where);
          }
        }
      }
    }
    return retval;  
  }

  int PickLess5(int musthave,int cards[],int num,int where[]){
    int retval=SameRank(RRRR(musthave),cards,num,where);
    return retval;
  }  

  int HasPFour(int start,int cards[],int numcards,int where[]){
    int loc[4];
    int begin=RRRR(start);
    int found=-1;
    int retval=0;
    int i;
    if(numcards>=5){
      for(i=begin;i<=12 && found==-1 ;i++){
        if(SameRank(i,cards,numcards,loc)==4)
          found=i;
      }
      if(found!=-1){
        retval=1;
        if(found!=begin)
          where[0]=Search(start,cards,numcards);
        else
          where[0]=4;
        for(i=0;i<4;i++)
          where[i+1]=loc[i];
      }
    }
    return retval;
  }

  int HasStraight(int start,int cards[],int numcards,int where[]){
    int loc[4][4];
    int loccount[4];
    int ok=1;
    int begin=RRRR(start);
    int i=0;
    int total=(begin==0)?3:4;
    for(i=0;i<total && ok;i++){
      ok=SameRank(begin+1+i,cards,numcards,loc[i]);
      loccount[i]=ok;
    }
    if(ok && begin==0){
        ok=SameRank(4,cards,numcards,loc[3]);
        if(!ok)
          ok=SameRank(12,cards,numcards,loc[3]);
        loccount[3]=ok;
    }
      
    if(ok){
      where[0]=Search(start,cards,numcards);
      for(int i=0;i<4;i++){
        where[i+1]=loc[i][0];
      }
      if(allsamesuit(cards,where)){
        int changed=0;
        for(int i=0;i<4 && !changed;i++){
          if(loccount[i]>1){
            where[i+1]=loc[i][1];
            changed=1;
          }
        }
        if(!changed)
          ok=0;
      }
    }
    return ok;
  }
  int allsamesuit(int cards[],int where[]){
    int init=SSSS(cards[where[0]]);
    int same=1;
    for(int i=1;i<4 && same;i++){
      if(SSSS(cards[where[i]])!=init)
        same=0;
    }
    return same;
  }

  int isastraight(int cards[],int where[]){
    int init=RRRR(cards[where[0]]);
    int same=1;
    int total=(init==0)?3:4;
    for(int i=1;i<total && same;i++){
      if(RRRR(cards[where[i]])!=init+i)
        same=0;
    }
    if(init==0){
      if((RRRR(cards[where[4]])!=12) && (RRRR(cards[where[4]])!=4))
        same=0;
    }
    return same;
  }
  int HasFlush(int start,int cards[],int numcards,int where[]){
    int loc[13];
    int begin=SSSS(start);
    int numsame=SameSuit(begin,cards,numcards,loc);
    int i,j;
    int retval=0;
    if(numsame>=5){
      retval=1;
      where[0]=Search(start,cards,numcards);
      for(i=0,j=1;i<numsame && j<5;i++){
        if(cards[loc[i]]!=start)
          where[j++]=loc[i];
      }
      if(isastraight(cards,where)){
        if(numsame==5)
          retval=0;
         else if(where[0]!=loc[5])
           where[4]=loc[5];
         else
           where[4]=loc[4];
      }
    }
    return retval;
  }
  void SetCards(Card setcards[],int cards[],int pick[],int np){
    int i;
      char cardstr[3];
    cardstr[2]='\0';
    for(i=0;i<np;i++){
      cardstr[0]=changeint2suit(cards[pick[i]]);
      cardstr[1]=changeint2face(cards[pick[i]]);
      setcards[i].set(cardstr);
    }
  }
  int Pick5(const Play& oldhand,int*& cards,int num,int where[],int allpass){
    
    Card currcards[5];
    static int pick[5];
    int numpicks=0;
    Play currhand;
    int found=0;
    int i,j;
    int (*chckfn[5])(int,int[],int,int[])={HasStraight,HasFlush,
                                     HasFull,HasPFour,HasSFlush};

    if(num>=5){
      Sort(cards,num);
      for(j=0;j<5 && !found ;j++){
        for(i=0;i<num && !found;i++){
          if(chckfn[j](cards[i],cards,num,where)){
            if(allpass)
              found=1;
            else{
              SetCards(currcards,cards,where,5);
              currhand.set(currcards,5);
              Play oldhandcpy;
              oldhandcpy=oldhand;
              found=(currhand>oldhandcpy);
              //found=(currhand>oldhand);
            }     
          }
        }
      }
    }
    return found;

  }
  int PickLess5(const Play& oldhand,int cards[],int num,int where[],
                                                            int allpass){
    int found=0;
    int i;
    int begin;
    int numsame;
    int oldnc=oldhand.numCards();
    Play currhand;
    Card currcards[5];
    if(!allpass){
      if(num>=oldnc){
        begin=RRRR(cards[0]);
        for(i=begin;i<=12 && !found;i++){ 
          numsame=SameRank(i,cards,num,where);
          if(numsame==oldnc){
            SetCards(currcards,cards,where,numsame);
            currhand.set(currcards,numsame);
            Play oldhandcpy;
            oldhandcpy=oldhand;
            if(currhand>oldhandcpy)
              found=numsame;
            //if(currhand>oldhand)
            //  found=numsame;
          }
        }
      }
    }
    else if(num>0){
      begin=RRRR(cards[0]);
      int best=1;
      int bestrank=begin;
      for(i=begin;i<=12 && best<4;i++){ 
        numsame=SameRank(i,cards,num,where);
        if(numsame>best){
          best=numsame;
          bestrank=i;
        }
      }
      found=SameRank(bestrank,cards,num,where);
    }
    return found;
  }
}
using namespace ComputerPlayerCode;
bool Player::getPlay(const Play& oldplay,Play& currplay,bool allpass){
  Card* ghcardsinplayer;
  Card ghcardpick[5];
  int* ghcards;
  int ghpick[5];
  int ghcount;
  int newtotal;
  static char ghcstr[11];
  Card* ghnewhand;
  int retval;
  int ghnumcards=numCards();
  int i=0,j=0;
  if(ghnumcards>0){
    try{
      ghcardsinplayer=new Card[ghnumcards];
      ghcards=new int[ghnumcards];
    }
    catch(std::bad_alloc){
      printf("Memory allocation failed.\n");
      exit(0);
    }
    getCards(ghcardsinplayer,ghnumcards);

    for(int i=0;i<ghnumcards;i++){
      ghcards[i]=Convert(ghcardsinplayer[i]);
    }

    Sort(ghcards,ghnumcards);

    if(!allpass){
    /*if everyone has not passed then number of cards thrown must be same
      as those currently being played.*/
      if(oldplay.numCards()==5){
        retval=Pick5(oldplay,ghcards,ghnumcards,ghpick,allpass);
        ghcount=5;
      }
      else{
        retval=PickLess5(oldplay,ghcards,ghnumcards,ghpick,allpass);
        ghcount=oldplay.numCards();
      }
    }
    else{
      /*if everyone else has passed then play 5 cards if possible,*/
      retval=1;
      if(Pick5(oldplay,ghcards,ghnumcards,ghpick,allpass))
        ghcount=5;
      else
        ghcount=PickLess5(oldplay,ghcards,ghnumcards,ghpick,allpass);
    }
    if(retval){
      newtotal=ghnumcards-ghcount;
      try{
        ghnewhand=new Card[newtotal];
      }
      catch(std::bad_alloc){
        printf("Memory allocation failed.\n");
        exit(0);
      }
      SetCards(ghcardpick,ghcards,ghpick,ghcount);
      currplay.set(ghcardpick,ghcount);

      /*create the new array of cards that does not have the cards 
        chosen for the Play in it*/
      Redeal(ghcards,ghnumcards,ghnewhand,ghpick,ghcount);

      /*put it into the current object*/
      deal(ghnewhand,newtotal);
      delete [] ghcards;
      delete [] ghnewhand;
      delete [] ghcardsinplayer;
    }
  }
  else{
    retval=0;
  }
  if (retval==0) {return false;};
  return true;
  //return retval;
}

bool Player::getPlay(Play& firsthand){
  Card* ghcardsinplayer;
  Card ghcardpick[5];
  int* ghcards;
  int ghpick[5];
  int ghcount;
  int newtotal;
  static char ghcstr[11];
  Card* ghnewhand;
  int i=0,j=0;
  int retval=0;
  int ghnumcards=numCards();
  if(ghnumcards>0){
    try{
      ghcardsinplayer=new Card[ghnumcards];
      ghcards=new int[ghnumcards];
    }
    catch(std::bad_alloc){
      printf("Memory allocation failed.\n");
      exit(0);
    }
    getCards(ghcardsinplayer,ghnumcards);

    for(int i=0;i<ghnumcards;i++){
      ghcards[i]=Convert(ghcardsinplayer[i]);
    }

    Sort(ghcards,ghnumcards);

    if(Pick5(ghcards[0],ghcards,ghnumcards,ghpick))
      ghcount=5;
    else
      ghcount=PickLess5(ghcards[0],ghcards,ghnumcards,ghpick);

    newtotal=ghnumcards-ghcount;
    try{
      ghnewhand=new Card[newtotal];
    }
    catch(std::bad_alloc){
      printf("Memory allocation failed.\n");
      exit(0);    
    }
    SetCards(ghcardpick,ghcards,ghpick,ghcount);
    firsthand.set(ghcardpick,ghcount);

    Redeal(ghcards,ghnumcards,ghnewhand,ghpick,ghcount);

    deal(ghnewhand,newtotal);
    delete [] ghcards;
    delete [] ghnewhand;
    delete [] ghcardsinplayer;

  }
    if (ghnumcards==0) {return false;};
    return true;
}
    
2011-05-12 14:53
smsslin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-5-12
收藏
得分:0 
card.h
程序代码:
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[]);

 };
2011-05-12 14:54
smsslin
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-5-12
收藏
得分:0 
card.cpp
程序代码:

#include <new>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "card.h"

using namespace std;

//construct function
Card::Card()
{
  CardFace[0]='\0';
  CardFace[1]='\0';
  CardSuit[0]='\0';
  CardSuit[1]='\0';
};

Card::Card(char cardvar[])
{
    char *chrtmp,chr[250];

    chrtmp=strtok(cardvar," ");
    strcpy(chr,chrtmp);

    while (chrtmp!=NULL)
    {
        chrtmp=strtok(NULL," ");
        if (chrtmp!=NULL)
        {
            strcat(chr,chrtmp);
        };
    };

    CardSuit[0]=chr[0];
    CardFace[0]=chr[1];
};

//destroy function
Card::~Card()
{
  CardFace[0]='\0';
  CardFace[1]='\0';
  CardSuit[0]='\0';
  CardSuit[1]='\0';
};

//char suit() const
//     returns the letter corresponding to the suit of the Card. Always return the
//     Capital letter no matter what was used to initialize the Card.  If the Card
//     is invalid, this function returns the character 'X'
char Card::suit() const
{
    char Re_Suit;

    switch (CardSuit[0])
    {
    case 'd':
        Re_Suit='D';
        break;
    case 'c':
        Re_Suit='C';
        break;
    case 'h':
        Re_Suit='H';
        break;
    case 's':
        Re_Suit='S';
        break;
    case 'D':
        Re_Suit='D';
        break;
    case 'C':
        Re_Suit='C';
        break;
    case 'H':
        Re_Suit='H';
        break;
    case 'S':
        Re_Suit='S';
        break;
    default:
        Re_Suit='X';
        break;
    };
    return Re_Suit;
};

//void set(const char cardstr[])
//     sets the data in the Card according to the suit and face values passed in.  
//     The values use the same representation as that in the constructor description
void Card::set(const char cardstr[])
{
    char *chrtmp,chr[250],chr1[250];

    strcpy(chr1,cardstr);

    chrtmp=strtok(chr1," ");
    strcpy(chr,chrtmp);

    while (chrtmp!=NULL)
    {
        chrtmp=strtok(NULL," ");
        if (chrtmp!=NULL)
        {
            strcat(chr,chrtmp);
        };
    };

    CardSuit[0]=chr[0];
    CardFace[0]=chr[1];

    return;
};

//int face() const
//     returns the value face value of the card:
//             2 through 10, return the face value
//             Jack return 11
//             Queen return 12
//             King return 13
//             Ace return 1
//             Invalid Cards, return 0
int Card::face() const
{
   int Re_Face;

    switch (CardFace[0])
   {
   case 'j':
       Re_Face=11;
       break;
   case 'J':
       Re_Face=11;
       break;
   case 'q':
       Re_Face=12;
       break;
   case 'Q':
       Re_Face=12;
       break;
   case 'k':
       Re_Face=13;
       break;
   case 'K':
       Re_Face=13;
       break;
   /*case 'a':
       Re_Face=1;
       break;
   case 'A':
       Re_Face=1;
       break;*/
   case 't':
       Re_Face=10;
       break;
   case 'T':
       Re_Face=10;
       break;
   default:
       Re_Face=atoi(CardFace);
       if (Re_Face>0 && Re_Face<=10)
       {
       }
       else
       {
           Re_Face=0;
       };
       break;
   };

   return Re_Face;
};
    
2011-05-12 14:55
快速回复:C++的锄大D,请大大帮忙看看我的code哪里出现了问题。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.115178 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved