]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/support.c
clean up import, fix up Ids
[bsdgames-darwin.git] / cribbage / support.c
1 /* $NetBSD: support.c,v 1.3 1995/03/21 15:08:59 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)support.c 8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: support.c,v 1.3 1995/03/21 15:08:59 cgd Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <curses.h>
45 #include <string.h>
46
47 #include "deck.h"
48 #include "cribbage.h"
49 #include "cribcur.h"
50
51 #define NTV 10 /* number scores to test */
52
53 /* score to test reachability of, and order to test them in */
54 int tv[NTV] = {8, 7, 9, 6, 11, 12, 13, 14, 10, 5};
55
56 /*
57 * computer chooses what to play in pegging...
58 * only called if no playable card will score points
59 */
60 int
61 cchose(h, n, s)
62 CARD h[];
63 int n, s;
64 {
65 register int i, j, l;
66
67 if (n <= 1)
68 return (0);
69 if (s < 4) { /* try for good value */
70 if ((j = anysumto(h, n, s, 4)) >= 0)
71 return (j);
72 if ((j = anysumto(h, n, s, 3)) >= 0 && s == 0)
73 return (j);
74 }
75 if (s > 0 && s < 20) {
76 /* try for retaliation to 31 */
77 for (i = 1; i <= 10; i++) {
78 if ((j = anysumto(h, n, s, 21 - i)) >= 0) {
79 if ((l = numofval(h, n, i)) > 0) {
80 if (l > 1 || VAL(h[j].rank) != i)
81 return (j);
82 }
83 }
84 }
85 }
86 if (s < 15) {
87 /* for retaliation after 15 */
88 for (i = 0; i < NTV; i++) {
89 if ((j = anysumto(h, n, s, tv[i])) >= 0) {
90 if ((l = numofval(h, n, 15 - tv[i])) > 0) {
91 if (l > 1 ||
92 VAL(h[j].rank) != 15 - tv[i])
93 return (j);
94 }
95 }
96 }
97 }
98 j = -1;
99 /* remember: h is sorted */
100 for (i = n - 1; i >= 0; --i) {
101 l = s + VAL(h[i].rank);
102 if (l > 31)
103 continue;
104 if (l != 5 && l != 10 && l != 21) {
105 j = i;
106 break;
107 }
108 }
109 if (j >= 0)
110 return (j);
111 for (i = n - 1; i >= 0; --i) {
112 l = s + VAL(h[i].rank);
113 if (l > 31)
114 continue;
115 if (j < 0)
116 j = i;
117 if (l != 5 && l != 21) {
118 j = i;
119 break;
120 }
121 }
122 return (j);
123 }
124
125 /*
126 * plyrhand:
127 * Evaluate and score a player hand or crib
128 */
129 int
130 plyrhand(hand, s)
131 CARD hand[];
132 char *s;
133 {
134 static char prompt[BUFSIZ];
135 register int i, j;
136 register BOOLEAN win;
137
138 prhand(hand, CINHAND, Playwin, FALSE);
139 (void) sprintf(prompt, "Your %s scores ", s);
140 i = scorehand(hand, turnover, CINHAND, strcmp(s, "crib") == 0, explain);
141 if ((j = number(0, 29, prompt)) == 19)
142 j = 0;
143 if (i != j) {
144 if (i < j) {
145 win = chkscr(&pscore, i);
146 msg("It's really only %d points; I get %d", i, 2);
147 if (!win)
148 win = chkscr(&cscore, 2);
149 } else {
150 win = chkscr(&pscore, j);
151 msg("You should have taken %d, not %d!", i, j);
152 }
153 if (explain)
154 msg("Explanation: %s", expl);
155 do_wait();
156 } else
157 win = chkscr(&pscore, i);
158 return (win);
159 }
160
161 /*
162 * comphand:
163 * Handle scoring and displaying the computers hand
164 */
165 int
166 comphand(h, s)
167 CARD h[];
168 char *s;
169 {
170 register int j;
171
172 j = scorehand(h, turnover, CINHAND, strcmp(s, "crib") == 0, FALSE);
173 prhand(h, CINHAND, Compwin, FALSE);
174 msg("My %s scores %d", s, (j == 0 ? 19 : j));
175 return (chkscr(&cscore, j));
176 }
177
178 /*
179 * chkscr:
180 * Add inc to scr and test for > glimit, printing on the scoring
181 * board while we're at it.
182 */
183 int Lastscore[2] = {-1, -1};
184
185 int
186 chkscr(scr, inc)
187 int *scr, inc;
188 {
189 BOOLEAN myturn;
190
191 myturn = (scr == &cscore);
192 if (inc != 0) {
193 prpeg(Lastscore[myturn], '.', myturn);
194 Lastscore[myturn] = *scr;
195 *scr += inc;
196 prpeg(*scr, PEG, myturn);
197 refresh();
198 }
199 return (*scr >= glimit);
200 }
201
202 /*
203 * prpeg:
204 * Put out the peg character on the score board and put the
205 * score up on the board.
206 */
207 void
208 prpeg(score, peg, myturn)
209 register int score;
210 int peg;
211 BOOLEAN myturn;
212 {
213 register int y, x;
214
215 if (!myturn)
216 y = SCORE_Y + 2;
217 else
218 y = SCORE_Y + 5;
219
220 if (score <= 0 || score >= glimit) {
221 if (peg == '.')
222 peg = ' ';
223 if (score == 0)
224 x = SCORE_X + 2;
225 else {
226 x = SCORE_X + 2;
227 y++;
228 }
229 } else {
230 x = (score - 1) % 30;
231 if (score > 90 || (score > 30 && score <= 60)) {
232 y++;
233 x = 29 - x;
234 }
235 x += x / 5;
236 x += SCORE_X + 3;
237 }
238 mvaddch(y, x, peg);
239 mvprintw(SCORE_Y + (myturn ? 7 : 1), SCORE_X + 10, "%3d", score);
240 }
241
242 /*
243 * cdiscard -- the computer figures out what is the best discard for
244 * the crib and puts the best two cards at the end
245 */
246 void
247 cdiscard(mycrib)
248 BOOLEAN mycrib;
249 {
250 CARD d[CARDS], h[FULLHAND], cb[2];
251 register int i, j, k;
252 int nc, ns;
253 long sums[15];
254 static int undo1[15] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4};
255 static int undo2[15] = {1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5};
256
257 makedeck(d);
258 nc = CARDS;
259 for (i = 0; i < knownum; i++) { /* get all other cards */
260 cremove(known[i], d, nc--);
261 }
262 for (i = 0; i < 15; i++)
263 sums[i] = 0L;
264 ns = 0;
265 for (i = 0; i < (FULLHAND - 1); i++) {
266 cb[0] = chand[i];
267 for (j = i + 1; j < FULLHAND; j++) {
268 cb[1] = chand[j];
269 for (k = 0; k < FULLHAND; k++)
270 h[k] = chand[k];
271 cremove(chand[i], h, FULLHAND);
272 cremove(chand[j], h, FULLHAND - 1);
273 for (k = 0; k < nc; k++) {
274 sums[ns] +=
275 scorehand(h, d[k], CINHAND, TRUE, FALSE);
276 if (mycrib)
277 sums[ns] += adjust(cb, d[k]);
278 else
279 sums[ns] -= adjust(cb, d[k]);
280 }
281 ++ns;
282 }
283 }
284 j = 0;
285 for (i = 1; i < 15; i++)
286 if (sums[i] > sums[j])
287 j = i;
288 for (k = 0; k < FULLHAND; k++)
289 h[k] = chand[k];
290 cremove(h[undo1[j]], chand, FULLHAND);
291 cremove(h[undo2[j]], chand, FULLHAND - 1);
292 chand[4] = h[undo1[j]];
293 chand[5] = h[undo2[j]];
294 }
295
296 /*
297 * returns true if some card in hand can be played without exceeding 31
298 */
299 int
300 anymove(hand, n, sum)
301 CARD hand[];
302 int n, sum;
303 {
304 register int i, j;
305
306 if (n < 1)
307 return (FALSE);
308 j = hand[0].rank;
309 for (i = 1; i < n; i++) {
310 if (hand[i].rank < j)
311 j = hand[i].rank;
312 }
313 return (sum + VAL(j) <= 31);
314 }
315
316 /*
317 * anysumto returns the index (0 <= i < n) of the card in hand that brings
318 * the s up to t, or -1 if there is none
319 */
320 int
321 anysumto(hand, n, s, t)
322 CARD hand[];
323 int n, s, t;
324 {
325 register int i;
326
327 for (i = 0; i < n; i++) {
328 if (s + VAL(hand[i].rank) == t)
329 return (i);
330 }
331 return (-1);
332 }
333
334 /*
335 * return the number of cards in h having the given rank value
336 */
337 int
338 numofval(h, n, v)
339 CARD h[];
340 int n, v;
341 {
342 register int i, j;
343
344 j = 0;
345 for (i = 0; i < n; i++) {
346 if (VAL(h[i].rank) == v)
347 ++j;
348 }
349 return (j);
350 }
351
352 /*
353 * makeknown remembers all n cards in h for future recall
354 */
355 void
356 makeknown(h, n)
357 CARD h[];
358 int n;
359 {
360 register int i;
361
362 for (i = 0; i < n; i++)
363 known[knownum++] = h[i];
364 }