]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/crib.c
1 /* $NetBSD: crib.c,v 1.22 2008/08/08 16:10:47 drochner 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.22 2008/08/08 16:10:47 drochner Exp $");
57 #include "pathnames.h"
60 main(int argc
, char *argv
[])
68 f
= fopen(_PATH_LOG
, "a");
70 warn("fopen %s", _PATH_LOG
);
71 if (f
!= NULL
&& fileno(f
) < 3)
74 /* Revoke setgid privileges */
77 /* Set close-on-exec flag on log file */
80 flags
= fcntl(fd
, F_GETFD
);
82 err(1, "fcntl F_GETFD");
84 if (fcntl(fd
, F_SETFD
, flags
) == -1)
85 err(1, "fcntl F_SETFD");
88 while ((ch
= getopt(argc
, argv
, "eqr")) != -1)
101 (void) fprintf(stderr
, "usage: cribbage [-eqr]\n");
106 errx(0, "couldn't initialize screen");
107 (void)signal(SIGINT
, receive_intr
);
111 Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
112 Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
113 Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
114 Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
115 leaveok(Playwin
, TRUE
);
116 leaveok(Tablewin
, TRUE
);
117 leaveok(Compwin
, TRUE
);
118 clearok(stdscr
, FALSE
);
121 msg("Do you need instructions for cribbage? ");
122 if (getuchar() == 'Y') {
125 mvcur(0, COLS
- 1, LINES
- 1, 0);
132 msg("For cribbage rules, use \"man cribbage\"");
138 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
140 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
142 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
144 msg("Another game? ");
145 playing
= (getuchar() == 'Y');
149 (void)fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
150 getlogin(), cgames
, pgames
);
159 * Print out the initial board on the screen
164 mvaddstr(SCORE_Y
+ 0, SCORE_X
,
165 "+---------------------------------------+");
166 mvaddstr(SCORE_Y
+ 1, SCORE_X
,
168 mvaddstr(SCORE_Y
+ 2, SCORE_X
,
169 "| *.....:.....:.....:.....:.....:..... |");
170 mvaddstr(SCORE_Y
+ 3, SCORE_X
,
171 "| *.....:.....:.....:.....:.....:..... |");
172 mvaddstr(SCORE_Y
+ 4, SCORE_X
,
174 mvaddstr(SCORE_Y
+ 5, SCORE_X
,
175 "| *.....:.....:.....:.....:.....:..... |");
176 mvaddstr(SCORE_Y
+ 6, SCORE_X
,
177 "| *.....:.....:.....:.....:.....:..... |");
178 mvaddstr(SCORE_Y
+ 7, SCORE_X
,
180 mvaddstr(SCORE_Y
+ 8, SCORE_X
,
181 "+---------------------------------------+");
187 * Print out the current game score
192 if (pgames
|| cgames
) {
193 mvprintw(SCORE_Y
+ 1, SCORE_X
+ 28, "Games: %3d", pgames
);
194 mvprintw(SCORE_Y
+ 7, SCORE_X
+ 28, "Games: %3d", cgames
);
202 * Play one game up to glimit points. Actually, we only ASK the
203 * player what card to turn. We do a random one, anyway.
215 if (gamecount
== 0) {
218 if (!rflag
) { /* player cuts deck */
219 msg(quiet
? "Cut for crib? " :
220 "Cut to see whose crib it is -- low card wins? ");
223 i
= (rand() >> 4) % CARDS
; /* random cut */
224 do { /* comp cuts deck */
225 j
= (rand() >> 4) % CARDS
;
227 addmsg(quiet
? "You cut " : "You cut the ");
228 msgcard(deck
[i
], FALSE
);
230 addmsg(quiet
? "I cut " : "I cut the ");
231 msgcard(deck
[j
], FALSE
);
233 flag
= (deck
[i
].rank
== deck
[j
].rank
);
235 msg(quiet
? "We tied..." :
236 "We tied and have to try again...");
240 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
253 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
261 flag
= !playhand(compcrib
);
262 compcrib
= !compcrib
;
265 if (cscore
< pscore
) {
266 if (glimit
- cscore
> 60) {
267 msg("YOU DOUBLE SKUNKED ME!");
270 if (glimit
- cscore
> 30) {
271 msg("YOU SKUNKED ME!");
279 if (glimit
- pscore
> 60) {
280 msg("I DOUBLE SKUNKED YOU!");
283 if (glimit
- pscore
> 30) {
284 msg("I SKUNKED YOU!");
297 * Do up one hand of the game
300 playhand(BOOLEAN mycrib
)
310 deckpos
= deal(mycrib
);
311 sorthand(chand
, FULLHAND
);
312 sorthand(phand
, FULLHAND
);
313 makeknown(chand
, FULLHAND
);
314 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
316 if (cut(mycrib
, deckpos
))
328 * deal cards to both players from deck
335 for (i
= j
= 0; i
< FULLHAND
; i
++) {
337 phand
[i
] = deck
[j
++];
338 chand
[i
] = deck
[j
++];
340 chand
[i
] = deck
[j
++];
341 phand
[i
] = deck
[j
++];
349 * Handle players discarding into the crib...
350 * Note: we call cdiscard() after prining first message so player doesn't wait
353 discard(BOOLEAN mycrib
)
358 prcrib(mycrib
, TRUE
);
359 prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
360 cdiscard(mycrib
); /* puts best discard at end */
361 crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
362 cremove(crd
, phand
, FULLHAND
);
363 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
366 /* Next four lines same as last four except for cdiscard(). */
367 crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
368 cremove(crd
, phand
, FULLHAND
- 1);
369 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
373 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
378 * Cut the deck and set turnover. Actually, we only ASK the
379 * player what card to turn. We do a random one, anyway.
382 cut(BOOLEAN mycrib
, int pos
)
389 if (!rflag
) { /* random cut */
390 msg(quiet
? "Cut the deck? " :
391 "How many cards down do you wish to cut the deck? ");
394 i
= (rand() >> 4) % (CARDS
- pos
);
395 turnover
= deck
[i
+ pos
];
396 addmsg(quiet
? "You cut " : "You cut the ");
397 msgcard(turnover
, FALSE
);
399 if (turnover
.rank
== JACK
) {
400 msg("I get two for his heels");
401 win
= chkscr(&cscore
, 2);
404 i
= (rand() >> 4) % (CARDS
- pos
) + pos
;
406 addmsg(quiet
? "I cut " : "I cut the ");
407 msgcard(turnover
, FALSE
);
409 if (turnover
.rank
== JACK
) {
410 msg("You get two for his heels");
411 win
= chkscr(&pscore
, 2);
414 makeknown(&turnover
, 1);
415 prcrib(mycrib
, FALSE
);
421 * Print out the turnover card with crib indicator
424 prcrib(BOOLEAN mycrib
, BOOLEAN blank
)
433 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
434 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
441 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
442 mvaddstr(y
, cardx
, " ");
448 * Handle all the pegging...
450 static CARD Table
[14];
456 static CARD ch
[CINHAND
], ph
[CINHAND
];
460 BOOLEAN myturn
, mego
, ugo
, last
, played
;
464 cnum
= pnum
= CINHAND
;
465 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
469 Tcnt
= 0; /* index to table of cards played */
470 sum
= 0; /* sum of cards played */
474 last
= TRUE
; /* enable last flag */
475 prhand(ph
, pnum
, Playwin
, FALSE
);
476 prhand(ch
, cnum
, Compwin
, TRUE
);
478 if (myturn
) { /* my tyrn to play */
479 if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
480 if (!mego
&& cnum
) { /* go for comp? */
484 /* can player move? */
485 if (anymove(ph
, pnum
, sum
))
487 else { /* give him his point */
488 msg(quiet
? "You get one" :
489 "You get one point");
491 if (chkscr(&pscore
, 1))
502 for (i
= 0; i
< cnum
; i
++) {
503 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
509 if (j
< 0) /* if nothing scores */
510 j
= cchose(ch
, cnum
, sum
);
512 cremove(crd
, ch
, cnum
--);
513 sum
+= VAL(crd
.rank
);
516 addmsg(quiet
? "I get %d playing " :
517 "I get %d points playing ", k
);
520 if (chkscr(&cscore
, k
))
526 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
527 if (!ugo
&& pnum
) { /* go for player */
528 msg("You have a GO");
531 /* can computer play? */
532 if (anymove(ch
, cnum
, sum
))
535 msg(quiet
? "I get one" :
538 if (chkscr(&cscore
, 1))
544 } else { /* player plays */
548 msg("You play your last card");
552 pnum
, Playwin
, FALSE
);
554 pnum
, "Your play: ")];
555 if (sum
+ VAL(crd
.rank
) <= 31)
558 msg("Total > 31 -- try again");
561 cremove(crd
, ph
, pnum
--);
562 i
= pegscore(crd
, Table
, Tcnt
, sum
);
563 sum
+= VAL(crd
.rank
);
566 msg(quiet
? "You got %d" :
567 "You got %d points", i
);
570 if (chkscr(&pscore
, i
))
582 last
= FALSE
; /* disable last flag */
585 break; /* both done */
587 prhand(ph
, pnum
, Playwin
, FALSE
);
588 prhand(ch
, cnum
, Compwin
, TRUE
);
592 msg(quiet
? "I get one for last" :
593 "I get one point for last");
595 if (chkscr(&cscore
, 1))
598 msg(quiet
? "You get one for last" :
599 "You get one point for last");
601 if (chkscr(&pscore
, 1))
610 * Print out the table with the current score
613 prtable(int curscore
)
615 prhand(Table
, Tcnt
, Tablewin
, FALSE
);
616 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", curscore
);
622 * Handle the scoring of the hands
625 score(BOOLEAN mycrib
)
627 sorthand(crib
, CINHAND
);
629 if (plyrhand(phand
, "hand"))
631 if (comphand(chand
, "hand"))
634 if (comphand(crib
, "crib"))
638 if (comphand(chand
, "hand"))
640 if (plyrhand(phand
, "hand"))
642 if (plyrhand(crib
, "crib"))