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