]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/crib.c
Since we've got an int to store the value, why store it in a function
[bsdgames-darwin.git] / cribbage / crib.c
1 /* $NetBSD: crib.c,v 1.9 1997/10/11 02:44:30 lukem 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)crib.c 8.1 (Berkeley) 5/31/93";
45 #else
46 __RCSID("$NetBSD: crib.c,v 1.9 1997/10/11 02:44:30 lukem Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <curses.h>
51 #include <err.h>
52 #include <signal.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #include "deck.h"
58 #include "cribbage.h"
59 #include "cribcur.h"
60 #include "pathnames.h"
61
62 int main __P((int, char *[]));
63
64 int
65 main(argc, argv)
66 int argc;
67 char *argv[];
68 {
69 BOOLEAN playing;
70 FILE *f;
71 int ch;
72
73 while ((ch = getopt(argc, argv, "eqr")) != -1)
74 switch (ch) {
75 case 'e':
76 explain = TRUE;
77 break;
78 case 'q':
79 quiet = TRUE;
80 break;
81 case 'r':
82 rflag = TRUE;
83 break;
84 case '?':
85 default:
86 (void) fprintf(stderr, "usage: cribbage [-eqr]\n");
87 exit(1);
88 }
89
90 initscr();
91 (void)signal(SIGINT, rint);
92 crmode();
93 noecho();
94
95 Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, 0);
96 Tablewin = subwin(stdscr, TABLE_Y, TABLE_X, 0, PLAY_X);
97 Compwin = subwin(stdscr, COMP_Y, COMP_X, 0, TABLE_X + PLAY_X);
98 Msgwin = subwin(stdscr, MSG_Y, MSG_X, Y_MSG_START, SCORE_X + 1);
99 leaveok(Playwin, TRUE);
100 leaveok(Tablewin, TRUE);
101 leaveok(Compwin, TRUE);
102 clearok(stdscr, FALSE);
103
104 if (!quiet) {
105 msg("Do you need instructions for cribbage? ");
106 if (getuchar() == 'Y') {
107 endwin();
108 clear();
109 mvcur(0, COLS - 1, LINES - 1, 0);
110 fflush(stdout);
111 instructions();
112 crmode();
113 noecho();
114 clear();
115 refresh();
116 msg("For cribbage rules, use \"man cribbage\"");
117 }
118 }
119 playing = TRUE;
120 do {
121 wclrtobot(Msgwin);
122 msg(quiet ? "L or S? " : "Long (to 121) or Short (to 61)? ");
123 if (glimit == SGAME)
124 glimit = (getuchar() == 'L' ? LGAME : SGAME);
125 else
126 glimit = (getuchar() == 'S' ? SGAME : LGAME);
127 game();
128 msg("Another game? ");
129 playing = (getuchar() == 'Y');
130 } while (playing);
131
132 if ((f = fopen(_PATH_LOG, "a")) != NULL) {
133 (void)fprintf(f, "%s: won %5.5d, lost %5.5d\n",
134 getlogin(), cgames, pgames);
135 (void) fclose(f);
136 }
137 bye();
138 if (!f)
139 errx(1, "can't open %s", _PATH_LOG);
140 exit(0);
141 }
142
143 /*
144 * makeboard:
145 * Print out the initial board on the screen
146 */
147 void
148 makeboard()
149 {
150 mvaddstr(SCORE_Y + 0, SCORE_X,
151 "+---------------------------------------+");
152 mvaddstr(SCORE_Y + 1, SCORE_X,
153 "| Score: 0 YOU |");
154 mvaddstr(SCORE_Y + 2, SCORE_X,
155 "| *.....:.....:.....:.....:.....:..... |");
156 mvaddstr(SCORE_Y + 3, SCORE_X,
157 "| *.....:.....:.....:.....:.....:..... |");
158 mvaddstr(SCORE_Y + 4, SCORE_X,
159 "| |");
160 mvaddstr(SCORE_Y + 5, SCORE_X,
161 "| *.....:.....:.....:.....:.....:..... |");
162 mvaddstr(SCORE_Y + 6, SCORE_X,
163 "| *.....:.....:.....:.....:.....:..... |");
164 mvaddstr(SCORE_Y + 7, SCORE_X,
165 "| Score: 0 ME |");
166 mvaddstr(SCORE_Y + 8, SCORE_X,
167 "+---------------------------------------+");
168 gamescore();
169 }
170
171 /*
172 * gamescore:
173 * Print out the current game score
174 */
175 void
176 gamescore()
177 {
178 extern int Lastscore[];
179
180 if (pgames || cgames) {
181 mvprintw(SCORE_Y + 1, SCORE_X + 28, "Games: %3d", pgames);
182 mvprintw(SCORE_Y + 7, SCORE_X + 28, "Games: %3d", cgames);
183 }
184 Lastscore[0] = -1;
185 Lastscore[1] = -1;
186 }
187
188 /*
189 * game:
190 * Play one game up to glimit points. Actually, we only ASK the
191 * player what card to turn. We do a random one, anyway.
192 */
193 void
194 game()
195 {
196 int i, j;
197 BOOLEAN flag;
198 BOOLEAN compcrib;
199
200 compcrib = FALSE;
201 makedeck(deck);
202 shuffle(deck);
203 if (gamecount == 0) {
204 flag = TRUE;
205 do {
206 if (!rflag) { /* player cuts deck */
207 msg(quiet ? "Cut for crib? " :
208 "Cut to see whose crib it is -- low card wins? ");
209 getline();
210 }
211 i = (rand() >> 4) % CARDS; /* random cut */
212 do { /* comp cuts deck */
213 j = (rand() >> 4) % CARDS;
214 } while (j == i);
215 addmsg(quiet ? "You cut " : "You cut the ");
216 msgcard(deck[i], FALSE);
217 endmsg();
218 addmsg(quiet ? "I cut " : "I cut the ");
219 msgcard(deck[j], FALSE);
220 endmsg();
221 flag = (deck[i].rank == deck[j].rank);
222 if (flag) {
223 msg(quiet ? "We tied..." :
224 "We tied and have to try again...");
225 shuffle(deck);
226 continue;
227 } else
228 compcrib = (deck[i].rank > deck[j].rank);
229 } while (flag);
230 do_wait();
231 clear();
232 makeboard();
233 refresh();
234 } else {
235 makeboard();
236 refresh();
237 werase(Tablewin);
238 wrefresh(Tablewin);
239 werase(Compwin);
240 wrefresh(Compwin);
241 msg("Loser (%s) gets first crib", (iwon ? "you" : "me"));
242 compcrib = !iwon;
243 }
244
245 pscore = cscore = 0;
246 flag = TRUE;
247 do {
248 shuffle(deck);
249 flag = !playhand(compcrib);
250 compcrib = !compcrib;
251 } while (flag);
252 ++gamecount;
253 if (cscore < pscore) {
254 if (glimit - cscore > 60) {
255 msg("YOU DOUBLE SKUNKED ME!");
256 pgames += 4;
257 } else
258 if (glimit - cscore > 30) {
259 msg("YOU SKUNKED ME!");
260 pgames += 2;
261 } else {
262 msg("YOU WON!");
263 ++pgames;
264 }
265 iwon = FALSE;
266 } else {
267 if (glimit - pscore > 60) {
268 msg("I DOUBLE SKUNKED YOU!");
269 cgames += 4;
270 } else
271 if (glimit - pscore > 30) {
272 msg("I SKUNKED YOU!");
273 cgames += 2;
274 } else {
275 msg("I WON!");
276 ++cgames;
277 }
278 iwon = TRUE;
279 }
280 gamescore();
281 }
282
283 /*
284 * playhand:
285 * Do up one hand of the game
286 */
287 int
288 playhand(mycrib)
289 BOOLEAN mycrib;
290 {
291 int deckpos;
292
293 werase(Compwin);
294 wrefresh(Compwin);
295 werase(Tablewin);
296 wrefresh(Tablewin);
297
298 knownum = 0;
299 deckpos = deal(mycrib);
300 sorthand(chand, FULLHAND);
301 sorthand(phand, FULLHAND);
302 makeknown(chand, FULLHAND);
303 prhand(phand, FULLHAND, Playwin, FALSE);
304 discard(mycrib);
305 if (cut(mycrib, deckpos))
306 return TRUE;
307 if (peg(mycrib))
308 return TRUE;
309 werase(Tablewin);
310 wrefresh(Tablewin);
311 if (score(mycrib))
312 return TRUE;
313 return FALSE;
314 }
315
316 /*
317 * deal cards to both players from deck
318 */
319 int
320 deal(mycrib)
321 BOOLEAN mycrib;
322 {
323 int i, j;
324
325 for (i = j = 0; i < FULLHAND; i++) {
326 if (mycrib) {
327 phand[i] = deck[j++];
328 chand[i] = deck[j++];
329 } else {
330 chand[i] = deck[j++];
331 phand[i] = deck[j++];
332 }
333 }
334 return (j);
335 }
336
337 /*
338 * discard:
339 * Handle players discarding into the crib...
340 * Note: we call cdiscard() after prining first message so player doesn't wait
341 */
342 void
343 discard(mycrib)
344 BOOLEAN mycrib;
345 {
346 char *prompt;
347 CARD crd;
348
349 prcrib(mycrib, TRUE);
350 prompt = (quiet ? "Discard --> " : "Discard a card --> ");
351 cdiscard(mycrib); /* puts best discard at end */
352 crd = phand[infrom(phand, FULLHAND, prompt)];
353 cremove(crd, phand, FULLHAND);
354 prhand(phand, FULLHAND, Playwin, FALSE);
355 crib[0] = crd;
356
357 /* Next four lines same as last four except for cdiscard(). */
358 crd = phand[infrom(phand, FULLHAND - 1, prompt)];
359 cremove(crd, phand, FULLHAND - 1);
360 prhand(phand, FULLHAND, Playwin, FALSE);
361 crib[1] = crd;
362 crib[2] = chand[4];
363 crib[3] = chand[5];
364 chand[4].rank = chand[4].suit = chand[5].rank = chand[5].suit = EMPTY;
365 }
366
367 /*
368 * cut:
369 * Cut the deck and set turnover. Actually, we only ASK the
370 * player what card to turn. We do a random one, anyway.
371 */
372 int
373 cut(mycrib, pos)
374 BOOLEAN mycrib;
375 int pos;
376 {
377 int i;
378 BOOLEAN win;
379
380 win = FALSE;
381 if (mycrib) {
382 if (!rflag) { /* random cut */
383 msg(quiet ? "Cut the deck? " :
384 "How many cards down do you wish to cut the deck? ");
385 getline();
386 }
387 i = (rand() >> 4) % (CARDS - pos);
388 turnover = deck[i + pos];
389 addmsg(quiet ? "You cut " : "You cut the ");
390 msgcard(turnover, FALSE);
391 endmsg();
392 if (turnover.rank == JACK) {
393 msg("I get two for his heels");
394 win = chkscr(&cscore, 2);
395 }
396 } else {
397 i = (rand() >> 4) % (CARDS - pos) + pos;
398 turnover = deck[i];
399 addmsg(quiet ? "I cut " : "I cut the ");
400 msgcard(turnover, FALSE);
401 endmsg();
402 if (turnover.rank == JACK) {
403 msg("You get two for his heels");
404 win = chkscr(&pscore, 2);
405 }
406 }
407 makeknown(&turnover, 1);
408 prcrib(mycrib, FALSE);
409 return (win);
410 }
411
412 /*
413 * prcrib:
414 * Print out the turnover card with crib indicator
415 */
416 void
417 prcrib(mycrib, blank)
418 BOOLEAN mycrib, blank;
419 {
420 int y, cardx;
421
422 if (mycrib)
423 cardx = CRIB_X;
424 else
425 cardx = 0;
426
427 mvaddstr(CRIB_Y, cardx + 1, "CRIB");
428 prcard(stdscr, CRIB_Y + 1, cardx, turnover, blank);
429
430 if (mycrib)
431 cardx = 0;
432 else
433 cardx = CRIB_X;
434
435 for (y = CRIB_Y; y <= CRIB_Y + 5; y++)
436 mvaddstr(y, cardx, " ");
437 refresh();
438 }
439
440 /*
441 * peg:
442 * Handle all the pegging...
443 */
444 static CARD Table[14];
445 static int Tcnt;
446
447 int
448 peg(mycrib)
449 BOOLEAN mycrib;
450 {
451 static CARD ch[CINHAND], ph[CINHAND];
452 int i, j, k;
453 int l;
454 int cnum, pnum, sum;
455 BOOLEAN myturn, mego, ugo, last, played;
456 CARD crd;
457
458 played = FALSE;
459 cnum = pnum = CINHAND;
460 for (i = 0; i < CINHAND; i++) { /* make copies of hands */
461 ch[i] = chand[i];
462 ph[i] = phand[i];
463 }
464 Tcnt = 0; /* index to table of cards played */
465 sum = 0; /* sum of cards played */
466 mego = ugo = FALSE;
467 myturn = !mycrib;
468 for (;;) {
469 last = TRUE; /* enable last flag */
470 prhand(ph, pnum, Playwin, FALSE);
471 prhand(ch, cnum, Compwin, TRUE);
472 prtable(sum);
473 if (myturn) { /* my tyrn to play */
474 if (!anymove(ch, cnum, sum)) { /* if no card to play */
475 if (!mego && cnum) { /* go for comp? */
476 msg("GO");
477 mego = TRUE;
478 }
479 /* can player move? */
480 if (anymove(ph, pnum, sum))
481 myturn = !myturn;
482 else { /* give him his point */
483 msg(quiet ? "You get one" :
484 "You get one point");
485 do_wait();
486 if (chkscr(&pscore, 1))
487 return TRUE;
488 sum = 0;
489 mego = ugo = FALSE;
490 Tcnt = 0;
491 }
492 } else {
493 played = TRUE;
494 j = -1;
495 k = 0;
496 /* maximize score */
497 for (i = 0; i < cnum; i++) {
498 l = pegscore(ch[i], Table, Tcnt, sum);
499 if (l > k) {
500 k = l;
501 j = i;
502 }
503 }
504 if (j < 0) /* if nothing scores */
505 j = cchose(ch, cnum, sum);
506 crd = ch[j];
507 cremove(crd, ch, cnum--);
508 sum += VAL(crd.rank);
509 Table[Tcnt++] = crd;
510 if (k > 0) {
511 addmsg(quiet ? "I get %d playing " :
512 "I get %d points playing ", k);
513 msgcard(crd, FALSE);
514 endmsg();
515 if (chkscr(&cscore, k))
516 return TRUE;
517 }
518 myturn = !myturn;
519 }
520 } else {
521 if (!anymove(ph, pnum, sum)) { /* can player move? */
522 if (!ugo && pnum) { /* go for player */
523 msg("You have a GO");
524 ugo = TRUE;
525 }
526 /* can computer play? */
527 if (anymove(ch, cnum, sum))
528 myturn = !myturn;
529 else {
530 msg(quiet ? "I get one" :
531 "I get one point");
532 do_wait();
533 if (chkscr(&cscore, 1))
534 return TRUE;
535 sum = 0;
536 mego = ugo = FALSE;
537 Tcnt = 0;
538 }
539 } else { /* player plays */
540 played = FALSE;
541 if (pnum == 1) {
542 crd = ph[0];
543 msg("You play your last card");
544 } else
545 for (;;) {
546 prhand(ph,
547 pnum, Playwin, FALSE);
548 crd = ph[infrom(ph,
549 pnum, "Your play: ")];
550 if (sum + VAL(crd.rank) <= 31)
551 break;
552 else
553 msg("Total > 31 -- try again");
554 }
555 makeknown(&crd, 1);
556 cremove(crd, ph, pnum--);
557 i = pegscore(crd, Table, Tcnt, sum);
558 sum += VAL(crd.rank);
559 Table[Tcnt++] = crd;
560 if (i > 0) {
561 msg(quiet ? "You got %d" :
562 "You got %d points", i);
563 if (pnum == 0)
564 do_wait();
565 if (chkscr(&pscore, i))
566 return TRUE;
567 }
568 myturn = !myturn;
569 }
570 }
571 if (sum >= 31) {
572 if (!myturn)
573 do_wait();
574 sum = 0;
575 mego = ugo = FALSE;
576 Tcnt = 0;
577 last = FALSE; /* disable last flag */
578 }
579 if (!pnum && !cnum)
580 break; /* both done */
581 }
582 prhand(ph, pnum, Playwin, FALSE);
583 prhand(ch, cnum, Compwin, TRUE);
584 prtable(sum);
585 if (last)
586 if (played) {
587 msg(quiet ? "I get one for last" :
588 "I get one point for last");
589 do_wait();
590 if (chkscr(&cscore, 1))
591 return TRUE;
592 } else {
593 msg(quiet ? "You get one for last" :
594 "You get one point for last");
595 do_wait();
596 if (chkscr(&pscore, 1))
597 return TRUE;
598 }
599 return (FALSE);
600 }
601
602 /*
603 * prtable:
604 * Print out the table with the current score
605 */
606 void
607 prtable(score)
608 int score;
609 {
610 prhand(Table, Tcnt, Tablewin, FALSE);
611 mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score);
612 wrefresh(Tablewin);
613 }
614
615 /*
616 * score:
617 * Handle the scoring of the hands
618 */
619 int
620 score(mycrib)
621 BOOLEAN mycrib;
622 {
623 sorthand(crib, CINHAND);
624 if (mycrib) {
625 if (plyrhand(phand, "hand"))
626 return (TRUE);
627 if (comphand(chand, "hand"))
628 return (TRUE);
629 do_wait();
630 if (comphand(crib, "crib"))
631 return (TRUE);
632 do_wait();
633 } else {
634 if (comphand(chand, "hand"))
635 return (TRUE);
636 if (plyrhand(phand, "hand"))
637 return (TRUE);
638 if (plyrhand(crib, "crib"))
639 return (TRUE);
640 }
641 return (FALSE);
642 }