/* outcom.c       print outcome of hand(s)
 */

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

static CASH value = 0;

/* outcom      - print outcome of hand and compute results
 */
CASH outcom(bet, tophand, isinsur, isdbl)
   CASH  bet;           /* amount of player's bet */
   short tophand;       /* how many player hands */
   bool  isinsur;       /* player took insurance? */
   bool  isdbl;         /* is player DBLDN? */
{
   void prmsg();
   short h;             /* which hand */

   value = 0;
   if (isinsur && isbj(DEALER))
      prmsg(1, 1, "Insurance wins\n", bet / (isdbl ? 4 : 2));
   else if (isinsur)
      prmsg(1, 1, "Insurance loses\n", -bet / (isdbl ? 4 : 2));
   if (isbj(DEALER) && !isbj(1))
      prmsg(1, 1, "Dealer BJ beats all but BJ", -bet / (isdbl ? 2 : 1));
   else if (isbj(DEALER) && isbj(1))
      prmsg(1, 1, "Both BJ: push", (CASH)0);
   else if (isbj(1))
      prmsg(1, 1, "Your BJ wins 3 for 2", (3 * bet) / 2);
   else {
      for (h = 1; h <= tophand; ++h) {
         if (21 < score(h))
            value -= bet;        /* "Bust" message already printed */
         else if (score(DEALER) == score(h))
            prmsg(h, tophand, "Push", (CASH)0);
         else if (score(DEALER) < score(h) || 21 < score(DEALER))
            prmsg(h, tophand, "Win", bet);
         else
            prmsg(h, tophand, "Lose", -bet);
      }
   }
   return (value);
}

/* prmsg    - prints appropriate message
 */
void prmsg(h, tophand, s, delta)
   short h;       /* which hand */
   short tophand; /* how many hands */
   char s[];      /* message */
   CASH delta;    /* change of value ( + | - ) */
{
   if (tophand == 2)
      printf("On hand %d, ", h);
   printf("%s\n", s);
   value += delta;
}

#ifdef TRYMAIN
static short bj[2] = 0;    /* isbj, for each hand */
static short sc[3] = 0;    /* hand scores, for testing */
main()
{
   char  line[BUFSIZ];     /* line of test input */
   short len;              /* returned value from input fn */
   short ibet;             /* player's bet, as short int */
   short ins;              /* isinsur? */
   short toph;             /* tophand? */
   short dbl;              /* isdbl? */
   CASH  value;            /* return from outcom */

   FOREVER {
      printf("%-8s %-8s %-8s %-8s %-8s %-8s %-8s %-8s %-8s\n",
            "bet", "toph", "ins", "dbl",
            "bj[0]", "bj[1]", "sc[0]", "sc[1]", "sc[2]");
      len = echoln(line, BUFSIZ);
      if (len == EOF)
         break;
      if (9 != sscanf(line, "%hd %hd %hd %hd %hd %hd %hd %hd %hd",
            &ibet, &toph, &ins, &dbl,
            &bj[0], &bj[1], &sc[0], &sc[1], &sc[2]))
            error("outcom input error", "");
      value = outcom((CASH)ibet, toph, ins, dbl);
      printf("outcom) = ");
      printf(CASHOUT, value);
      printf("\n");
   }
}

/* score       - dummy version for testing
 */
short score(h)
   short h;    /* which hand */
{
   return(sc[h]);
}

/* isbj        - dummy version for testing
 */
bool isbj(h)
   short h;    /* which hand */
{
   return (bj[h]);
}

/* echoln      - get and echo an input line
 */
short echoln(line, size)
   char  line[];
   short size;
{
   short len;

   if ((len = getln(line, size)) != EOF)
      printf("%s", line);
   return(len);
}
#endif

