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