]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/support.c
1 /* $NetBSD: support.c,v 1.5 1999/09/08 21:17:47 jsm Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)support.c 8.1 (Berkeley) 5/31/93";
41 __RCSID("$NetBSD: support.c,v 1.5 1999/09/08 21:17:47 jsm Exp $");
52 #define NTV 10 /* number scores to test */
54 /* score to test reachability of, and order to test them in */
55 const int tv
[NTV
] = {8, 7, 9, 6, 11, 12, 13, 14, 10, 5};
58 * computer chooses what to play in pegging...
59 * only called if no playable card will score points
70 if (s
< 4) { /* try for good value */
71 if ((j
= anysumto(h
, n
, s
, 4)) >= 0)
73 if ((j
= anysumto(h
, n
, s
, 3)) >= 0 && s
== 0)
76 if (s
> 0 && s
< 20) {
77 /* try for retaliation to 31 */
78 for (i
= 1; i
<= 10; i
++) {
79 if ((j
= anysumto(h
, n
, s
, 21 - i
)) >= 0) {
80 if ((l
= numofval(h
, n
, i
)) > 0) {
81 if (l
> 1 || VAL(h
[j
].rank
) != i
)
88 /* for retaliation after 15 */
89 for (i
= 0; i
< NTV
; i
++) {
90 if ((j
= anysumto(h
, n
, s
, tv
[i
])) >= 0) {
91 if ((l
= numofval(h
, n
, 15 - tv
[i
])) > 0) {
93 VAL(h
[j
].rank
) != 15 - tv
[i
])
100 /* remember: h is sorted */
101 for (i
= n
- 1; i
>= 0; --i
) {
102 l
= s
+ VAL(h
[i
].rank
);
105 if (l
!= 5 && l
!= 10 && l
!= 21) {
112 for (i
= n
- 1; i
>= 0; --i
) {
113 l
= s
+ VAL(h
[i
].rank
);
118 if (l
!= 5 && l
!= 21) {
128 * Evaluate and score a player hand or crib
135 static char prompt
[BUFSIZ
];
139 prhand(hand
, CINHAND
, Playwin
, FALSE
);
140 (void) sprintf(prompt
, "Your %s scores ", s
);
141 i
= scorehand(hand
, turnover
, CINHAND
, strcmp(s
, "crib") == 0, explain
);
142 if ((j
= number(0, 29, prompt
)) == 19)
146 win
= chkscr(&pscore
, i
);
147 msg("It's really only %d points; I get %d", i
, 2);
149 win
= chkscr(&cscore
, 2);
151 win
= chkscr(&pscore
, j
);
152 msg("You should have taken %d, not %d!", i
, j
);
155 msg("Explanation: %s", expl
);
158 win
= chkscr(&pscore
, i
);
164 * Handle scoring and displaying the computers hand
173 j
= scorehand(h
, turnover
, CINHAND
, strcmp(s
, "crib") == 0, FALSE
);
174 prhand(h
, CINHAND
, Compwin
, FALSE
);
175 msg("My %s scores %d", s
, (j
== 0 ? 19 : j
));
176 return (chkscr(&cscore
, j
));
181 * Add inc to scr and test for > glimit, printing on the scoring
182 * board while we're at it.
184 int Lastscore
[2] = {-1, -1};
192 myturn
= (scr
== &cscore
);
194 prpeg(Lastscore
[(int)myturn
], '.', myturn
);
195 Lastscore
[(int)myturn
] = *scr
;
197 prpeg(*scr
, PEG
, myturn
);
200 return (*scr
>= glimit
);
205 * Put out the peg character on the score board and put the
206 * score up on the board.
209 prpeg(score
, peg
, myturn
)
221 if (score
<= 0 || score
>= glimit
) {
231 x
= (score
- 1) % 30;
232 if (score
> 90 || (score
> 30 && score
<= 60)) {
240 mvprintw(SCORE_Y
+ (myturn
? 7 : 1), SCORE_X
+ 10, "%3d", score
);
244 * cdiscard -- the computer figures out what is the best discard for
245 * the crib and puts the best two cards at the end
251 CARD d
[CARDS
], h
[FULLHAND
], cb
[2];
255 static int undo1
[15] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4};
256 static int undo2
[15] = {1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5};
260 for (i
= 0; i
< knownum
; i
++) { /* get all other cards */
261 cremove(known
[i
], d
, nc
--);
263 for (i
= 0; i
< 15; i
++)
266 for (i
= 0; i
< (FULLHAND
- 1); i
++) {
268 for (j
= i
+ 1; j
< FULLHAND
; j
++) {
270 for (k
= 0; k
< FULLHAND
; k
++)
272 cremove(chand
[i
], h
, FULLHAND
);
273 cremove(chand
[j
], h
, FULLHAND
- 1);
274 for (k
= 0; k
< nc
; k
++) {
276 scorehand(h
, d
[k
], CINHAND
, TRUE
, FALSE
);
278 sums
[ns
] += adjust(cb
, d
[k
]);
280 sums
[ns
] -= adjust(cb
, d
[k
]);
286 for (i
= 1; i
< 15; i
++)
287 if (sums
[i
] > sums
[j
])
289 for (k
= 0; k
< FULLHAND
; k
++)
291 cremove(h
[undo1
[j
]], chand
, FULLHAND
);
292 cremove(h
[undo2
[j
]], chand
, FULLHAND
- 1);
293 chand
[4] = h
[undo1
[j
]];
294 chand
[5] = h
[undo2
[j
]];
298 * returns true if some card in hand can be played without exceeding 31
301 anymove(hand
, n
, sum
)
310 for (i
= 1; i
< n
; i
++) {
311 if (hand
[i
].rank
< j
)
314 return (sum
+ VAL(j
) <= 31);
318 * anysumto returns the index (0 <= i < n) of the card in hand that brings
319 * the s up to t, or -1 if there is none
322 anysumto(hand
, n
, s
, t
)
328 for (i
= 0; i
< n
; i
++) {
329 if (s
+ VAL(hand
[i
].rank
) == t
)
336 * return the number of cards in h having the given rank value
346 for (i
= 0; i
< n
; i
++) {
347 if (VAL(h
[i
].rank
) == v
)
354 * makeknown remembers all n cards in h for future recall
363 for (i
= 0; i
< n
; i
++)
364 known
[knownum
++] = h
[i
];