]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/crib.c
1 /* $NetBSD: crib.c,v 1.5 1995/03/21 15:08:42 cgd 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
37 static char copyright
[] =
38 "@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
44 static char sccsid
[] = "@(#)crib.c 8.1 (Berkeley) 5/31/93";
46 static char rcsid
[] = "$NetBSD: crib.c,v 1.5 1995/03/21 15:08:42 cgd Exp $";
59 #include "pathnames.h"
70 while ((ch
= getopt(argc
, argv
, "eqr")) != EOF
)
83 (void) fprintf(stderr
, "usage: cribbage [-eqr]\n");
88 (void)signal(SIGINT
, rint
);
92 Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
93 Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
94 Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
95 Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
96 leaveok(Playwin
, TRUE
);
97 leaveok(Tablewin
, TRUE
);
98 leaveok(Compwin
, TRUE
);
99 clearok(stdscr
, FALSE
);
102 msg("Do you need instructions for cribbage? ");
103 if (getuchar() == 'Y') {
106 mvcur(0, COLS
- 1, LINES
- 1, 0);
113 msg("For cribbage rules, use \"man cribbage\"");
119 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
121 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
123 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
125 msg("Another game? ");
126 playing
= (getuchar() == 'Y');
129 if (f
= fopen(_PATH_LOG
, "a")) {
130 (void)fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
131 getlogin(), cgames
, pgames
);
136 (void) fprintf(stderr
, "\ncribbage: can't open %s.\n",
145 * Print out the initial board on the screen
150 mvaddstr(SCORE_Y
+ 0, SCORE_X
,
151 "+---------------------------------------+");
152 mvaddstr(SCORE_Y
+ 1, SCORE_X
,
154 mvaddstr(SCORE_Y
+ 2, SCORE_X
,
155 "| *.....:.....:.....:.....:.....:..... |");
156 mvaddstr(SCORE_Y
+ 3, SCORE_X
,
157 "| *.....:.....:.....:.....:.....:..... |");
158 mvaddstr(SCORE_Y
+ 4, SCORE_X
,
160 mvaddstr(SCORE_Y
+ 5, SCORE_X
,
161 "| *.....:.....:.....:.....:.....:..... |");
162 mvaddstr(SCORE_Y
+ 6, SCORE_X
,
163 "| *.....:.....:.....:.....:.....:..... |");
164 mvaddstr(SCORE_Y
+ 7, SCORE_X
,
166 mvaddstr(SCORE_Y
+ 8, SCORE_X
,
167 "+---------------------------------------+");
173 * Print out the current game score
178 extern int Lastscore
[];
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
);
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.
202 if (gamecount
== 0) {
205 if (!rflag
) { /* player cuts deck */
206 msg(quiet
? "Cut for crib? " :
207 "Cut to see whose crib it is -- low card wins? ");
210 i
= (rand() >> 4) % CARDS
; /* random cut */
211 do { /* comp cuts deck */
212 j
= (rand() >> 4) % CARDS
;
214 addmsg(quiet
? "You cut " : "You cut the ");
215 msgcard(deck
[i
], FALSE
);
217 addmsg(quiet
? "I cut " : "I cut the ");
218 msgcard(deck
[j
], FALSE
);
220 flag
= (deck
[i
].rank
== deck
[j
].rank
);
222 msg(quiet
? "We tied..." :
223 "We tied and have to try again...");
227 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
237 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
245 flag
= !playhand(compcrib
);
246 compcrib
= !compcrib
;
249 if (cscore
< pscore
) {
250 if (glimit
- cscore
> 60) {
251 msg("YOU DOUBLE SKUNKED ME!");
254 if (glimit
- cscore
> 30) {
255 msg("YOU SKUNKED ME!");
263 if (glimit
- pscore
> 60) {
264 msg("I DOUBLE SKUNKED YOU!");
267 if (glimit
- pscore
> 30) {
268 msg("I SKUNKED YOU!");
281 * Do up one hand of the game
287 register int deckpos
;
292 deckpos
= deal(mycrib
);
293 sorthand(chand
, FULLHAND
);
294 sorthand(phand
, FULLHAND
);
295 makeknown(chand
, FULLHAND
);
296 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
298 if (cut(mycrib
, deckpos
))
310 * deal cards to both players from deck
318 for (i
= j
= 0; i
< FULLHAND
; i
++) {
320 phand
[i
] = deck
[j
++];
321 chand
[i
] = deck
[j
++];
323 chand
[i
] = deck
[j
++];
324 phand
[i
] = deck
[j
++];
332 * Handle players discarding into the crib...
333 * Note: we call cdiscard() after prining first message so player doesn't wait
339 register char *prompt
;
342 prcrib(mycrib
, TRUE
);
343 prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
344 cdiscard(mycrib
); /* puts best discard at end */
345 crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
346 cremove(crd
, phand
, FULLHAND
);
347 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
350 /* Next four lines same as last four except for cdiscard(). */
351 crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
352 cremove(crd
, phand
, FULLHAND
- 1);
353 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
357 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
362 * Cut the deck and set turnover. Actually, we only ASK the
363 * player what card to turn. We do a random one, anyway.
375 if (!rflag
) { /* random cut */
376 msg(quiet
? "Cut the deck? " :
377 "How many cards down do you wish to cut the deck? ");
380 i
= (rand() >> 4) % (CARDS
- pos
);
381 turnover
= deck
[i
+ pos
];
382 addmsg(quiet
? "You cut " : "You cut the ");
383 msgcard(turnover
, FALSE
);
385 if (turnover
.rank
== JACK
) {
386 msg("I get two for his heels");
387 win
= chkscr(&cscore
, 2);
390 i
= (rand() >> 4) % (CARDS
- pos
) + pos
;
392 addmsg(quiet
? "I cut " : "I cut the ");
393 msgcard(turnover
, FALSE
);
395 if (turnover
.rank
== JACK
) {
396 msg("You get two for his heels");
397 win
= chkscr(&pscore
, 2);
400 makeknown(&turnover
, 1);
401 prcrib(mycrib
, FALSE
);
407 * Print out the turnover card with crib indicator
410 prcrib(mycrib
, blank
)
411 BOOLEAN mycrib
, blank
;
413 register int y
, cardx
;
420 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
421 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
428 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
429 mvaddstr(y
, cardx
, " ");
434 * Handle all the pegging...
436 static CARD Table
[14];
443 static CARD ch
[CINHAND
], ph
[CINHAND
];
444 register int i
, j
, k
;
446 register int cnum
, pnum
, sum
;
447 register BOOLEAN myturn
, mego
, ugo
, last
, played
;
450 cnum
= pnum
= CINHAND
;
451 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
455 Tcnt
= 0; /* index to table of cards played */
456 sum
= 0; /* sum of cards played */
460 last
= TRUE
; /* enable last flag */
461 prhand(ph
, pnum
, Playwin
, FALSE
);
462 prhand(ch
, cnum
, Compwin
, TRUE
);
464 if (myturn
) { /* my tyrn to play */
465 if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
466 if (!mego
&& cnum
) { /* go for comp? */
470 /* can player move? */
471 if (anymove(ph
, pnum
, sum
))
473 else { /* give him his point */
474 msg(quiet
? "You get one" :
475 "You get one point");
476 if (chkscr(&pscore
, 1))
487 for (i
= 0; i
< cnum
; i
++) {
488 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
494 if (j
< 0) /* if nothing scores */
495 j
= cchose(ch
, cnum
, sum
);
497 cremove(crd
, ch
, cnum
--);
498 sum
+= VAL(crd
.rank
);
501 addmsg(quiet
? "I get %d playing " :
502 "I get %d points playing ", k
);
505 if (chkscr(&cscore
, k
))
511 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
512 if (!ugo
&& pnum
) { /* go for player */
513 msg("You have a GO");
516 /* can computer play? */
517 if (anymove(ch
, cnum
, sum
))
520 msg(quiet
? "I get one" :
523 if (chkscr(&cscore
, 1))
529 } else { /* player plays */
533 msg("You play your last card");
537 pnum
, Playwin
, FALSE
);
539 pnum
, "Your play: ")];
540 if (sum
+ VAL(crd
.rank
) <= 31)
543 msg("Total > 31 -- try again");
546 cremove(crd
, ph
, pnum
--);
547 i
= pegscore(crd
, Table
, Tcnt
, sum
);
548 sum
+= VAL(crd
.rank
);
551 msg(quiet
? "You got %d" :
552 "You got %d points", i
);
553 if (chkscr(&pscore
, i
))
565 last
= FALSE
; /* disable last flag */
568 break; /* both done */
570 prhand(ph
, pnum
, Playwin
, FALSE
);
571 prhand(ch
, cnum
, Compwin
, TRUE
);
575 msg(quiet
? "I get one for last" :
576 "I get one point for last");
578 if (chkscr(&cscore
, 1))
581 msg(quiet
? "You get one for last" :
582 "You get one point for last");
583 if (chkscr(&pscore
, 1))
591 * Print out the table with the current score
597 prhand(Table
, Tcnt
, Tablewin
, FALSE
);
598 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", score
);
604 * Handle the scoring of the hands
610 sorthand(crib
, CINHAND
);
612 if (plyrhand(phand
, "hand"))
614 if (comphand(chand
, "hand"))
617 if (comphand(crib
, "crib"))
620 if (comphand(chand
, "hand"))
622 if (plyrhand(phand
, "hand"))
624 if (plyrhand(crib
, "crib"))