1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include "deck.h"
#include "cribbage.h"
#define NTV 10 /* number scores to test */
/* score to test reachability of, and order to test them in */
int tv[ NTV ] = { 8, 7, 9, 6, 11, 12, 13, 14, 10, 5 };
/*
* computer chooses what to play in pegging...
* only called if no playable card will score points
*/
int
cchose( CARD *h, int n, int s )
{
register int i, j, l;
if( n <= 1 ) return( 0 );
if( s < 4 ) { /* try for good value */
if( ( j = anysumto(h, n, s, 4) ) >= 0 ) return( j );
if( ( j = anysumto(h, n, s, 3) ) >= 0 && s == 0 )
return( j );
}
if( s > 0 && s < 20 ) {
for( i = 1; i <= 10; i++ ) { /* try for retaliation to 31 */
if( ( j = anysumto(h, n, s, 21-i) ) >= 0 ) {
if( ( l = numofval(h, n, i) ) > 0 ) {
if( l > 1 || VAL( h[j].rank ) != i ) return( j );
}
}
}
}
if( s < 15 ) {
for( i = 0; i < NTV; i++ ) { /* for retaliation after 15 */
if( ( j = anysumto(h, n, s, tv[i]) ) >= 0 ) {
if( ( l = numofval(h, n, 15-tv[i]) ) > 0 ) {
if( l > 1 || VAL( h[j].rank ) != 15-tv[i] ) return( j );
}
}
}
}
j = -1;
for( i = n - 1; i >= 0; --i ) { /* remember: h is sorted */
l = s + VAL( h[i].rank );
if( l > 31 ) continue;
if( l != 5 && l != 10 && l != 21 ) {
j = i;
break;
}
}
if( j >= 0 ) return( j );
for( i = n - 1; i >= 0; --i ) {
l = s + VAL( h[i].rank );
if( l > 31 ) continue;
if( j < 0 ) j = i;
if( l != 5 && l != 21 ) {
j = i;
break;
}
}
return( j );
}
/*
* plyrhand:
* Evaluate and score a player hand or crib
*/
BOOLEAN
plyrhand(CARD *hand, char *s)
{
register int i, j;
register BOOLEAN win;
static char prompt[512];
UIPrintHand (hand, CINHAND, PLAYER, FALSE);
sprintf(prompt, "Your %s scores ", s);
i = scorehand(hand, turnover, CINHAND, strcmp(s, "crib") == 0, explain);
if ((j = number(0, 29, prompt)) == 19)
j = 0;
if (i != j) {
if (i < j) {
win = chkscr(&pscore, i);
msg("It's really only %d points; I get %d", i, 2);
if (!win)
win = chkscr(&cscore, 2);
}
else {
win = chkscr(&pscore, j);
msg("You should have taken %d, not %d!", i, j);
}
if (explain)
msg("Explanation: %s", expl);
UIWait ();
}
else
win = chkscr(&pscore, i);
return win;
}
/*
* comphand:
* Handle scoring and displaying the computers hand
*/
BOOLEAN
comphand(CARD *h, char *s)
{
register int j;
j = scorehand(h, turnover, CINHAND, strcmp(s, "crib") == 0, explain);
UIPrintHand (h, CINHAND, COMPUTER, FALSE);
msg("My %s scores %d%s%s%s",
s, (j == 0 ? 19 : j),
explain ? " (" : "",
explain ? expl : "",
explain ? ")" : "");
return chkscr(&cscore, j);
}
/*
* chkscr:
* Add inc to scr and test for > glimit, printing on the scoring
* board while we're at it.
*/
int Lastscore[2] = {-1, -1};
BOOLEAN
chkscr(int *scr, int inc)
{
int who;
if (scr == &cscore)
who = COMPUTER;
else
who = PLAYER;
if (inc != 0) {
UIPrintPeg(Lastscore[who], FALSE, who);
Lastscore[who] = *scr;
*scr += inc;
UIPrintPeg(*scr, TRUE, who);
UIRefresh ();
}
return (*scr >= glimit);
}
/*
* cdiscard -- the computer figures out what is the best discard for
* the crib and puts the best two cards at the end
*/
void
cdiscard( BOOLEAN mycrib )
{
CARD d[ CARDS ], h[ FULLHAND ], cb[ 2 ];
register int i, j, k;
int nc, ns;
long sums[ 15 ];
static int undo1[15] = {0,0,0,0,0,1,1,1,1,2,2,2,3,3,4};
static int undo2[15] = {1,2,3,4,5,2,3,4,5,3,4,5,4,5,5};
makedeck( d );
nc = CARDS;
for( i = 0; i < knownum; i++ ) { /* get all other cards */
remove_card( known[i], d, nc-- );
}
for( i = 0; i < 15; i++ ) sums[i] = 0L;
ns = 0;
for( i = 0; i < (FULLHAND - 1); i++ ) {
cb[0] = chand[i];
for( j = i + 1; j < FULLHAND; j++ ) {
cb[1] = chand[j];
for( k = 0; k < FULLHAND; k++ ) h[k] = chand[k];
remove_card( chand[i], h, FULLHAND );
remove_card( chand[j], h, FULLHAND - 1 );
for( k = 0; k < nc; k++ ) {
sums[ns] += scorehand( h, d[k], CINHAND, TRUE, FALSE );
if( mycrib ) sums[ns] += adjust( cb, d[k] );
else sums[ns] -= adjust( cb, d[k] );
}
++ns;
}
}
j = 0;
for( i = 1; i < 15; i++ ) if( sums[i] > sums[j] ) j = i;
for( k = 0; k < FULLHAND; k++ ) h[k] = chand[k];
remove_card( h[ undo1[j] ], chand, FULLHAND );
remove_card( h[ undo2[j] ], chand, FULLHAND - 1 );
chand[4] = h[ undo1[j] ];
chand[5] = h[ undo2[j] ];
}
/*
* returns true if some card in hand can be played without exceeding 31
*/
BOOLEAN
anymove( CARD *hand, int n, int sum )
{
register int i, j;
if( n < 1 ) return( FALSE );
j = hand[0].rank;
for( i = 1; i < n; i++ ) {
if( hand[i].rank < j ) j = hand[i].rank;
}
return( sum + VAL( j ) <= 31 );
}
/*
* anysumto returns the index (0 <= i < n) of the card in hand that brings
* the s up to t, or -1 if there is none
*/
int
anysumto( CARD *hand, int n, int s, int t )
{
register int i;
for( i = 0; i < n; i++ ) {
if( s + VAL( hand[i].rank ) == t ) return( i );
}
return( -1 );
}
/*
* return the number of cards in h having the given rank value
*/
int
numofval( CARD *h, int n, int v )
{
register int i, j;
j = 0;
for( i = 0; i < n; i++ ) {
if( VAL( h[i].rank ) == v ) ++j;
}
return( j );
}
/*
* makeknown remembers all n cards in h for future recall
*/
void
makeknown( CARD *h, int n )
{
register int i;
for( i = 0; i < n; i++ ) {
known[ knownum++ ] = h[i];
}
}
|