contest/
contest/Merc21/
contest/Merc21/log/
contest/Merc21/player/
/******************************************************************************
 Copyright 2005-2007 Richard Woolcock.  All rights reserved.

 Redistribution and use in source and binary forms, with or without 
 modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer. 

 * Redistributions in binary form must reproduce the above copyright notice, 
   this list of conditions and the following disclaimer in the documentation 
   and/or other materials provided with the distribution. 

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/

/*****************************************************************************
 * Headers
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hand.h"
#include "score.h"

/*****************************************************************************
 * Local function prototypes
 *****************************************************************************/

/* Calculate the highest of two numbers */
static int HighestOf( int aFirst, int aSecond );

/*****************************************************************************
 * Global functions
 *****************************************************************************/

/* Function: ScorePoker
 *
 * This function calculates and returns your poker combination and score.  If 
 * the combination is NULL, then the function will instead return a rundown on 
 * your current score in a displayable format.
 *
 * Arguments: The hand to check, and the location to store the combination.
 *
 * Returns: A string containing the score information.
 */
const char *ScorePoker( hand_t *apHand, combination_t *apCombination )
{
   static char OutputBuffer[1024];
   combination_t Combo = eCombinationMax;

   /* Clear the output buffer */
   OutputBuffer[0] = '\0';

   if ( apHand != NULL )
   {
      int Clubs = HandGetSuit(apHand, eSuitClubs);
      int Spades = HandGetSuit(apHand, eSuitSpades);
      int Hearts = HandGetSuit(apHand, eSuitHearts);
      int Diamonds = HandGetSuit(apHand, eSuitDiamonds);

      int Suit = HighestOf(HighestOf(Clubs,Spades), HighestOf(Hearts,Diamonds));

      int i,j; /* Loop counters */
      int Fours = 0, Threes = 0, Pairs = 0, Sequence = 0, HighestSequence = 0;
      int RoyalFlush = 0, StraightFlush = 0;

      char KindData[15] = {'\0'}, SuitData[4][15] = { {'\0'}, {'\0'}, {'\0'}, {'\0'} };

      SuitData[0][13] = '-';
      SuitData[1][13] = '-';
      SuitData[2][13] = '-';
      SuitData[3][13] = '-';

      for ( i = 0; i < 13; ++i )
      {
         KindData[i]  = '0' + HandGetRank(apHand, (rank_t)i);

         for ( j = 0; j < 4; ++j )
         {
            if ( HandGetSuitRank(apHand, (suit_t)j, (rank_t)i) == eTrue )
            {
               SuitData[j][i] = 'X';

               /* Ace counts as high as well as low */
               if ( i == 0 )
                  SuitData[j][13] = 'X';
            }
            else /* No match for this one */
            {
               SuitData[j][i] = '-';
            }
         }
      }

      /* Ace counts as high as well as low */
      KindData[13] = KindData[0];

      for ( i = 0; i <= 13; ++i )
      {
         if ( i < 13 )
         {
            /* Note, four of a kind is also counted as three of a kind, etc */
            Fours  += (KindData[i] == '4');
            Threes += (KindData[i] >= '3');
            Pairs  += (KindData[i] >= '2');
         }

         if ( KindData[i] == '0' )
         {
            /* Store it, if it's the highest yet */
            if ( Sequence > HighestSequence )
               HighestSequence = Sequence;
            Sequence = 0;
         }
         else /* Increment it */
            Sequence++;
      }

      strcat( OutputBuffer, "           A23456789TJQKA" );
      strcat( OutputBuffer, "\n\rClubs    : " );
      strcat( OutputBuffer, &SuitData[0][0] );
      strcat( OutputBuffer, "\n\rSpades   : " );
      strcat( OutputBuffer, &SuitData[1][0] );
      strcat( OutputBuffer, "\n\rHearts   : " );
      strcat( OutputBuffer, &SuitData[2][0] );
      strcat( OutputBuffer, "\n\rDiamonds : " );
      strcat( OutputBuffer, &SuitData[3][0] );
      strcat( OutputBuffer, "\n\rTotal    : " );
      strcat( OutputBuffer, KindData );
      strcat( OutputBuffer, "\n\r\n\r" );

      if ( Suit >= 5 )
      {
         for ( i = 0; i < 4; ++i )
         {
            RoyalFlush += !strcmp(&SuitData[i][9],"XXXXX");
            StraightFlush += !!strstr( &SuitData[i][0], "XXXXX" );
         }
      }

      if ( RoyalFlush >= 1 )
      {
         strcat( OutputBuffer, "ROYAL FLUSH!\n\r" );
         Combo = eCombinationRoyalFlush;
      }
      else if ( StraightFlush >= 1 )
      {
         strcat( OutputBuffer, "STRAIGHT FLUSH!\n\r" );
         Combo = eCombinationStraightFlush;
      }
      else if ( Fours >= 1 )
      {
         strcat( OutputBuffer, "FOUR OF A KIND!\n\r" );
         Combo = eCombinationFourOfAKind;
      }
      else if ( Threes >= 1 && Pairs >= 2 )
      {
         strcat( OutputBuffer, "FULL HOUSE!\n\r" );
         Combo = eCombinationFullHouse;
      }
      else if ( Suit >= 5 )
      {
         strcat( OutputBuffer, "FLUSH!\n\r" );
         Combo = eCombinationFlush;
      }
      else if ( HighestSequence >= 5 )
      {
         strcat( OutputBuffer, "STRAIGHT!\n\r" );
         Combo = eCombinationStraight;
      }
      else if ( Threes >= 1 )
      {
         strcat( OutputBuffer, "THREE OF A KIND!\n\r" );
         Combo = eCombinationThreeOfAKind;
      }
      else if ( Pairs >= 2 )
      {
         strcat( OutputBuffer, "TWO PAIRS!\n\r" );
         Combo = eCombinationTwoPairs;
      }
      else if ( Pairs >= 1 )
      {
         strcat( OutputBuffer, "ONE PAIR!\n\r" );
         Combo = eCombinationOnePair;
      }
      else /* Anything else */
      {
         strcat( OutputBuffer, "HIGH CARD!\n\r" );
         Combo = eCombinationHighCard;
      }

      /* Set the combination, if required */
      if ( apCombination != NULL )
      {
         int Score = 0;
         int HighAce = 1;

         /* The Ace is usually high, unless it's part of a low flush */
         if ( Combo >= eCombinationStraightFlush && KindData[12] >= '0' )
            HighAce = 0;

         *apCombination = Combo;

         /* Set the score as a string of letters (A-O, for calculating worth) */
         for ( i = 4; i >= 1; --i )
         {
            for ( j = 12+HighAce; j >= HighAce; --j )
            {
               if ( KindData[j] >= '0'+i )
               {
                  OutputBuffer[Score++] = 'B' + j;
                  KindData[j] = '0';
               }
            }
         }

         /* Pad the rest of it out with lower value characters */
         while ( Score < 5 )
            OutputBuffer[Score++] = 'A';

         /* Terminate the string */
         OutputBuffer[Score] = '\0';
      }
   }

   return OutputBuffer;
}

