1 /* $NetBSD: crib.c,v 1.23 2009/07/13 19:05:40 roy 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)crib.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: crib.c,v 1.23 2009/07/13 19:05:40 roy Exp $");
57 #include "pathnames.h"
59 static void makeboard(void);
60 static void gamescore(void);
61 static void game(void);
62 static int playhand(BOOLEAN
);
63 static int deal(BOOLEAN
);
64 static void discard(BOOLEAN
);
65 static int cut(BOOLEAN
, int);
66 static void prcrib(BOOLEAN
, BOOLEAN
);
67 static int peg(BOOLEAN
);
68 static void prtable(int);
69 static int score(BOOLEAN
);
72 main(int argc
, char *argv
[])
80 f
= fopen(_PATH_LOG
, "a");
82 warn("fopen %s", _PATH_LOG
);
83 if (f
!= NULL
&& fileno(f
) < 3)
86 /* Revoke setgid privileges */
89 /* Set close-on-exec flag on log file */
92 flags
= fcntl(fd
, F_GETFD
);
94 err(1, "fcntl F_GETFD");
96 if (fcntl(fd
, F_SETFD
, flags
) == -1)
97 err(1, "fcntl F_SETFD");
100 while ((ch
= getopt(argc
, argv
, "eqr")) != -1)
113 (void) fprintf(stderr
, "usage: cribbage [-eqr]\n");
118 errx(0, "couldn't initialize screen");
119 (void)signal(SIGINT
, receive_intr
);
123 Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
124 Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
125 Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
126 Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
127 leaveok(Playwin
, TRUE
);
128 leaveok(Tablewin
, TRUE
);
129 leaveok(Compwin
, TRUE
);
130 clearok(stdscr
, FALSE
);
133 msg("Do you need instructions for cribbage? ");
134 if (getuchar() == 'Y') {
137 mvcur(0, COLS
- 1, LINES
- 1, 0);
144 msg("For cribbage rules, use \"man cribbage\"");
150 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
152 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
154 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
156 msg("Another game? ");
157 playing
= (getuchar() == 'Y');
161 (void)fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
162 getlogin(), cgames
, pgames
);
171 * Print out the initial board on the screen
176 mvaddstr(SCORE_Y
+ 0, SCORE_X
,
177 "+---------------------------------------+");
178 mvaddstr(SCORE_Y
+ 1, SCORE_X
,
180 mvaddstr(SCORE_Y
+ 2, SCORE_X
,
181 "| *.....:.....:.....:.....:.....:..... |");
182 mvaddstr(SCORE_Y
+ 3, SCORE_X
,
183 "| *.....:.....:.....:.....:.....:..... |");
184 mvaddstr(SCORE_Y
+ 4, SCORE_X
,
186 mvaddstr(SCORE_Y
+ 5, SCORE_X
,
187 "| *.....:.....:.....:.....:.....:..... |");
188 mvaddstr(SCORE_Y
+ 6, SCORE_X
,
189 "| *.....:.....:.....:.....:.....:..... |");
190 mvaddstr(SCORE_Y
+ 7, SCORE_X
,
192 mvaddstr(SCORE_Y
+ 8, SCORE_X
,
193 "+---------------------------------------+");
199 * Print out the current game score
204 if (pgames
|| cgames
) {
205 mvprintw(SCORE_Y
+ 1, SCORE_X
+ 28, "Games: %3d", pgames
);
206 mvprintw(SCORE_Y
+ 7, SCORE_X
+ 28, "Games: %3d", cgames
);
214 * Play one game up to glimit points. Actually, we only ASK the
215 * player what card to turn. We do a random one, anyway.
227 if (gamecount
== 0) {
230 if (!rflag
) { /* player cuts deck */
231 msg(quiet
? "Cut for crib? " :
232 "Cut to see whose crib it is -- low card wins? ");
235 i
= (rand() >> 4) % CARDS
; /* random cut */
236 do { /* comp cuts deck */
237 j
= (rand() >> 4) % CARDS
;
239 addmsg(quiet
? "You cut " : "You cut the ");
240 msgcard(deck
[i
], FALSE
);
242 addmsg(quiet
? "I cut " : "I cut the ");
243 msgcard(deck
[j
], FALSE
);
245 flag
= (deck
[i
].rank
== deck
[j
].rank
);
247 msg(quiet
? "We tied..." :
248 "We tied and have to try again...");
252 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
265 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
273 flag
= !playhand(compcrib
);
274 compcrib
= !compcrib
;
277 if (cscore
< pscore
) {
278 if (glimit
- cscore
> 60) {
279 msg("YOU DOUBLE SKUNKED ME!");
282 if (glimit
- cscore
> 30) {
283 msg("YOU SKUNKED ME!");
291 if (glimit
- pscore
> 60) {
292 msg("I DOUBLE SKUNKED YOU!");
295 if (glimit
- pscore
> 30) {
296 msg("I SKUNKED YOU!");
309 * Do up one hand of the game
312 playhand(BOOLEAN mycrib
)
322 deckpos
= deal(mycrib
);
323 sorthand(chand
, FULLHAND
);
324 sorthand(phand
, FULLHAND
);
325 makeknown(chand
, FULLHAND
);
326 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
328 if (cut(mycrib
, deckpos
))
340 * deal cards to both players from deck
347 for (i
= j
= 0; i
< FULLHAND
; i
++) {
349 phand
[i
] = deck
[j
++];
350 chand
[i
] = deck
[j
++];
352 chand
[i
] = deck
[j
++];
353 phand
[i
] = deck
[j
++];
361 * Handle players discarding into the crib...
362 * Note: we call cdiscard() after prining first message so player doesn't wait
365 discard(BOOLEAN mycrib
)
370 prcrib(mycrib
, TRUE
);
371 prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
372 cdiscard(mycrib
); /* puts best discard at end */
373 crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
374 cremove(crd
, phand
, FULLHAND
);
375 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
378 /* Next four lines same as last four except for cdiscard(). */
379 crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
380 cremove(crd
, phand
, FULLHAND
- 1);
381 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
385 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
390 * Cut the deck and set turnover. Actually, we only ASK the
391 * player what card to turn. We do a random one, anyway.
394 cut(BOOLEAN mycrib
, int pos
)
401 if (!rflag
) { /* random cut */
402 msg(quiet
? "Cut the deck? " :
403 "How many cards down do you wish to cut the deck? ");
406 i
= (rand() >> 4) % (CARDS
- pos
);
407 turnover
= deck
[i
+ pos
];
408 addmsg(quiet
? "You cut " : "You cut the ");
409 msgcard(turnover
, FALSE
);
411 if (turnover
.rank
== JACK
) {
412 msg("I get two for his heels");
413 win
= chkscr(&cscore
, 2);
416 i
= (rand() >> 4) % (CARDS
- pos
) + pos
;
418 addmsg(quiet
? "I cut " : "I cut the ");
419 msgcard(turnover
, FALSE
);
421 if (turnover
.rank
== JACK
) {
422 msg("You get two for his heels");
423 win
= chkscr(&pscore
, 2);
426 makeknown(&turnover
, 1);
427 prcrib(mycrib
, FALSE
);
433 * Print out the turnover card with crib indicator
436 prcrib(BOOLEAN mycrib
, BOOLEAN blank
)
445 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
446 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
453 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
454 mvaddstr(y
, cardx
, " ");
460 * Handle all the pegging...
462 static CARD Table
[14];
468 static CARD ch
[CINHAND
], ph
[CINHAND
];
472 BOOLEAN myturn
, mego
, ugo
, last
, played
;
476 cnum
= pnum
= CINHAND
;
477 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
481 Tcnt
= 0; /* index to table of cards played */
482 sum
= 0; /* sum of cards played */
486 last
= TRUE
; /* enable last flag */
487 prhand(ph
, pnum
, Playwin
, FALSE
);
488 prhand(ch
, cnum
, Compwin
, TRUE
);
490 if (myturn
) { /* my tyrn to play */
491 if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
492 if (!mego
&& cnum
) { /* go for comp? */
496 /* can player move? */
497 if (anymove(ph
, pnum
, sum
))
499 else { /* give him his point */
500 msg(quiet
? "You get one" :
501 "You get one point");
503 if (chkscr(&pscore
, 1))
514 for (i
= 0; i
< cnum
; i
++) {
515 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
521 if (j
< 0) /* if nothing scores */
522 j
= cchose(ch
, cnum
, sum
);
524 cremove(crd
, ch
, cnum
--);
525 sum
+= VAL(crd
.rank
);
528 addmsg(quiet
? "I get %d playing " :
529 "I get %d points playing ", k
);
532 if (chkscr(&cscore
, k
))
538 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
539 if (!ugo
&& pnum
) { /* go for player */
540 msg("You have a GO");
543 /* can computer play? */
544 if (anymove(ch
, cnum
, sum
))
547 msg(quiet
? "I get one" :
550 if (chkscr(&cscore
, 1))
556 } else { /* player plays */
560 msg("You play your last card");
564 pnum
, Playwin
, FALSE
);
566 pnum
, "Your play: ")];
567 if (sum
+ VAL(crd
.rank
) <= 31)
570 msg("Total > 31 -- try again");
573 cremove(crd
, ph
, pnum
--);
574 i
= pegscore(crd
, Table
, Tcnt
, sum
);
575 sum
+= VAL(crd
.rank
);
578 msg(quiet
? "You got %d" :
579 "You got %d points", i
);
582 if (chkscr(&pscore
, i
))
594 last
= FALSE
; /* disable last flag */
597 break; /* both done */
599 prhand(ph
, pnum
, Playwin
, FALSE
);
600 prhand(ch
, cnum
, Compwin
, TRUE
);
604 msg(quiet
? "I get one for last" :
605 "I get one point for last");
607 if (chkscr(&cscore
, 1))
610 msg(quiet
? "You get one for last" :
611 "You get one point for last");
613 if (chkscr(&pscore
, 1))
622 * Print out the table with the current score
625 prtable(int curscore
)
627 prhand(Table
, Tcnt
, Tablewin
, FALSE
);
628 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", curscore
);
634 * Handle the scoring of the hands
637 score(BOOLEAN mycrib
)
639 sorthand(crib
, CINHAND
);
641 if (plyrhand(phand
, "hand"))
643 if (comphand(chand
, "hand"))
646 if (comphand(crib
, "crib"))
650 if (comphand(chand
, "hand"))
652 if (plyrhand(phand
, "hand"))
654 if (plyrhand(crib
, "crib"))