1 /* $OpenBSD: score.c,v 1.4 2001/08/10 23:50:22 pjanzen Exp $ */ 2 /* $NetBSD: score.c,v 1.3 1995/03/21 15:08:57 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)score.c 8.1 (Berkeley) 5/31/93"; 40 #else 41 static char rcsid[] = "$OpenBSD: score.c,v 1.4 2001/08/10 23:50:22 pjanzen Exp $"; 42 #endif 43 #endif /* not lint */ 44 45 #include <curses.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 50 #include "deck.h" 51 #include "cribbage.h" 52 53 /* 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 58 */ 59 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 79 }; 80 81 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 101 }; 102 103 static int ichoose2[5] = { 0, 0, 2, 6, 12 }; 104 static int pairpoints, runpoints; /* Globals from pairuns. */ 105 106 /* 107 * scorehand: 108 * Score the given hand of n cards and the starter card. 109 * n must be <= 4 110 */ 111 int 112 scorehand(hand, starter, n, crb, do_explain) 113 CARD hand[]; 114 CARD starter; 115 int n; 116 bool crb; /* true if scoring crib */ 117 bool do_explain; /* true if must explain this hand */ 118 { 119 int i, k; 120 int score; 121 bool flag; 122 CARD h[(CINHAND + 1)]; 123 char buf[32]; 124 125 expl[0] = '\0'; /* initialize explanation */ 126 score = 0; 127 flag = TRUE; 128 k = hand[0].suit; 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 nobs */ 132 if (hand[i].suit == starter.suit) { 133 score++; 134 if (do_explain) 135 strcat(expl, "His Nobs"); 136 } 137 h[i] = hand[i]; 138 } 139 140 if (flag && n >= CINHAND) { 141 if (do_explain && expl[0] != '\0') 142 strcat(expl, ", "); 143 if (starter.suit == k) { 144 score += 5; 145 if (do_explain) 146 strcat(expl, "Five-flush"); 147 } else 148 if (!crb) { 149 score += 4; 150 if (do_explain && expl[0] != '\0') 151 strcat(expl, ", Four-flush"); 152 else 153 strcpy(expl, "Four-flush"); 154 } 155 } 156 if (do_explain && expl[0] != '\0') 157 strcat(expl, ", "); 158 h[n] = starter; 159 sorthand(h, n + 1); /* sort by rank */ 160 i = 2 * fifteens(h, n + 1); 161 score += i; 162 if (do_explain) { 163 if (i > 0) { 164 (void) sprintf(buf, "%d points in fifteens", i); 165 strcat(expl, buf); 166 } else 167 strcat(expl, "No fifteens"); 168 } 169 i = pairuns(h, n + 1); 170 score += i; 171 if (do_explain) { 172 if (i > 0) { 173 (void) sprintf(buf, ", %d points in pairs, %d in runs", 174 pairpoints, runpoints); 175 strcat(expl, buf); 176 } else 177 strcat(expl, ", No pairs/runs"); 178 } 179 return (score); 180 } 181 182 /* 183 * fifteens: 184 * Return number of fifteens in hand of n cards 185 */ 186 int 187 fifteens(hand, n) 188 CARD hand[]; 189 int n; 190 { 191 int *sp, *np; 192 int i; 193 CARD *endp; 194 static int sums[15], nsums[15]; 195 196 np = nsums; 197 sp = sums; 198 i = 16; 199 while (--i) { 200 *np++ = 0; 201 *sp++ = 0; 202 } 203 for (endp = &hand[n]; hand < endp; hand++) { 204 i = hand->rank + 1; 205 if (i > 10) 206 i = 10; 207 np = &nsums[i]; 208 np[-1]++; /* one way to make this */ 209 sp = sums; 210 while (i < 15) { 211 *np++ += *sp++; 212 i++; 213 } 214 sp = sums; 215 np = nsums; 216 i = 16; 217 while (--i) 218 *sp++ = *np++; 219 } 220 return sums[14]; 221 } 222 223 /* 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 228 */ 229 int 230 pairuns(h, n) 231 CARD h[]; 232 int n; 233 { 234 int i; 235 int runlength, runmult, lastmult, curmult; 236 int mult1, mult2, pair1, pair2; 237 bool run; 238 239 run = TRUE; 240 runlength = 1; 241 mult1 = 1; 242 pair1 = -1; 243 mult2 = 1; 244 pair2 = -1; 245 curmult = runmult = 1; 246 for (i = 1; i < n; i++) { 247 lastmult = curmult; 248 if (h[i].rank == h[i - 1].rank) { 249 if (pair1 < 0) { 250 pair1 = h[i].rank; 251 mult1 = curmult = 2; 252 } else { 253 if (h[i].rank == pair1) { 254 curmult = ++mult1; 255 } else { 256 if (pair2 < 0) { 257 pair2 = h[i].rank; 258 mult2 = curmult = 2; 259 } else { 260 curmult = ++mult2; 261 } 262 } 263 } 264 if (i == (n - 1) && run) { 265 runmult *= curmult; 266 } 267 } else { 268 curmult = 1; 269 if (h[i].rank == h[i - 1].rank + 1) { 270 if (run) { 271 ++runlength; 272 } else { 273 /* only if old short */ 274 if (runlength < 3) { 275 run = TRUE; 276 runlength = 2; 277 runmult = 1; 278 } 279 } 280 runmult *= lastmult; 281 } else { 282 /* if just ended */ 283 if (run) 284 runmult *= lastmult; 285 run = FALSE; 286 } 287 } 288 } 289 pairpoints = ichoose2[mult1] + ichoose2[mult2]; 290 runpoints = (runlength >= 3 ? runlength * runmult : 0); 291 return (pairpoints + runpoints); 292 } 293 294 /* 295 * pegscore tells how many points crd would get if played after 296 * the n cards in tbl during pegging 297 */ 298 int 299 pegscore(crd, tbl, n, sum) 300 CARD crd, tbl[]; 301 int n, sum; 302 { 303 bool got[RANKS]; 304 int i, j, scr; 305 int k, lo, hi; 306 307 sum += VAL(crd.rank); 308 if (sum > 31) 309 return (-1); 310 if (sum == 31 || sum == 15) 311 scr = 2; 312 else 313 scr = 0; 314 if (!n) 315 return (scr); 316 j = 1; 317 while ((crd.rank == tbl[n - j].rank) && (n - j >= 0)) 318 ++j; 319 if (j > 1) 320 return (scr + ichoose2[j]); 321 if (n < 2) 322 return (scr); 323 lo = hi = crd.rank; 324 for (i = 0; i < RANKS; i++) 325 got[i] = FALSE; 326 got[crd.rank] = TRUE; 327 k = -1; 328 for (i = n - 1; i >= 0; --i) { 329 if (got[tbl[i].rank]) 330 break; 331 got[tbl[i].rank] = TRUE; 332 if (tbl[i].rank < lo) 333 lo = tbl[i].rank; 334 if (tbl[i].rank > hi) 335 hi = tbl[i].rank; 336 for (j = lo; j <= hi; j++) 337 if (!got[j]) 338 break; 339 if (j > hi) 340 k = hi - lo + 1; 341 } 342 if (k >= 3) 343 return (scr + k); 344 else 345 return (scr); 346 } 347 348 /* 349 * adjust takes a two card hand that will be put in the crib 350 * and returns an adjusted normalized score for the number of 351 * points such a crib will get. 352 */ 353 int 354 adjust(cb, tnv) 355 CARD cb[], tnv; 356 { 357 long scr; 358 int i, c0, c1; 359 360 c0 = cb[0].rank; 361 c1 = cb[1].rank; 362 if (c0 > c1) { 363 i = c0; 364 c0 = c1; 365 c1 = i; 366 } 367 if (cb[0].suit != cb[1].suit) 368 scr = crbnescr[RANKS * c0 + c1]; 369 else 370 scr = crbescr[RANKS * c0 + c1]; 371 if (scr <= 0) { 372 printf("\nADJUST: internal error %d %d\n", c0, c1); 373 exit(93); 374 } 375 return ((scr + 29400) / 58800); 376 } 377