home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume6 / bj2 / shuffle.c < prev    next >
C/C++ Source or Header  |  1989-07-06  |  2KB  |  121 lines

  1. /* shuffle.c */
  2. /*
  3.             B.J. - Las Vegas Blackjack, Version 1.0
  4.             by Nathan Glasser
  5.             nathan@brokaw.lcs.mit.edu (internet)
  6.             nathan@mit-eddie.uucp (usenet)
  7.  
  8.             April, 1989
  9. ------------------------------------------------------------------------------
  10. Copyright 1989 by Nathan Glasser.
  11. You may feel free to distribute this program in its current form.
  12. Please do not remove this copyright information.
  13. */
  14.  
  15. #include "bj.h"
  16.  
  17. static int num_cards;
  18.  
  19. /* Fill the decks with (NUM_SUITS * num_decks) different occurances of
  20.    each of the numbers from 0 to NUM_RANKS-1. */
  21. shuffle_decks()
  22. {
  23.     int i,j;
  24.     int pos;
  25.     CARD *tmp;
  26.     int tmp_num = num_cards;
  27.  
  28.     printf("\n******    SHUFFLING CARDS    ******\n");
  29.     fflush(stdout);
  30.  
  31.     for (i = 0; i < tmp_num; i++)
  32.     deck[i] = -1;
  33.  
  34.     for (i = 0; i < NUM_RANKS; i++)
  35.     for (j = NUM_SUITS * num_decks; j > 0; j--)
  36.     {
  37.         pos = rnum(tmp_num);
  38.         tmp = deck;
  39.         do
  40.         {
  41.         while (*tmp >= 0)
  42.             tmp++;
  43.         tmp++;
  44.         }
  45.         while (pos--);
  46.         tmp[-1] = i;
  47.         tmp_num--;
  48.     }
  49.  
  50.     cardptr = deck + 1;
  51.  
  52.     if (show_burn)
  53.         printf("\nCard burned: %c\n",cardinfo[*deck].display_char);
  54.  
  55. #if 0
  56.     for (i = 0; i < num_cards; i++)
  57.     printf("%c\t",cardinfo[deck[i]].display_char);
  58.     putchar('\n');
  59. #endif
  60. }
  61.  
  62. check_shuffle()
  63. {
  64.     if (100 * (cardptr - deck) / num_cards >= reshuffle_percentage)
  65.     shuffle_decks();
  66.  
  67.     if (show_pct)
  68.     {
  69. #define DECK_SPACE 64
  70.     int i;
  71.     int limit = 0.5 +
  72.       DECK_SPACE * (1.0 - ((double)(cardptr - deck)) / num_cards);
  73.     int shuf_pt = 0.5 + 
  74.       DECK_SPACE * (1.0 - reshuffle_percentage / 100.0);
  75.  
  76.     printf("Deck remaining: ");
  77.     for (i = 1; i <= limit; i++)
  78.         putchar((i == shuf_pt) ? 'S' : '*');
  79.     putchar('\n');
  80.     }
  81. }
  82.  
  83. /* Returns a random integer from 0 to max */
  84. /* This will be very slightly biased in favor of the lower numbers
  85.    if max isn't a power of 2 */
  86. #ifndef MSDOS
  87. rnum(max)
  88. int max;
  89. {
  90.     long random();
  91.     long tmp = random();
  92.  
  93.     return((int)(tmp % max));
  94. }
  95. #else
  96. rnum(max)
  97. int max;
  98. {
  99.     int tmp = rand();
  100.  
  101.     return((int)(tmp % max));
  102. }
  103. #endif
  104.  
  105. init_decks()
  106. {
  107.     long time();
  108.     long tmp;
  109.  
  110.     tmp = time(NULL);
  111. #ifndef MSDOS
  112.     srandom((int)tmp);
  113. #else
  114.     srand((int)tmp);
  115. #endif
  116.  
  117.     num_cards = NUM_RANKS * NUM_SUITS * num_decks;
  118.     deck = (CARD *)malloc(sizeof(CARD) * num_cards);
  119.     cardptr = deck + num_cards;
  120. }
  121.