score.c revision 1.1.1.2       1  1.1.1.2  cgd /*-
      2  1.1.1.2  cgd  * Copyright (c) 1980, 1993
      3  1.1.1.2  cgd  *	The Regents of the University of California.  All rights reserved.
      4      1.1  cgd  *
      5      1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1  cgd  * modification, are permitted provided that the following conditions
      7      1.1  cgd  * are met:
      8      1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1  cgd  *    must display the following acknowledgement:
     15      1.1  cgd  *	This product includes software developed by the University of
     16      1.1  cgd  *	California, Berkeley and its contributors.
     17      1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1  cgd  *    may be used to endorse or promote products derived from this software
     19      1.1  cgd  *    without specific prior written permission.
     20      1.1  cgd  *
     21      1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1  cgd  * SUCH DAMAGE.
     32      1.1  cgd  */
     33      1.1  cgd 
     34      1.1  cgd #ifndef lint
     35  1.1.1.2  cgd static char sccsid[] = "@(#)score.c	8.1 (Berkeley) 5/31/93";
     36      1.1  cgd #endif /* not lint */
     37      1.1  cgd 
     38  1.1.1.2  cgd #include <curses.h>
     39  1.1.1.2  cgd #include <stdio.h>
     40  1.1.1.2  cgd #include <stdlib.h>
     41  1.1.1.2  cgd #include <string.h>
     42      1.1  cgd 
     43  1.1.1.2  cgd #include "deck.h"
     44  1.1.1.2  cgd #include "cribbage.h"
     45      1.1  cgd 
     46      1.1  cgd /*
     47      1.1  cgd  * the following arrays give the sum of the scores of the (50 2)*48 = 58800
     48      1.1  cgd  * hands obtainable for the crib given the two cards whose ranks index the
     49      1.1  cgd  * array.  the two arrays are for the case where the suits are equal and
     50      1.1  cgd  * not equal respectively
     51      1.1  cgd  */
     52  1.1.1.2  cgd long crbescr[169] = {
     53      1.1  cgd     -10000, 271827, 278883, 332319, 347769, 261129, 250653, 253203, 248259,
     54      1.1  cgd     243435, 256275, 237435, 231051, -10000, -10000, 412815, 295707, 349497,
     55      1.1  cgd     267519, 262521, 259695, 254019, 250047, 262887, 244047, 237663, -10000,
     56      1.1  cgd     -10000, -10000, 333987, 388629, 262017, 266787, 262971, 252729, 254475,
     57      1.1  cgd     267315, 248475, 242091, -10000, -10000, -10000, -10000, 422097, 302787,
     58      1.1  cgd     256437, 263751, 257883, 254271, 267111, 248271, 241887, -10000, -10000,
     59      1.1  cgd     -10000, -10000, -10000, 427677, 387837, 349173, 347985, 423861, 436701,
     60      1.1  cgd     417861, 411477, -10000, -10000, -10000, -10000, -10000, -10000, 336387,
     61      1.1  cgd     298851, 338667, 236487, 249327, 230487, 224103, -10000, -10000, -10000,
     62      1.1  cgd     -10000, -10000, -10000, -10000, 408483, 266691, 229803, 246195, 227355,
     63      1.1  cgd     220971, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
     64      1.1  cgd     300675, 263787, 241695, 226407, 220023, -10000, -10000, -10000, -10000,
     65      1.1  cgd     -10000, -10000, -10000, -10000, -10000, 295635, 273543, 219771, 216939,
     66      1.1  cgd     -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
     67      1.1  cgd     -10000, 306519, 252747, 211431, -10000, -10000, -10000, -10000, -10000,
     68      1.1  cgd     -10000, -10000, -10000, -10000, -10000, -10000, 304287, 262971, -10000,
     69      1.1  cgd     -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
     70      1.1  cgd     -10000, -10000, 244131, -10000, -10000, -10000, -10000, -10000, -10000,
     71  1.1.1.2  cgd     -10000, -10000, -10000, -10000, -10000, -10000, -10000
     72  1.1.1.2  cgd };
     73      1.1  cgd 
     74  1.1.1.2  cgd long crbnescr[169] = {
     75      1.1  cgd     325272, 260772, 267828, 321264, 336714, 250074, 239598, 242148, 237204,
     76      1.1  cgd     232380, 246348, 226380, 219996, -10000, 342528, 401760, 284652, 338442,
     77      1.1  cgd     256464, 251466, 248640, 242964, 238992, 252960, 232992, 226608, -10000,
     78      1.1  cgd     -10000, 362280, 322932, 377574, 250962, 255732, 251916, 241674, 243420,
     79      1.1  cgd     257388, 237420, 231036, -10000, -10000, -10000, 360768, 411042, 291732,
     80      1.1  cgd     245382, 252696, 246828, 243216, 257184, 237216, 230832, -10000, -10000,
     81      1.1  cgd     -10000, -10000, 528768, 416622, 376782, 338118, 336930, 412806, 426774,
     82      1.1  cgd     406806, 400422, -10000, -10000, -10000, -10000, -10000, 369864, 325332,
     83      1.1  cgd     287796, 327612, 225432, 239400, 219432, 213048, -10000, -10000, -10000,
     84      1.1  cgd     -10000, -10000, -10000, 359160, 397428, 255636, 218748, 236268, 216300,
     85      1.1  cgd     209916, -10000, -10000, -10000, -10000, -10000, -10000, -10000, 331320,
     86      1.1  cgd     289620, 252732, 231768, 215352, 208968, -10000, -10000, -10000, -10000,
     87      1.1  cgd     -10000, -10000, -10000, -10000, 325152, 284580, 263616, 208716, 205884,
     88      1.1  cgd     -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
     89      1.1  cgd     321240, 296592, 241692, 200376, -10000, -10000, -10000, -10000, -10000,
     90      1.1  cgd     -10000, -10000, -10000, -10000, -10000, 348600, 294360, 253044, -10000,
     91      1.1  cgd     -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
     92      1.1  cgd     -10000, 308664, 233076, -10000, -10000, -10000, -10000, -10000, -10000,
     93  1.1.1.2  cgd     -10000, -10000, -10000, -10000, -10000, -10000, 295896
     94  1.1.1.2  cgd };
     95      1.1  cgd 
     96  1.1.1.2  cgd static int ichoose2[5] = { 0, 0, 2, 6, 12 };
     97  1.1.1.2  cgd static int pairpoints, runpoints;		/* Globals from pairuns. */
     98      1.1  cgd 
     99      1.1  cgd /*
    100      1.1  cgd  * scorehand:
    101      1.1  cgd  *	Score the given hand of n cards and the starter card.
    102      1.1  cgd  *	n must be <= 4
    103      1.1  cgd  */
    104  1.1.1.2  cgd int
    105      1.1  cgd scorehand(hand, starter, n, crb, do_explain)
    106  1.1.1.2  cgd 	register CARD hand[];
    107  1.1.1.2  cgd 	CARD starter;
    108  1.1.1.2  cgd 	int n;
    109  1.1.1.2  cgd 	BOOLEAN crb;		/* true if scoring crib */
    110  1.1.1.2  cgd 	BOOLEAN do_explain;	/* true if must explain this hand */
    111      1.1  cgd {
    112  1.1.1.2  cgd 	register int i, k;
    113  1.1.1.2  cgd 	register int score;
    114  1.1.1.2  cgd 	register BOOLEAN flag;
    115  1.1.1.2  cgd 	CARD h[(CINHAND + 1)];
    116  1.1.1.2  cgd 	char buf[32];
    117      1.1  cgd 
    118      1.1  cgd 	expl[0] = NULL;		/* initialize explanation */
    119      1.1  cgd 	score = 0;
    120      1.1  cgd 	flag = TRUE;
    121      1.1  cgd 	k = hand[0].suit;
    122  1.1.1.2  cgd 	for (i = 0; i < n; i++) {	/* check for flush */
    123  1.1.1.2  cgd 		flag = (flag && (hand[i].suit == k));
    124  1.1.1.2  cgd 		if (hand[i].rank == JACK)	/* check for his nibs */
    125  1.1.1.2  cgd 			if (hand[i].suit == starter.suit) {
    126  1.1.1.2  cgd 				score++;
    127  1.1.1.2  cgd 				if (do_explain)
    128  1.1.1.2  cgd 					strcat(expl, "His Nobs");
    129  1.1.1.2  cgd 			}
    130  1.1.1.2  cgd 		h[i] = hand[i];
    131      1.1  cgd 	}
    132      1.1  cgd 
    133      1.1  cgd 	if (flag && n >= CINHAND) {
    134      1.1  cgd 		if (do_explain && expl[0] != NULL)
    135  1.1.1.2  cgd 			strcat(expl, ", ");
    136  1.1.1.2  cgd 		if (starter.suit == k) {
    137  1.1.1.2  cgd 			score += 5;
    138  1.1.1.2  cgd 			if (do_explain)
    139  1.1.1.2  cgd 				strcat(expl, "Five-flush");
    140  1.1.1.2  cgd 		} else
    141  1.1.1.2  cgd 			if (!crb) {
    142  1.1.1.2  cgd 				score += 4;
    143  1.1.1.2  cgd 				if (do_explain && expl[0] != NULL)
    144  1.1.1.2  cgd 					strcat(expl, ", Four-flush");
    145  1.1.1.2  cgd 				else
    146  1.1.1.2  cgd 					strcpy(expl, "Four-flush");
    147  1.1.1.2  cgd 			}
    148      1.1  cgd 	}
    149      1.1  cgd 	if (do_explain && expl[0] != NULL)
    150  1.1.1.2  cgd 		strcat(expl, ", ");
    151      1.1  cgd 	h[n] = starter;
    152  1.1.1.2  cgd 	sorthand(h, n + 1);	/* sort by rank */
    153      1.1  cgd 	i = 2 * fifteens(h, n + 1);
    154      1.1  cgd 	score += i;
    155      1.1  cgd 	if (do_explain)
    156  1.1.1.2  cgd 		if (i > 0) {
    157  1.1.1.2  cgd 			(void) sprintf(buf, "%d points in fifteens", i);
    158  1.1.1.2  cgd 			strcat(expl, buf);
    159  1.1.1.2  cgd 		} else
    160  1.1.1.2  cgd 			strcat(expl, "No fifteens");
    161      1.1  cgd 	i = pairuns(h, n + 1);
    162      1.1  cgd 	score += i;
    163      1.1  cgd 	if (do_explain)
    164  1.1.1.2  cgd 		if (i > 0) {
    165  1.1.1.2  cgd 			(void) sprintf(buf, ", %d points in pairs, %d in runs",
    166  1.1.1.2  cgd 			    pairpoints, runpoints);
    167  1.1.1.2  cgd 			strcat(expl, buf);
    168  1.1.1.2  cgd 		} else
    169  1.1.1.2  cgd 			strcat(expl, ", No pairs/runs");
    170  1.1.1.2  cgd 	return (score);
    171      1.1  cgd }
    172      1.1  cgd 
    173      1.1  cgd /*
    174      1.1  cgd  * fifteens:
    175      1.1  cgd  *	Return number of fifteens in hand of n cards
    176      1.1  cgd  */
    177  1.1.1.2  cgd int
    178      1.1  cgd fifteens(hand, n)
    179  1.1.1.2  cgd 	register CARD hand[];
    180  1.1.1.2  cgd 	int n;
    181      1.1  cgd {
    182  1.1.1.2  cgd 	register int *sp, *np;
    183  1.1.1.2  cgd 	register int i;
    184  1.1.1.2  cgd 	register CARD *endp;
    185  1.1.1.2  cgd 	static int sums[15], nsums[15];
    186      1.1  cgd 
    187      1.1  cgd 	np = nsums;
    188      1.1  cgd 	sp = sums;
    189      1.1  cgd 	i = 16;
    190      1.1  cgd 	while (--i) {
    191  1.1.1.2  cgd 		*np++ = 0;
    192  1.1.1.2  cgd 		*sp++ = 0;
    193      1.1  cgd 	}
    194      1.1  cgd 	for (endp = &hand[n]; hand < endp; hand++) {
    195  1.1.1.2  cgd 		i = hand->rank + 1;
    196  1.1.1.2  cgd 		if (i > 10)
    197  1.1.1.2  cgd 			i = 10;
    198  1.1.1.2  cgd 		np = &nsums[i];
    199  1.1.1.2  cgd 		np[-1]++;	/* one way to make this */
    200  1.1.1.2  cgd 		sp = sums;
    201  1.1.1.2  cgd 		while (i < 15) {
    202  1.1.1.2  cgd 			*np++ += *sp++;
    203  1.1.1.2  cgd 			i++;
    204  1.1.1.2  cgd 		}
    205  1.1.1.2  cgd 		sp = sums;
    206  1.1.1.2  cgd 		np = nsums;
    207  1.1.1.2  cgd 		i = 16;
    208  1.1.1.2  cgd 		while (--i)
    209  1.1.1.2  cgd 			*sp++ = *np++;
    210      1.1  cgd 	}
    211      1.1  cgd 	return sums[14];
    212      1.1  cgd }
    213      1.1  cgd 
    214      1.1  cgd /*
    215      1.1  cgd  * pairuns returns the number of points in the n card sorted hand
    216      1.1  cgd  * due to pairs and runs
    217      1.1  cgd  * this routine only works if n is strictly less than 6
    218      1.1  cgd  * sets the globals pairpoints and runpoints appropriately
    219      1.1  cgd  */
    220  1.1.1.2  cgd int
    221  1.1.1.2  cgd pairuns(h, n)
    222  1.1.1.2  cgd 	CARD h[];
    223  1.1.1.2  cgd 	int n;
    224      1.1  cgd {
    225  1.1.1.2  cgd 	register int i;
    226  1.1.1.2  cgd 	int runlength, runmult, lastmult, curmult;
    227  1.1.1.2  cgd 	int mult1, mult2, pair1, pair2;
    228  1.1.1.2  cgd 	BOOLEAN run;
    229      1.1  cgd 
    230      1.1  cgd 	run = TRUE;
    231      1.1  cgd 	runlength = 1;
    232      1.1  cgd 	mult1 = 1;
    233      1.1  cgd 	pair1 = -1;
    234      1.1  cgd 	mult2 = 1;
    235      1.1  cgd 	pair2 = -1;
    236      1.1  cgd 	curmult = runmult = 1;
    237  1.1.1.2  cgd 	for (i = 1; i < n; i++) {
    238  1.1.1.2  cgd 		lastmult = curmult;
    239  1.1.1.2  cgd 		if (h[i].rank == h[i - 1].rank) {
    240  1.1.1.2  cgd 			if (pair1 < 0) {
    241  1.1.1.2  cgd 				pair1 = h[i].rank;
    242  1.1.1.2  cgd 				mult1 = curmult = 2;
    243  1.1.1.2  cgd 			} else {
    244  1.1.1.2  cgd 				if (h[i].rank == pair1) {
    245  1.1.1.2  cgd 					curmult = ++mult1;
    246  1.1.1.2  cgd 				} else {
    247  1.1.1.2  cgd 					if (pair2 < 0) {
    248  1.1.1.2  cgd 						pair2 = h[i].rank;
    249  1.1.1.2  cgd 						mult2 = curmult = 2;
    250  1.1.1.2  cgd 					} else {
    251  1.1.1.2  cgd 						curmult = ++mult2;
    252  1.1.1.2  cgd 					}
    253  1.1.1.2  cgd 				}
    254      1.1  cgd 			}
    255  1.1.1.2  cgd 			if (i == (n - 1) && run) {
    256  1.1.1.2  cgd 				runmult *= curmult;
    257      1.1  cgd 			}
    258  1.1.1.2  cgd 		} else {
    259  1.1.1.2  cgd 			curmult = 1;
    260  1.1.1.2  cgd 			if (h[i].rank == h[i - 1].rank + 1) {
    261  1.1.1.2  cgd 				if (run) {
    262  1.1.1.2  cgd 					++runlength;
    263  1.1.1.2  cgd 				} else {
    264  1.1.1.2  cgd 							/* only if old short */
    265  1.1.1.2  cgd 					if (runlength < 3) {
    266  1.1.1.2  cgd 						run = TRUE;
    267  1.1.1.2  cgd 						runlength = 2;
    268  1.1.1.2  cgd 						runmult = 1;
    269  1.1.1.2  cgd 					}
    270  1.1.1.2  cgd 				}
    271  1.1.1.2  cgd 				runmult *= lastmult;
    272  1.1.1.2  cgd 			} else {
    273  1.1.1.2  cgd 							/* if just ended */
    274  1.1.1.2  cgd 				if (run)
    275  1.1.1.2  cgd 					runmult *= lastmult;
    276  1.1.1.2  cgd 				run = FALSE;
    277      1.1  cgd 			}
    278      1.1  cgd 		}
    279      1.1  cgd 	}
    280  1.1.1.2  cgd 	pairpoints = ichoose2[mult1] + ichoose2[mult2];
    281  1.1.1.2  cgd 	runpoints = (runlength >= 3 ? runlength * runmult : 0);
    282  1.1.1.2  cgd 	return (pairpoints + runpoints);
    283      1.1  cgd }
    284      1.1  cgd 
    285      1.1  cgd /*
    286      1.1  cgd  * pegscore tells how many points crd would get if played after
    287      1.1  cgd  * the n cards in tbl during pegging
    288      1.1  cgd  */
    289  1.1.1.2  cgd int
    290  1.1.1.2  cgd pegscore(crd, tbl, n, sum)
    291  1.1.1.2  cgd 	CARD crd, tbl[];
    292  1.1.1.2  cgd 	int n, sum;
    293      1.1  cgd {
    294  1.1.1.2  cgd 	BOOLEAN got[RANKS];
    295  1.1.1.2  cgd 	register int i, j, scr;
    296  1.1.1.2  cgd 	int k, lo, hi;
    297  1.1.1.2  cgd 
    298  1.1.1.2  cgd 	sum += VAL(crd.rank);
    299  1.1.1.2  cgd 	if (sum > 31)
    300  1.1.1.2  cgd 		return (-1);
    301  1.1.1.2  cgd 	if (sum == 31 || sum == 15)
    302  1.1.1.2  cgd 		scr = 2;
    303  1.1.1.2  cgd 	else
    304  1.1.1.2  cgd 		scr = 0;
    305  1.1.1.2  cgd 	if (!n)
    306  1.1.1.2  cgd 		return (scr);
    307      1.1  cgd 	j = 1;
    308  1.1.1.2  cgd 	while ((crd.rank == tbl[n - j].rank) && (n - j >= 0))
    309  1.1.1.2  cgd 		++j;
    310  1.1.1.2  cgd 	if (j > 1)
    311  1.1.1.2  cgd 		return (scr + ichoose2[j]);
    312  1.1.1.2  cgd 	if (n < 2)
    313  1.1.1.2  cgd 		return (scr);
    314      1.1  cgd 	lo = hi = crd.rank;
    315  1.1.1.2  cgd 	for (i = 0; i < RANKS; i++)
    316  1.1.1.2  cgd 		got[i] = FALSE;
    317  1.1.1.2  cgd 	got[crd.rank] = TRUE;
    318      1.1  cgd 	k = -1;
    319  1.1.1.2  cgd 	for (i = n - 1; i >= 0; --i) {
    320  1.1.1.2  cgd 		if (got[tbl[i].rank])
    321  1.1.1.2  cgd 			break;
    322  1.1.1.2  cgd 		got[tbl[i].rank] = TRUE;
    323  1.1.1.2  cgd 		if (tbl[i].rank < lo)
    324  1.1.1.2  cgd 			lo = tbl[i].rank;
    325  1.1.1.2  cgd 		if (tbl[i].rank > hi)
    326  1.1.1.2  cgd 			hi = tbl[i].rank;
    327  1.1.1.2  cgd 		for (j = lo; j <= hi; j++)
    328  1.1.1.2  cgd 			if (!got[j])
    329  1.1.1.2  cgd 				break;
    330  1.1.1.2  cgd 		if (j > hi)
    331  1.1.1.2  cgd 			k = hi - lo + 1;
    332      1.1  cgd 	}
    333  1.1.1.2  cgd 	if (k >= 3)
    334  1.1.1.2  cgd 		return (scr + k);
    335  1.1.1.2  cgd 	else
    336  1.1.1.2  cgd 		return (scr);
    337      1.1  cgd }
    338      1.1  cgd 
    339      1.1  cgd /*
    340      1.1  cgd  * adjust takes a two card hand that will be put in the crib
    341      1.1  cgd  * and returns an adjusted normalized score for the number of
    342      1.1  cgd  * points such a crib will get.
    343      1.1  cgd  */
    344  1.1.1.2  cgd int
    345  1.1.1.2  cgd adjust(cb, tnv)
    346  1.1.1.2  cgd 	CARD cb[], tnv;
    347      1.1  cgd {
    348  1.1.1.2  cgd 	long scr;
    349  1.1.1.2  cgd 	int i, c0, c1;
    350      1.1  cgd 
    351      1.1  cgd 	c0 = cb[0].rank;
    352      1.1  cgd 	c1 = cb[1].rank;
    353  1.1.1.2  cgd 	if (c0 > c1) {
    354  1.1.1.2  cgd 		i = c0;
    355  1.1.1.2  cgd 		c0 = c1;
    356  1.1.1.2  cgd 		c1 = i;
    357      1.1  cgd 	}
    358  1.1.1.2  cgd 	if (cb[0].suit != cb[1].suit)
    359  1.1.1.2  cgd 		scr = crbnescr[RANKS * c0 + c1];
    360  1.1.1.2  cgd 	else
    361  1.1.1.2  cgd 		scr = crbescr[RANKS * c0 + c1];
    362  1.1.1.2  cgd 	if (scr <= 0) {
    363  1.1.1.2  cgd 		printf("\nADJUST: internal error %d %d\n", c0, c1);
    364  1.1.1.2  cgd 		exit(93);
    365      1.1  cgd 	}
    366  1.1.1.2  cgd 	return ((scr + 29400) / 58800);
    367      1.1  cgd }
    368