support.c revision 1.11 1 /* $NetBSD: support.c,v 1.11 2006/03/19 12:09:39 he 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)support.c 8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: support.c,v 1.11 2006/03/19 12:09:39 he Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <curses.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <stdlib.h>
45
46 #include "deck.h"
47 #include "cribbage.h"
48 #include "cribcur.h"
49
50 #define NTV 10 /* number scores to test */
51
52 /* score to test reachability of, and order to test them in */
53 const int tv[NTV] = {8, 7, 9, 6, 11, 12, 13, 14, 10, 5};
54
55 /*
56 * computer chooses what to play in pegging...
57 * only called if no playable card will score points
58 */
59 int
60 cchose(const CARD h[], int n, int s)
61 {
62 int i, j, l;
63
64 if (n <= 1)
65 return (0);
66 if (s < 4) { /* try for good value */
67 if ((j = anysumto(h, n, s, 4)) >= 0)
68 return (j);
69 if ((j = anysumto(h, n, s, 3)) >= 0 && s == 0)
70 return (j);
71 }
72 if (s > 0 && s < 20) {
73 /* try for retaliation to 31 */
74 for (i = 1; i <= 10; i++) {
75 if ((j = anysumto(h, n, s, 21 - i)) >= 0) {
76 if ((l = numofval(h, n, i)) > 0) {
77 if (l > 1 || VAL(h[j].rank) != i)
78 return (j);
79 }
80 }
81 }
82 }
83 if (s < 15) {
84 /* for retaliation after 15 */
85 for (i = 0; i < NTV; i++) {
86 if ((j = anysumto(h, n, s, tv[i])) >= 0) {
87 if ((l = numofval(h, n, 15 - tv[i])) > 0) {
88 if (l > 1 ||
89 VAL(h[j].rank) != 15 - tv[i])
90 return (j);
91 }
92 }
93 }
94 }
95 j = -1;
96 /* remember: h is sorted */
97 for (i = n - 1; i >= 0; --i) {
98 l = s + VAL(h[i].rank);
99 if (l > 31)
100 continue;
101 if (l != 5 && l != 10 && l != 21) {
102 j = i;
103 break;
104 }
105 }
106 if (j >= 0)
107 return (j);
108 for (i = n - 1; i >= 0; --i) {
109 l = s + VAL(h[i].rank);
110 if (l > 31)
111 continue;
112 if (j < 0)
113 j = i;
114 if (l != 5 && l != 21) {
115 j = i;
116 break;
117 }
118 }
119 if (j < 0) {
120 printf("\ncchose: internal error %d %d\n", j, n);
121 exit(93);
122 }
123 return (j);
124 }
125
126 /*
127 * plyrhand:
128 * Evaluate and score a player hand or crib
129 */
130 int
131 plyrhand(const CARD hand[], const char *s)
132 {
133 static char prompt[BUFSIZ];
134 int i, j;
135 BOOLEAN win;
136
137 prhand(hand, CINHAND, Playwin, FALSE);
138 (void) sprintf(prompt, "Your %s scores ", s);
139 i = scorehand(hand, turnover, CINHAND, strcmp(s, "crib") == 0, explain);
140 if ((j = number(0, 29, prompt)) == 19)
141 j = 0;
142 if (i != j) {
143 if (i < j) {
144 win = chkscr(&pscore, i);
145 msg("It's really only %d points; I get %d", i, 2);
146 if (!win)
147 win = chkscr(&cscore, 2);
148 } else {
149 win = chkscr(&pscore, j);
150 msg("You should have taken %d, not %d!", i, j);
151 }
152 if (explain)
153 msg("Explanation: %s", explan);
154 do_wait();
155 } else
156 win = chkscr(&pscore, i);
157 return (win);
158 }
159
160 /*
161 * comphand:
162 * Handle scoring and displaying the computers hand
163 */
164 int
165 comphand(const CARD h[], const char *s)
166 {
167 int j;
168
169 j = scorehand(h, turnover, CINHAND, strcmp(s, "crib") == 0, FALSE);
170 prhand(h, CINHAND, Compwin, FALSE);
171 msg("My %s scores %d", s, (j == 0 ? 19 : j));
172 return (chkscr(&cscore, j));
173 }
174
175 /*
176 * chkscr:
177 * Add inc to scr and test for > glimit, printing on the scoring
178 * board while we're at it.
179 */
180 int Lastscore[2] = {-1, -1};
181
182 int
183 chkscr(int *scr, int inc)
184 {
185 BOOLEAN myturn;
186
187 myturn = (scr == &cscore);
188 if (inc != 0) {
189 prpeg(Lastscore[(int)myturn], '.', myturn);
190 Lastscore[(int)myturn] = *scr;
191 *scr += inc;
192 prpeg(*scr, PEG, myturn);
193 refresh();
194 }
195 return (*scr >= glimit);
196 }
197
198 /*
199 * prpeg:
200 * Put out the peg character on the score board and put the
201 * score up on the board.
202 */
203 void
204 prpeg(int curscore, int pegc, BOOLEAN myturn)
205 {
206 int y, x;
207
208 if (!myturn)
209 y = SCORE_Y + 2;
210 else
211 y = SCORE_Y + 5;
212
213 if (curscore <= 0 || curscore >= glimit) {
214 if (pegc == '.')
215 pegc = ' ';
216 if (curscore == 0)
217 x = SCORE_X + 2;
218 else {
219 x = SCORE_X + 2;
220 y++;
221 }
222 } else {
223 x = (curscore - 1) % 30;
224 if (curscore > 90 || (curscore > 30 && curscore <= 60)) {
225 y++;
226 x = 29 - x;
227 }
228 x += x / 5;
229 x += SCORE_X + 3;
230 }
231 mvaddch(y, x, pegc);
232 mvprintw(SCORE_Y + (myturn ? 7 : 1), SCORE_X + 10, "%3d", curscore);
233 }
234
235 /*
236 * cdiscard -- the computer figures out what is the best discard for
237 * the crib and puts the best two cards at the end
238 */
239 void
240 cdiscard(BOOLEAN mycrib)
241 {
242 CARD d[CARDS], h[FULLHAND], cb[2];
243 int i, j, k;
244 int nc, ns;
245 long sums[15];
246 static int undo1[15] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4};
247 static int undo2[15] = {1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5};
248
249 makedeck(d);
250 nc = CARDS;
251 for (i = 0; i < knownum; i++) { /* get all other cards */
252 cremove(known[i], d, nc--);
253 }
254 for (i = 0; i < 15; i++)
255 sums[i] = 0L;
256 ns = 0;
257 for (i = 0; i < (FULLHAND - 1); i++) {
258 cb[0] = chand[i];
259 for (j = i + 1; j < FULLHAND; j++) {
260 cb[1] = chand[j];
261 for (k = 0; k < FULLHAND; k++)
262 h[k] = chand[k];
263 cremove(chand[i], h, FULLHAND);
264 cremove(chand[j], h, FULLHAND - 1);
265 for (k = 0; k < nc; k++) {
266 sums[ns] +=
267 scorehand(h, d[k], CINHAND, TRUE, FALSE);
268 if (mycrib)
269 sums[ns] += adjust(cb, d[k]);
270 else
271 sums[ns] -= adjust(cb, d[k]);
272 }
273 ++ns;
274 }
275 }
276 j = 0;
277 for (i = 1; i < 15; i++)
278 if (sums[i] > sums[j])
279 j = i;
280 for (k = 0; k < FULLHAND; k++)
281 h[k] = chand[k];
282 cremove(h[undo1[j]], chand, FULLHAND);
283 cremove(h[undo2[j]], chand, FULLHAND - 1);
284 chand[4] = h[undo1[j]];
285 chand[5] = h[undo2[j]];
286 }
287
288 /*
289 * returns true if some card in hand can be played without exceeding 31
290 */
291 int
292 anymove(const CARD hand[], int n, int sum)
293 {
294 int i, j;
295
296 if (n < 1)
297 return (FALSE);
298 j = hand[0].rank;
299 for (i = 1; i < n; i++) {
300 if (hand[i].rank < j)
301 j = hand[i].rank;
302 }
303 return (sum + VAL(j) <= 31);
304 }
305
306 /*
307 * anysumto returns the index (0 <= i < n) of the card in hand that brings
308 * the s up to t, or -1 if there is none
309 */
310 int
311 anysumto(const CARD hand[], int n, int s, int t)
312 {
313 int i;
314
315 for (i = 0; i < n; i++) {
316 if (s + VAL(hand[i].rank) == t)
317 return (i);
318 }
319 return (-1);
320 }
321
322 /*
323 * return the number of cards in h having the given rank value
324 */
325 int
326 numofval(const CARD h[], int n, int v)
327 {
328 int i, j;
329
330 j = 0;
331 for (i = 0; i < n; i++) {
332 if (VAL(h[i].rank) == v)
333 ++j;
334 }
335 return (j);
336 }
337
338 /*
339 * makeknown remembers all n cards in h for future recall
340 */
341 void
342 makeknown(const CARD h[], int n)
343 {
344 int i;
345
346 for (i = 0; i < n; i++)
347 known[knownum++] = h[i];
348 }
349