home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / yahtzee.t.Z / yahtzee.t / eval.c < prev    next >
Text File  |  1988-07-28  |  3KB  |  147 lines

  1. /* eval.c
  2.  *    contains routines to determine the score achieved by
  3.  *    a set of dice for any yahtzee category, these functions
  4.  *    are called very frequently by the heuristic functions.
  5.  */
  6. #include "defs.h"
  7.  
  8. #define sortd(dice) \
  9.     { \
  10.     register int i, j, tmp; \
  11.     for (i = 0; i < Five_Dice; ++i) \
  12.         for (j = i; j < Five_Dice; ++j) \
  13.             if (dice[i] < dice[j]) \
  14.                 { \
  15.                 tmp = dice[i]; \
  16.                 dice[i] = dice[j]; \
  17.                 dice[j] = tmp; \
  18.                 } \
  19.     }
  20.  
  21. int eval_kind[14] = {0, 1, 2, 3, 4, 5, 6, 3, 4, 0, 4, 5, 5, 0};
  22. static run_score[2] = {small_straight_score, large_straight_score};
  23.  
  24. extern int abs();
  25.  
  26. /* if all dice are identical then return yahtzee_score, otherwise 0 */
  27. static int yahtzee(number, dice)
  28.  
  29. int number, dice[Five_Dice];
  30.  
  31.     {
  32.     return(of_a_kind(Five_Dice, dice) ? yahtzee_score : 0);
  33.     }
  34.  
  35. /* return score based on size of a straight */
  36. static int run(size, dice)
  37.  
  38. int size, dice[Five_Dice];
  39.  
  40.     {
  41. #ifdef BSD
  42.     register int i, max_dist = 0, dist = 0, score;
  43.     int new_dice[Five_Dice];
  44. #else
  45.     register int i, new_dice[Five_Dice], max_dist = 0, dist = 0, score;
  46. #endif
  47.  
  48. /* duplicate dice */
  49.     memcpy(new_dice, dice, DICE_COPY_N);
  50.  
  51. /* sort new dice */
  52.     sortd(new_dice);
  53.  
  54. /* determine size of the largest run in the dice */
  55.     for (i = 0; i < 4; ++i)
  56.         {
  57.         while (i < 4 && new_dice[i] == new_dice[i + 1])
  58.             ++i;
  59.         if (i < 4)
  60.             if (abs(new_dice[i] - new_dice[i + 1]) == 1)
  61.                 ++dist;
  62.             else
  63.                 {
  64.                 if (dist > max_dist)
  65.                     max_dist = dist;
  66.                 dist = 0;
  67.                 }
  68.         }
  69.     if (dist > max_dist)
  70.         max_dist = dist;
  71.  
  72. /* determine score based on size of largest run and expected size of run */
  73.     if (max_dist + 1 >= size)
  74.         score = run_score[size - 4];
  75.     else
  76.         score = 0;
  77.     return(score);
  78.     }
  79.  
  80. static int full_house(dummy, dice)
  81.  
  82. int dummy, dice[Five_Dice];
  83.  
  84.     {
  85. #ifdef BSD
  86.     int new_dice[Five_Dice];
  87. #else
  88.     register int new_dice[Five_Dice];
  89. #endif
  90.  
  91.     memcpy(new_dice, dice, DICE_COPY_N);
  92.     sortd(new_dice);
  93.  
  94. /* kwik and dirty */
  95.     if ((new_dice[0] == new_dice[1] && new_dice[1] == new_dice[2] &&
  96.          new_dice[3] == new_dice[4]) ||
  97.         (new_dice[2] == new_dice[3] && new_dice[3] == new_dice[4] &&
  98.          new_dice[0] == new_dice[1]))
  99.         return(full_house_score);
  100.     return(0);
  101.     }
  102.  
  103. static int of_a_kind(count, dice)
  104.  
  105. int count, dice[Five_Dice];
  106.  
  107.     {
  108.     register int marks[dicecount], i, j;
  109.  
  110.     for (i = 0; i < dicecount; ++i)
  111.         marks[i] = 0;
  112.     for (i = 0; i < Five_Dice; ++i)
  113.         {
  114.         j = dice[i] - 1;
  115.         ++marks[j];
  116.         if (marks[j] == count)
  117.             return(addup(0, dice));
  118.         }
  119.     return(0);
  120.     }
  121.  
  122. /* sum all dice values matching number, if number is zero then sum all dice */
  123. static int addup(number, dice)
  124.  
  125. int number, *dice;
  126.  
  127.     {
  128.     register int i, score = 0;
  129.  
  130.     if (number)
  131.         for (i = 0; i < Five_Dice; ++i, ++dice)
  132.             {
  133.             if (*dice == number)
  134.                 score += *dice;
  135.             }
  136.     else
  137.         for (i = 0; i < Five_Dice; ++i)
  138.             score += (*dice++);
  139.     return(score);
  140.     }
  141.  
  142. /* calls to the above functions are made via a table of function
  143.  * calls, this is done to avoid an expensive switch statement */
  144.  
  145. int (*eval_table[])() = {addup, addup, addup, addup, addup, addup, addup,
  146.     of_a_kind, of_a_kind, full_house, run, run, yahtzee, addup};
  147.