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