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 >
Wrap
Text File
|
1988-07-28
|
3KB
|
116 lines
/* find.c
* contains the heuristic function to determine which
* dice should be held to achieve the highest possible
* score.
*/
#include <curses.h>
#include "defs.h"
#ifdef BSD
#ifdef register
#undef register
#endif
#define register
#endif
#define Chance_Category 13
extern int scoreboard[max_players][max_marks];
extern int available[13], avail_count;
extern float cost_list[13][51];
extern WINDOW *screen;
/* computer_move() forms a list of available categories then passes
* that list to the heuristic function find_hold() */
computer_move(player, dice, hold)
int player, dice[Five_Dice], hold[Five_Dice];
{
int mark;
wmove(screen, 5, 0);
avail_count = 0;
/* form list of available categories,
* keep 'chance' category out of list of available
* categories, otherwise the heuristics treat it as
* a sure bet instead of a last resort */
for (mark = 1; mark < Chance_Category; ++mark)
if (scoreboard[player][mark] == category_available)
available[avail_count++] = mark;
/* ...but slide it in if nothing else is available */
if (avail_count == 0)
{
available[0] = Chance_Category;
avail_count = 1;
}
/* call heuristic function */
find_hold(dice, available, avail_count, hold);
/* restore chance category to list of available categories */
if (scoreboard[player][Chance_Category] == category_available
&& available[0] != Chance_Category)
{
available[avail_count] = Chance_Category;
++avail_count;
}
}
/* find_hold() is the heuristic function to determine the best hold */
int find_hold(dice, available, avail_count, new_hold)
int dice[Five_Dice], available[13], avail_count, new_hold[Five_Dice];
{
register int i, avail_mark, holds[Five_Dice], new_dice[Five_Dice],
mark;
/* set best to an impossibly bad value */
register float sum, evaluation, best = -1000.0, average;
/* iterate mark through all available categories */
for (mark = 0; mark < avail_count; ++mark)
{
/* preserve old dice by using a new copy */
memcpy(new_dice, dice, DICE_COPY_N);
avail_mark = available[mark];
/* determine best hold for the given category, find_best_hold is
* converted to a table of function calls (indexed by avail_mark)
* by /lib/cpp at compile time (see defs.h) */
find_best_hold(new_dice, avail_mark, holds);
sum = 0.0;
for (i = 0; i < STAT_COUNT; ++i)
{
/* roll dice STAT_COUNT times (dice held by call to find_best_hold()
* will not be altered) */
roll_dice(new_dice, holds);
/* sum the evaluation of the dice (based on the current category aval_mark) */
sum += eval(avail_mark, new_dice);
}
/* calculate the projected average score based on the above trials */
average = sum / STAT_COUNT;
/* offset this by cost_list so that the evaluation is aware of the
* bonus for 63, the perils of chasing straights, the escape route
* of the 1 and 2 categories, etc. */
evaluation = average + cost_list[mark][(int) average];
/* if this category has produced the best evaluation... */
if (best < evaluation)
{
/* ...then store best evaluation and store best holds */
best = evaluation;
memcpy(new_hold, holds, DICE_COPY_N);
}
}
}