]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/crib.c
1 /* $NetBSD: crib.c,v 1.20 2005/07/02 08:32:32 jmc 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\n\
35 The Regents of the University of California. All rights reserved.\n");
40 static char sccsid
[] = "@(#)crib.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: crib.c,v 1.20 2005/07/02 08:32:32 jmc 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 (void)signal(SIGINT
, receive_intr
);
110 Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
111 Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
112 Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
113 Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
114 leaveok(Playwin
, TRUE
);
115 leaveok(Tablewin
, TRUE
);
116 leaveok(Compwin
, TRUE
);
117 clearok(stdscr
, FALSE
);
120 msg("Do you need instructions for cribbage? ");
121 if (getuchar() == 'Y') {
124 mvcur(0, COLS
- 1, LINES
- 1, 0);
131 msg("For cribbage rules, use \"man cribbage\"");
137 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
139 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
141 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
143 msg("Another game? ");
144 playing
= (getuchar() == 'Y');
148 (void)fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
149 getlogin(), cgames
, pgames
);
158 * Print out the initial board on the screen
163 mvaddstr(SCORE_Y
+ 0, SCORE_X
,
164 "+---------------------------------------+");
165 mvaddstr(SCORE_Y
+ 1, SCORE_X
,
167 mvaddstr(SCORE_Y
+ 2, SCORE_X
,
168 "| *.....:.....:.....:.....:.....:..... |");
169 mvaddstr(SCORE_Y
+ 3, SCORE_X
,
170 "| *.....:.....:.....:.....:.....:..... |");
171 mvaddstr(SCORE_Y
+ 4, SCORE_X
,
173 mvaddstr(SCORE_Y
+ 5, SCORE_X
,
174 "| *.....:.....:.....:.....:.....:..... |");
175 mvaddstr(SCORE_Y
+ 6, SCORE_X
,
176 "| *.....:.....:.....:.....:.....:..... |");
177 mvaddstr(SCORE_Y
+ 7, SCORE_X
,
179 mvaddstr(SCORE_Y
+ 8, SCORE_X
,
180 "+---------------------------------------+");
186 * Print out the current game score
191 if (pgames
|| cgames
) {
192 mvprintw(SCORE_Y
+ 1, SCORE_X
+ 28, "Games: %3d", pgames
);
193 mvprintw(SCORE_Y
+ 7, SCORE_X
+ 28, "Games: %3d", cgames
);
201 * Play one game up to glimit points. Actually, we only ASK the
202 * player what card to turn. We do a random one, anyway.
214 if (gamecount
== 0) {
217 if (!rflag
) { /* player cuts deck */
218 msg(quiet
? "Cut for crib? " :
219 "Cut to see whose crib it is -- low card wins? ");
222 i
= (rand() >> 4) % CARDS
; /* random cut */
223 do { /* comp cuts deck */
224 j
= (rand() >> 4) % CARDS
;
226 addmsg(quiet
? "You cut " : "You cut the ");
227 msgcard(deck
[i
], FALSE
);
229 addmsg(quiet
? "I cut " : "I cut the ");
230 msgcard(deck
[j
], FALSE
);
232 flag
= (deck
[i
].rank
== deck
[j
].rank
);
234 msg(quiet
? "We tied..." :
235 "We tied and have to try again...");
239 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
252 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
260 flag
= !playhand(compcrib
);
261 compcrib
= !compcrib
;
264 if (cscore
< pscore
) {
265 if (glimit
- cscore
> 60) {
266 msg("YOU DOUBLE SKUNKED ME!");
269 if (glimit
- cscore
> 30) {
270 msg("YOU SKUNKED ME!");
278 if (glimit
- pscore
> 60) {
279 msg("I DOUBLE SKUNKED YOU!");
282 if (glimit
- pscore
> 30) {
283 msg("I SKUNKED YOU!");
296 * Do up one hand of the game
299 playhand(BOOLEAN mycrib
)
309 deckpos
= deal(mycrib
);
310 sorthand(chand
, FULLHAND
);
311 sorthand(phand
, FULLHAND
);
312 makeknown(chand
, FULLHAND
);
313 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
315 if (cut(mycrib
, deckpos
))
327 * deal cards to both players from deck
334 for (i
= j
= 0; i
< FULLHAND
; i
++) {
336 phand
[i
] = deck
[j
++];
337 chand
[i
] = deck
[j
++];
339 chand
[i
] = deck
[j
++];
340 phand
[i
] = deck
[j
++];
348 * Handle players discarding into the crib...
349 * Note: we call cdiscard() after prining first message so player doesn't wait
352 discard(BOOLEAN mycrib
)
357 prcrib(mycrib
, TRUE
);
358 prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
359 cdiscard(mycrib
); /* puts best discard at end */
360 crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
361 cremove(crd
, phand
, FULLHAND
);
362 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
365 /* Next four lines same as last four except for cdiscard(). */
366 crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
367 cremove(crd
, phand
, FULLHAND
- 1);
368 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
372 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
377 * Cut the deck and set turnover. Actually, we only ASK the
378 * player what card to turn. We do a random one, anyway.
381 cut(BOOLEAN mycrib
, int pos
)
388 if (!rflag
) { /* random cut */
389 msg(quiet
? "Cut the deck? " :
390 "How many cards down do you wish to cut the deck? ");
393 i
= (rand() >> 4) % (CARDS
- pos
);
394 turnover
= deck
[i
+ pos
];
395 addmsg(quiet
? "You cut " : "You cut the ");
396 msgcard(turnover
, FALSE
);
398 if (turnover
.rank
== JACK
) {
399 msg("I get two for his heels");
400 win
= chkscr(&cscore
, 2);
403 i
= (rand() >> 4) % (CARDS
- pos
) + pos
;
405 addmsg(quiet
? "I cut " : "I cut the ");
406 msgcard(turnover
, FALSE
);
408 if (turnover
.rank
== JACK
) {
409 msg("You get two for his heels");
410 win
= chkscr(&pscore
, 2);
413 makeknown(&turnover
, 1);
414 prcrib(mycrib
, FALSE
);
420 * Print out the turnover card with crib indicator
423 prcrib(BOOLEAN mycrib
, BOOLEAN blank
)
432 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
433 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
440 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
441 mvaddstr(y
, cardx
, " ");
447 * Handle all the pegging...
449 static CARD Table
[14];
455 static CARD ch
[CINHAND
], ph
[CINHAND
];
459 BOOLEAN myturn
, mego
, ugo
, last
, played
;
463 cnum
= pnum
= CINHAND
;
464 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
468 Tcnt
= 0; /* index to table of cards played */
469 sum
= 0; /* sum of cards played */
473 last
= TRUE
; /* enable last flag */
474 prhand(ph
, pnum
, Playwin
, FALSE
);
475 prhand(ch
, cnum
, Compwin
, TRUE
);
477 if (myturn
) { /* my tyrn to play */
478 if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
479 if (!mego
&& cnum
) { /* go for comp? */
483 /* can player move? */
484 if (anymove(ph
, pnum
, sum
))
486 else { /* give him his point */
487 msg(quiet
? "You get one" :
488 "You get one point");
490 if (chkscr(&pscore
, 1))
501 for (i
= 0; i
< cnum
; i
++) {
502 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
508 if (j
< 0) /* if nothing scores */
509 j
= cchose(ch
, cnum
, sum
);
511 cremove(crd
, ch
, cnum
--);
512 sum
+= VAL(crd
.rank
);
515 addmsg(quiet
? "I get %d playing " :
516 "I get %d points playing ", k
);
519 if (chkscr(&cscore
, k
))
525 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
526 if (!ugo
&& pnum
) { /* go for player */
527 msg("You have a GO");
530 /* can computer play? */
531 if (anymove(ch
, cnum
, sum
))
534 msg(quiet
? "I get one" :
537 if (chkscr(&cscore
, 1))
543 } else { /* player plays */
547 msg("You play your last card");
551 pnum
, Playwin
, FALSE
);
553 pnum
, "Your play: ")];
554 if (sum
+ VAL(crd
.rank
) <= 31)
557 msg("Total > 31 -- try again");
560 cremove(crd
, ph
, pnum
--);
561 i
= pegscore(crd
, Table
, Tcnt
, sum
);
562 sum
+= VAL(crd
.rank
);
565 msg(quiet
? "You got %d" :
566 "You got %d points", i
);
569 if (chkscr(&pscore
, i
))
581 last
= FALSE
; /* disable last flag */
584 break; /* both done */
586 prhand(ph
, pnum
, Playwin
, FALSE
);
587 prhand(ch
, cnum
, Compwin
, TRUE
);
591 msg(quiet
? "I get one for last" :
592 "I get one point for last");
594 if (chkscr(&cscore
, 1))
597 msg(quiet
? "You get one for last" :
598 "You get one point for last");
600 if (chkscr(&pscore
, 1))
609 * Print out the table with the current score
612 prtable(int curscore
)
614 prhand(Table
, Tcnt
, Tablewin
, FALSE
);
615 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", curscore
);
621 * Handle the scoring of the hands
624 score(BOOLEAN mycrib
)
626 sorthand(crib
, CINHAND
);
628 if (plyrhand(phand
, "hand"))
630 if (comphand(chand
, "hand"))
633 if (comphand(crib
, "crib"))
637 if (comphand(chand
, "hand"))
639 if (plyrhand(phand
, "hand"))
641 if (plyrhand(crib
, "crib"))