Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / cribbage / crib.c
... / ...
CommitLineData
1/*-
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)crib.c 8.1 (Berkeley) 5/31/93";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: src/games/cribbage/crib.c,v 1.10 1999/12/12 03:04:14 billf Exp $";
46#endif /* not lint */
47
48#include <curses.h>
49#include <signal.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53#include <stdio.h>
54
55#include "deck.h"
56#include "cribbage.h"
57#include "cribcur.h"
58#include "pathnames.h"
59
60int
61main(argc, argv)
62 int argc;
63 char *argv[];
64{
65 BOOLEAN playing;
66 FILE *f;
67 int ch;
68
69 f = fopen(_PATH_LOG, "a");
70
71 /* revoke */
72 setgid(getgid());
73
74 while ((ch = getopt(argc, argv, "eqr")) != -1)
75 switch (ch) {
76 case 'e':
77 explain = TRUE;
78 break;
79 case 'q':
80 quiet = TRUE;
81 break;
82 case 'r':
83 rflag = TRUE;
84 break;
85 case '?':
86 default:
87 (void) fprintf(stderr, "usage: cribbage [-eqr]\n");
88 exit(1);
89 }
90
91 initscr();
92 (void)signal(SIGINT, rint);
93 crmode();
94 noecho();
95
96 Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, 0);
97 Tablewin = subwin(stdscr, TABLE_Y, TABLE_X, 0, PLAY_X);
98 Compwin = subwin(stdscr, COMP_Y, COMP_X, 0, TABLE_X + PLAY_X);
99 Msgwin = subwin(stdscr, MSG_Y, MSG_X, Y_MSG_START, SCORE_X + 1);
100 leaveok(Playwin, TRUE);
101 leaveok(Tablewin, TRUE);
102 leaveok(Compwin, TRUE);
103 clearok(stdscr, FALSE);
104
105 if (!quiet) {
106 msg("Do you need instructions for cribbage? ");
107 if (getuchar() == 'Y') {
108 endwin();
109 clear();
110 mvcur(0, COLS - 1, LINES - 1, 0);
111 fflush(stdout);
112 instructions();
113 crmode();
114 noecho();
115 clear();
116 refresh();
117 msg("For cribbage rules, use \"man cribbage\"");
118 }
119 }
120 playing = TRUE;
121 do {
122 wclrtobot(Msgwin);
123 msg(quiet ? "L or S? " : "Long (to 121) or Short (to 61)? ");
124 if (glimit == SGAME)
125 glimit = (getuchar() == 'L' ? LGAME : SGAME);
126 else
127 glimit = (getuchar() == 'S' ? SGAME : LGAME);
128 game();
129 msg("Another game? ");
130 playing = (getuchar() == 'Y');
131 } while (playing);
132
133 if (f != NULL) {
134 (void)fprintf(f, "%s: won %5.5d, lost %5.5d\n",
135 getlogin(), cgames, pgames);
136 (void) fclose(f);
137 }
138 bye();
139 if (!f) {
140 (void) fprintf(stderr, "\ncribbage: can't open %s.\n",
141 _PATH_LOG);
142 exit(1);
143 }
144 exit(0);
145}
146
147/*
148 * makeboard:
149 * Print out the initial board on the screen
150 */
151void
152makeboard()
153{
154 mvaddstr(SCORE_Y + 0, SCORE_X,
155 "+---------------------------------------+");
156 mvaddstr(SCORE_Y + 1, SCORE_X,
157 "| Score: 0 YOU |");
158 mvaddstr(SCORE_Y + 2, SCORE_X,
159 "| *.....:.....:.....:.....:.....:..... |");
160 mvaddstr(SCORE_Y + 3, SCORE_X,
161 "| *.....:.....:.....:.....:.....:..... |");
162 mvaddstr(SCORE_Y + 4, SCORE_X,
163 "| |");
164 mvaddstr(SCORE_Y + 5, SCORE_X,
165 "| *.....:.....:.....:.....:.....:..... |");
166 mvaddstr(SCORE_Y + 6, SCORE_X,
167 "| *.....:.....:.....:.....:.....:..... |");
168 mvaddstr(SCORE_Y + 7, SCORE_X,
169 "| Score: 0 ME |");
170 mvaddstr(SCORE_Y + 8, SCORE_X,
171 "+---------------------------------------+");
172 gamescore();
173}
174
175/*
176 * gamescore:
177 * Print out the current game score
178 */
179void
180gamescore()
181{
182
183 if (pgames || cgames) {
184 mvprintw(SCORE_Y + 1, SCORE_X + 28, "Games: %3d", pgames);
185 mvprintw(SCORE_Y + 7, SCORE_X + 28, "Games: %3d", cgames);
186 }
187 Lastscore[0] = -1;
188 Lastscore[1] = -1;
189}
190
191/*
192 * game:
193 * Play one game up to glimit points. Actually, we only ASK the
194 * player what card to turn. We do a random one, anyway.
195 */
196void
197game()
198{
199 int i, j;
200 BOOLEAN flag;
201 BOOLEAN compcrib;
202
203 compcrib = FALSE;
204 makedeck(deck);
205 shuffle(deck);
206 if (gamecount == 0) {
207 flag = TRUE;
208 do {
209 if (!rflag) { /* player cuts deck */
210 msg(quiet ? "Cut for crib? " :
211 "Cut to see whose crib it is -- low card wins? ");
212 getline();
213 }
214 i = random() % CARDS; /* random cut */
215 do { /* comp cuts deck */
216 j = random() % CARDS;
217 } while (j == i);
218 addmsg(quiet ? "You cut " : "You cut the ");
219 msgcard(deck[i], FALSE);
220 endmsg();
221 addmsg(quiet ? "I cut " : "I cut the ");
222 msgcard(deck[j], FALSE);
223 endmsg();
224 flag = (deck[i].rank == deck[j].rank);
225 if (flag) {
226 msg(quiet ? "We tied..." :
227 "We tied and have to try again...");
228 shuffle(deck);
229 continue;
230 } else
231 compcrib = (deck[i].rank > deck[j].rank);
232 } while (flag);
233 clear();
234 makeboard();
235 refresh();
236 } else {
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 */
287int
288playhand(mycrib)
289 BOOLEAN mycrib;
290{
291 int deckpos;
292
293 werase(Compwin);
294
295 knownum = 0;
296 deckpos = deal(mycrib);
297 sorthand(chand, FULLHAND);
298 sorthand(phand, FULLHAND);
299 makeknown(chand, FULLHAND);
300 prhand(phand, FULLHAND, Playwin, FALSE);
301 discard(mycrib);
302 if (cut(mycrib, deckpos))
303 return TRUE;
304 if (peg(mycrib))
305 return TRUE;
306 werase(Tablewin);
307 wrefresh(Tablewin);
308 if (score(mycrib))
309 return TRUE;
310 return FALSE;
311}
312
313/*
314 * deal cards to both players from deck
315 */
316int
317deal(mycrib)
318 BOOLEAN mycrib;
319{
320 int i, j;
321
322 for (i = j = 0; i < FULLHAND; i++) {
323 if (mycrib) {
324 phand[i] = deck[j++];
325 chand[i] = deck[j++];
326 } else {
327 chand[i] = deck[j++];
328 phand[i] = deck[j++];
329 }
330 }
331 return (j);
332}
333
334/*
335 * discard:
336 * Handle players discarding into the crib...
337 * Note: we call cdiscard() after prining first message so player doesn't wait
338 */
339void
340discard(mycrib)
341 BOOLEAN mycrib;
342{
343 char *prompt;
344 CARD crd;
345
346 prcrib(mycrib, TRUE);
347 prompt = (quiet ? "Discard --> " : "Discard a card --> ");
348 cdiscard(mycrib); /* puts best discard at end */
349 crd = phand[infrom(phand, FULLHAND, prompt)];
350 cremove(crd, phand, FULLHAND);
351 prhand(phand, FULLHAND, Playwin, FALSE);
352 crib[0] = crd;
353
354 /* Next four lines same as last four except for cdiscard(). */
355 crd = phand[infrom(phand, FULLHAND - 1, prompt)];
356 cremove(crd, phand, FULLHAND - 1);
357 prhand(phand, FULLHAND, Playwin, FALSE);
358 crib[1] = crd;
359 crib[2] = chand[4];
360 crib[3] = chand[5];
361 chand[4].rank = chand[4].suit = chand[5].rank = chand[5].suit = EMPTY;
362}
363
364/*
365 * cut:
366 * Cut the deck and set turnover. Actually, we only ASK the
367 * player what card to turn. We do a random one, anyway.
368 */
369int
370cut(mycrib, pos)
371 BOOLEAN mycrib;
372 int pos;
373{
374 int i;
375 BOOLEAN win;
376
377 win = FALSE;
378 if (mycrib) {
379 if (!rflag) { /* random cut */
380 msg(quiet ? "Cut the deck? " :
381 "How many cards down do you wish to cut the deck? ");
382 getline();
383 }
384 i = random() % (CARDS - pos);
385 turnover = deck[i + pos];
386 addmsg(quiet ? "You cut " : "You cut the ");
387 msgcard(turnover, FALSE);
388 endmsg();
389 if (turnover.rank == JACK) {
390 msg("I get two for his heels");
391 win = chkscr(&cscore, 2);
392 }
393 } else {
394 i = random() % (CARDS - pos) + pos;
395 turnover = deck[i];
396 addmsg(quiet ? "I cut " : "I cut the ");
397 msgcard(turnover, FALSE);
398 endmsg();
399 if (turnover.rank == JACK) {
400 msg("You get two for his heels");
401 win = chkscr(&pscore, 2);
402 }
403 }
404 makeknown(&turnover, 1);
405 prcrib(mycrib, FALSE);
406 return (win);
407}
408
409/*
410 * prcrib:
411 * Print out the turnover card with crib indicator
412 */
413void
414prcrib(mycrib, blank)
415 BOOLEAN mycrib, blank;
416{
417 int y, cardx;
418
419 if (mycrib)
420 cardx = CRIB_X;
421 else
422 cardx = 0;
423
424 mvaddstr(CRIB_Y, cardx + 1, "CRIB");
425 prcard(stdscr, CRIB_Y + 1, cardx, turnover, blank);
426
427 if (mycrib)
428 cardx = 0;
429 else
430 cardx = CRIB_X;
431
432 for (y = CRIB_Y; y <= CRIB_Y + 5; y++)
433 mvaddstr(y, cardx, " ");
434}
435
436/*
437 * peg:
438 * Handle all the pegging...
439 */
440static CARD Table[14];
441static int Tcnt;
442
443int
444peg(mycrib)
445 BOOLEAN mycrib;
446{
447 static CARD ch[CINHAND], ph[CINHAND];
448 int i, j, k;
449 int l;
450 int cnum, pnum, sum;
451 BOOLEAN myturn, mego, ugo, last, played;
452 CARD crd;
453
454 cnum = pnum = CINHAND;
455 for (i = 0; i < CINHAND; i++) { /* make copies of hands */
456 ch[i] = chand[i];
457 ph[i] = phand[i];
458 }
459 Tcnt = 0; /* index to table of cards played */
460 sum = 0; /* sum of cards played */
461 played = mego = ugo = FALSE;
462 myturn = !mycrib;
463 for (;;) {
464 last = TRUE; /* enable last flag */
465 prhand(ph, pnum, Playwin, FALSE);
466 prhand(ch, cnum, Compwin, TRUE);
467 prtable(sum);
468 if (myturn) { /* my tyrn to play */
469 if (!anymove(ch, cnum, sum)) { /* if no card to play */
470 if (!mego && cnum) { /* go for comp? */
471 msg("GO");
472 mego = TRUE;
473 }
474 /* can player move? */
475 if (anymove(ph, pnum, sum))
476 myturn = !myturn;
477 else { /* give him his point */
478 msg(quiet ? "You get one" :
479 "You get one point");
480 if (chkscr(&pscore, 1))
481 return TRUE;
482 sum = 0;
483 mego = ugo = FALSE;
484 Tcnt = 0;
485 }
486 } else {
487 played = TRUE;
488 j = -1;
489 k = 0;
490 /* maximize score */
491 for (i = 0; i < cnum; i++) {
492 l = pegscore(ch[i], Table, Tcnt, sum);
493 if (l > k) {
494 k = l;
495 j = i;
496 }
497 }
498 if (j < 0) /* if nothing scores */
499 j = cchose(ch, cnum, sum);
500 crd = ch[j];
501 cremove(crd, ch, cnum--);
502 sum += VAL(crd.rank);
503 Table[Tcnt++] = crd;
504 if (k > 0) {
505 addmsg(quiet ? "I get %d playing " :
506 "I get %d points playing ", k);
507 msgcard(crd, FALSE);
508 endmsg();
509 if (chkscr(&cscore, k))
510 return TRUE;
511 }
512 myturn = !myturn;
513 }
514 } else {
515 if (!anymove(ph, pnum, sum)) { /* can player move? */
516 if (!ugo && pnum) { /* go for player */
517 msg("You have a GO");
518 ugo = TRUE;
519 }
520 /* can computer play? */
521 if (anymove(ch, cnum, sum))
522 myturn = !myturn;
523 else {
524 msg(quiet ? "I get one" :
525 "I get one point");
526 do_wait();
527 if (chkscr(&cscore, 1))
528 return TRUE;
529 sum = 0;
530 mego = ugo = FALSE;
531 Tcnt = 0;
532 }
533 } else { /* player plays */
534 played = FALSE;
535 if (pnum == 1) {
536 crd = ph[0];
537 msg("You play your last card");
538 } else
539 for (;;) {
540 prhand(ph,
541 pnum, Playwin, FALSE);
542 crd = ph[infrom(ph,
543 pnum, "Your play: ")];
544 if (sum + VAL(crd.rank) <= 31)
545 break;
546 else
547 msg("Total > 31 -- try again");
548 }
549 makeknown(&crd, 1);
550 cremove(crd, ph, pnum--);
551 i = pegscore(crd, Table, Tcnt, sum);
552 sum += VAL(crd.rank);
553 Table[Tcnt++] = crd;
554 if (i > 0) {
555 msg(quiet ? "You got %d" :
556 "You got %d points", i);
557 if (chkscr(&pscore, i))
558 return TRUE;
559 }
560 myturn = !myturn;
561 }
562 }
563 if (sum >= 31) {
564 if (!myturn)
565 do_wait();
566 sum = 0;
567 mego = ugo = FALSE;
568 Tcnt = 0;
569 last = FALSE; /* disable last flag */
570 }
571 if (!pnum && !cnum)
572 break; /* both done */
573 }
574 prhand(ph, pnum, Playwin, FALSE);
575 prhand(ch, cnum, Compwin, TRUE);
576 prtable(sum);
577 if (last) {
578 if (played) {
579 msg(quiet ? "I get one for last" :
580 "I get one point for last");
581 do_wait();
582 if (chkscr(&cscore, 1))
583 return TRUE;
584 } else {
585 msg(quiet ? "You get one for last" :
586 "You get one point for last");
587 if (chkscr(&pscore, 1))
588 return TRUE;
589 }
590 }
591 return (FALSE);
592}
593
594/*
595 * prtable:
596 * Print out the table with the current score
597 */
598void
599prtable(score)
600 int score;
601{
602 prhand(Table, Tcnt, Tablewin, FALSE);
603 mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score);
604 wrefresh(Tablewin);
605}
606
607/*
608 * score:
609 * Handle the scoring of the hands
610 */
611int
612score(mycrib)
613 BOOLEAN mycrib;
614{
615 sorthand(crib, CINHAND);
616 if (mycrib) {
617 if (plyrhand(phand, "hand"))
618 return (TRUE);
619 if (comphand(chand, "hand"))
620 return (TRUE);
621 do_wait();
622 if (comphand(crib, "crib"))
623 return (TRUE);
624 } else {
625 if (comphand(chand, "hand"))
626 return (TRUE);
627 if (plyrhand(phand, "hand"))
628 return (TRUE);
629 if (plyrhand(crib, "crib"))
630 return (TRUE);
631 }
632 return (FALSE);
633}