/*-
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
static const int ichoose2[5] = { 0, 0, 2, 6, 12 };
static int pairpoints, runpoints; /* Globals from pairuns. */
/*
* scorehand:
* Score the given hand of n cards and the starter card.
* n must be <= 4
*/
int
scorehand(const CARD hand[], CARD starter, int n, BOOLEAN crb,
BOOLEAN do_explain)
{
int i, k;
int hscore;
BOOLEAN flag;
CARD h[(CINHAND + 1)];
char buf[52];
explan[0] = '\0'; /* initialize explanation */
hscore = 0;
flag = TRUE;
k = hand[0].suit;
for (i = 0; i < n; i++) { /* check for flush */
flag = (flag && (hand[i].suit == k));
if (hand[i].rank == JACK) /* check for his nibs */
if (hand[i].suit == starter.suit) {
hscore++;
if (do_explain)
strcat(explan, "His Nobs");
}
h[i] = hand[i];
}
if (flag && n >= CINHAND) {
if (do_explain && explan[0] != '\0')
strcat(explan, ", ");
if (starter.suit == k) {
hscore += 5;
if (do_explain)
strcat(explan, "Five-flush");
} else
if (!crb) {
hscore += 4;
if (do_explain && explan[0] != '\0')
strcat(explan, ", Four-flush");
else
strcpy(explan, "Four-flush");
}
}
if (do_explain && explan[0] != '\0')
strcat(explan, ", ");
h[n] = starter;
sorthand(h, n + 1); /* sort by rank */
i = 2 * fifteens(h, n + 1);
hscore += i;
if (do_explain) {
if (i > 0) {
(void) snprintf(buf, sizeof(buf),
"%d points in fifteens", i);
strcat(explan, buf);
} else
strcat(explan, "No fifteens");
}
i = pairuns(h, n + 1);
hscore += i;
if (do_explain) {
if (i > 0) {
(void) snprintf(buf, sizeof(buf),
", %d points in pairs, %d in runs",
pairpoints, runpoints);
strcat(explan, buf);
} else
strcat(explan, ", No pairs/runs");
}
return (hscore);
}
/*
* fifteens:
* Return number of fifteens in hand of n cards
*/
static int
fifteens(const CARD hand[], int n)
{
int *sp, *np;
int i;
const CARD *endp;
static int sums[15], nsums[15];
np = nsums;
sp = sums;
i = 16;
while (--i) {
*np++ = 0;
*sp++ = 0;
}
for (endp = &hand[n]; hand < endp; hand++) {
i = hand->rank + 1;
if (i > 10)
i = 10;
np = &nsums[i];
np[-1]++; /* one way to make this */
sp = sums;
while (i < 15) {
*np++ += *sp++;
i++;
}
sp = sums;
np = nsums;
i = 16;
while (--i)
*sp++ = *np++;
}
return sums[14];
}
/*
* pairuns returns the number of points in the n card sorted hand
* due to pairs and runs
* this routine only works if n is strictly less than 6
* sets the globals pairpoints and runpoints appropriately
*/
static int
pairuns(const CARD h[], int n)
{
int i;
int runlength, runmult, lastmult, curmult;
int mult1, mult2, pair1, pair2;
BOOLEAN run;
run = TRUE;
runlength = 1;
mult1 = 1;
pair1 = -1;
mult2 = 1;
pair2 = -1;
curmult = runmult = 1;
for (i = 1; i < n; i++) {
lastmult = curmult;
if (h[i].rank == h[i - 1].rank) {
if (pair1 < 0) {
pair1 = h[i].rank;
mult1 = curmult = 2;
} else {
if (h[i].rank == pair1) {
curmult = ++mult1;
} else {
if (pair2 < 0) {
pair2 = h[i].rank;
mult2 = curmult = 2;
} else {
curmult = ++mult2;
}
}
}
if (i == (n - 1) && run) {
runmult *= curmult;
}
} else {
curmult = 1;
if (h[i].rank == h[i - 1].rank + 1) {
if (run) {
++runlength;
} else {
/* only if old short */
if (runlength < 3) {
run = TRUE;
runlength = 2;
runmult = 1;
}
}
runmult *= lastmult;
} else {
/* if just ended */
if (run)
runmult *= lastmult;
run = FALSE;
}
}
}
pairpoints = ichoose2[mult1] + ichoose2[mult2];
runpoints = (runlength >= 3 ? runlength * runmult : 0);
return (pairpoints + runpoints);
}
/*
* pegscore tells how many points crd would get if played after
* the n cards in tbl during pegging
*/
int
pegscore(CARD crd, const CARD tbl[], unsigned n, int sum)
{
BOOLEAN got[RANKS];
int i, j, scr;
int k, lo, hi;
unsigned ju;
sum += VAL(crd.rank);
if (sum > 31)
return (-1);
if (sum == 31 || sum == 15)
scr = 2;
else
scr = 0;
if (!n)
return (scr);
ju = 1;
while (ju <= n && crd.rank == tbl[n - ju].rank)
++ju;
if (ju > 1)
return (scr + ichoose2[ju]);
if (n < 2)
return (scr);
lo = hi = crd.rank;
for (i = 0; i < RANKS; i++)
got[i] = FALSE;
got[crd.rank] = TRUE;
k = -1;
for (i = n - 1; i >= 0; --i) {
if (got[tbl[i].rank])
break;
got[tbl[i].rank] = TRUE;
if (tbl[i].rank < lo)
lo = tbl[i].rank;
if (tbl[i].rank > hi)
hi = tbl[i].rank;
for (j = lo; j <= hi; j++)
if (!got[j])
break;
if (j > hi)
k = hi - lo + 1;
}
if (k >= 3)
return (scr + k);
else
return (scr);
}
/*
* adjust takes a two card hand that will be put in the crib
* and returns an adjusted normalized score for the number of
* points such a crib will get.
*/
int
adjust(const CARD cb[], CARD tnv __unused)
{
long scr;
int i, c0, c1;