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 / deal.c < prev    next >
C/C++ Source or Header  |  1991-10-03  |  6KB  |  243 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 <X11/StringDefs.h>
  31. #include "hearts.h"
  32.  
  33. void show_deal () ;
  34. void order_hand () ;
  35. static void shuffle () ;
  36. static int sdeck[CARDS_PER_DECK] ;
  37.  
  38. #define N_SHUFFLES 100
  39.  
  40. void 
  41. deal ()
  42. {
  43. int i, j ;
  44. char text1[20] ;
  45. char text2[20] ;
  46.  
  47. shuffle () ;
  48.  
  49. for (i=0;  i <  CARDS_PER_PLAYER;  i++)
  50.     {
  51.     for (j=0;  j <  N_PLAYERS;  j++)
  52.     {
  53.     int card_number ;
  54.     card_number = sdeck[i*N_PLAYERS + j] ;
  55.     game->deck[card_number].dealt_to = j ;
  56.     pvt_history[j].cards_dealt[i] = card_number ;
  57.     }
  58.     }
  59.  
  60. for (i=0;  i < N_PLAYERS;  i++)
  61.     {
  62.     game->player[i].n_chosen = 0 ;
  63.     info->n_points_taken[i] = 0 ;
  64.     info->n_tricks_taken[i] = 0 ;
  65.     order_hand (i) ;
  66.     show_deal (i) ;
  67.     }
  68.  
  69. /*
  70.  * Clear out the old table
  71.  * Clear out the old discards
  72.  * Initialize the results in the player points and tricks boxes
  73.  */
  74.  
  75. for (i=0;  i < N_PLAYERS;  i++)
  76.     {
  77.     if (game->player[i].physiology == ComputerPhysiology)
  78.     continue ;
  79.     for (j=0;  j < N_DISCARDS;  j++)
  80.     {
  81.     Arg arg ;
  82.     XtSetArg (arg, XtNlabel, " ") ;
  83.     XtSetValues (game->x.discards[i][j], &arg, 1) ;
  84.     }
  85.     }
  86.  
  87. sprintf (text1, "Points:%3d", 0) ;
  88. sprintf (text2, "Tricks:%3d", 0) ;
  89. for (i=0;  i < N_PLAYERS;  i++)
  90.     {
  91.     if (game->player[i].physiology == ComputerPhysiology)
  92.     continue ;
  93.     for (j=0;  j < N_PLAYERS;  j++)
  94.     {
  95.     Arg arg ;
  96.     XtSetArg (arg, XtNlabel, text1) ;
  97.     XtSetValues (game->x.points[i][j], &arg, 1) ;
  98.     XtSetArg (arg, XtNlabel, text2) ;
  99.     XtSetValues (game->x.tricks[i][j], &arg, 1) ;
  100.     }
  101.     }
  102.  
  103. /*
  104.  * Initialize the per-deal variables
  105.  */
  106.  
  107. CurrentTrick = 0 ;
  108. info->total_hearts_played = 0 ;
  109. info->n_point_cards_taken = 0 ;
  110. info->total_points_taken = 0 ;
  111. info->points_broken = False ;
  112. info->jack_played = False ;
  113. info->queen_played = False ;
  114. game->conceded = False ;
  115.  
  116. return ;
  117. }
  118.  
  119. static void
  120. shuffle ()
  121. {
  122. int i, j ;
  123.  
  124. #ifndef NO_LOGGING
  125. if (logerr) fprintf (logerr, "\nThe Shuffle\n") ;
  126. #endif
  127. for (i=0;  i < CARDS_PER_DECK;  i++)
  128.     sdeck[i] = i ;
  129.  
  130. for (j=0;  j < N_SHUFFLES;  j++)
  131.     {
  132.     for (i=0;  i < CARDS_PER_DECK;  i++)
  133.     {
  134.     int n, temp ;
  135. #ifdef NRAND48
  136.     n = nrand48(history->xsubi) % CARDS_PER_DECK ;
  137. #else
  138.     n = rand() % CARDS_PER_DECK ;
  139. #endif
  140.     temp = sdeck[i] ;
  141.     sdeck[i] = sdeck[n] ;
  142.     sdeck[n] = temp ;
  143.     }
  144.     }
  145.  
  146. #ifndef NO_LOGGING
  147. for (i=0;  i < CARDS_PER_DECK;  i++)
  148.     if (logerr) fprintf (logerr, "%d ", sdeck[i]) ;
  149. if (logerr) fprintf (logerr, "\n\n") ;
  150. #endif
  151.  
  152. return ;
  153. }
  154.  
  155. void
  156. clear_hands ()
  157. {
  158. int i, j, k ;
  159. Arg arg ;
  160.  
  161. for (i=0;  i < N_PLAYERS;  i++)
  162.     {
  163.     if (game->player[i].physiology == ComputerPhysiology)
  164.     continue ;
  165.     for (j=0;  j < N_SUITS;  j++)
  166.     {
  167.     for (k=0;  k < pvt_history[i].n_in_suit[j];  k++)
  168.         {
  169.         XtSetArg (arg, XtNlabel, " ") ;
  170.         XtSetValues (game->x.hands[i][j][k], &arg, 1) ;
  171.         }
  172.     }
  173.     }
  174. return ;
  175. }
  176.  
  177. void
  178. show_deal (i)
  179.     int i ;
  180. {
  181. int j, k ;
  182. Arg arg ;
  183.  
  184. if (game->player[i].physiology == ComputerPhysiology)
  185.     return ;
  186.  
  187. for (j=0;  j < N_SUITS;  j++)
  188.     {
  189.     for (k=0;  k < pvt_history[i].n_in_suit[j];  k++)
  190.     {
  191.     char name_string[40] ;
  192.     int card_number = pvt_history[i].suit[j][k] ;
  193.     int card_rank = info->deck[card_number].rank ;
  194.     sprintf (name_string, "%s%d", game->suit[j].character, k) ;
  195.     XtSetArg (arg, XtNlabel, game->suit[i].rank[card_rank].symbol) ;
  196.     XtSetValues (game->x.hands[i][j][k], &arg, 1) ;
  197.     }
  198.     }
  199. return ;
  200. }
  201.  
  202. void
  203. order_hand (n)
  204.     int n ;
  205. {
  206. int i, j, k ;
  207.  
  208. /*
  209.  * This is made easy by the fact that we can just look through the
  210.  * deck structure for cards in this particular hand.  Since
  211.  * the cards are arranged in order, we'll just fill the suit arrays
  212.  * as they appear...
  213.  */
  214.  
  215. for (i=0;  i < N_SUITS;  i++)
  216.     {
  217.     int *current_number_in_suit = &pvt_history[n].n_in_suit[i] ;
  218.     *current_number_in_suit = 0 ;
  219.     for (j=info->cards_per_suit[i]-1;  j >= 0;  j--)
  220.     {
  221.     int card_number = game->suit[i].rank[j].card ;
  222.     if (game->deck[card_number].dealt_to == n)
  223.         {
  224.         /*
  225.          * But we don't want to order in any that have been selected
  226.          * as discards
  227.          */
  228.         for (k=0;  k < game->player[n].n_chosen;  k++)
  229.         {
  230.         if (pvt_history[n].cards_passed[k] == card_number)
  231.             break ;
  232.         }
  233.         if (k == game->player[n].n_chosen)
  234.         {
  235.         pvt_history[n].suit[i][*current_number_in_suit] = card_number ;
  236.         (*current_number_in_suit)++ ;
  237.         }
  238.         }
  239.     }
  240.     }
  241. return ;
  242. }
  243.