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 >
Wrap
Text File
|
1988-07-28
|
3KB
|
147 lines
/* eval.c
* contains routines to determine the score achieved by
* a set of dice for any yahtzee category, these functions
* are called very frequently by the heuristic functions.
*/
#include "defs.h"
#define sortd(dice) \
{ \
register int i, j, tmp; \
for (i = 0; i < Five_Dice; ++i) \
for (j = i; j < Five_Dice; ++j) \
if (dice[i] < dice[j]) \
{ \
tmp = dice[i]; \
dice[i] = dice[j]; \
dice[j] = tmp; \
} \
}
int eval_kind[14] = {0, 1, 2, 3, 4, 5, 6, 3, 4, 0, 4, 5, 5, 0};
static run_score[2] = {small_straight_score, large_straight_score};
extern int abs();
/* if all dice are identical then return yahtzee_score, otherwise 0 */
static int yahtzee(number, dice)
int number, dice[Five_Dice];
{
return(of_a_kind(Five_Dice, dice) ? yahtzee_score : 0);
}
/* return score based on size of a straight */
static int run(size, dice)
int size, dice[Five_Dice];
{
#ifdef BSD
register int i, max_dist = 0, dist = 0, score;
int new_dice[Five_Dice];
#else
register int i, new_dice[Five_Dice], max_dist = 0, dist = 0, score;
#endif
/* duplicate dice */
memcpy(new_dice, dice, DICE_COPY_N);
/* sort new dice */
sortd(new_dice);
/* determine size of the largest run in the dice */
for (i = 0; i < 4; ++i)
{
while (i < 4 && new_dice[i] == new_dice[i + 1])
++i;
if (i < 4)
if (abs(new_dice[i] - new_dice[i + 1]) == 1)
++dist;
else
{
if (dist > max_dist)
max_dist = dist;
dist = 0;
}
}
if (dist > max_dist)
max_dist = dist;
/* determine score based on size of largest run and expected size of run */
if (max_dist + 1 >= size)
score = run_score[size - 4];
else
score = 0;
return(score);
}
static int full_house(dummy, dice)
int dummy, dice[Five_Dice];
{
#ifdef BSD
int new_dice[Five_Dice];
#else
register int new_dice[Five_Dice];
#endif
memcpy(new_dice, dice, DICE_COPY_N);
sortd(new_dice);
/* kwik and dirty */
if ((new_dice[0] == new_dice[1] && new_dice[1] == new_dice[2] &&
new_dice[3] == new_dice[4]) ||
(new_dice[2] == new_dice[3] && new_dice[3] == new_dice[4] &&
new_dice[0] == new_dice[1]))
return(full_house_score);
return(0);
}
static int of_a_kind(count, dice)
int count, dice[Five_Dice];
{
register int marks[dicecount], i, j;
for (i = 0; i < dicecount; ++i)
marks[i] = 0;
for (i = 0; i < Five_Dice; ++i)
{
j = dice[i] - 1;
++marks[j];
if (marks[j] == count)
return(addup(0, dice));
}
return(0);
}
/* sum all dice values matching number, if number is zero then sum all dice */
static int addup(number, dice)
int number, *dice;
{
register int i, score = 0;
if (number)
for (i = 0; i < Five_Dice; ++i, ++dice)
{
if (*dice == number)
score += *dice;
}
else
for (i = 0; i < Five_Dice; ++i)
score += (*dice++);
return(score);
}
/* calls to the above functions are made via a table of function
* calls, this is done to avoid an expensive switch statement */
int (*eval_table[])() = {addup, addup, addup, addup, addup, addup, addup,
of_a_kind, of_a_kind, full_house, run, run, yahtzee, addup};