score.c revision 1.14 1 /* $NetBSD: score.c,v 1.14 2009/06/04 04:48:04 dholland 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.14 2009/06/04 04:48:04 dholland 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 /*
50 * the following arrays give the sum of the scores of the (50 2)*48 = 58800
51 * hands obtainable for the crib given the two cards whose ranks index the
52 * array. the two arrays are for the case where the suits are equal and
53 * not equal respectively
54 */
55 const long crbescr[169] = {
56 -10000, 271827, 278883, 332319, 347769, 261129, 250653, 253203, 248259,
57 243435, 256275, 237435, 231051, -10000, -10000, 412815, 295707, 349497,
58 267519, 262521, 259695, 254019, 250047, 262887, 244047, 237663, -10000,
59 -10000, -10000, 333987, 388629, 262017, 266787, 262971, 252729, 254475,
60 267315, 248475, 242091, -10000, -10000, -10000, -10000, 422097, 302787,
61 256437, 263751, 257883, 254271, 267111, 248271, 241887, -10000, -10000,
62 -10000, -10000, -10000, 427677, 387837, 349173, 347985, 423861, 436701,
63 417861, 411477, -10000, -10000, -10000, -10000, -10000, -10000, 336387,
64 298851, 338667, 236487, 249327, 230487, 224103, -10000, -10000, -10000,
65 -10000, -10000, -10000, -10000, 408483, 266691, 229803, 246195, 227355,
66 220971, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
67 300675, 263787, 241695, 226407, 220023, -10000, -10000, -10000, -10000,
68 -10000, -10000, -10000, -10000, -10000, 295635, 273543, 219771, 216939,
69 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
70 -10000, 306519, 252747, 211431, -10000, -10000, -10000, -10000, -10000,
71 -10000, -10000, -10000, -10000, -10000, -10000, 304287, 262971, -10000,
72 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
73 -10000, -10000, 244131, -10000, -10000, -10000, -10000, -10000, -10000,
74 -10000, -10000, -10000, -10000, -10000, -10000, -10000
75 };
76
77 const long crbnescr[169] = {
78 325272, 260772, 267828, 321264, 336714, 250074, 239598, 242148, 237204,
79 232380, 246348, 226380, 219996, -10000, 342528, 401760, 284652, 338442,
80 256464, 251466, 248640, 242964, 238992, 252960, 232992, 226608, -10000,
81 -10000, 362280, 322932, 377574, 250962, 255732, 251916, 241674, 243420,
82 257388, 237420, 231036, -10000, -10000, -10000, 360768, 411042, 291732,
83 245382, 252696, 246828, 243216, 257184, 237216, 230832, -10000, -10000,
84 -10000, -10000, 528768, 416622, 376782, 338118, 336930, 412806, 426774,
85 406806, 400422, -10000, -10000, -10000, -10000, -10000, 369864, 325332,
86 287796, 327612, 225432, 239400, 219432, 213048, -10000, -10000, -10000,
87 -10000, -10000, -10000, 359160, 397428, 255636, 218748, 236268, 216300,
88 209916, -10000, -10000, -10000, -10000, -10000, -10000, -10000, 331320,
89 289620, 252732, 231768, 215352, 208968, -10000, -10000, -10000, -10000,
90 -10000, -10000, -10000, -10000, 325152, 284580, 263616, 208716, 205884,
91 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
92 321240, 296592, 241692, 200376, -10000, -10000, -10000, -10000, -10000,
93 -10000, -10000, -10000, -10000, -10000, 348600, 294360, 253044, -10000,
94 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
95 -10000, 308664, 233076, -10000, -10000, -10000, -10000, -10000, -10000,
96 -10000, -10000, -10000, -10000, -10000, -10000, 295896
97 };
98
99 static const int ichoose2[5] = { 0, 0, 2, 6, 12 };
100 static int pairpoints, runpoints; /* Globals from pairuns. */
101
102 /*
103 * scorehand:
104 * Score the given hand of n cards and the starter card.
105 * n must be <= 4
106 */
107 int
108 scorehand(const CARD hand[], CARD starter, int n, BOOLEAN crb,
109 BOOLEAN do_explain)
110 {
111 int i, k;
112 int hscore;
113 BOOLEAN flag;
114 CARD h[(CINHAND + 1)];
115 char buf[32];
116
117 explan[0] = '\0'; /* initialize explanation */
118 hscore = 0;
119 flag = TRUE;
120 k = hand[0].suit;
121 for (i = 0; i < n; i++) { /* check for flush */
122 flag = (flag && (hand[i].suit == k));
123 if (hand[i].rank == JACK) /* check for his nibs */
124 if (hand[i].suit == starter.suit) {
125 hscore++;
126 if (do_explain)
127 strcat(explan, "His Nobs");
128 }
129 h[i] = hand[i];
130 }
131
132 if (flag && n >= CINHAND) {
133 if (do_explain && explan[0] != '\0')
134 strcat(explan, ", ");
135 if (starter.suit == k) {
136 hscore += 5;
137 if (do_explain)
138 strcat(explan, "Five-flush");
139 } else
140 if (!crb) {
141 hscore += 4;
142 if (do_explain && explan[0] != '\0')
143 strcat(explan, ", Four-flush");
144 else
145 strcpy(explan, "Four-flush");
146 }
147 }
148 if (do_explain && explan[0] != '\0')
149 strcat(explan, ", ");
150 h[n] = starter;
151 sorthand(h, n + 1); /* sort by rank */
152 i = 2 * fifteens(h, n + 1);
153 hscore += i;
154 if (do_explain) {
155 if (i > 0) {
156 (void) snprintf(buf, sizeof(buf),
157 "%d points in fifteens", i);
158 strcat(explan, buf);
159 } else
160 strcat(explan, "No fifteens");
161 }
162 i = pairuns(h, n + 1);
163 hscore += i;
164 if (do_explain) {
165 if (i > 0) {
166 (void) snprintf(buf, sizeof(buf),
167 ", %d points in pairs, %d in runs",
168 pairpoints, runpoints);
169 strcat(explan, buf);
170 } else
171 strcat(explan, ", No pairs/runs");
172 }
173 return (hscore);
174 }
175
176 /*
177 * fifteens:
178 * Return number of fifteens in hand of n cards
179 */
180 int
181 fifteens(const CARD hand[], int n)
182 {
183 int *sp, *np;
184 int i;
185 const CARD *endp;
186 static int sums[15], nsums[15];
187
188 np = nsums;
189 sp = sums;
190 i = 16;
191 while (--i) {
192 *np++ = 0;
193 *sp++ = 0;
194 }
195 for (endp = &hand[n]; hand < endp; hand++) {
196 i = hand->rank + 1;
197 if (i > 10)
198 i = 10;
199 np = &nsums[i];
200 np[-1]++; /* one way to make this */
201 sp = sums;
202 while (i < 15) {
203 *np++ += *sp++;
204 i++;
205 }
206 sp = sums;
207 np = nsums;
208 i = 16;
209 while (--i)
210 *sp++ = *np++;
211 }
212 return sums[14];
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 int
222 pairuns(const CARD h[], int n)
223 {
224 int i;
225 int runlength, runmult, lastmult, curmult;
226 int mult1, mult2, pair1, pair2;
227 BOOLEAN run;
228
229 run = TRUE;
230 runlength = 1;
231 mult1 = 1;
232 pair1 = -1;
233 mult2 = 1;
234 pair2 = -1;
235 curmult = runmult = 1;
236 for (i = 1; i < n; i++) {
237 lastmult = curmult;
238 if (h[i].rank == h[i - 1].rank) {
239 if (pair1 < 0) {
240 pair1 = h[i].rank;
241 mult1 = curmult = 2;
242 } else {
243 if (h[i].rank == pair1) {
244 curmult = ++mult1;
245 } else {
246 if (pair2 < 0) {
247 pair2 = h[i].rank;
248 mult2 = curmult = 2;
249 } else {
250 curmult = ++mult2;
251 }
252 }
253 }
254 if (i == (n - 1) && run) {
255 runmult *= curmult;
256 }
257 } else {
258 curmult = 1;
259 if (h[i].rank == h[i - 1].rank + 1) {
260 if (run) {
261 ++runlength;
262 } else {
263 /* only if old short */
264 if (runlength < 3) {
265 run = TRUE;
266 runlength = 2;
267 runmult = 1;
268 }
269 }
270 runmult *= lastmult;
271 } else {
272 /* if just ended */
273 if (run)
274 runmult *= lastmult;
275 run = FALSE;
276 }
277 }
278 }
279 pairpoints = ichoose2[mult1] + ichoose2[mult2];
280 runpoints = (runlength >= 3 ? runlength * runmult : 0);
281 return (pairpoints + runpoints);
282 }
283
284 /*
285 * pegscore tells how many points crd would get if played after
286 * the n cards in tbl during pegging
287 */
288 int
289 pegscore(CARD crd, const CARD tbl[], int n, int sum)
290 {
291 BOOLEAN got[RANKS];
292 int i, j, scr;
293 int k, lo, hi;
294
295 sum += VAL(crd.rank);
296 if (sum > 31)
297 return (-1);
298 if (sum == 31 || sum == 15)
299 scr = 2;
300 else
301 scr = 0;
302 if (!n)
303 return (scr);
304 j = 1;
305 while ((crd.rank == tbl[n - j].rank) && (n - j >= 0))
306 ++j;
307 if (j > 1)
308 return (scr + ichoose2[j]);
309 if (n < 2)
310 return (scr);
311 lo = hi = crd.rank;
312 for (i = 0; i < RANKS; i++)
313 got[i] = FALSE;
314 got[crd.rank] = TRUE;
315 k = -1;
316 for (i = n - 1; i >= 0; --i) {
317 if (got[tbl[i].rank])
318 break;
319 got[tbl[i].rank] = TRUE;
320 if (tbl[i].rank < lo)
321 lo = tbl[i].rank;
322 if (tbl[i].rank > hi)
323 hi = tbl[i].rank;
324 for (j = lo; j <= hi; j++)
325 if (!got[j])
326 break;
327 if (j > hi)
328 k = hi - lo + 1;
329 }
330 if (k >= 3)
331 return (scr + k);
332 else
333 return (scr);
334 }
335
336 /*
337 * adjust takes a two card hand that will be put in the crib
338 * and returns an adjusted normalized score for the number of
339 * points such a crib will get.
340 */
341 int
342 adjust(const CARD cb[], CARD tnv __unused)
343 {
344 long scr;
345 int i, c0, c1;
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)
355 scr = crbnescr[RANKS * c0 + c1];
356 else
357 scr = crbescr[RANKS * c0 + c1];
358 if (scr <= 0) {
359 printf("\nADJUST: internal error %d %d\n", c0, c1);
360 exit(93);
361 }
362 return ((scr + 29400) / 58800);
363 }
364