-+-+-+-+-+-+-+-+ START OF PART 2 -+-+-+-+-+-+-+-+
X`09  player_counts(your_hand, YOU);
X`09  display_hand(my_hand, 4, VISIBLE, ME);
X`09  I_count(my_hand, ME);
X`09  sleep(3);
X`09  undisplay_hand(ME);
X`09  display_hand(crib_hand, 4, VISIBLE, ME);
X`09  I_count(crib_hand, CRIB);
X`09  undisplay_hand(YOU);
X`09  undisplay_hand(ME);
X`09`7D
X`09else `7B /* player crib...computer counts up */
X`09  display_hand(my_hand, 4, VISIBLE, ME);
X`09  I_count(my_hand, ME);
X`09  display_hand(your_hand, 4, VISIBLE, YOU);
X`09  player_counts(your_hand, YOU);
X`09  undisplay_hand(YOU);
X`09  display_hand(crib_hand, 4, VISIBLE, YOU);
X`09  player_counts(crib_hand, CRIB);`09
X`09  undisplay_hand(YOU);
X`09  undisplay_hand(ME);
X`09`7D`09
X`7D
X
Xplayer_counts(hand, what)
Xstruct card_hand hand;
Xint what;
X`7B
X`09/** sum up value of hand...**/
X
X`09int value, input_value;
X`09char line`5B10`5D;
X
X`09if (what == YOU)
X          show_hand(hand, 4, "Your hand is ","... \n",PLAYED);
X`09else
X`09  show_hand(hand, 4, "Your crib is ","... \n",PLAYED);
X`09
X`09merge(hand, starter);
X`09value = hand_value(hand, 0,1,2,3,4, QUIET);
X
X`09wprintw(i_o_win,"How many points? ");
X`09wrefresh(i_o_win);
X`09gets(line);
X`09wprintw(i_o_win,"%s\n",line);
X`09if (strncmp(line, "qu",2)==0) leave();
X`09sscanf(line,"%d", &input_value);
X
X`09if (input_value > value) `7B
X`09  if (value == 0)`09
X`09    wprintw(i_o_win,"\nC'mon!  There aren't any points in that hand!\n");
X`09  else `7B
X`09    (void) hand_value(hand, 0,1,2,3,4, EXPLAIN);
X`09    wprintw(i_o_win,"\nThat hand's worth exactly %s point%s!!\n",
X                   say(value), plural(value));
X`09  `7D
X`09`7D else if (input_value != value) `7B /* counted too low! */
X`09  (void) hand_value(hand, 0,1,2,3,4, EXPLAIN);
X`09  wprintw(i_o_win,"\n...I counted %s points!  Too bad, eh?\n", say(value)
V);
X`09  value = input_value;
X`09`7D
X`09wrefresh(i_o_win);
X`09add_points(YOU, value);
X`7D
X
X
XI_count(hand, who)
Xstruct card_hand hand;
Xint who;
X`7B
X`09/** let the computer count as appropriate.. **/
X`09int points_in_hand;
X`09
X        if (who == ME) `7B
X          show_hand(my_hand, 4, "My hand is: ", "\n",PLAYED);
X`09  merge(my_hand, starter);
X`09  points_in_hand = hand_value(my_hand, 0, 1, 2, 3, 4, QUIET);
X`09  wprintw(i_o_win,"giving me %s point%s!\n",
X                 say(points_in_hand), plural(points_in_hand));
X`09  add_points(ME, points_in_hand);
X`09`7D
X`09else `7B
X          show_hand(crib_hand, 4, "The crib is: ","... it's worth\n",PLAYED)
V;
X`09  merge(crib_hand, starter);
X`09  points_in_hand = hand_value(crib_hand, 0, 1, 2, 3, 4, QUIET);
X`09  if (points_in_hand == 0)
X`09    wprintw(i_o_win,"no points!\n");
X`09  else `7B
X`09    wprintw(i_o_win,"an additional %s point%s!\n",`20
X                   say(points_in_hand), plural(points_in_hand));
X`09    add_points(ME, points_in_hand);
X`09  `7D
X`09`7D
X      wrefresh(i_o_win);
X`7D
X
X
Xread_a_card(prompt, howmany, card1, card2)
Xchar *prompt;
Xint howmany, *card1, *card2;
X`7B
X`09char line`5B10`5D;
X`09int  loc, error, cardz`5B2`5D, val, i;
X
Xinput:
X`09wprintw(i_o_win,"%s",prompt);
X`09wrefresh(i_o_win);
X`09gets(line);
X`09wprintw(i_o_win,"%s\n",line);
X`09wrefresh(i_o_win);
X`09if (strncmp(line, "qu",2)==0) leave();
X`09if (line`5Bstrlen(line)-1`5D == '\n') line`5Bstrlen(line)-1`5D = '\0';
X
X`09loc = 0;
X`09i = 0;
X`09do `7B
X`09  error=0;
X`09  while (line`5Bloc`5D == ' ' `7C`7C line`5Bloc`5D == ',') loc++;
X`09  if (line`5Bloc`5D == '\0')`20
X            error=4;
X`09  else
X`09    switch(tolower(line`5Bloc`5D)) `7B
X`09      case 'a' : val = ACE;`09`09break;
X`09      case '1' : if (line`5B++loc`5D != '0') error=1;
X`09`09         else val=10;`09break;
X`09      case 'j' : val = JACK;`09break;
X`09      case 'q' : val = QUEEN; `09break;`09
X`09      case 'k' : val = KING;`09break;
X`09      default  : if (line`5Bloc`5D > '1' && line`5Bloc`5D <= '9')
X`09 `09`09    val = (int) line`5Bloc`5D - (int) '0';
X`09`09         else
X`09`09            error=2;
X`09    `7D
X`09  if (!error) `7B
X`09    switch(tolower(line`5B++loc`5D)) `7B
X`09      case 's' : `09break;
X`09      case 'c' : val += 13;`09break;
X`09      case 'h' : val += 26;`09break;
X`09      case 'd' : val += 39;`09break;
X`09      default  : error=3;
X`09    `7D
X`09  `7D
X
X`09  switch (error) `7B
X`09    case 0:`09break;`09`09/** all okay! **/
X`09    case 1: wprintw(i_o_win,"Use 'A' for ace, or '10' for ten card\n");
X`09`09    wrefresh(i_o_win);
X`09            goto input;
X`09    case 2: wprintw(i_o_win,"Unknown card!  Use same notation as presente
Vd!\n");
X`09`09    wrefresh(i_o_win);
X`09`09    goto input;
X`09    case 3: wprintw(i_o_win,"Unknown suite!  Use notation above!\n");
X`09`09    wrefresh(i_o_win);
X`09`09    goto input;
X`09    case 4: wprintw(i_o_win,"expecting %d card%s...\n",howmany,plural(how
Vmany));`09
X`09`09    wrefresh(i_o_win);
X`09`09    goto input;
X`09    default: wprintw(i_o_win,"Error %d on input!\n",error);
X`09`09     wrefresh(i_o_win);
X`09             goto input;
X`09  `7D
X`09  loc++;
X`09  cardz`5Bi++`5D = val;
X`09`7D while (i < howmany);
X
X`09*card1 = cardz`5B0`5D;
X`09*card2 = cardz`5B1`5D;
X`7D
X
Xint
Xcribval(full_hand, partial_hand)
Xstruct card_hand full_hand, partial_hand;
X`7B
X`09/* compute crib value of the two cards in hand */
X`09int value, card`5B2`5D, i, j;
X
X`09get_exceptions(full_hand, partial_hand, &i, &j);
X`09
X`09card`5B0`5D = partial_hand.card`5Bi`5D;
X`09card`5B1`5D = partial_hand.card`5Bj`5D;
X
X`09value = 0;
X`09if (value_of(card`5B0`5D) + value_of(card`5B1`5D) == 15) value++;
X`09if (value_of(card`5B0`5D) ==  5) value++;
X`09if (value_of(card`5B1`5D) ==  5) value++;
X`09if (rank(card`5B0`5D) == rank(card`5B1`5D)) value++;
X`09if (suite(card`5B0`5D) == suite(card`5B1`5D)) value++;
X
X`09return(value);
X`7D
X
Xint
Xhand_value(hand, c1,c2,c3,c4,c5, verbose)
Xstruct card_hand hand;
Xint c1,c2,c3,c4,c5, verbose;
X`7B
X`09/* compute point value of hand returning value.  Verbose lets the`20
X`09   computer enumerate the point computation.. */
X
X`09int value = 0, norun=1, i,j,k,l, card`5B5`5D, cval`5B5`5D, csuite`5B5`5D,
V crank`5B5`5D;
X
X`09/** only use flag if final counting up! **/
X`09if (c5 != 0 && show_counting) verbose++;
X
X`09card`5B0`5D = hand.card`5Bc1`5D;
X`09card`5B1`5D = hand.card`5Bc2`5D;
X`09card`5B2`5D = hand.card`5Bc3`5D;
X`09card`5B3`5D = hand.card`5Bc4`5D;
X`09card`5B4`5D = hand.card`5Bc5`5D;
X
X`09for (i=0; i<5; i++) `7B
X`09  cval`5Bi`5D   = value_of(card`5Bi`5D);
X`09  crank`5Bi`5D  = rank(card`5Bi`5D);
X`09  csuite`5Bi`5D = suite(card`5Bi`5D);
X`09`7D
X
X`09if (c5 == 0) `7B `09/* not here - set to all unmatching values! */
X`09  cval`5B4`5D   = -1;
X `09  crank`5B4`5D  = -1;
X`09  csuite`5B4`5D = -1;
X`09`7D
X
X`09if (verbose)
X`09  `7B
X`09    wprintw(i_o_win,"\nI score this hand as:\n");
X`09    wrefresh(i_o_win);
X`09  `7D
X
X`09/* first off, let's compute all point values of two card combos.. */
X
X`09for (i=0; i<4; i++)
X`09  for (j=i+1; j<5; j++) `7B
X`09    if (cval`5Bi`5D + cval`5Bj`5D == 15) `7B
X              value += 2;`20
X`09      if (verbose)
X`09`09`7B
X`09`09  wprintw(i_o_win,"two-card fifteen: %s\n", say(value));
X`09`09  wrefresh(i_o_win);
X`09`09`7D
X`09    `7D
X`09    else if (crank`5Bi`5D == crank`5Bj`5D) `7B
X`09      value += 2;`20
X`09      if (verbose)
X`09`09`7B
X`09`09  wprintw(i_o_win,"a pair: %s\n", say(value));
X`09`09  wrefresh(i_o_win);
X`09`09`7D
X`09    `7D
X`09  `7D
X
X`09/* now three card fifteens... */
X
X`09for (i=0; i<3; i++)
X`09  for (j=i+1; j<4; j++)`20
X`09    for (k=j+1; k<5; k++) `7B
X`09      if ((cval`5Bi`5D+cval`5Bj`5D+cval`5Bk`5D) == 15)`20
X`09        if (c5 `7C`7C k != 4) `7B
X`09          value += 2;
X`09          if (verbose)
X`09`09    `7B
X`09`09      wprintw(i_o_win,"a three-card fifteen: %s\n", say(value));
X`09`09      wrefresh(i_o_win);
X`09`09    `7D
X`09        `7D
X`09    `7D
X
X`09/* check for a four-card 15 combination (pretty unlikely!) */
X
X`09for (i=0; i<2; i++)
X`09  for (j=i+1; j<3; j++)`20
X`09    for (k=j+1; k<4; k++)
X`09      for (l=k+1;l<5; l++)`20
X`09        if (cval`5Bi`5D+cval`5Bj`5D+cval`5Bk`5D+cval`5Bl`5D == 15)`20
X`09          if (c5 `7C`7C l != 4) `7B
X`09            value += 2;
X`09            if (verbose)
X`09`09      `7B
X`09`09`09wprintw(i_o_win,"four-card fifteen: %s\n", say(value));
X`09`09`09wrefresh(i_o_win);
X`09`09      `7D
X`09          `7D
X
X`09/* check for a five-card 15 combination! (fat chance!) */
X
X`09if (cval`5B0`5D+cval`5B1`5D+cval`5B2`5D+cval`5B3`5D+cval`5B4`5D == 15)`20
X`09  if (c5) `7B
X`09    value += 2;
X`09    if (verbose)
X`09      `7B
X`09`09wprintw(i_o_win,"five-card fifteen: %s\n", say(value));
X`09`09wrefresh(i_o_win);
X`09      `7D
X`09  `7D
X`09
X`09/* check for five card run.. */
X
X`09if ((crank`5B0`5D+1 == crank`5B1`5D) && (crank`5B1`5D+1 == crank`5B2`5D)
V &&
X`09    (crank`5B2`5D+1 == crank`5B3`5D) && (crank`5B3`5D+1 == crank`5B4`5D))
V `7B
X`09  value += 5;
X`09  norun--;
X`09  if (verbose)
X`09    `7B
X`09      wprintw(i_o_win,"a run of five: %s\n", say(value));
X`09      wrefresh(i_o_win);
X`09    `7D
X        `7D
X
X`09/* check for four card run.. */
X
X`09else `7B
X`09  for (i=0; i<2; i++)
X`09    for (j=i+1; j<3; j++)`20
X`09      for (k=j+1; k<4; k++)`20
X`09        for (l=k+1; l<5; l++)
X`09         if ((crank`5Bi`5D+1 == crank`5Bj`5D) && (crank`5Bj`5D+1 == crank
V`5Bk`5D) &&
X`09             (crank`5Bk`5D+1 == crank`5Bl`5D)) `7B
X`09           value += 4;
X`09           norun--;
X`09           if (verbose)
X`09`09     `7B
X`09`09       wprintw(i_o_win,"a run of four: %s\n",say(value));
X`09`09       wrefresh(i_o_win);
X`09`09     `7D
X`09    `09 `7D     `20
X`09`7D
X
X`09/* then check for three card run */
X
X`09if (norun == 1) `20
X`09  for (i=0; i<3; i++)
X`09    for (j=i+1; j<4; j++)`20
X`09      for (k=j+1; k<5; k++)`20
X`09         if ((crank`5Bi`5D+1 == crank`5Bj`5D) && (crank`5Bj`5D+1 == crank
V`5Bk`5D)) `7B
X`09           value += 3;
X`09           if (verbose)
X`09`09     `7B
X`09`09`09wprintw(i_o_win,"a run of three: %s\n",say(value));
X`09`09`09wrefresh(i_o_win);
X`09`09     `7D
X`09         `7D
X
X`09/* and finally check for flush! */
X`09
X`09/** five card? **/
X
X`09if ((csuite`5B0`5D == csuite`5B1`5D) && (csuite`5B1`5D == csuite`5B2`5D)
V &&
X            (csuite`5B2`5D == csuite`5B3`5D) && (csuite`5B3`5D == csuite`5B4
V`5D))  `7B
X`09      value += 5;
X`09      if (verbose)
X`09`09`7B
X`09`09  wprintw(i_o_win,"five card flush: %s\n",say(value));
X`09`09  wrefresh(i_o_win);
X`09`09`7D
X`09`7D
X`09else `7B
X`09  for (i=0; i<2; i++)
X`09    for (j=i+1; j<3; j++)`20
X`09      for (k=j+1; k<4; k++)`20
X`09        for (l=k+1; l<5; l++)
X`09          if ((csuite`5Bi`5D == csuite`5Bj`5D) && (csuite`5Bj`5D == csuit
Ve`5Bk`5D) &&
X`09              (csuite`5Bk`5D == csuite`5Bl`5D))`20
X`09              if (suite(starter) != csuite`5Bi`5D) `7B /* not starter? */
X`09                value += 4;
X`09                if (verbose)
X`09`09`09  `7B
X`09`09`09    wprintw(i_o_win,"four card flush: %s\n",say(value));
X`09`09`09    wrefresh(i_o_win);
X`09`09`09  `7D
X`09              `7D
X`09`7D
X
X`09/** one final check: if all five cards are given to the program`20
X`09    (indicating final counting) AND the hand contains a jack that
X`09    is of the same suite as the starter, add a point for 'his nobs' **/
X
X`09if (c5)`20
X`09  for (i=0;i<5;i++)
X`09    if (crank`5Bi`5D == JACK)
X`09      if (csuite`5Bi`5D == suite(starter) && card`5Bi`5D != starter) `7B
X`09        value += 1;
X`09`09if (verbose)
X`09`09  `7B
X`09`09    wprintw(i_o_win,"and one for his nobs: %s\n", say(value));
X`09`09    wrefresh(i_o_win);
X`09`09  `7D
X`09      `7D
X
X`09return(value);
X`7D
X
Xshow_hand(hand, max, prefix, suffix, alt_stat)
Xstruct card_hand hand;
Xint max, alt_stat;
Xchar *prefix, *suffix;
X`7B
X`09/** display hand on one line... **/
X`09register int i;
X
X`09wprintw(i_o_win,"%s",prefix);
X`09for (i=0;i<max;i++)`20
X`09  if (hand.status`5Bi`5D == AVAILABLE `7C`7C hand.status`5Bi`5D == alt_st
Vat)
X`09    wprintw(i_o_win,"%s ",say_card(hand.card`5Bi`5D,SHORT_FORM));
X`09  else if (show_played_cards)
X`09    wprintw(i_o_win,"(%s) ",say_card(hand.card`5Bi`5D,SHORT_FORM));
X`09wprintw(i_o_win,"%s",suffix);
X`09wrefresh(i_o_win);
X`7D
X
Xchar *say_card(n, pw)
Xint n, pw;
X`7B
X`09/* display card 'n'.  PW = Partial Word flag... */
X
X`09static char buffer`5B40`5D;
X`09register int r;
X`09char temp`5B15`5D;
X`09
X`09switch ((r = rank(n))) `7B
X`09  case ACE  : strcpy(buffer,pw?"A":"Ace"); `09break;
X`09  case JACK : strcpy(buffer,pw?"J":"Jack");`09break;
X`09  case QUEEN: strcpy(buffer,pw?"Q":"Queen");break;
X`09  case KING : strcpy(buffer,pw?"K":"King");`09break;
X`09  default   : if (pw) sprintf(buffer,"%d",r);
X`09              else `7B
X`09`09        strcpy(temp, say(r));
X`09`09`09temp`5B0`5D = toupper(temp`5B0`5D);
X`09                strcpy(buffer,temp);
X`09              `7D
X`09`7D
X
X`09if (! pw) strcat(buffer, " of ");
X
X`09switch(suite(n)) `7B
X`09  case 0: strcat(buffer,pw?"s":"Spades");`09`09break;
X`09  case 1: strcat(buffer,pw?"c":"Clubs");`09`09break;
X`09  case 2: strcat(buffer,pw?"h":"Hearts");`09`09break;
X`09  case 3: strcat(buffer,pw?"d":"Diamonds");`09`09break;
X`09`7D
X
X`09return( (char *) buffer);
X`7D
X
Xint
Xvalue_of(n)
Xint n;
X`7B
X`09int r, v;
X`09
X`09if ((r = n % 13) == 0) v = 10;
X`09else if (r > 10)       v = 10;
X`09else v = r;
X
X`09return(v);
X`7D
X
Xorder(hand, max)
Xstruct card_hand *hand;
Xint max;
X`7B
X`09/* reorder hand according to rank, lowest to highest */
X
X`09int i, changed, temp, tempstat;
X
X`09do `7B
X          changed = 0;
X`09  for (i = 0; i < max-1; i++)
X`09    if (rank(hand->card`5Bi`5D) > rank(hand->card`5Bi+1`5D)) `7B
X`09      temp = hand->card`5Bi`5D;
X`09      tempstat = hand->status`5Bi`5D;
X`09      hand->card`5Bi`5D = hand->card`5Bi+1`5D;
X`09      hand->status`5Bi`5D = hand->status`5Bi+1`5D;
X`09      hand->card`5Bi+1`5D = temp;
X`09      hand->status`5Bi+1`5D = tempstat;
X`09      changed++;
X`09    `7D
X`09 `7D while (changed);
X`7D
X
Xint
Xmark_as_played(hand, card, whose)
Xstruct card_hand *hand;
Xint card, whose;
X/*
+-+-+-+-+-+-+-+-  END  OF PART 2 +-+-+-+-+-+-+-+-
