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