]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/score.c
1 /* $NetBSD: score.c,v 1.8 1999/09/08 21:45:26 jsm Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)score.c 8.1 (Berkeley) 5/31/93";
41 __RCSID("$NetBSD: score.c,v 1.8 1999/09/08 21:45:26 jsm Exp $");
54 * the following arrays give the sum of the scores of the (50 2)*48 = 58800
55 * hands obtainable for the crib given the two cards whose ranks index the
56 * array. the two arrays are for the case where the suits are equal and
57 * not equal respectively
59 const long crbescr
[169] = {
60 -10000, 271827, 278883, 332319, 347769, 261129, 250653, 253203, 248259,
61 243435, 256275, 237435, 231051, -10000, -10000, 412815, 295707, 349497,
62 267519, 262521, 259695, 254019, 250047, 262887, 244047, 237663, -10000,
63 -10000, -10000, 333987, 388629, 262017, 266787, 262971, 252729, 254475,
64 267315, 248475, 242091, -10000, -10000, -10000, -10000, 422097, 302787,
65 256437, 263751, 257883, 254271, 267111, 248271, 241887, -10000, -10000,
66 -10000, -10000, -10000, 427677, 387837, 349173, 347985, 423861, 436701,
67 417861, 411477, -10000, -10000, -10000, -10000, -10000, -10000, 336387,
68 298851, 338667, 236487, 249327, 230487, 224103, -10000, -10000, -10000,
69 -10000, -10000, -10000, -10000, 408483, 266691, 229803, 246195, 227355,
70 220971, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
71 300675, 263787, 241695, 226407, 220023, -10000, -10000, -10000, -10000,
72 -10000, -10000, -10000, -10000, -10000, 295635, 273543, 219771, 216939,
73 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
74 -10000, 306519, 252747, 211431, -10000, -10000, -10000, -10000, -10000,
75 -10000, -10000, -10000, -10000, -10000, -10000, 304287, 262971, -10000,
76 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
77 -10000, -10000, 244131, -10000, -10000, -10000, -10000, -10000, -10000,
78 -10000, -10000, -10000, -10000, -10000, -10000, -10000
81 const long crbnescr
[169] = {
82 325272, 260772, 267828, 321264, 336714, 250074, 239598, 242148, 237204,
83 232380, 246348, 226380, 219996, -10000, 342528, 401760, 284652, 338442,
84 256464, 251466, 248640, 242964, 238992, 252960, 232992, 226608, -10000,
85 -10000, 362280, 322932, 377574, 250962, 255732, 251916, 241674, 243420,
86 257388, 237420, 231036, -10000, -10000, -10000, 360768, 411042, 291732,
87 245382, 252696, 246828, 243216, 257184, 237216, 230832, -10000, -10000,
88 -10000, -10000, 528768, 416622, 376782, 338118, 336930, 412806, 426774,
89 406806, 400422, -10000, -10000, -10000, -10000, -10000, 369864, 325332,
90 287796, 327612, 225432, 239400, 219432, 213048, -10000, -10000, -10000,
91 -10000, -10000, -10000, 359160, 397428, 255636, 218748, 236268, 216300,
92 209916, -10000, -10000, -10000, -10000, -10000, -10000, -10000, 331320,
93 289620, 252732, 231768, 215352, 208968, -10000, -10000, -10000, -10000,
94 -10000, -10000, -10000, -10000, 325152, 284580, 263616, 208716, 205884,
95 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
96 321240, 296592, 241692, 200376, -10000, -10000, -10000, -10000, -10000,
97 -10000, -10000, -10000, -10000, -10000, 348600, 294360, 253044, -10000,
98 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
99 -10000, 308664, 233076, -10000, -10000, -10000, -10000, -10000, -10000,
100 -10000, -10000, -10000, -10000, -10000, -10000, 295896
103 static const int ichoose2
[5] = { 0, 0, 2, 6, 12 };
104 static int pairpoints
, runpoints
; /* Globals from pairuns. */
108 * Score the given hand of n cards and the starter card.
112 scorehand(hand
, starter
, n
, crb
, do_explain
)
116 BOOLEAN crb
; /* true if scoring crib */
117 BOOLEAN do_explain
; /* true if must explain this hand */
122 CARD h
[(CINHAND
+ 1)];
125 expl
[0] = '\0'; /* initialize explanation */
129 for (i
= 0; i
< n
; i
++) { /* check for flush */
130 flag
= (flag
&& (hand
[i
].suit
== k
));
131 if (hand
[i
].rank
== JACK
) /* check for his nibs */
132 if (hand
[i
].suit
== starter
.suit
) {
135 strcat(expl
, "His Nobs");
140 if (flag
&& n
>= CINHAND
) {
141 if (do_explain
&& expl
[0] != '\0')
143 if (starter
.suit
== k
) {
146 strcat(expl
, "Five-flush");
150 if (do_explain
&& expl
[0] != '\0')
151 strcat(expl
, ", Four-flush");
153 strcpy(expl
, "Four-flush");
156 if (do_explain
&& expl
[0] != '\0')
159 sorthand(h
, n
+ 1); /* sort by rank */
160 i
= 2 * fifteens(h
, n
+ 1);
164 (void) sprintf(buf
, "%d points in fifteens", i
);
167 strcat(expl
, "No fifteens");
169 i
= pairuns(h
, n
+ 1);
173 (void) sprintf(buf
, ", %d points in pairs, %d in runs",
174 pairpoints
, runpoints
);
177 strcat(expl
, ", No pairs/runs");
184 * Return number of fifteens in hand of n cards
194 static int sums
[15], nsums
[15];
203 for (endp
= &hand
[n
]; hand
< endp
; hand
++) {
208 np
[-1]++; /* one way to make this */
224 * pairuns returns the number of points in the n card sorted hand
225 * due to pairs and runs
226 * this routine only works if n is strictly less than 6
227 * sets the globals pairpoints and runpoints appropriately
235 int runlength
, runmult
, lastmult
, curmult
;
236 int mult1
, mult2
, pair1
, pair2
;
245 curmult
= runmult
= 1;
246 for (i
= 1; i
< n
; i
++) {
248 if (h
[i
].rank
== h
[i
- 1].rank
) {
253 if (h
[i
].rank
== pair1
) {
264 if (i
== (n
- 1) && run
) {
269 if (h
[i
].rank
== h
[i
- 1].rank
+ 1) {
273 /* only if old short */
289 pairpoints
= ichoose2
[mult1
] + ichoose2
[mult2
];
290 runpoints
= (runlength
>= 3 ? runlength
* runmult
: 0);
291 return (pairpoints
+ runpoints
);
295 * pegscore tells how many points crd would get if played after
296 * the n cards in tbl during pegging
299 pegscore(crd
, tbl
, n
, sum
)
308 sum
+= VAL(crd
.rank
);
311 if (sum
== 31 || sum
== 15)
318 while ((crd
.rank
== tbl
[n
- j
].rank
) && (n
- j
>= 0))
321 return (scr
+ ichoose2
[j
]);
325 for (i
= 0; i
< RANKS
; i
++)
327 got
[crd
.rank
] = TRUE
;
329 for (i
= n
- 1; i
>= 0; --i
) {
330 if (got
[tbl
[i
].rank
])
332 got
[tbl
[i
].rank
] = TRUE
;
333 if (tbl
[i
].rank
< lo
)
335 if (tbl
[i
].rank
> hi
)
337 for (j
= lo
; j
<= hi
; j
++)
350 * adjust takes a two card hand that will be put in the crib
351 * and returns an adjusted normalized score for the number of
352 * points such a crib will get.
357 CARD tnv
__attribute__((__unused__
));
369 if (cb
[0].suit
!= cb
[1].suit
)
370 scr
= crbnescr
[RANKS
* c0
+ c1
];
372 scr
= crbescr
[RANKS
* c0
+ c1
];
374 printf("\nADJUST: internal error %d %d\n", c0
, c1
);
377 return ((scr
+ 29400) / 58800);