/* BJ.C        Blackjack
 */

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

main()
{
   CASH  action;     /* how much money has crossed the table */
   CASH  bet;        /* amount of player's current bet per hand */
   CASH  result;     /* net result of this hand, plus or minus */
   CASH  standing;   /* how much has player won or lost */
   bool  canhit;     /* can player's hand take a hit? */
   bool  isdbl;      /* did player take DBLDN? */
   bool  isinsur;    /* did player take insurance? */
   short hand;       /* current hand number */
   short reply;      /* player's reply to DBLDN, SPLIT */
   short tophand;    /* how many hands is player playing, 1 or 2 */

   void  opndek(), shuffl(), deal(), show();
   CASH  getbet(), outcom();
   bool  deklow(), hit(), takes(), allbst();
   short val(), query(), split(), score();

#ifdef AMIGA	/* Braindamaged Lattice C has no default ^C checking */
   extern int Enable_Abort;
   Enable_Abort = 1;
#endif

   printf("Copyright (C) Plum Hall Inc., 1983\n");
   /* permission to copy and modify is granted, provided that this
    * printout and comment remain intact.
    */
   printf("\nWelcome to the Blackjack table\n");
   action = standing = 0;
   opndek();
   while ((bet = getbet()) != 0) {
      tophand = 1;
      isinsur = isdbl = NO;
      if (deklow())
         shuffl();
      deal();
      if (val(DEALER, 0) == 11)
         isinsur = takes("i");
      reply = query();
      if (reply == SPLIT)
         tophand = split();
      else if (reply == DBLDN) {
         hit(1);
         printf("\n");
         isdbl = YES;
         bet *= 2;
      }
      for (hand = 1; hand <= tophand; ++hand) {
         if (tophand == 2)
            printf("Hand %d:\n", hand);
         canhit = !isdbl;
         canhit &= !isbj(1);
         canhit &= (reply != SPLIT || val(1, 0) != 11);
         while (canhit && takes("h")) {
            canhit = hit(hand);
            printf("\n");
         }
         if (21 < score(hand))
            printf("Bust\n");
      }
      printf("Dealer has ");
      show(DEALER, 0);
      printf(" + ");
      show(DEALER, 1);
      if (!allbst())
         while (score(DEALER) < 17)
            hit(DEALER);
      printf(" = %d\n", score(DEALER));
      result = outcom(bet, tophand, isinsur, isdbl);
      action += ABS(result);
      standing += result;
      printf("action= ");
      printf(CASHOUT, action);
      printf(" standing = ");
      printf(CASHOUT, standing);
      printf("\n");
   }
   printf("\nThanks for the game.\n");
   exit(SUCCEED);
}

