zoj 1111 Poker Hands

#include <cstdlib>

#include <iostream>



using namespace std;

struct Card{

       char type;

       int  value;

       Card* next;

};



void Insert(Card* hand, Card* c)

{

    Card* p = hand->next, *q = hand;

    while(p)

    {

            if (p->value >= c->value) p = NULL;

            else

            {

                q = p; p = p->next;

            }

    }

    c->next = q->next;

    q->next = c;

}

int Straight(Card* hand);

int Flush(Card* hand);

int ThreeOfKind(Card* hand);

/**

同花顺 

**/

int StraightFlush(Card* hand)

{

    if (Flush(hand) == 0) return 0;

    if (Straight(hand) == 0) return 0;

    return hand->next->value;

}

/**

4张同 

**/

int FourOfKind(Card* hand)

{

    Card* p = hand->next;

    int val = 0;

    int cnt = 0;

    while (p)

    {

          if (val != p->value){ val = p->value; cnt=0; }

          else { cnt++; if (cnt == 3) return val; }

          //cout<<val<<endl;

          p = p->next;

    }

    return 0;

}



/**

2-3同 

**/

int FullHouse(Card* hand)

{

    int val = ThreeOfKind(hand);

    if (val == 0) return 0;

    Card* p = hand->next;

    while ( val == p->value) p=p->next;

    return p->value == p->next->value ? val : 0;

}



/**

清一色 

**/

int Flush(Card* hand)

{

    Card*p = hand->next;

    char type=p->type;

    for (p = p->next; p; p = p->next)

      if (p->type != type) return 0;

    return 1;

}

/**

一条龙 

**/

int Straight(Card* hand)

{

    Card* p = hand->next; int val=p->value;

    for (p = p->next; p ; p = p->next)

       if (p->value == val + 1) val++;

       else return 0;

    return val;

}

/**

三张同

**/ 

int ThreeOfKind(Card* hand)

{

    Card* p = hand->next; int val=p->value;

    int cnt=0;

    for (p = p->next; p; p = p->next)

      if (val == p->value) { cnt++; if (cnt == 2) return val; }

      else {

           cnt=0;

           val = p->value;

      }

    return 0;

}



/**

两对 

**/

int TwoPair(Card* hand)

{

    Card* p = hand->next; int val=p->value;

    int cnt=0;

    for (p = p->next; p; p = p->next)

      if (val == p->value) cnt++;

      else val = p->value;

    return cnt == 2 ? val : 0;

}



/**

对子

**/ 

int Pair(Card* hand)

{

    Card* p = hand->next; int val=p->value;

    for (p = p->next; p; p = p->next)

      if (val == p->value) return val;

      else val = p->value;

    return 0;

}



int HighCard(Card* hand)

{

    Card* p = hand->next;

    for (; p->next; p = p->next);

    return p->value;

}



bool Input(Card* hand)

{

     char cd[3];

     for (int i = 0; i < 5; i++)

     {

         if (cin>>cd);

         else return false;

         Card * c = new Card;

         c->type = cd[1];

         switch(cd[0])

         {

           case 'A':

                c->value=14;

                break;

           case 'K':

                c->value = 13;

                break;

           case 'Q':

                c->value=12;

                break;

           case 'J':

                c->value=11;

                break;

           case 'T':

                c->value = 10;

                break;

           default:

                 c->value = cd[0] - '0';

                 break;

           }

           Insert(hand, c);

     }

     return true;

}

void Clear(Card* hand)

{

     Card* p = hand->next;

     while(p)

     {

             hand->next = p->next;

             delete p;

             p = hand->next;

     }

}

char* WIN = "White wins./n";

char* LOSE = "Black wins./n";

char* TIE = "Tie./n";

bool Test(int vB, int vW)

{

     if (vB == 0 && vW == 0 ) return false;

     if (vB > vW)  cout<<LOSE;

     else if (vB < vW) cout<<WIN;

     else cout<<TIE;

     return true;

}



void Delete(Card* hand, int val)

{

     Card* q;

     for (Card* p = hand; p && p->next; )

       if (p->next->value == val)

       {

           q = p->next;

           p->next = q->next;

           delete q;

       }

       else p = p->next;

}



bool TestTwoPair(Card* black, Card* white)

{

     int vB, vW;

     vB = TwoPair(black);

     vW = TwoPair(white);

     if (vB ==0 && vW==0) return false;

     if (vB > vW){ cout<<LOSE; return true; }

     if (vB < vW){ cout<<WIN; return true; }

     Delete(black, vB);

     Delete(white, vW);

     

     vB = Pair(black);

     vW = Pair(white);

     if (vB > vW){ cout<<LOSE; return true;}

     if (vB < vW){ cout<<WIN; return true; }

     Delete(black, vB);

     Delete(white, vW);

     

     if (black->next->value == white->next->value) cout<<TIE;

     else  if(black->next->value > white->next->value) cout<<LOSE;

     else cout<<WIN;

     return true;

}



bool TestHighCard(Card* black, Card* white)

{

     int vB, vW;

     while (black->next)

     {

           vB=HighCard(black);

           vW=HighCard(white);

           if (vB > vW) { cout<<LOSE; return true; }

           else if (vB < vW) { cout<<WIN; return true; }

           Delete(black, vB); Delete(white, vB);

     }

     cout<<TIE;

     return false;

}



bool TestPair(Card*black, Card* white)

{

     int vB, vW;

     vB = Pair(black); vW = Pair(white);

     if (vB > vW){ cout<<LOSE; return true; }

     else if (vB < vW){ cout<<WIN; return true; }

     if ( vB > 0)

     {

          Delete(black, vB); Delete(white, vB);

     }

     return false;

}



void Compare(Card* black, Card* white)

{

     int vB, vW;     

     vB = StraightFlush(black);

     vW = StraightFlush(white);

     if (Test(vB, vW)) return;

     

     vB = FourOfKind(black);

     vW = FourOfKind(white);

     if (Test(vB, vW)) return;

     

     vB = FullHouse(black);

     vW = FullHouse(white);

     if (Test(vB, vW)) return;

     

     vB = Flush(black);

     vW = Flush(white);

     if (vB == vW && vB > 0)

     { 

            TestHighCard(black, white);

            return;

     }

     if (Test(vB, vW)) return;

     

     vB = Straight(black);

     vW = Straight(white);

     if (Test(vB, vW)) return;

     

     vB = ThreeOfKind(black);

     vW = ThreeOfKind(white);

     if (Test(vB, vW)) return;

     

     if (TestTwoPair(black, white)) return;

     if (TestPair(black, white)) return ;

     TestHighCard(black, white);

}



void Print(Card* hand)

{

     Card* p = hand->next;

     while (p)

     {

           cout<<p->value<<p->type<<":";

           p = p->next;

     }

     cout<<endl;

}

int main(int argc, char *argv[])

{

    Card* black = new Card, *white = new Card;

    black->value=0; white->value=0;

    black->next = white->next = NULL;

    

    while(Input(black))

    {

         Input(white);

         //cout<<"-----------------------------------------"<<endl;

        // Print(black);

        // Print(white);

         

         Compare(black, white);

         Clear(black);

         Clear(white);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值