/*  HNDMGR.C      Hand manager
 */

#include "local.h"
#include "bj.h"
#include "hndmgr.h"
#include "dekmgr.h"

static char spots[13][3] =
   {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
static char suits[4][2] = {"S", "H", "D", "C"};
static short hands[3][12] = {0,0,0};   /* three hands */
static short ncards[3] = {0,0,0};      /* how many cards in each hand */
static short tophand = 0;              /* how many player hands active */

/* allbst      - are all player's hands busted?
 */
bool allbst()
{
   short score();

   if (score(1) <= 21 || (tophand == 2 && score(2) <= 21))
      return(NO);
   else
      return(YES);
}

/* deal     - initialize the hands
 */
void deal()
{
   short tkcard();
   void  show();

   hands[1][0] = tkcard();
   hands[DEALER][0] = tkcard();
   hands[1][1] = tkcard();
   hands[DEALER][1] = tkcard();
   ncards[DEALER] = ncards[1] = 2;
   tophand = 1;
   printf("The dealer shows ");
   show(DEALER, 0);
   printf("\nYou have ");
   show(1,0);
   printf(" + ");
   show(1, 1);
   printf("\n");
}

/* hit  -  add a card to a hand
 */
bool hit(h)
   short h;       /* which hand */
{
   short score(), tkcard();
   void  show();

   hands[h][ncards[h]] = tkcard();
   printf(" + ");
   show(h, ncards[h]);
   ++ncards[h];
   if (21 < score(h) || h == DEALER && 17 <= score(h))
      return(NO);
   else
      return(YES);
}

/* ISBJ     - is hand a "natural" 2-card blackjack?
 */
bool isbj(h)
   short h;       /* which hand */
{
   short score();

   if (h == DEALER)
      return (ncards[DEALER] == 2 && score(DEALER) == 21);
   else if (h == 1)
      return (tophand == 1 && ncards[1] == 2 && score(1) == 21);
   else
      return (NO);
}

/* score    - tell blackjack value of hand
 */
short score(h)
   short h;       /* which hand */
{
   short val();

   short aces = 0;   /* number of aces in hand */
   short i;          /* card counter */
   short sum = 0;    /* accumulated value of hand */

   for (i = 0; i < ncards[h]; ++i) {
      sum += val(h, i);
      if (val(h, i) == 11)
         ++aces;
   }
   for (i = aces; 0 < i; --i)
      if (21 < sum)
         sum -= 10;
   return(sum);
}

/* show     - print a card
 */
void show(h, i)
   short h;       /* which hand */
   short i;       /* which card */
{
   printf("%s", spots[hands[h][i] % 13]);
   printf("%s", suits[hands[h][i] / 13]);
}

/* split    - split the player's pair if allowed
 */
short split()
{
   void  show();
   short tkcard(), val();

   if (val(1, 0) != val(1, 1))
      return(1);
   hands[2][0] = hands[1][1];
   hands[1][1] = tkcard();
   hands[2][1] = tkcard();
   ncards[2] = 2;
   printf("Hand 1: "); show(1, 0); printf(" + "); show(1, 1);
   printf("\n");
   printf("Hand 2: "); show(2, 0); printf(" + "); show(2, 1);
   printf("\n");
   tophand = 2;
   return(2);
}

/* val      - tell value of card n of hand h
 */
short val(h, i)
   short h;    /* which hand */
   short i;    /* which card */
{
   short n;    /* spots value of card */

   n = (hands[h][i] % 13) + 1;
   if (n > 9)
      return (10);
   else if (n == 1)
      return (11);
   else
      return (n);
}

