]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/crib.c
1 /* $NetBSD: crib.c,v 1.9 1997/10/11 02:44:30 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.9 1997/10/11 02:44:30 lukem Exp $");
60 #include "pathnames.h"
62 int main
__P((int, char *[]));
73 while ((ch
= getopt(argc
, argv
, "eqr")) != -1)
86 (void) fprintf(stderr
, "usage: cribbage [-eqr]\n");
91 (void)signal(SIGINT
, rint
);
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
);
105 msg("Do you need instructions for cribbage? ");
106 if (getuchar() == 'Y') {
109 mvcur(0, COLS
- 1, LINES
- 1, 0);
116 msg("For cribbage rules, use \"man cribbage\"");
122 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
124 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
126 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
128 msg("Another game? ");
129 playing
= (getuchar() == 'Y');
132 if ((f
= fopen(_PATH_LOG
, "a")) != NULL
) {
133 (void)fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
134 getlogin(), cgames
, pgames
);
139 errx(1, "can't open %s", _PATH_LOG
);
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.
203 if (gamecount
== 0) {
206 if (!rflag
) { /* player cuts deck */
207 msg(quiet
? "Cut for crib? " :
208 "Cut to see whose crib it is -- low card wins? ");
211 i
= (rand() >> 4) % CARDS
; /* random cut */
212 do { /* comp cuts deck */
213 j
= (rand() >> 4) % CARDS
;
215 addmsg(quiet
? "You cut " : "You cut the ");
216 msgcard(deck
[i
], FALSE
);
218 addmsg(quiet
? "I cut " : "I cut the ");
219 msgcard(deck
[j
], FALSE
);
221 flag
= (deck
[i
].rank
== deck
[j
].rank
);
223 msg(quiet
? "We tied..." :
224 "We tied and have to try again...");
228 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
241 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
249 flag
= !playhand(compcrib
);
250 compcrib
= !compcrib
;
253 if (cscore
< pscore
) {
254 if (glimit
- cscore
> 60) {
255 msg("YOU DOUBLE SKUNKED ME!");
258 if (glimit
- cscore
> 30) {
259 msg("YOU SKUNKED ME!");
267 if (glimit
- pscore
> 60) {
268 msg("I DOUBLE SKUNKED YOU!");
271 if (glimit
- pscore
> 30) {
272 msg("I SKUNKED YOU!");
285 * Do up one hand of the game
299 deckpos
= deal(mycrib
);
300 sorthand(chand
, FULLHAND
);
301 sorthand(phand
, FULLHAND
);
302 makeknown(chand
, FULLHAND
);
303 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
305 if (cut(mycrib
, deckpos
))
317 * deal cards to both players from deck
325 for (i
= j
= 0; i
< FULLHAND
; i
++) {
327 phand
[i
] = deck
[j
++];
328 chand
[i
] = deck
[j
++];
330 chand
[i
] = deck
[j
++];
331 phand
[i
] = deck
[j
++];
339 * Handle players discarding into the crib...
340 * Note: we call cdiscard() after prining first message so player doesn't wait
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
);
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
);
364 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
369 * Cut the deck and set turnover. Actually, we only ASK the
370 * player what card to turn. We do a random one, anyway.
382 if (!rflag
) { /* random cut */
383 msg(quiet
? "Cut the deck? " :
384 "How many cards down do you wish to cut the deck? ");
387 i
= (rand() >> 4) % (CARDS
- pos
);
388 turnover
= deck
[i
+ pos
];
389 addmsg(quiet
? "You cut " : "You cut the ");
390 msgcard(turnover
, FALSE
);
392 if (turnover
.rank
== JACK
) {
393 msg("I get two for his heels");
394 win
= chkscr(&cscore
, 2);
397 i
= (rand() >> 4) % (CARDS
- pos
) + pos
;
399 addmsg(quiet
? "I cut " : "I cut the ");
400 msgcard(turnover
, FALSE
);
402 if (turnover
.rank
== JACK
) {
403 msg("You get two for his heels");
404 win
= chkscr(&pscore
, 2);
407 makeknown(&turnover
, 1);
408 prcrib(mycrib
, FALSE
);
414 * Print out the turnover card with crib indicator
417 prcrib(mycrib
, blank
)
418 BOOLEAN mycrib
, blank
;
427 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
428 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
435 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
436 mvaddstr(y
, cardx
, " ");
442 * Handle all the pegging...
444 static CARD Table
[14];
451 static CARD ch
[CINHAND
], ph
[CINHAND
];
455 BOOLEAN myturn
, mego
, ugo
, last
, played
;
459 cnum
= pnum
= CINHAND
;
460 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
464 Tcnt
= 0; /* index to table of cards played */
465 sum
= 0; /* sum of cards played */
469 last
= TRUE
; /* enable last flag */
470 prhand(ph
, pnum
, Playwin
, FALSE
);
471 prhand(ch
, cnum
, Compwin
, TRUE
);
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? */
479 /* can player move? */
480 if (anymove(ph
, pnum
, sum
))
482 else { /* give him his point */
483 msg(quiet
? "You get one" :
484 "You get one point");
486 if (chkscr(&pscore
, 1))
497 for (i
= 0; i
< cnum
; i
++) {
498 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
504 if (j
< 0) /* if nothing scores */
505 j
= cchose(ch
, cnum
, sum
);
507 cremove(crd
, ch
, cnum
--);
508 sum
+= VAL(crd
.rank
);
511 addmsg(quiet
? "I get %d playing " :
512 "I get %d points playing ", k
);
515 if (chkscr(&cscore
, k
))
521 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
522 if (!ugo
&& pnum
) { /* go for player */
523 msg("You have a GO");
526 /* can computer play? */
527 if (anymove(ch
, cnum
, sum
))
530 msg(quiet
? "I get one" :
533 if (chkscr(&cscore
, 1))
539 } else { /* player plays */
543 msg("You play your last card");
547 pnum
, Playwin
, FALSE
);
549 pnum
, "Your play: ")];
550 if (sum
+ VAL(crd
.rank
) <= 31)
553 msg("Total > 31 -- try again");
556 cremove(crd
, ph
, pnum
--);
557 i
= pegscore(crd
, Table
, Tcnt
, sum
);
558 sum
+= VAL(crd
.rank
);
561 msg(quiet
? "You got %d" :
562 "You got %d points", i
);
565 if (chkscr(&pscore
, i
))
577 last
= FALSE
; /* disable last flag */
580 break; /* both done */
582 prhand(ph
, pnum
, Playwin
, FALSE
);
583 prhand(ch
, cnum
, Compwin
, TRUE
);
587 msg(quiet
? "I get one for last" :
588 "I get one point for last");
590 if (chkscr(&cscore
, 1))
593 msg(quiet
? "You get one for last" :
594 "You get one point for last");
596 if (chkscr(&pscore
, 1))
604 * Print out the table with the current score
610 prhand(Table
, Tcnt
, Tablewin
, FALSE
);
611 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", score
);
617 * Handle the scoring of the hands
623 sorthand(crib
, CINHAND
);
625 if (plyrhand(phand
, "hand"))
627 if (comphand(chand
, "hand"))
630 if (comphand(crib
, "crib"))
634 if (comphand(chand
, "hand"))
636 if (plyrhand(phand
, "hand"))
638 if (plyrhand(crib
, "crib"))