Home | History | Annotate | Line # | Download | only in cribbage
score.c revision 1.5
      1 /*	$NetBSD: score.c,v 1.5 1997/10/10 12:32:34 lukem 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.5 1997/10/10 12:32:34 lukem 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 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 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 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 	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 	i = pairuns(h, n + 1);
    169 	score += i;
    170 	if (do_explain)
    171 		if (i > 0) {
    172 			(void) sprintf(buf, ", %d points in pairs, %d in runs",
    173 			    pairpoints, runpoints);
    174 			strcat(expl, buf);
    175 		} else
    176 			strcat(expl, ", No pairs/runs");
    177 	return (score);
    178 }
    179 
    180 /*
    181  * fifteens:
    182  *	Return number of fifteens in hand of n cards
    183  */
    184 int
    185 fifteens(hand, n)
    186 	CARD hand[];
    187 	int n;
    188 {
    189 	int *sp, *np;
    190 	int i;
    191 	CARD *endp;
    192 	static int sums[15], nsums[15];
    193 
    194 	np = nsums;
    195 	sp = sums;
    196 	i = 16;
    197 	while (--i) {
    198 		*np++ = 0;
    199 		*sp++ = 0;
    200 	}
    201 	for (endp = &hand[n]; hand < endp; hand++) {
    202 		i = hand->rank + 1;
    203 		if (i > 10)
    204 			i = 10;
    205 		np = &nsums[i];
    206 		np[-1]++;	/* one way to make this */
    207 		sp = sums;
    208 		while (i < 15) {
    209 			*np++ += *sp++;
    210 			i++;
    211 		}
    212 		sp = sums;
    213 		np = nsums;
    214 		i = 16;
    215 		while (--i)
    216 			*sp++ = *np++;
    217 	}
    218 	return sums[14];
    219 }
    220 
    221 /*
    222  * pairuns returns the number of points in the n card sorted hand
    223  * due to pairs and runs
    224  * this routine only works if n is strictly less than 6
    225  * sets the globals pairpoints and runpoints appropriately
    226  */
    227 int
    228 pairuns(h, n)
    229 	CARD h[];
    230 	int n;
    231 {
    232 	int i;
    233 	int runlength, runmult, lastmult, curmult;
    234 	int mult1, mult2, pair1, pair2;
    235 	BOOLEAN run;
    236 
    237 	run = TRUE;
    238 	runlength = 1;
    239 	mult1 = 1;
    240 	pair1 = -1;
    241 	mult2 = 1;
    242 	pair2 = -1;
    243 	curmult = runmult = 1;
    244 	for (i = 1; i < n; i++) {
    245 		lastmult = curmult;
    246 		if (h[i].rank == h[i - 1].rank) {
    247 			if (pair1 < 0) {
    248 				pair1 = h[i].rank;
    249 				mult1 = curmult = 2;
    250 			} else {
    251 				if (h[i].rank == pair1) {
    252 					curmult = ++mult1;
    253 				} else {
    254 					if (pair2 < 0) {
    255 						pair2 = h[i].rank;
    256 						mult2 = curmult = 2;
    257 					} else {
    258 						curmult = ++mult2;
    259 					}
    260 				}
    261 			}
    262 			if (i == (n - 1) && run) {
    263 				runmult *= curmult;
    264 			}
    265 		} else {
    266 			curmult = 1;
    267 			if (h[i].rank == h[i - 1].rank + 1) {
    268 				if (run) {
    269 					++runlength;
    270 				} else {
    271 							/* only if old short */
    272 					if (runlength < 3) {
    273 						run = TRUE;
    274 						runlength = 2;
    275 						runmult = 1;
    276 					}
    277 				}
    278 				runmult *= lastmult;
    279 			} else {
    280 							/* if just ended */
    281 				if (run)
    282 					runmult *= lastmult;
    283 				run = FALSE;
    284 			}
    285 		}
    286 	}
    287 	pairpoints = ichoose2[mult1] + ichoose2[mult2];
    288 	runpoints = (runlength >= 3 ? runlength * runmult : 0);
    289 	return (pairpoints + runpoints);
    290 }
    291 
    292 /*
    293  * pegscore tells how many points crd would get if played after
    294  * the n cards in tbl during pegging
    295  */
    296 int
    297 pegscore(crd, tbl, n, sum)
    298 	CARD crd, tbl[];
    299 	int n, sum;
    300 {
    301 	BOOLEAN got[RANKS];
    302 	int i, j, scr;
    303 	int k, lo, hi;
    304 
    305 	sum += VAL(crd.rank);
    306 	if (sum > 31)
    307 		return (-1);
    308 	if (sum == 31 || sum == 15)
    309 		scr = 2;
    310 	else
    311 		scr = 0;
    312 	if (!n)
    313 		return (scr);
    314 	j = 1;
    315 	while ((crd.rank == tbl[n - j].rank) && (n - j >= 0))
    316 		++j;
    317 	if (j > 1)
    318 		return (scr + ichoose2[j]);
    319 	if (n < 2)
    320 		return (scr);
    321 	lo = hi = crd.rank;
    322 	for (i = 0; i < RANKS; i++)
    323 		got[i] = FALSE;
    324 	got[crd.rank] = TRUE;
    325 	k = -1;
    326 	for (i = n - 1; i >= 0; --i) {
    327 		if (got[tbl[i].rank])
    328 			break;
    329 		got[tbl[i].rank] = TRUE;
    330 		if (tbl[i].rank < lo)
    331 			lo = tbl[i].rank;
    332 		if (tbl[i].rank > hi)
    333 			hi = tbl[i].rank;
    334 		for (j = lo; j <= hi; j++)
    335 			if (!got[j])
    336 				break;
    337 		if (j > hi)
    338 			k = hi - lo + 1;
    339 	}
    340 	if (k >= 3)
    341 		return (scr + k);
    342 	else
    343 		return (scr);
    344 }
    345 
    346 /*
    347  * adjust takes a two card hand that will be put in the crib
    348  * and returns an adjusted normalized score for the number of
    349  * points such a crib will get.
    350  */
    351 int
    352 adjust(cb, tnv)
    353 	CARD cb[], tnv;
    354 {
    355 	long scr;
    356 	int i, c0, c1;
    357 
    358 	c0 = cb[0].rank;
    359 	c1 = cb[1].rank;
    360 	if (c0 > c1) {
    361 		i = c0;
    362 		c0 = c1;
    363 		c1 = i;
    364 	}
    365 	if (cb[0].suit != cb[1].suit)
    366 		scr = crbnescr[RANKS * c0 + c1];
    367 	else
    368 		scr = crbescr[RANKS * c0 + c1];
    369 	if (scr <= 0) {
    370 		printf("\nADJUST: internal error %d %d\n", c0, c1);
    371 		exit(93);
    372 	}
    373 	return ((scr + 29400) / 58800);
    374 }
    375