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