* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char sccsid
[] = "@(#)support.c 5.1 (Berkeley) %G%";
#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
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 )
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
);
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
);
for( i
= n
- 1; i
>= 0; --i
) { /* remember: h is sorted */
l
= s
+ VAL( h
[i
].rank
);
if( l
!= 5 && l
!= 10 && l
!= 21 ) {
if( j
>= 0 ) return( j
);
for( i
= n
- 1; i
>= 0; --i
) {
l
= s
+ VAL( h
[i
].rank
);
if( l
!= 5 && l
!= 21 ) {
* Evaluate and score a player hand or crib
static char prompt
[BUFSIZ
];
prhand(hand
, CINHAND
, Playwin
, FALSE
);
sprintf(prompt
, "Your %s scores ", s
);
i
= scorehand(hand
, turnover
, CINHAND
, strcmp(s
, "crib") == 0, explain
);
if ((j
= number(0, 29, prompt
)) == 19)
win
= chkscr(&pscore
, i
);
msg("It's really only %d points; I get %d", i
, 2);
win
= chkscr(&cscore
, 2);
win
= chkscr(&pscore
, j
);
msg("You should have taken %d, not %d!", i
, j
);
msg("Explanation: %s", expl
);
win
= chkscr(&pscore
, i
);
* Handle scoring and displaying the computers hand
j
= scorehand(h
, turnover
, CINHAND
, strcmp(s
, "crib") == 0, FALSE
);
prhand(h
, CINHAND
, Compwin
, FALSE
);
msg("My %s scores %d", s
, (j
== 0 ? 19 : j
));
return chkscr(&cscore
, j
);
* Add inc to scr and test for > glimit, printing on the scoring
* board while we're at it.
int Lastscore
[2] = {-1, -1};
myturn
= (scr
== &cscore
);
prpeg(Lastscore
[myturn
], '.', myturn
);
Lastscore
[myturn
] = *scr
;
prpeg(*scr
, PEG
, myturn
);
* Put out the peg character on the score board and put the
prpeg(score
, peg
, myturn
)
if (score
<= 0 || score
>= glimit
) {
if (score
> 90 || (score
> 30 && score
<= 60)) {
mvprintw(SCORE_Y
+ (myturn
? 7 : 1), SCORE_X
+ 10, "%3d", score
);
* cdiscard -- the computer figures out what is the best discard for
* the crib and puts the best two cards at the end
CARD d
[ CARDS
], h
[ FULLHAND
], cb
[ 2 ];
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};
for( i
= 0; i
< knownum
; i
++ ) { /* get all other cards */
remove( known
[i
], d
, nc
-- );
for( i
= 0; i
< 15; i
++ ) sums
[i
] = 0L;
for( i
= 0; i
< (FULLHAND
- 1); i
++ ) {
for( j
= i
+ 1; j
< FULLHAND
; j
++ ) {
for( k
= 0; k
< FULLHAND
; k
++ ) h
[k
] = chand
[k
];
remove( chand
[i
], h
, FULLHAND
);
remove( 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
] );
for( i
= 1; i
< 15; i
++ ) if( sums
[i
] > sums
[j
] ) j
= i
;
for( k
= 0; k
< FULLHAND
; k
++ ) h
[k
] = chand
[k
];
remove( h
[ undo1
[j
] ], chand
, FULLHAND
);
remove( 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
if( n
< 1 ) return( FALSE
);
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
anysumto( hand
, n
, s
, t
)
for( i
= 0; i
< n
; i
++ ) {
if( s
+ VAL( hand
[i
].rank
) == t
) return( i
);
* return the number of cards in h having the given rank value
for( i
= 0; i
< n
; i
++ ) {
if( VAL( h
[i
].rank
) == v
) ++j
;
* makeknown remembers all n cards in h for future recall
for( i
= 0; i
< n
; i
++ ) {
known
[ knownum
++ ] = h
[i
];