1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * 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 34 #ifndef lint 35 /*static char sccsid[] = "from: @(#)score.c 5.5 (Berkeley) 6/1/90";*/ 36 static char rcsid[] = "$Id: score.c,v 1.2 1993/08/01 18:55:11 mycroft Exp $"; 37 #endif /* not lint */ 38 39 #include <stdio.h> 40 #include "deck.h" 41 #include "cribbage.h" 42 43 44 /* 45 * the following arrays give the sum of the scores of the (50 2)*48 = 58800 46 * hands obtainable for the crib given the two cards whose ranks index the 47 * array. the two arrays are for the case where the suits are equal and 48 * not equal respectively 49 */ 50 51 long crbescr[ 169 ] = { 52 -10000, 271827, 278883, 332319, 347769, 261129, 250653, 253203, 248259, 53 243435, 256275, 237435, 231051, -10000, -10000, 412815, 295707, 349497, 54 267519, 262521, 259695, 254019, 250047, 262887, 244047, 237663, -10000, 55 -10000, -10000, 333987, 388629, 262017, 266787, 262971, 252729, 254475, 56 267315, 248475, 242091, -10000, -10000, -10000, -10000, 422097, 302787, 57 256437, 263751, 257883, 254271, 267111, 248271, 241887, -10000, -10000, 58 -10000, -10000, -10000, 427677, 387837, 349173, 347985, 423861, 436701, 59 417861, 411477, -10000, -10000, -10000, -10000, -10000, -10000, 336387, 60 298851, 338667, 236487, 249327, 230487, 224103, -10000, -10000, -10000, 61 -10000, -10000, -10000, -10000, 408483, 266691, 229803, 246195, 227355, 62 220971, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, 63 300675, 263787, 241695, 226407, 220023, -10000, -10000, -10000, -10000, 64 -10000, -10000, -10000, -10000, -10000, 295635, 273543, 219771, 216939, 65 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, 66 -10000, 306519, 252747, 211431, -10000, -10000, -10000, -10000, -10000, 67 -10000, -10000, -10000, -10000, -10000, -10000, 304287, 262971, -10000, 68 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, 69 -10000, -10000, 244131, -10000, -10000, -10000, -10000, -10000, -10000, 70 -10000, -10000, -10000, -10000, -10000, -10000, -10000 }; 71 72 long crbnescr[ 169 ] = { 73 325272, 260772, 267828, 321264, 336714, 250074, 239598, 242148, 237204, 74 232380, 246348, 226380, 219996, -10000, 342528, 401760, 284652, 338442, 75 256464, 251466, 248640, 242964, 238992, 252960, 232992, 226608, -10000, 76 -10000, 362280, 322932, 377574, 250962, 255732, 251916, 241674, 243420, 77 257388, 237420, 231036, -10000, -10000, -10000, 360768, 411042, 291732, 78 245382, 252696, 246828, 243216, 257184, 237216, 230832, -10000, -10000, 79 -10000, -10000, 528768, 416622, 376782, 338118, 336930, 412806, 426774, 80 406806, 400422, -10000, -10000, -10000, -10000, -10000, 369864, 325332, 81 287796, 327612, 225432, 239400, 219432, 213048, -10000, -10000, -10000, 82 -10000, -10000, -10000, 359160, 397428, 255636, 218748, 236268, 216300, 83 209916, -10000, -10000, -10000, -10000, -10000, -10000, -10000, 331320, 84 289620, 252732, 231768, 215352, 208968, -10000, -10000, -10000, -10000, 85 -10000, -10000, -10000, -10000, 325152, 284580, 263616, 208716, 205884, 86 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, 87 321240, 296592, 241692, 200376, -10000, -10000, -10000, -10000, -10000, 88 -10000, -10000, -10000, -10000, -10000, 348600, 294360, 253044, -10000, 89 -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, 90 -10000, 308664, 233076, -10000, -10000, -10000, -10000, -10000, -10000, 91 -10000, -10000, -10000, -10000, -10000, -10000, 295896 }; 92 93 94 static int ichoose2[ 5 ] = { 0, 0, 2, 6, 12 }; 95 static int pairpoints, runpoints; /* globals from pairuns */ 96 97 98 /* 99 * scorehand: 100 * Score the given hand of n cards and the starter card. 101 * n must be <= 4 102 */ 103 scorehand(hand, starter, n, crb, do_explain) 104 register CARD hand[]; 105 CARD starter; 106 int n; 107 BOOLEAN crb; /* true if scoring crib */ 108 BOOLEAN do_explain; /* true if must explain this hand */ 109 { 110 CARD h[(CINHAND + 1)]; 111 register int i, k; 112 register int score; 113 register BOOLEAN flag; 114 char buf[32]; 115 116 expl[0] = NULL; /* initialize explanation */ 117 score = 0; 118 flag = TRUE; 119 k = hand[0].suit; 120 for (i = 0; i < n; i++) { /* check for flush */ 121 flag = (flag && (hand[i].suit == k)); 122 if (hand[i].rank == JACK) /* check for his nibs */ 123 if (hand[i].suit == starter.suit) { 124 score++; 125 if (do_explain) 126 strcat(expl, "His Nobs"); 127 } 128 h[i] = hand[i]; 129 } 130 131 if (flag && n >= CINHAND) { 132 if (do_explain && expl[0] != NULL) 133 strcat(expl, ", "); 134 if (starter.suit == k) { 135 score += 5; 136 if (do_explain) 137 strcat(expl, "Five-flush"); 138 } 139 else if (!crb) { 140 score += 4; 141 if (do_explain && expl[0] != NULL) 142 strcat(expl, ", Four-flush"); 143 else 144 strcpy(expl, "Four-flush"); 145 } 146 } 147 148 if (do_explain && expl[0] != NULL) 149 strcat(expl, ", "); 150 h[n] = starter; 151 sorthand(h, n + 1); /* sort by rank */ 152 i = 2 * fifteens(h, n + 1); 153 score += i; 154 if (do_explain) 155 if (i > 0) { 156 (void)sprintf(buf, "%d points in fifteens", i); 157 strcat(expl, buf); 158 } 159 else 160 strcat(expl, "No fifteens"); 161 i = pairuns(h, n + 1); 162 score += i; 163 if (do_explain) 164 if (i > 0) { 165 (void)sprintf(buf, ", %d points in pairs, %d in runs", 166 pairpoints, runpoints); 167 strcat(expl, buf); 168 } 169 else 170 strcat(expl, ", No pairs/runs"); 171 return score; 172 } 173 174 /* 175 * fifteens: 176 * Return number of fifteens in hand of n cards 177 */ 178 fifteens(hand, n) 179 register CARD hand[]; 180 int n; 181 { 182 register int *sp, *np; 183 register int i; 184 register CARD *endp; 185 static int sums[15], nsums[15]; 186 187 np = nsums; 188 sp = sums; 189 i = 16; 190 while (--i) { 191 *np++ = 0; 192 *sp++ = 0; 193 } 194 for (endp = &hand[n]; hand < endp; hand++) { 195 i = hand->rank + 1; 196 if (i > 10) 197 i = 10; 198 np = &nsums[i]; 199 np[-1]++; /* one way to make this */ 200 sp = sums; 201 while (i < 15) { 202 *np++ += *sp++; 203 i++; 204 } 205 sp = sums; 206 np = nsums; 207 i = 16; 208 while (--i) 209 *sp++ = *np++; 210 } 211 return sums[14]; 212 } 213 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 223 pairuns( h, n ) 224 225 CARD h[]; 226 int n; 227 { 228 register int i; 229 int runlength, runmult, lastmult, curmult; 230 int mult1, mult2, pair1, pair2; 231 BOOLEAN run; 232 233 run = TRUE; 234 runlength = 1; 235 mult1 = 1; 236 pair1 = -1; 237 mult2 = 1; 238 pair2 = -1; 239 curmult = runmult = 1; 240 for( i = 1; i < n; i++ ) { 241 lastmult = curmult; 242 if( h[i].rank == h[i - 1].rank ) { 243 if( pair1 < 0 ) { 244 pair1 = h[i].rank; 245 mult1 = curmult = 2; 246 } 247 else { 248 if( h[i].rank == pair1 ) { 249 curmult = ++mult1; 250 } 251 else { 252 if( pair2 < 0 ) { 253 pair2 = h[i].rank; 254 mult2 = curmult = 2; 255 } 256 else { 257 curmult = ++mult2; 258 } 259 } 260 } 261 if( i == (n - 1) && run ) { 262 runmult *= curmult; 263 } 264 } 265 else { 266 curmult = 1; 267 if( h[i].rank == h[i - 1].rank + 1 ) { 268 if( run ) { 269 ++runlength; 270 } 271 else { 272 if( runlength < 3 ) { /* only if old short */ 273 run = TRUE; 274 runlength = 2; 275 runmult = 1; 276 } 277 } 278 runmult *= lastmult; 279 } 280 else { 281 if( run ) runmult *= lastmult; /* if just ended */ 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 293 /* 294 * pegscore tells how many points crd would get if played after 295 * the n cards in tbl during pegging 296 */ 297 298 pegscore( crd, tbl, n, sum ) 299 300 CARD crd, tbl[]; 301 int n; 302 int sum; 303 { 304 BOOLEAN got[ RANKS ]; 305 register int i, j, scr; 306 int k, lo, hi; 307 308 sum += VAL( crd.rank ); 309 if( sum > 31 ) return( -1 ); 310 if( sum == 31 || sum == 15 ) scr = 2; 311 else scr = 0; 312 if( !n ) return( scr ); 313 j = 1; 314 while( ( crd.rank == tbl[n - j].rank ) && ( n - j >= 0 ) ) ++j; 315 if( j > 1 ) return( scr + ichoose2[j] ); 316 if( n < 2 ) return( scr ); 317 lo = hi = crd.rank; 318 for( i = 0; i < RANKS; i++ ) 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 ] ) break; 323 got[ tbl[i].rank ] = TRUE; 324 if( tbl[i].rank < lo ) lo = tbl[i].rank; 325 if( tbl[i].rank > hi ) hi = tbl[i].rank; 326 for( j = lo; j <= hi; j++ ) if( !got[j] ) break; 327 if( j > hi ) k = hi - lo + 1; 328 } 329 if( k >= 3 ) return( scr + k ); 330 else return( scr ); 331 } 332 333 334 335 /* 336 * adjust takes a two card hand that will be put in the crib 337 * and returns an adjusted normalized score for the number of 338 * points such a crib will get. 339 */ 340 341 adjust( cb, tnv ) 342 343 CARD cb[], tnv; 344 { 345 int i, c0, c1; 346 long scr; 347 348 c0 = cb[0].rank; 349 c1 = cb[1].rank; 350 if( c0 > c1 ) { 351 i = c0; 352 c0 = c1; 353 c1 = i; 354 } 355 if( cb[0].suit != cb[1].suit ) scr = crbnescr[ RANKS*c0 + c1 ]; 356 else scr = crbescr[ RANKS*c0 + c1 ]; 357 if( scr <= 0 ) { 358 printf( "\nADJUST: internal error %d %d\n", c0, c1 ); 359 exit( 93 ); 360 } 361 return( (scr + 29400)/58800 ); 362 } 363 364 365 366