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