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 / find.c < prev    next >
Text File  |  1988-07-28  |  3KB  |  116 lines

  1. /* find.c
  2.  *    contains the heuristic function to determine which
  3.  *    dice should be held to achieve the highest possible
  4.  *    score.
  5.  */
  6. #include <curses.h>
  7. #include "defs.h"
  8. #ifdef BSD
  9. #ifdef register
  10. #undef register
  11. #endif
  12. #define register
  13. #endif
  14.  
  15. #define Chance_Category 13
  16.  
  17. extern int scoreboard[max_players][max_marks];
  18. extern int available[13], avail_count;
  19. extern float cost_list[13][51];
  20. extern WINDOW *screen;
  21.  
  22. /* computer_move() forms a list of available categories then passes
  23.  * that list to the heuristic function find_hold() */
  24. computer_move(player, dice, hold)
  25.  
  26. int player, dice[Five_Dice], hold[Five_Dice];
  27.  
  28.     {
  29.     int mark;
  30.  
  31.     wmove(screen, 5, 0);
  32.     avail_count = 0;
  33.  
  34. /*    form list of available categories,
  35.  *    keep 'chance' category out of list of available
  36.  *    categories, otherwise the heuristics treat it as
  37.  *    a sure bet instead of a last resort */
  38.     for (mark = 1; mark < Chance_Category; ++mark)
  39.         if (scoreboard[player][mark] == category_available)
  40.             available[avail_count++] = mark;
  41.  
  42. /*    ...but slide it in if nothing else is available */
  43.     if (avail_count == 0)
  44.         {
  45.         available[0] = Chance_Category;
  46.         avail_count = 1;
  47.         }
  48.  
  49. /* call heuristic function */
  50.     find_hold(dice, available, avail_count, hold);
  51.  
  52. /* restore chance category to list of available categories */
  53.     if (scoreboard[player][Chance_Category] == category_available
  54.         && available[0] != Chance_Category)
  55.         {
  56.         available[avail_count] = Chance_Category;
  57.         ++avail_count;
  58.         }
  59.     }
  60.  
  61. /* find_hold() is the heuristic function to determine the best hold */
  62. int find_hold(dice, available, avail_count, new_hold)
  63.  
  64. int dice[Five_Dice], available[13], avail_count, new_hold[Five_Dice];
  65.  
  66.     {
  67.     register int i, avail_mark, holds[Five_Dice], new_dice[Five_Dice],
  68.         mark; 
  69.  
  70. /* set best to an impossibly bad value */
  71.     register float sum, evaluation, best = -1000.0, average;
  72.  
  73. /* iterate mark through all available categories */
  74.     for (mark = 0; mark < avail_count; ++mark)
  75.         {
  76.  
  77. /* preserve old dice by using a new copy */
  78.         memcpy(new_dice, dice, DICE_COPY_N);
  79.         avail_mark = available[mark];
  80.  
  81. /* determine best hold for the given category, find_best_hold is
  82.  * converted to a table of function calls (indexed by avail_mark)
  83.  * by /lib/cpp at compile time (see defs.h) */
  84.         find_best_hold(new_dice, avail_mark, holds);
  85.  
  86.         sum = 0.0;
  87.         for (i = 0; i < STAT_COUNT; ++i)
  88.             {
  89.  
  90. /* roll dice STAT_COUNT times (dice held by call to find_best_hold()
  91.  * will not be altered) */
  92.             roll_dice(new_dice, holds);
  93.  
  94. /* sum the evaluation of the dice (based on the current category aval_mark) */
  95.             sum += eval(avail_mark, new_dice);
  96.             }
  97.  
  98. /* calculate the projected average score based on the above trials */
  99.         average = sum / STAT_COUNT;
  100.  
  101. /* offset this by cost_list so that the evaluation is aware of the
  102.  * bonus for 63, the perils of chasing straights, the escape route
  103.  * of the 1 and 2 categories, etc. */
  104.         evaluation = average + cost_list[mark][(int) average];
  105.  
  106. /* if this category has produced the best evaluation... */
  107.         if (best < evaluation)
  108.             {
  109.  
  110. /* ...then store best evaluation and store best holds */
  111.             best = evaluation;
  112.             memcpy(new_hold, holds, DICE_COPY_N);
  113.             }
  114.         }
  115.     }
  116.