home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xcu16.zip / clients / xhearts / cdiscard.c < prev    next >
C/C++ Source or Header  |  1991-10-03  |  10KB  |  314 lines

  1. /*
  2.  * Copyright 1991 Cornell University
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Cornell U. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Cornell U. makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * CORNELL UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16.  * EVENT SHALL CORNELL UNIVERSITY BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  18.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20.  * PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  * Author:  Gene W. Dykes, Program of Computer Graphics
  23.  *          580 Theory Center, Cornell University, Ithaca, NY 14853
  24.  *          (607) 255-6713   gwd@graphics.cornell.edu
  25.  */
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <X11/Intrinsic.h>
  30. #include "hearts.h"
  31.  
  32. void show_hand () ;
  33. void show_card () ;
  34. void show_card2 () ;
  35. extern void card_picked () ;
  36.  
  37. #define NA 0 /* Not Applicable */
  38.  
  39. static short value[N_SUITS][CARDS_PER_SUIT][CARDS_PER_SUIT] =
  40.     {
  41.      /* Spades */
  42.      {
  43.       { NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  2 */
  44.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  3 */
  45.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  4 */
  46.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  5 */
  47.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  6 */
  48.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  7 */
  49.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  8 */
  50.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  9 */
  51.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /* 10 */
  52.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /* Jack */
  53.      ,{ NA,10,10, 8, 3, 1, 0, 0, 0, 0, 0, 0, 0 } /* Queen */
  54.      ,{ NA,10, 9, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0 } /* King */
  55.      ,{ NA,10, 9, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0 } /* Ace */
  56.      }
  57.      /* Hearts */
  58.     ,{
  59.       { NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  2 */
  60.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  3 */
  61.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  4 */
  62.      ,{ NA, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  5 */
  63.      ,{ NA,10, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  6 */
  64.      ,{ NA,10, 7, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0 } /*  7 */
  65.      ,{ NA,10,10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5 } /*  8 */
  66.      ,{ NA,10,10, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0 } /*  9 */
  67.      ,{ NA,10, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0 } /* 10 */
  68.      ,{ NA, 9, 7, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0 } /* Jack */
  69.      ,{ NA, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0 } /* Queen */
  70.      ,{ NA, 7, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0 } /* King */
  71.      ,{ NA, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /* Ace */
  72.      }
  73.      /* Diamonds */
  74.     ,{
  75.       { NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  2 */
  76.      ,{ NA, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  3 */
  77.      ,{ NA, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  4 */
  78.      ,{ NA, 6, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  5 */
  79.      ,{ NA, 8, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  6 */
  80.      ,{ NA, 9, 7, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0 } /*  7 */
  81.      ,{ NA,10, 7, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0 } /*  8 */
  82.      ,{ NA,10, 8, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0 } /*  9 */
  83.      ,{ NA,10, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0 } /* 10 */
  84.      ,{ NA,10, 9, 7, 5, 3, 1, 0, 0, 0, 0, 0, 0 } /* Jack */
  85.      ,{ NA,10, 9, 8, 6, 3, 2, 0, 0, 0, 0, 0, 0 } /* Queen */
  86.      ,{ NA,10,10, 9, 7, 4, 1, 0, 0, 0, 0, 0, 0 } /* King */
  87.      ,{ NA,10,10, 9, 7, 4, 1, 0, 0, 0, 0, 0, 0 } /* Ace */
  88.      }
  89.      /* Clubs */
  90.     ,{
  91.       { NA, 9, 7, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0 } /*  2 */
  92.      ,{ NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  3 */
  93.      ,{ NA, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  4 */
  94.      ,{ NA, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  5 */
  95.      ,{ NA, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /*  6 */
  96.      ,{ NA, 7, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0 } /*  7 */
  97.      ,{ NA, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0 } /*  8 */
  98.      ,{ NA, 9, 7, 6, 3, 1, 0, 0, 0, 0, 0, 0, 0 } /*  9 */
  99.      ,{ NA,10, 8, 6, 4, 2, 0, 0, 0, 0, 0, 0, 0 } /* 10 */
  100.      ,{ NA,10, 9, 7, 5, 3, 0, 0, 0, 0, 0, 0, 0 } /* Jack */
  101.      ,{ NA,10, 9, 8, 6, 4, 1, 0, 0, 0, 0, 0, 0 } /* Queen */
  102.      ,{ NA,10,10, 9, 7, 5, 2, 0, 0, 0, 0, 0, 0 } /* King */
  103.      ,{ NA,10,10, 9, 7, 6, 3, 0, 0, 0, 0, 0, 0 } /* Ace */
  104.      }
  105.     } ;
  106.  
  107. void
  108. discard_program (ph, player_index)
  109.     PvtHistory *ph ;
  110.     int player_index ;
  111. {
  112. long data[3] ;
  113. int discard_val[N_DISCARDS] ;
  114. int discard_suit[N_DISCARDS] ;
  115. int discard_rank[N_DISCARDS] ;
  116. int discard_card[N_DISCARDS] ;
  117. int real_rank[N_DISCARDS] ;
  118. int n_in_suit[N_SUITS] ;
  119. int d, i, j ;
  120. int defensive_heart_thrown = False ;
  121.  
  122. data[0] = player_index ;
  123.  
  124. show_hand (player_index) ;
  125.  
  126. for (i=0;  i < N_SUITS;  i++)
  127.     {
  128.     /* Need to decide based on original number in suit */
  129.     n_in_suit[i] = ph->n_in_suit[i] ;
  130.     }
  131.  
  132. for (d=0;  d < N_DISCARDS;  d++)
  133.   {
  134.   discard_val[d] = -1 ;
  135.   for (i=0;  i < CARDS_PER_PLAYER;  i++)
  136.     {
  137.     int card = ph->cards_dealt[i] ;
  138.     int suit = info->deck[card].suit ;
  139.     int rank = info->deck[card].rank ;
  140.     int val = value[suit][rank][n_in_suit[suit]] ;
  141. #ifndef NO_LOGGING
  142. show_card2 (suit, rank) ;
  143. if (logerr)
  144. fprintf (logerr, " (%d) ", val) ;
  145. #endif
  146.     if (val > discard_val[d] ||
  147.     (val == discard_val[d] && rank > real_rank[d]))
  148.     {
  149.     for (j=0;  j < d;  j++)
  150.         {
  151.         if (card == discard_card[j])
  152.         break ;
  153.         }
  154.     if (j < d)
  155.         {
  156. #ifndef NO_LOGGING
  157. if (logerr)
  158. fprintf (logerr, "No because already discarded\n") ;
  159. #endif
  160.         continue ;
  161.         }
  162.     if (defensive_heart_thrown && suit == SUIT_HEARTS &&
  163.         rank <= RANK_TEN && rank >= RANK_SIX &&
  164.         ph->n_in_suit[suit] > 2)
  165.         {
  166. /* TODO: refine this */
  167. #ifndef NO_LOGGING
  168. if (logerr)
  169. fprintf (logerr, "No because defensive heart already thrown\n") ;
  170. #endif
  171.         continue ;
  172.         }
  173.         if (!defensive_heart_thrown &&
  174.         suit == SUIT_HEARTS &&
  175.         info->deck[card].rank <= RANK_TEN &&
  176.         info->deck[card].rank >= RANK_SIX)
  177.         {
  178.         int highest_heart_card =
  179.           ph->suit[SUIT_HEARTS][0] ;
  180.         int heart_rank = info->deck[highest_heart_card].rank ;
  181. #ifndef NO_LOGGING
  182. if (logerr)
  183. fprintf (logerr, "Checking whether to throw a defensive heart (%d, %d, %d)\n",
  184. n_in_suit[SUIT_HEARTS], highest_heart_card, heart_rank) ;
  185. #endif
  186.         if (
  187.               ( n_in_suit[SUIT_HEARTS] >= 2 && heart_rank == RANK_ACE) ||
  188.           ( n_in_suit[SUIT_HEARTS] >= 3 && heart_rank == RANK_KING) ||
  189.           ( n_in_suit[SUIT_HEARTS] >= 4 && heart_rank == RANK_QUEEN) ||
  190.           ( n_in_suit[SUIT_HEARTS] >= 5 && heart_rank == RANK_JACK)
  191.            )
  192.           {
  193. #ifndef NO_LOGGING
  194. if (logerr)
  195. fprintf (logerr, "No need to throw a defensive heart\n") ;
  196. #endif
  197.           continue ;
  198.           }
  199.          else
  200.           {
  201. #ifndef NO_LOGGING
  202. if (logerr)
  203. fprintf (logerr, "Defensive heart may be thrown\n") ;
  204. #endif
  205.           }
  206.          }
  207.     discard_val[d] = val ;
  208.     discard_suit[d] = suit ;
  209.     discard_card[d] = card ;
  210.     real_rank[d] = rank ;
  211. #ifndef NO_LOGGING
  212. if (logerr)
  213. fprintf (logerr, "Best yet\n") ;
  214. #endif
  215.     /* TODO : need to make it easier to find the location in the hand */
  216.     for (j=0;  j < ph->n_in_suit[suit];  j++)
  217.         {
  218.         if (ph->suit[suit][j] == card)
  219.         {
  220.         discard_rank[d] = j ;
  221.         break ;
  222.         }
  223.         }
  224.     }
  225. #ifndef NO_LOGGING
  226. else
  227. if (logerr)
  228. fprintf (logerr, "No because value lower\n") ;
  229. #endif
  230.     }
  231.   data[1] = discard_suit[d] ;
  232.   data[2] = discard_rank[d] ;
  233.   card_picked (NULL, data, NULL) ;
  234.     if (discard_suit[d] == SUIT_HEARTS &&
  235.     info->deck[discard_card[d]].rank <= RANK_TEN &&
  236.     info->deck[discard_card[d]].rank >= RANK_SIX)
  237.       {
  238.       defensive_heart_thrown = True ;
  239.       }
  240.   }
  241.  
  242. return ;
  243. }
  244.  
  245. void
  246. show_hand (pi)
  247.     int pi ;
  248. {
  249. int i, j, n=0 ;
  250. #ifndef NO_LOGGING
  251. if (logerr)
  252. fprintf (logerr, "\n\n") ;
  253. for (i=0;  i < N_SUITS;  i++)
  254.     {
  255.     if (logerr)
  256.     fprintf (logerr, "%s : ", game->suit[i].character) ;
  257.     for (j=0;  j < pvt_history[pi].n_in_suit[i]; j++)
  258.     {
  259.     int card = pvt_history[pi].suit[i][j] ;
  260.     int suit = info->deck[card].suit ;
  261.     int rank = info->deck[card].rank ;
  262.     int val = value[suit][rank][pvt_history[pi].n_in_suit[suit]] ;
  263.     if (logerr)
  264.     fprintf (logerr, "%3s(%2d) ", game->suit[suit].rank[rank].symbol, val) ;
  265.     n++ ;
  266.     }
  267.     if (logerr)
  268.     fprintf (logerr, "\n") ;
  269.     }
  270. if (logerr)
  271. fprintf (logerr, "\n") ;
  272. if (n + CurrentTrick != 13)
  273. if (logerr)
  274. fprintf (logerr, "*** Number of Cards is Wrong! *** (%d,%d)\n",
  275. n, CurrentTrick) ;
  276. #endif
  277. }
  278.  
  279. void
  280. show_card (pi, i, j)
  281.     int pi ;
  282.     long i, j ;
  283. {
  284. int card = pvt_history[pi].suit[i][j] ;
  285. int suit = info->deck[card].suit ;
  286. int rank = info->deck[card].rank ;
  287. #ifndef NO_LOGGING
  288. if (logerr)
  289. fprintf (logerr, " %3s%s\n",
  290. game->suit[suit].rank[rank].symbol,
  291. game->suit[suit].character) ;
  292. #endif
  293. }
  294.  
  295. void
  296. show_card2 (suit, rank)
  297.     int suit, rank ;
  298. {
  299. #ifndef NO_LOGGING
  300. if (logerr)
  301. fprintf (logerr, " %3s%s",
  302. game->suit[suit].rank[rank].symbol,
  303. game->suit[suit].character) ;
  304. #endif
  305. }
  306.  
  307. /* TODO: If I am going to hold the queen, then priority for short-suiting */
  308. /* TODO: Whether or not to throw two of clubs depends on remaining clubs */
  309. /* TODO: Whether or not to throw the queen from a four card suit depends
  310.      on the likelihood of being short-suited */
  311. /* TODO: Whether or not to throw the queen depends highly on which direction
  312.      we are passing. */
  313.  
  314.