]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/crib.c
1 /* $NetBSD: crib.c,v 1.8 1997/10/10 12:32:24 lukem 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
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: crib.c,v 1.8 1997/10/10 12:32:24 lukem Exp $");
59 #include "pathnames.h"
61 int main
__P((int, char *[]));
72 while ((ch
= getopt(argc
, argv
, "eqr")) != -1)
85 (void) fprintf(stderr
, "usage: cribbage [-eqr]\n");
90 (void)signal(SIGINT
, rint
);
94 Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
95 Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
96 Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
97 Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
98 leaveok(Playwin
, TRUE
);
99 leaveok(Tablewin
, TRUE
);
100 leaveok(Compwin
, TRUE
);
101 clearok(stdscr
, FALSE
);
104 msg("Do you need instructions for cribbage? ");
105 if (getuchar() == 'Y') {
108 mvcur(0, COLS
- 1, LINES
- 1, 0);
115 msg("For cribbage rules, use \"man cribbage\"");
121 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
123 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
125 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
127 msg("Another game? ");
128 playing
= (getuchar() == 'Y');
131 if ((f
= fopen(_PATH_LOG
, "a")) != NULL
) {
132 (void)fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
133 getlogin(), cgames
, pgames
);
138 (void) fprintf(stderr
, "\ncribbage: can't open %s.\n",
147 * Print out the initial board on the screen
152 mvaddstr(SCORE_Y
+ 0, SCORE_X
,
153 "+---------------------------------------+");
154 mvaddstr(SCORE_Y
+ 1, SCORE_X
,
156 mvaddstr(SCORE_Y
+ 2, SCORE_X
,
157 "| *.....:.....:.....:.....:.....:..... |");
158 mvaddstr(SCORE_Y
+ 3, SCORE_X
,
159 "| *.....:.....:.....:.....:.....:..... |");
160 mvaddstr(SCORE_Y
+ 4, SCORE_X
,
162 mvaddstr(SCORE_Y
+ 5, SCORE_X
,
163 "| *.....:.....:.....:.....:.....:..... |");
164 mvaddstr(SCORE_Y
+ 6, SCORE_X
,
165 "| *.....:.....:.....:.....:.....:..... |");
166 mvaddstr(SCORE_Y
+ 7, SCORE_X
,
168 mvaddstr(SCORE_Y
+ 8, SCORE_X
,
169 "+---------------------------------------+");
175 * Print out the current game score
180 extern int Lastscore
[];
182 if (pgames
|| cgames
) {
183 mvprintw(SCORE_Y
+ 1, SCORE_X
+ 28, "Games: %3d", pgames
);
184 mvprintw(SCORE_Y
+ 7, SCORE_X
+ 28, "Games: %3d", cgames
);
192 * Play one game up to glimit points. Actually, we only ASK the
193 * player what card to turn. We do a random one, anyway.
205 if (gamecount
== 0) {
208 if (!rflag
) { /* player cuts deck */
209 msg(quiet
? "Cut for crib? " :
210 "Cut to see whose crib it is -- low card wins? ");
213 i
= (rand() >> 4) % CARDS
; /* random cut */
214 do { /* comp cuts deck */
215 j
= (rand() >> 4) % CARDS
;
217 addmsg(quiet
? "You cut " : "You cut the ");
218 msgcard(deck
[i
], FALSE
);
220 addmsg(quiet
? "I cut " : "I cut the ");
221 msgcard(deck
[j
], FALSE
);
223 flag
= (deck
[i
].rank
== deck
[j
].rank
);
225 msg(quiet
? "We tied..." :
226 "We tied and have to try again...");
230 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
243 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
251 flag
= !playhand(compcrib
);
252 compcrib
= !compcrib
;
255 if (cscore
< pscore
) {
256 if (glimit
- cscore
> 60) {
257 msg("YOU DOUBLE SKUNKED ME!");
260 if (glimit
- cscore
> 30) {
261 msg("YOU SKUNKED ME!");
269 if (glimit
- pscore
> 60) {
270 msg("I DOUBLE SKUNKED YOU!");
273 if (glimit
- pscore
> 30) {
274 msg("I SKUNKED YOU!");
287 * Do up one hand of the game
301 deckpos
= deal(mycrib
);
302 sorthand(chand
, FULLHAND
);
303 sorthand(phand
, FULLHAND
);
304 makeknown(chand
, FULLHAND
);
305 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
307 if (cut(mycrib
, deckpos
))
319 * deal cards to both players from deck
327 for (i
= j
= 0; i
< FULLHAND
; i
++) {
329 phand
[i
] = deck
[j
++];
330 chand
[i
] = deck
[j
++];
332 chand
[i
] = deck
[j
++];
333 phand
[i
] = deck
[j
++];
341 * Handle players discarding into the crib...
342 * Note: we call cdiscard() after prining first message so player doesn't wait
351 prcrib(mycrib
, TRUE
);
352 prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
353 cdiscard(mycrib
); /* puts best discard at end */
354 crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
355 cremove(crd
, phand
, FULLHAND
);
356 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
359 /* Next four lines same as last four except for cdiscard(). */
360 crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
361 cremove(crd
, phand
, FULLHAND
- 1);
362 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
366 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
371 * Cut the deck and set turnover. Actually, we only ASK the
372 * player what card to turn. We do a random one, anyway.
384 if (!rflag
) { /* random cut */
385 msg(quiet
? "Cut the deck? " :
386 "How many cards down do you wish to cut the deck? ");
389 i
= (rand() >> 4) % (CARDS
- pos
);
390 turnover
= deck
[i
+ pos
];
391 addmsg(quiet
? "You cut " : "You cut the ");
392 msgcard(turnover
, FALSE
);
394 if (turnover
.rank
== JACK
) {
395 msg("I get two for his heels");
396 win
= chkscr(&cscore
, 2);
399 i
= (rand() >> 4) % (CARDS
- pos
) + pos
;
401 addmsg(quiet
? "I cut " : "I cut the ");
402 msgcard(turnover
, FALSE
);
404 if (turnover
.rank
== JACK
) {
405 msg("You get two for his heels");
406 win
= chkscr(&pscore
, 2);
409 makeknown(&turnover
, 1);
410 prcrib(mycrib
, FALSE
);
416 * Print out the turnover card with crib indicator
419 prcrib(mycrib
, blank
)
420 BOOLEAN mycrib
, blank
;
429 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
430 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
437 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
438 mvaddstr(y
, cardx
, " ");
444 * Handle all the pegging...
446 static CARD Table
[14];
453 static CARD ch
[CINHAND
], ph
[CINHAND
];
457 BOOLEAN myturn
, mego
, ugo
, last
, played
;
461 cnum
= pnum
= CINHAND
;
462 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
466 Tcnt
= 0; /* index to table of cards played */
467 sum
= 0; /* sum of cards played */
471 last
= TRUE
; /* enable last flag */
472 prhand(ph
, pnum
, Playwin
, FALSE
);
473 prhand(ch
, cnum
, Compwin
, TRUE
);
475 if (myturn
) { /* my tyrn to play */
476 if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
477 if (!mego
&& cnum
) { /* go for comp? */
481 /* can player move? */
482 if (anymove(ph
, pnum
, sum
))
484 else { /* give him his point */
485 msg(quiet
? "You get one" :
486 "You get one point");
488 if (chkscr(&pscore
, 1))
499 for (i
= 0; i
< cnum
; i
++) {
500 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
506 if (j
< 0) /* if nothing scores */
507 j
= cchose(ch
, cnum
, sum
);
509 cremove(crd
, ch
, cnum
--);
510 sum
+= VAL(crd
.rank
);
513 addmsg(quiet
? "I get %d playing " :
514 "I get %d points playing ", k
);
517 if (chkscr(&cscore
, k
))
523 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
524 if (!ugo
&& pnum
) { /* go for player */
525 msg("You have a GO");
528 /* can computer play? */
529 if (anymove(ch
, cnum
, sum
))
532 msg(quiet
? "I get one" :
535 if (chkscr(&cscore
, 1))
541 } else { /* player plays */
545 msg("You play your last card");
549 pnum
, Playwin
, FALSE
);
551 pnum
, "Your play: ")];
552 if (sum
+ VAL(crd
.rank
) <= 31)
555 msg("Total > 31 -- try again");
558 cremove(crd
, ph
, pnum
--);
559 i
= pegscore(crd
, Table
, Tcnt
, sum
);
560 sum
+= VAL(crd
.rank
);
563 msg(quiet
? "You got %d" :
564 "You got %d points", i
);
567 if (chkscr(&pscore
, i
))
579 last
= FALSE
; /* disable last flag */
582 break; /* both done */
584 prhand(ph
, pnum
, Playwin
, FALSE
);
585 prhand(ch
, cnum
, Compwin
, TRUE
);
589 msg(quiet
? "I get one for last" :
590 "I get one point for last");
592 if (chkscr(&cscore
, 1))
595 msg(quiet
? "You get one for last" :
596 "You get one point for last");
598 if (chkscr(&pscore
, 1))
606 * Print out the table with the current score
612 prhand(Table
, Tcnt
, Tablewin
, FALSE
);
613 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", score
);
619 * Handle the scoring of the hands
625 sorthand(crib
, CINHAND
);
627 if (plyrhand(phand
, "hand"))
629 if (comphand(chand
, "hand"))
632 if (comphand(crib
, "crib"))
636 if (comphand(chand
, "hand"))
638 if (plyrhand(phand
, "hand"))
640 if (plyrhand(crib
, "crib"))