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