]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/score.c
Import from 4.4BSD-Lite2
[bsdgames-darwin.git] / cribbage / score.c
1 /* $NetBSD: score.c,v 1.3 1995/03/21 15:08:57 cgd 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)score.c 8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: score.c,v 1.3 1995/03/21 15:08:57 cgd Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <curses.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "deck.h"
50 #include "cribbage.h"
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 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 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 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
111 scorehand(hand, starter, n, crb, do_explain)
112 register CARD hand[];
113 CARD starter;
114 int n;
115 BOOLEAN crb; /* true if scoring crib */
116 BOOLEAN do_explain; /* true if must explain this hand */
117 {
118 register int i, k;
119 register int score;
120 register BOOLEAN flag;
121 CARD h[(CINHAND + 1)];
122 char buf[32];
123
124 expl[0] = NULL; /* initialize explanation */
125 score = 0;
126 flag = TRUE;
127 k = hand[0].suit;
128 for (i = 0; i < n; i++) { /* check for flush */
129 flag = (flag && (hand[i].suit == k));
130 if (hand[i].rank == JACK) /* check for his nibs */
131 if (hand[i].suit == starter.suit) {
132 score++;
133 if (do_explain)
134 strcat(expl, "His Nobs");
135 }
136 h[i] = hand[i];
137 }
138
139 if (flag && n >= CINHAND) {
140 if (do_explain && expl[0] != NULL)
141 strcat(expl, ", ");
142 if (starter.suit == k) {
143 score += 5;
144 if (do_explain)
145 strcat(expl, "Five-flush");
146 } else
147 if (!crb) {
148 score += 4;
149 if (do_explain && expl[0] != NULL)
150 strcat(expl, ", Four-flush");
151 else
152 strcpy(expl, "Four-flush");
153 }
154 }
155 if (do_explain && expl[0] != NULL)
156 strcat(expl, ", ");
157 h[n] = starter;
158 sorthand(h, n + 1); /* sort by rank */
159 i = 2 * fifteens(h, n + 1);
160 score += i;
161 if (do_explain)
162 if (i > 0) {
163 (void) sprintf(buf, "%d points in fifteens", i);
164 strcat(expl, buf);
165 } else
166 strcat(expl, "No fifteens");
167 i = pairuns(h, n + 1);
168 score += i;
169 if (do_explain)
170 if (i > 0) {
171 (void) sprintf(buf, ", %d points in pairs, %d in runs",
172 pairpoints, runpoints);
173 strcat(expl, buf);
174 } else
175 strcat(expl, ", No pairs/runs");
176 return (score);
177 }
178
179 /*
180 * fifteens:
181 * Return number of fifteens in hand of n cards
182 */
183 int
184 fifteens(hand, n)
185 register CARD hand[];
186 int n;
187 {
188 register int *sp, *np;
189 register int i;
190 register CARD *endp;
191 static int sums[15], nsums[15];
192
193 np = nsums;
194 sp = sums;
195 i = 16;
196 while (--i) {
197 *np++ = 0;
198 *sp++ = 0;
199 }
200 for (endp = &hand[n]; hand < endp; hand++) {
201 i = hand->rank + 1;
202 if (i > 10)
203 i = 10;
204 np = &nsums[i];
205 np[-1]++; /* one way to make this */
206 sp = sums;
207 while (i < 15) {
208 *np++ += *sp++;
209 i++;
210 }
211 sp = sums;
212 np = nsums;
213 i = 16;
214 while (--i)
215 *sp++ = *np++;
216 }
217 return sums[14];
218 }
219
220 /*
221 * pairuns returns the number of points in the n card sorted hand
222 * due to pairs and runs
223 * this routine only works if n is strictly less than 6
224 * sets the globals pairpoints and runpoints appropriately
225 */
226 int
227 pairuns(h, n)
228 CARD h[];
229 int n;
230 {
231 register int i;
232 int runlength, runmult, lastmult, curmult;
233 int mult1, mult2, pair1, pair2;
234 BOOLEAN run;
235
236 run = TRUE;
237 runlength = 1;
238 mult1 = 1;
239 pair1 = -1;
240 mult2 = 1;
241 pair2 = -1;
242 curmult = runmult = 1;
243 for (i = 1; i < n; i++) {
244 lastmult = curmult;
245 if (h[i].rank == h[i - 1].rank) {
246 if (pair1 < 0) {
247 pair1 = h[i].rank;
248 mult1 = curmult = 2;
249 } else {
250 if (h[i].rank == pair1) {
251 curmult = ++mult1;
252 } else {
253 if (pair2 < 0) {
254 pair2 = h[i].rank;
255 mult2 = curmult = 2;
256 } else {
257 curmult = ++mult2;
258 }
259 }
260 }
261 if (i == (n - 1) && run) {
262 runmult *= curmult;
263 }
264 } else {
265 curmult = 1;
266 if (h[i].rank == h[i - 1].rank + 1) {
267 if (run) {
268 ++runlength;
269 } else {
270 /* only if old short */
271 if (runlength < 3) {
272 run = TRUE;
273 runlength = 2;
274 runmult = 1;
275 }
276 }
277 runmult *= lastmult;
278 } else {
279 /* if just ended */
280 if (run)
281 runmult *= lastmult;
282 run = FALSE;
283 }
284 }
285 }
286 pairpoints = ichoose2[mult1] + ichoose2[mult2];
287 runpoints = (runlength >= 3 ? runlength * runmult : 0);
288 return (pairpoints + runpoints);
289 }
290
291 /*
292 * pegscore tells how many points crd would get if played after
293 * the n cards in tbl during pegging
294 */
295 int
296 pegscore(crd, tbl, n, sum)
297 CARD crd, tbl[];
298 int n, sum;
299 {
300 BOOLEAN got[RANKS];
301 register 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 CARD cb[], tnv;
353 {
354 long scr;
355 int i, c0, c1;
356
357 c0 = cb[0].rank;
358 c1 = cb[1].rank;
359 if (c0 > c1) {
360 i = c0;
361 c0 = c1;
362 c1 = i;
363 }
364 if (cb[0].suit != cb[1].suit)
365 scr = crbnescr[RANKS * c0 + c1];
366 else
367 scr = crbescr[RANKS * c0 + c1];
368 if (scr <= 0) {
369 printf("\nADJUST: internal error %d %d\n", c0, c1);
370 exit(93);
371 }
372 return ((scr + 29400) / 58800);
373 }