]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/crib.c
1 /* $NetBSD: crib.c,v 1.16 2001/12/06 11:59:45 blymn 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.16 2001/12/06 11:59:45 blymn Exp $");
61 #include "pathnames.h"
63 int main
__P((int, char *[]));
76 f
= fopen(_PATH_LOG
, "a");
78 warn("fopen %s", _PATH_LOG
);
79 if (f
!= NULL
&& fileno(f
) < 3)
82 /* Revoke setgid privileges */
85 /* Set close-on-exec flag on log file */
88 flags
= fcntl(fd
, F_GETFD
);
90 err(1, "fcntl F_GETFD");
92 if (fcntl(fd
, F_SETFD
, flags
) == -1)
93 err(1, "fcntl F_SETFD");
96 while ((ch
= getopt(argc
, argv
, "eqr")) != -1)
109 (void) fprintf(stderr
, "usage: cribbage [-eqr]\n");
114 (void)signal(SIGINT
, rint
);
118 Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
119 Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
120 Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
121 Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
122 leaveok(Playwin
, TRUE
);
123 leaveok(Tablewin
, TRUE
);
124 leaveok(Compwin
, TRUE
);
125 clearok(stdscr
, FALSE
);
128 msg("Do you need instructions for cribbage? ");
129 if (getuchar() == 'Y') {
132 mvcur(0, COLS
- 1, LINES
- 1, 0);
139 msg("For cribbage rules, use \"man cribbage\"");
145 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
147 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
149 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
151 msg("Another game? ");
152 playing
= (getuchar() == 'Y');
156 (void)fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
157 getlogin(), cgames
, pgames
);
166 * Print out the initial board on the screen
171 mvaddstr(SCORE_Y
+ 0, SCORE_X
,
172 "+---------------------------------------+");
173 mvaddstr(SCORE_Y
+ 1, SCORE_X
,
175 mvaddstr(SCORE_Y
+ 2, SCORE_X
,
176 "| *.....:.....:.....:.....:.....:..... |");
177 mvaddstr(SCORE_Y
+ 3, SCORE_X
,
178 "| *.....:.....:.....:.....:.....:..... |");
179 mvaddstr(SCORE_Y
+ 4, SCORE_X
,
181 mvaddstr(SCORE_Y
+ 5, SCORE_X
,
182 "| *.....:.....:.....:.....:.....:..... |");
183 mvaddstr(SCORE_Y
+ 6, SCORE_X
,
184 "| *.....:.....:.....:.....:.....:..... |");
185 mvaddstr(SCORE_Y
+ 7, SCORE_X
,
187 mvaddstr(SCORE_Y
+ 8, SCORE_X
,
188 "+---------------------------------------+");
194 * Print out the current game score
199 if (pgames
|| cgames
) {
200 mvprintw(SCORE_Y
+ 1, SCORE_X
+ 28, "Games: %3d", pgames
);
201 mvprintw(SCORE_Y
+ 7, SCORE_X
+ 28, "Games: %3d", cgames
);
209 * Play one game up to glimit points. Actually, we only ASK the
210 * player what card to turn. We do a random one, anyway.
222 if (gamecount
== 0) {
225 if (!rflag
) { /* player cuts deck */
226 msg(quiet
? "Cut for crib? " :
227 "Cut to see whose crib it is -- low card wins? ");
230 i
= (rand() >> 4) % CARDS
; /* random cut */
231 do { /* comp cuts deck */
232 j
= (rand() >> 4) % CARDS
;
234 addmsg(quiet
? "You cut " : "You cut the ");
235 msgcard(deck
[i
], FALSE
);
237 addmsg(quiet
? "I cut " : "I cut the ");
238 msgcard(deck
[j
], FALSE
);
240 flag
= (deck
[i
].rank
== deck
[j
].rank
);
242 msg(quiet
? "We tied..." :
243 "We tied and have to try again...");
247 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
260 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
268 flag
= !playhand(compcrib
);
269 compcrib
= !compcrib
;
272 if (cscore
< pscore
) {
273 if (glimit
- cscore
> 60) {
274 msg("YOU DOUBLE SKUNKED ME!");
277 if (glimit
- cscore
> 30) {
278 msg("YOU SKUNKED ME!");
286 if (glimit
- pscore
> 60) {
287 msg("I DOUBLE SKUNKED YOU!");
290 if (glimit
- pscore
> 30) {
291 msg("I SKUNKED YOU!");
304 * Do up one hand of the game
318 deckpos
= deal(mycrib
);
319 sorthand(chand
, FULLHAND
);
320 sorthand(phand
, FULLHAND
);
321 makeknown(chand
, FULLHAND
);
322 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
324 if (cut(mycrib
, deckpos
))
336 * deal cards to both players from deck
344 for (i
= j
= 0; i
< FULLHAND
; i
++) {
346 phand
[i
] = deck
[j
++];
347 chand
[i
] = deck
[j
++];
349 chand
[i
] = deck
[j
++];
350 phand
[i
] = deck
[j
++];
358 * Handle players discarding into the crib...
359 * Note: we call cdiscard() after prining first message so player doesn't wait
368 prcrib(mycrib
, TRUE
);
369 prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
370 cdiscard(mycrib
); /* puts best discard at end */
371 crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
372 cremove(crd
, phand
, FULLHAND
);
373 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
376 /* Next four lines same as last four except for cdiscard(). */
377 crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
378 cremove(crd
, phand
, FULLHAND
- 1);
379 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
383 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
388 * Cut the deck and set turnover. Actually, we only ASK the
389 * player what card to turn. We do a random one, anyway.
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(mycrib
, blank
)
437 BOOLEAN mycrib
, blank
;
446 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
447 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
454 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
455 mvaddstr(y
, cardx
, " ");
461 * Handle all the pegging...
463 static CARD Table
[14];
470 static CARD ch
[CINHAND
], ph
[CINHAND
];
474 BOOLEAN myturn
, mego
, ugo
, last
, played
;
478 cnum
= pnum
= CINHAND
;
479 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
483 Tcnt
= 0; /* index to table of cards played */
484 sum
= 0; /* sum of cards played */
488 last
= TRUE
; /* enable last flag */
489 prhand(ph
, pnum
, Playwin
, FALSE
);
490 prhand(ch
, cnum
, Compwin
, TRUE
);
492 if (myturn
) { /* my tyrn to play */
493 if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
494 if (!mego
&& cnum
) { /* go for comp? */
498 /* can player move? */
499 if (anymove(ph
, pnum
, sum
))
501 else { /* give him his point */
502 msg(quiet
? "You get one" :
503 "You get one point");
505 if (chkscr(&pscore
, 1))
516 for (i
= 0; i
< cnum
; i
++) {
517 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
523 if (j
< 0) /* if nothing scores */
524 j
= cchose(ch
, cnum
, sum
);
526 cremove(crd
, ch
, cnum
--);
527 sum
+= VAL(crd
.rank
);
530 addmsg(quiet
? "I get %d playing " :
531 "I get %d points playing ", k
);
534 if (chkscr(&cscore
, k
))
540 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
541 if (!ugo
&& pnum
) { /* go for player */
542 msg("You have a GO");
545 /* can computer play? */
546 if (anymove(ch
, cnum
, sum
))
549 msg(quiet
? "I get one" :
552 if (chkscr(&cscore
, 1))
558 } else { /* player plays */
562 msg("You play your last card");
566 pnum
, Playwin
, FALSE
);
568 pnum
, "Your play: ")];
569 if (sum
+ VAL(crd
.rank
) <= 31)
572 msg("Total > 31 -- try again");
575 cremove(crd
, ph
, pnum
--);
576 i
= pegscore(crd
, Table
, Tcnt
, sum
);
577 sum
+= VAL(crd
.rank
);
580 msg(quiet
? "You got %d" :
581 "You got %d points", i
);
584 if (chkscr(&pscore
, i
))
596 last
= FALSE
; /* disable last flag */
599 break; /* both done */
601 prhand(ph
, pnum
, Playwin
, FALSE
);
602 prhand(ch
, cnum
, Compwin
, TRUE
);
606 msg(quiet
? "I get one for last" :
607 "I get one point for last");
609 if (chkscr(&cscore
, 1))
612 msg(quiet
? "You get one for last" :
613 "You get one point for last");
615 if (chkscr(&pscore
, 1))
624 * Print out the table with the current score
630 prhand(Table
, Tcnt
, Tablewin
, FALSE
);
631 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", score
);
637 * Handle the scoring of the hands
643 sorthand(crib
, CINHAND
);
645 if (plyrhand(phand
, "hand"))
647 if (comphand(chand
, "hand"))
650 if (comphand(crib
, "crib"))
654 if (comphand(chand
, "hand"))
656 if (plyrhand(phand
, "hand"))
658 if (plyrhand(crib
, "crib"))