summaryrefslogtreecommitdiffstats
path: root/cribbage/score.c
diff options
context:
space:
mode:
authorcgd <cgd@NetBSD.org>1995-03-21 15:03:38 +0000
committercgd <cgd@NetBSD.org>1995-03-21 15:03:38 +0000
commit5ff78ede5ea1a80c654d3f0684cd5f36d5253ae1 (patch)
tree81a19a5b24b0353e8cbac8b1f0ad82be1cf16323 /cribbage/score.c
parent45171656c266fc63f08d074d856b5ae99ac29211 (diff)
downloadbsdgames-darwin-5ff78ede5ea1a80c654d3f0684cd5f36d5253ae1.tar.gz
bsdgames-darwin-5ff78ede5ea1a80c654d3f0684cd5f36d5253ae1.tar.zst
bsdgames-darwin-5ff78ede5ea1a80c654d3f0684cd5f36d5253ae1.zip
clean up import
Diffstat (limited to 'cribbage/score.c')
-rw-r--r--cribbage/score.c400
1 files changed, 204 insertions, 196 deletions
diff --git a/cribbage/score.c b/cribbage/score.c
index d3fddd9a..93f8c075 100644
--- a/cribbage/score.c
+++ b/cribbage/score.c
@@ -1,6 +1,8 @@
-/*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+/* $NetBSD: score.c,v 1.3 1995/03/21 15:08:57 cgd Exp $ */
+
+/*-
+ * Copyright (c) 1980, 1993
+ * The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -32,14 +34,20 @@
*/
#ifndef lint
-/*static char sccsid[] = "from: @(#)score.c 5.5 (Berkeley) 6/1/90";*/
-static char rcsid[] = "$Id: score.c,v 1.2 1993/08/01 18:55:11 mycroft Exp $";
+#if 0
+static char sccsid[] = "@(#)score.c 8.1 (Berkeley) 5/31/93";
+#else
+static char rcsid[] = "$NetBSD: score.c,v 1.3 1995/03/21 15:08:57 cgd Exp $";
+#endif
#endif /* not lint */
-#include <stdio.h>
-#include "deck.h"
-#include "cribbage.h"
+#include <curses.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "deck.h"
+#include "cribbage.h"
/*
* the following arrays give the sum of the scores of the (50 2)*48 = 58800
@@ -47,8 +55,7 @@ static char rcsid[] = "$Id: score.c,v 1.2 1993/08/01 18:55:11 mycroft Exp $";
* array. the two arrays are for the case where the suits are equal and
* not equal respectively
*/
-
-long crbescr[ 169 ] = {
+long crbescr[169] = {
-10000, 271827, 278883, 332319, 347769, 261129, 250653, 253203, 248259,
243435, 256275, 237435, 231051, -10000, -10000, 412815, 295707, 349497,
267519, 262521, 259695, 254019, 250047, 262887, 244047, 237663, -10000,
@@ -67,9 +74,10 @@ long crbescr[ 169 ] = {
-10000, -10000, -10000, -10000, -10000, -10000, 304287, 262971, -10000,
-10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
-10000, -10000, 244131, -10000, -10000, -10000, -10000, -10000, -10000,
- -10000, -10000, -10000, -10000, -10000, -10000, -10000 };
+ -10000, -10000, -10000, -10000, -10000, -10000, -10000
+};
-long crbnescr[ 169 ] = {
+long crbnescr[169] = {
325272, 260772, 267828, 321264, 336714, 250074, 239598, 242148, 237204,
232380, 246348, 226380, 219996, -10000, 342528, 401760, 284652, 338442,
256464, 251466, 248640, 242964, 238992, 252960, 232992, 226608, -10000,
@@ -88,147 +96,142 @@ long crbnescr[ 169 ] = {
-10000, -10000, -10000, -10000, -10000, 348600, 294360, 253044, -10000,
-10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000, -10000,
-10000, 308664, 233076, -10000, -10000, -10000, -10000, -10000, -10000,
- -10000, -10000, -10000, -10000, -10000, -10000, 295896 };
-
-
-static int ichoose2[ 5 ] = { 0, 0, 2, 6, 12 };
-static int pairpoints, runpoints; /* globals from pairuns */
+ -10000, -10000, -10000, -10000, -10000, -10000, 295896
+};
+static int ichoose2[5] = { 0, 0, 2, 6, 12 };
+static int pairpoints, runpoints; /* Globals from pairuns. */
/*
* scorehand:
* Score the given hand of n cards and the starter card.
* n must be <= 4
*/
+int
scorehand(hand, starter, n, crb, do_explain)
-register CARD hand[];
-CARD starter;
-int n;
-BOOLEAN crb; /* true if scoring crib */
-BOOLEAN do_explain; /* true if must explain this hand */
+ register CARD hand[];
+ CARD starter;
+ int n;
+ BOOLEAN crb; /* true if scoring crib */
+ BOOLEAN do_explain; /* true if must explain this hand */
{
- CARD h[(CINHAND + 1)];
- register int i, k;
- register int score;
- register BOOLEAN flag;
- char buf[32];
+ register int i, k;
+ register int score;
+ register BOOLEAN flag;
+ CARD h[(CINHAND + 1)];
+ char buf[32];
expl[0] = NULL; /* initialize explanation */
score = 0;
flag = TRUE;
k = hand[0].suit;
- for (i = 0; i < n; i++) { /* check for flush */
- flag = (flag && (hand[i].suit == k));
- if (hand[i].rank == JACK) /* check for his nibs */
- if (hand[i].suit == starter.suit) {
- score++;
- if (do_explain)
- strcat(expl, "His Nobs");
- }
- h[i] = hand[i];
+ for (i = 0; i < n; i++) { /* check for flush */
+ flag = (flag && (hand[i].suit == k));
+ if (hand[i].rank == JACK) /* check for his nibs */
+ if (hand[i].suit == starter.suit) {
+ score++;
+ if (do_explain)
+ strcat(expl, "His Nobs");
+ }
+ h[i] = hand[i];
}
if (flag && n >= CINHAND) {
- if (do_explain && expl[0] != NULL)
- strcat(expl, ", ");
- if (starter.suit == k) {
- score += 5;
- if (do_explain)
- strcat(expl, "Five-flush");
- }
- else if (!crb) {
- score += 4;
if (do_explain && expl[0] != NULL)
- strcat(expl, ", Four-flush");
- else
- strcpy(expl, "Four-flush");
- }
+ strcat(expl, ", ");
+ if (starter.suit == k) {
+ score += 5;
+ if (do_explain)
+ strcat(expl, "Five-flush");
+ } else
+ if (!crb) {
+ score += 4;
+ if (do_explain && expl[0] != NULL)
+ strcat(expl, ", Four-flush");
+ else
+ strcpy(expl, "Four-flush");
+ }
}
-
if (do_explain && expl[0] != NULL)
- strcat(expl, ", ");
+ strcat(expl, ", ");
h[n] = starter;
- sorthand(h, n + 1); /* sort by rank */
+ sorthand(h, n + 1); /* sort by rank */
i = 2 * fifteens(h, n + 1);
score += i;
if (do_explain)
- if (i > 0) {
- (void)sprintf(buf, "%d points in fifteens", i);
- strcat(expl, buf);
- }
- else
- strcat(expl, "No fifteens");
+ if (i > 0) {
+ (void) sprintf(buf, "%d points in fifteens", i);
+ strcat(expl, buf);
+ } else
+ strcat(expl, "No fifteens");
i = pairuns(h, n + 1);
score += i;
if (do_explain)
- if (i > 0) {
- (void)sprintf(buf, ", %d points in pairs, %d in runs",
- pairpoints, runpoints);
- strcat(expl, buf);
- }
- else
- strcat(expl, ", No pairs/runs");
- return score;
+ if (i > 0) {
+ (void) sprintf(buf, ", %d points in pairs, %d in runs",
+ pairpoints, runpoints);
+ strcat(expl, buf);
+ } else
+ strcat(expl, ", No pairs/runs");
+ return (score);
}
/*
* fifteens:
* Return number of fifteens in hand of n cards
*/
+int
fifteens(hand, n)
-register CARD hand[];
-int n;
+ register CARD hand[];
+ int n;
{
- register int *sp, *np;
- register int i;
- register CARD *endp;
- static int sums[15], nsums[15];
+ register int *sp, *np;
+ register int i;
+ register CARD *endp;
+ static int sums[15], nsums[15];
np = nsums;
sp = sums;
i = 16;
while (--i) {
- *np++ = 0;
- *sp++ = 0;
+ *np++ = 0;
+ *sp++ = 0;
}
for (endp = &hand[n]; hand < endp; hand++) {
- i = hand->rank + 1;
- if (i > 10)
- i = 10;
- np = &nsums[i];
- np[-1]++; /* one way to make this */
- sp = sums;
- while (i < 15) {
- *np++ += *sp++;
- i++;
- }
- sp = sums;
- np = nsums;
- i = 16;
- while (--i)
- *sp++ = *np++;
+ i = hand->rank + 1;
+ if (i > 10)
+ i = 10;
+ np = &nsums[i];
+ np[-1]++; /* one way to make this */
+ sp = sums;
+ while (i < 15) {
+ *np++ += *sp++;
+ i++;
+ }
+ sp = sums;
+ np = nsums;
+ i = 16;
+ while (--i)
+ *sp++ = *np++;
}
return sums[14];
}
-
-
/*
* pairuns returns the number of points in the n card sorted hand
* due to pairs and runs
* this routine only works if n is strictly less than 6
* sets the globals pairpoints and runpoints appropriately
*/
-
-pairuns( h, n )
-
- CARD h[];
- int n;
+int
+pairuns(h, n)
+ CARD h[];
+ int n;
{
- register int i;
- int runlength, runmult, lastmult, curmult;
- int mult1, mult2, pair1, pair2;
- BOOLEAN run;
+ register int i;
+ int runlength, runmult, lastmult, curmult;
+ int mult1, mult2, pair1, pair2;
+ BOOLEAN run;
run = TRUE;
runlength = 1;
@@ -237,129 +240,134 @@ pairuns( h, n )
mult2 = 1;
pair2 = -1;
curmult = runmult = 1;
- for( i = 1; i < n; i++ ) {
- lastmult = curmult;
- if( h[i].rank == h[i - 1].rank ) {
- if( pair1 < 0 ) {
- pair1 = h[i].rank;
- mult1 = curmult = 2;
- }
- else {
- if( h[i].rank == pair1 ) {
- curmult = ++mult1;
- }
- else {
- if( pair2 < 0 ) {
- pair2 = h[i].rank;
- mult2 = curmult = 2;
+ for (i = 1; i < n; i++) {
+ lastmult = curmult;
+ if (h[i].rank == h[i - 1].rank) {
+ if (pair1 < 0) {
+ pair1 = h[i].rank;
+ mult1 = curmult = 2;
+ } else {
+ if (h[i].rank == pair1) {
+ curmult = ++mult1;
+ } else {
+ if (pair2 < 0) {
+ pair2 = h[i].rank;
+ mult2 = curmult = 2;
+ } else {
+ curmult = ++mult2;
+ }
+ }
}
- else {
- curmult = ++mult2;
+ if (i == (n - 1) && run) {
+ runmult *= curmult;
}
- }
- }
- if( i == (n - 1) && run ) {
- runmult *= curmult;
- }
- }
- else {
- curmult = 1;
- if( h[i].rank == h[i - 1].rank + 1 ) {
- if( run ) {
- ++runlength;
- }
- else {
- if( runlength < 3 ) { /* only if old short */
- run = TRUE;
- runlength = 2;
- runmult = 1;
+ } else {
+ curmult = 1;
+ if (h[i].rank == h[i - 1].rank + 1) {
+ if (run) {
+ ++runlength;
+ } else {
+ /* only if old short */
+ if (runlength < 3) {
+ run = TRUE;
+ runlength = 2;
+ runmult = 1;
+ }
+ }
+ runmult *= lastmult;
+ } else {
+ /* if just ended */
+ if (run)
+ runmult *= lastmult;
+ run = FALSE;
}
- }
- runmult *= lastmult;
- }
- else {
- if( run ) runmult *= lastmult; /* if just ended */
- run = FALSE;
}
- }
}
- pairpoints = ichoose2[ mult1 ] + ichoose2[ mult2 ];
- runpoints = ( runlength >= 3 ? runlength*runmult : 0 );
- return( pairpoints + runpoints );
+ pairpoints = ichoose2[mult1] + ichoose2[mult2];
+ runpoints = (runlength >= 3 ? runlength * runmult : 0);
+ return (pairpoints + runpoints);
}
-
-
/*
* pegscore tells how many points crd would get if played after
* the n cards in tbl during pegging
*/
-
-pegscore( crd, tbl, n, sum )
-
- CARD crd, tbl[];
- int n;
- int sum;
+int
+pegscore(crd, tbl, n, sum)
+ CARD crd, tbl[];
+ int n, sum;
{
- BOOLEAN got[ RANKS ];
- register int i, j, scr;
- int k, lo, hi;
-
- sum += VAL( crd.rank );
- if( sum > 31 ) return( -1 );
- if( sum == 31 || sum == 15 ) scr = 2;
- else scr = 0;
- if( !n ) return( scr );
+ BOOLEAN got[RANKS];
+ register int i, j, scr;
+ int k, lo, hi;
+
+ sum += VAL(crd.rank);
+ if (sum > 31)
+ return (-1);
+ if (sum == 31 || sum == 15)
+ scr = 2;
+ else
+ scr = 0;
+ if (!n)
+ return (scr);
j = 1;
- while( ( crd.rank == tbl[n - j].rank ) && ( n - j >= 0 ) ) ++j;
- if( j > 1 ) return( scr + ichoose2[j] );
- if( n < 2 ) return( scr );
+ while ((crd.rank == tbl[n - j].rank) && (n - j >= 0))
+ ++j;
+ if (j > 1)
+ return (scr + ichoose2[j]);
+ if (n < 2)
+ return (scr);
lo = hi = crd.rank;
- for( i = 0; i < RANKS; i++ ) got[i] = FALSE;
- got[ crd.rank ] = TRUE;
+ for (i = 0; i < RANKS; i++)
+ got[i] = FALSE;
+ got[crd.rank] = TRUE;
k = -1;
- for( i = n - 1; i >= 0; --i ) {
- if( got[ tbl[i].rank ] ) break;
- got[ tbl[i].rank ] = TRUE;
- if( tbl[i].rank < lo ) lo = tbl[i].rank;
- if( tbl[i].rank > hi ) hi = tbl[i].rank;
- for( j = lo; j <= hi; j++ ) if( !got[j] ) break;
- if( j > hi ) k = hi - lo + 1;
+ for (i = n - 1; i >= 0; --i) {
+ if (got[tbl[i].rank])
+ break;
+ got[tbl[i].rank] = TRUE;
+ if (tbl[i].rank < lo)
+ lo = tbl[i].rank;
+ if (tbl[i].rank > hi)
+ hi = tbl[i].rank;
+ for (j = lo; j <= hi; j++)
+ if (!got[j])
+ break;
+ if (j > hi)
+ k = hi - lo + 1;
}
- if( k >= 3 ) return( scr + k );
- else return( scr );
+ if (k >= 3)
+ return (scr + k);
+ else
+ return (scr);
}
-
-
/*
* adjust takes a two card hand that will be put in the crib
* and returns an adjusted normalized score for the number of
* points such a crib will get.
*/
-
-adjust( cb, tnv )
-
- CARD cb[], tnv;
+int
+adjust(cb, tnv)
+ CARD cb[], tnv;
{
- int i, c0, c1;
- long scr;
+ long scr;
+ int i, c0, c1;
c0 = cb[0].rank;
c1 = cb[1].rank;
- if( c0 > c1 ) {
- i = c0;
- c0 = c1;
- c1 = i;
+ if (c0 > c1) {
+ i = c0;
+ c0 = c1;
+ c1 = i;
}
- if( cb[0].suit != cb[1].suit ) scr = crbnescr[ RANKS*c0 + c1 ];
- else scr = crbescr[ RANKS*c0 + c1 ];
- if( scr <= 0 ) {
- printf( "\nADJUST: internal error %d %d\n", c0, c1 );
- exit( 93 );
+ if (cb[0].suit != cb[1].suit)
+ scr = crbnescr[RANKS * c0 + c1];
+ else
+ scr = crbescr[RANKS * c0 + c1];
+ if (scr <= 0) {
+ printf("\nADJUST: internal error %d %d\n", c0, c1);
+ exit(93);
}
- return( (scr + 29400)/58800 );
+ return ((scr + 29400) / 58800);
}
-
-
-