home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / caribbeanstud / hands.cpp < prev    next >
C/C++ Source or Header  |  1997-12-07  |  6KB  |  264 lines

  1. /*
  2.  * (c) Copyright 1997, Qun Zhang.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without fee,
  6.  * provided that the above copyright notice appear in all copies and
  7.  * that both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of Qun Zhang not be used
  9.  * in advertising or publicity pertaining to distribution of the software
  10.  * without specific, written prior permission.  Qun Zhang make no
  11.  * representations about the suitability of this software for any purpose.
  12.  * It is provided "as is" without express or implied warranty.
  13.  *
  14.  * THE ABOVE-NAMED DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16.  * EVENT SHALL THE ABOVE-NAMED BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  18.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20.  * PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  */
  23.  
  24. #include "Card.h"
  25. #include "Hands.h"
  26.  
  27.  
  28. const int     Hands::Ratio[11] = {0, 1, 1, 2, 3, 4, 5, 7, 20, 50, 100 };
  29. const char * Hands::HandNames[11] = { "          ",
  30.                             "          ",
  31.                             " One Pair ",
  32.                             " Two Pairs",
  33.                             " 3 of a Kind",
  34.                             " Straight ",
  35.                             " Flush ",
  36.                             " Full House",
  37.                             " 4 of a Kind",
  38.                             " Straight Flush ",
  39.                             " Royal Flush " };
  40.  
  41.  
  42.  
  43. Hands::Hands() : _score(0), _hand(EX)
  44. {
  45.       for(int i=0; i < 5; i++)
  46.             _cards[i] = ( Card *)0;
  47.         _next = 0;
  48. }
  49.  
  50. #ifndef VMS
  51. Hand Hands::HandValue() const
  52. {
  53.        return    _hand;
  54. }
  55. #else
  56. void Hands::HandValue(const Hand _hand)
  57. {
  58. }
  59. #endif
  60.  
  61. int Hands::NumOfCards() const
  62. {
  63.     return (_cards[0] == (Card*) 0) ? 0 : 5;
  64. }
  65.  
  66. long Hands::Score() const
  67. {
  68.     if(_score == 0)
  69.         SortCards();
  70. #ifdef DEBUG
  71.    cout << "Score: " <<  _score <<endl;
  72. #endif
  73.     return _score;
  74. }
  75.  
  76. const char* const Hands::HandName() const
  77. {
  78.     return Hands::HandNames[_hand];
  79. }
  80.  
  81. int Hands::PayRate() const
  82. {
  83.     return Ratio[ (int)_hand ];
  84. }
  85.  
  86. void Hands::AddCard(Card *card)
  87. {
  88.     _cards[_next++] = card;
  89. }
  90.  
  91. void Hands::NewGame()
  92. {
  93.    _score =0;
  94.     _hand = EX;
  95.    _next = 0;
  96. //   for(int i=0; i < 5; i++)
  97. //      _cards[i] = ( Card *)0;
  98.  
  99. }
  100.  
  101. Card** Hands::Cards() const
  102. {
  103.     return _cards;
  104. }
  105.  
  106. void Hands::SortCards() const
  107. {
  108.     char ranks[6];
  109.  
  110.    if(!_next) return;
  111.  
  112.    for(int i=0; i<5; i++)
  113.         ranks[i] = _cards[i]->Rank();
  114.     
  115.     for(int j=0; j < 4; j++) // sorting based on rank
  116.         for( i=0; i < 4-j; i++)
  117.         {
  118.             if( ranks[i] < ranks[i+1] )
  119.             {
  120.                 ranks[6] = ranks[i];
  121.                 ranks[i] = ranks[i+1];
  122.                 ranks[i+1] = ranks[6];
  123.             }
  124.         }
  125.     ranks[6] = '\0';
  126.             
  127.    // first looking to 1p, 2p, 3k, fh and 4k
  128.  
  129.     char r = ranks[0];
  130.     char board[4];
  131.     for( i =0; i<4; i++) board[i] = '\0';
  132.  
  133.    int s=1;
  134.    for ( i=1; i< 6; i++)
  135.    {
  136.         if( r == ranks[i] ) s++;
  137.         else{
  138.             switch(s){
  139.             case 2:
  140.             {
  141.                 int t = (board[0] == '\0') ? 0 : 1;
  142.                 board[t] = r;
  143.             }
  144.                 break;
  145.             case 3:
  146.                 board[2] = r;
  147.                 break;
  148.             case 4:
  149.                 board[3] = r;
  150.                 break;
  151.             }
  152.             s = 1;
  153.             r = ranks[i];
  154.         }
  155.     }
  156.  
  157.     ((Hands*)this) ->_hand = NP;
  158.  
  159.    if( board[0] || board[1] || board[2] || board[3] )
  160.     {
  161.         if(board[3])
  162.             ((Hands*)this) ->_hand = K4;
  163.         else if(board[2])
  164.             ((Hands*)this) ->_hand = (board[0]) ? FH : K3;
  165.         else
  166.             ((Hands*)this) ->_hand = (board[1]) ? P2 : P1;
  167.  
  168.         ((Hands*)this) ->ReArrange(ranks, 5, board, _hand);
  169.    }
  170.     else{
  171.         if(ranks[0] == (char) '\14') ranks[5] = '\1';
  172.         for( i =0; i < 4; i++)
  173.             if(_cards[i]->Suit() != _cards[i+1]->Suit())
  174.             {
  175.                 i = 8;
  176.                 break;
  177.             }
  178.         if( i != 8 ) ((Hands*)this) ->_hand = FL;
  179.         for( i = 0; i<4; i++)
  180.             if(ranks[i] != ranks[i+1] + 1)
  181.             {
  182.                 i=8;
  183.                 break;
  184.             }
  185.         if(i==8 && ranks[i] == '\14') // for ACE as the first one
  186.             for(i=1; i<4; i++)
  187.                 if(ranks[i] != ranks[i+1] + 1)
  188.                 {
  189.                     i=8; break;
  190.                 }
  191.         if( i != 8 )
  192.         {
  193.             ((Hands*)this) ->_hand = ( _hand == FL ) ? SF : ST;
  194.             if(ranks[4] == ranks[5]+1)
  195.                 for(ranks[5] = '\14',i=0; i<5; i++)
  196.                     ranks[i] = ranks[i+1];
  197.             if(ranks[0] == '\14'&& _hand == SF) ((Hands*)this) ->_hand = RF;
  198.         }
  199.     }
  200.  
  201. // calculate the score for a hand
  202.  
  203.     ((Hands*)this) ->_score = ((int)_hand -1) * 7529536;
  204.     ((Hands*)this) ->_score+=((((ranks[0]*14)+ranks[1])*14+ranks[2])*14+ranks[3])*14+ranks[4];
  205.  
  206. // rearrange cards order
  207.  
  208.     Card* tmpcards[5];
  209.    for(i = 0; i < 5; i++) tmpcards[i] = _cards[i];
  210.     for(i = 0; i < 5; i++)
  211.         for(j=0; j<5; j++)
  212.             if(tmpcards[j] && ranks[i]==tmpcards[j]->Rank())
  213.             {
  214.                 ((Hands*)this) ->_cards[i] = tmpcards[j];
  215.                 tmpcards[j] = (Card *)0;
  216.                 break;
  217.             }
  218.  
  219. #ifdef DEBUG
  220.         for(i = 0; i < 5; i++)
  221.           cout << _cards[i]->Suit() << (int) _cards[i]->Rank() << " ";
  222.       cout << endl;
  223. #endif
  224.  
  225. }
  226.  
  227. void    Hands::ReArrange(char* ranks , int nel,char* board, Hand score)
  228. {
  229.     int j=0 , k = 0;
  230.     char tmprank[10];
  231.    for( int i = 0; i < nel; i++)  // copy all the cards to the tmprank
  232.         tmprank[i] = *(ranks+i);
  233.     tmprank[nel] = '\0';
  234.  
  235.     switch(score) {
  236.     case  P1:
  237.         for( k=2,i = 0; i < nel; i++)
  238.             if(tmprank[i] == *board) *(ranks+j) = tmprank[i], j++;
  239.            else *(ranks+k) = tmprank[i], k++;
  240.         break;
  241.     case  P2:
  242.         for( k=2,i = 0; i < nel; i++)
  243.             if(tmprank[i] == *board) *(ranks+j) = tmprank[i], j++;
  244.            else if(tmprank[i] == *(board+1)) *(ranks+k) = tmprank[i], k++;
  245.             else *(ranks+4) =  tmprank[i];
  246.         break;
  247.     case  K3:
  248.     case  FH:
  249.         for( k=3,i = 0; i < nel; i++)
  250.             if(tmprank[i] == *(board+2)) *(ranks+j) = tmprank[i], j++;
  251.            else *(ranks+k) = tmprank[i], k++;
  252.         break;
  253.     case  K4:
  254.         for(i = 0; i < nel; i++)
  255.             if(tmprank[i] !=*(board+3) )
  256.             {
  257.              *(ranks+i) = *(board+3);
  258.               *(ranks+4) = tmprank[i];
  259.               break;    
  260.             }
  261.    }
  262. }
  263.  
  264.