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