]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/score.c
Move UCB-licensed code from 4-clause to 3-clause licence.
[bsdgames-darwin.git] / cribbage / score.c
1 /* $NetBSD: score.c,v 1.10 2003/08/07 09:37:10 agc 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.10 2003/08/07 09:37:10 agc 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(hand, starter, n, crb, do_explain)
109 const CARD hand[];
110 CARD starter;
111 int n;
112 BOOLEAN crb; /* true if scoring crib */
113 BOOLEAN do_explain; /* true if must explain this hand */
114 {
115 int i, k;
116 int score;
117 BOOLEAN flag;
118 CARD h[(CINHAND + 1)];
119 char buf[32];
120
121 explan[0] = '\0'; /* initialize explanation */
122 score = 0;
123 flag = TRUE;
124 k = hand[0].suit;
125 for (i = 0; i < n; i++) { /* check for flush */
126 flag = (flag && (hand[i].suit == k));
127 if (hand[i].rank == JACK) /* check for his nibs */
128 if (hand[i].suit == starter.suit) {
129 score++;
130 if (do_explain)
131 strcat(explan, "His Nobs");
132 }
133 h[i] = hand[i];
134 }
135
136 if (flag && n >= CINHAND) {
137 if (do_explain && explan[0] != '\0')
138 strcat(explan, ", ");
139 if (starter.suit == k) {
140 score += 5;
141 if (do_explain)
142 strcat(explan, "Five-flush");
143 } else
144 if (!crb) {
145 score += 4;
146 if (do_explain && explan[0] != '\0')
147 strcat(explan, ", Four-flush");
148 else
149 strcpy(explan, "Four-flush");
150 }
151 }
152 if (do_explain && explan[0] != '\0')
153 strcat(explan, ", ");
154 h[n] = starter;
155 sorthand(h, n + 1); /* sort by rank */
156 i = 2 * fifteens(h, n + 1);
157 score += i;
158 if (do_explain) {
159 if (i > 0) {
160 (void) sprintf(buf, "%d points in fifteens", i);
161 strcat(explan, buf);
162 } else
163 strcat(explan, "No fifteens");
164 }
165 i = pairuns(h, n + 1);
166 score += i;
167 if (do_explain) {
168 if (i > 0) {
169 (void) sprintf(buf, ", %d points in pairs, %d in runs",
170 pairpoints, runpoints);
171 strcat(explan, buf);
172 } else
173 strcat(explan, ", No pairs/runs");
174 }
175 return (score);
176 }
177
178 /*
179 * fifteens:
180 * Return number of fifteens in hand of n cards
181 */
182 int
183 fifteens(hand, n)
184 const CARD hand[];
185 int n;
186 {
187 int *sp, *np;
188 int i;
189 const CARD *endp;
190 static int sums[15], nsums[15];
191
192 np = nsums;
193 sp = sums;
194 i = 16;
195 while (--i) {
196 *np++ = 0;
197 *sp++ = 0;
198 }
199 for (endp = &hand[n]; hand < endp; hand++) {
200 i = hand->rank + 1;
201 if (i > 10)
202 i = 10;
203 np = &nsums[i];
204 np[-1]++; /* one way to make this */
205 sp = sums;
206 while (i < 15) {
207 *np++ += *sp++;
208 i++;
209 }
210 sp = sums;
211 np = nsums;
212 i = 16;
213 while (--i)
214 *sp++ = *np++;
215 }
216 return sums[14];
217 }
218
219 /*
220 * pairuns returns the number of points in the n card sorted hand
221 * due to pairs and runs
222 * this routine only works if n is strictly less than 6
223 * sets the globals pairpoints and runpoints appropriately
224 */
225 int
226 pairuns(h, n)
227 const CARD h[];
228 int n;
229 {
230 int i;
231 int runlength, runmult, lastmult, curmult;
232 int mult1, mult2, pair1, pair2;
233 BOOLEAN run;
234
235 run = TRUE;
236 runlength = 1;
237 mult1 = 1;
238 pair1 = -1;
239 mult2 = 1;
240 pair2 = -1;
241 curmult = runmult = 1;
242 for (i = 1; i < n; i++) {
243 lastmult = curmult;
244 if (h[i].rank == h[i - 1].rank) {
245 if (pair1 < 0) {
246 pair1 = h[i].rank;
247 mult1 = curmult = 2;
248 } else {
249 if (h[i].rank == pair1) {
250 curmult = ++mult1;
251 } else {
252 if (pair2 < 0) {
253 pair2 = h[i].rank;
254 mult2 = curmult = 2;
255 } else {
256 curmult = ++mult2;
257 }
258 }
259 }
260 if (i == (n - 1) && run) {
261 runmult *= curmult;
262 }
263 } else {
264 curmult = 1;
265 if (h[i].rank == h[i - 1].rank + 1) {
266 if (run) {
267 ++runlength;
268 } else {
269 /* only if old short */
270 if (runlength < 3) {
271 run = TRUE;
272 runlength = 2;
273 runmult = 1;
274 }
275 }
276 runmult *= lastmult;
277 } else {
278 /* if just ended */
279 if (run)
280 runmult *= lastmult;
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 * pegscore tells how many points crd would get if played after
292 * the n cards in tbl during pegging
293 */
294 int
295 pegscore(crd, tbl, n, sum)
296 CARD crd;
297 const CARD tbl[];
298 int n, sum;
299 {
300 BOOLEAN got[RANKS];
301 int i, j, scr;
302 int k, lo, hi;
303
304 sum += VAL(crd.rank);
305 if (sum > 31)
306 return (-1);
307 if (sum == 31 || sum == 15)
308 scr = 2;
309 else
310 scr = 0;
311 if (!n)
312 return (scr);
313 j = 1;
314 while ((crd.rank == tbl[n - j].rank) && (n - j >= 0))
315 ++j;
316 if (j > 1)
317 return (scr + ichoose2[j]);
318 if (n < 2)
319 return (scr);
320 lo = hi = crd.rank;
321 for (i = 0; i < RANKS; i++)
322 got[i] = FALSE;
323 got[crd.rank] = TRUE;
324 k = -1;
325 for (i = n - 1; i >= 0; --i) {
326 if (got[tbl[i].rank])
327 break;
328 got[tbl[i].rank] = TRUE;
329 if (tbl[i].rank < lo)
330 lo = tbl[i].rank;
331 if (tbl[i].rank > hi)
332 hi = tbl[i].rank;
333 for (j = lo; j <= hi; j++)
334 if (!got[j])
335 break;
336 if (j > hi)
337 k = hi - lo + 1;
338 }
339 if (k >= 3)
340 return (scr + k);
341 else
342 return (scr);
343 }
344
345 /*
346 * adjust takes a two card hand that will be put in the crib
347 * and returns an adjusted normalized score for the number of
348 * points such a crib will get.
349 */
350 int
351 adjust(cb, tnv)
352 const CARD cb[];
353 CARD tnv __attribute__((__unused__));
354 {
355 long scr;
356 int i, c0, c1;
357
358 c0 = cb[0].rank;
359 c1 = cb[1].rank;
360 if (c0 > c1) {
361 i = c0;
362 c0 = c1;
363 c1 = i;
364 }
365 if (cb[0].suit != cb[1].suit)
366 scr = crbnescr[RANKS * c0 + c1];
367 else
368 scr = crbescr[RANKS * c0 + c1];
369 if (scr <= 0) {
370 printf("\nADJUST: internal error %d %d\n", c0, c1);
371 exit(93);
372 }
373 return ((scr + 29400) / 58800);
374 }