/* Function: ScorePoker
 *
 * This function calculates and returns your blackjack score.
 *
 * Arguments: The hand to check.
 *
 * Returns: The score.
 */
int ScoreBlackjack( hand_t *apHand )
{
   int Result = 0;

   if ( apHand != NULL )
   {
      const char *pCardList = HandShow(apHand, eFalse);
      if ( pCardList != NULL )
      {
         int Aces = 0; /* Count these up, add them afterwards */
         int Max = strlen(pCardList);
         int i; /* Loop counter */

         for ( i = 0; i < Max; i += 3 )
         {
            rank_t Rank = CardRank(pCardList[i+1]);
            switch ( Rank )
            {
               case eRankAce:
                  Aces++;
                  break;
               default: /* eRankTwo to eRankTen */
                  Result += Rank+1;
                  break;
               case eRankJack:
               case eRankQueen:
               case eRankKing:
               case eRankMax:
                  Result += 10;
                  break;
            }
         }

         if ( Aces > 0 )
         {
            /* Aces count as 11, or 1 if it would make you go bust */
            Result += Aces;
            if ( Result <= 11 )
               Result += 10;
         }
      }
   }

   /* If you're bust, your score is treated as 0 */
   if ( Result > 21 )
      Result = 0;

   return Result;
}

/*****************************************************************************
 * Local functions
 *****************************************************************************/

/* Function: HighestOf
 *
 * This function returns the highest of the two specified numbers.
 *
 * Arguments: The two numbers to compare.
 *
 * Returns: The value of the highest number.
 */
static int HighestOf( int aFirst, int aSecond ) 
{ 
   if ( aFirst > aSecond ) 
      return aFirst; 
   else 
      return aSecond; 
}