2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)crib.c 8.1 (Berkeley) 5/31/93
35 * $FreeBSD: src/games/cribbage/crib.c,v 1.10 1999/12/12 03:04:14 billf Exp $
36 * $DragonFly: src/games/cribbage/crib.c,v 1.3 2005/08/03 13:31:00 eirikn Exp $
46 #include "pathnames.h"
48 static bool cut(bool, int);
49 static int deal(bool);
50 static void discard(bool);
51 static void game(void);
52 static void gamescore(void);
53 static void makeboard(void);
54 static bool peg(bool);
55 static bool playhand(bool);
56 static void prcrib(bool, bool);
57 static void prtable(int);
58 static bool scoreh(bool);
61 main(int argc
, char *argv
[])
67 f
= fopen(_PATH_LOG
, "a");
72 while ((ch
= getopt(argc
, argv
, "eqr")) != -1)
85 fprintf(stderr
, "usage: cribbage [-eqr]\n");
94 Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
95 Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
96 Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
97 Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
98 leaveok(Playwin
, TRUE
);
99 leaveok(Tablewin
, TRUE
);
100 leaveok(Compwin
, TRUE
);
101 clearok(stdscr
, FALSE
);
104 msg("Do you need instructions for cribbage? ");
105 if (getuchar() == 'Y') {
108 mvcur(0, COLS
- 1, LINES
- 1, 0);
115 msg("For cribbage rules, use \"man cribbage\"");
121 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
123 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
125 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
127 msg("Another game? ");
128 playing
= (getuchar() == 'Y');
132 fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
133 getlogin(), cgames
, pgames
);
138 fprintf(stderr
, "\ncribbage: can't open %s.\n", _PATH_LOG
);
146 * Print out the initial board on the screen
151 mvaddstr(SCORE_Y
+ 0, SCORE_X
,
152 "+---------------------------------------+");
153 mvaddstr(SCORE_Y
+ 1, SCORE_X
,
155 mvaddstr(SCORE_Y
+ 2, SCORE_X
,
156 "| *.....:.....:.....:.....:.....:..... |");
157 mvaddstr(SCORE_Y
+ 3, SCORE_X
,
158 "| *.....:.....:.....:.....:.....:..... |");
159 mvaddstr(SCORE_Y
+ 4, SCORE_X
,
161 mvaddstr(SCORE_Y
+ 5, SCORE_X
,
162 "| *.....:.....:.....:.....:.....:..... |");
163 mvaddstr(SCORE_Y
+ 6, SCORE_X
,
164 "| *.....:.....:.....:.....:.....:..... |");
165 mvaddstr(SCORE_Y
+ 7, SCORE_X
,
167 mvaddstr(SCORE_Y
+ 8, SCORE_X
,
168 "+---------------------------------------+");
174 * Print out the current game score
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.
202 if (gamecount
== 0) {
205 if (!rflag
) { /* player cuts deck */
206 msg(quiet
? "Cut for crib? " :
207 "Cut to see whose crib it is -- low card wins? ");
210 i
= random() % CARDS
; /* random cut */
211 do { /* comp cuts deck */
212 j
= random() % CARDS
;
214 addmsg(quiet
? "You cut " : "You cut the ");
215 msgcard(deck
[i
], false);
217 addmsg(quiet
? "I cut " : "I cut the ");
218 msgcard(deck
[j
], false);
220 flag
= (deck
[i
].rank
== deck
[j
].rank
);
222 msg(quiet
? "We tied..." :
223 "We tied and have to try again...");
227 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
237 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
245 flag
= !playhand(compcrib
);
246 compcrib
= !compcrib
;
249 if (cscore
< pscore
) {
250 if (glimit
- cscore
> 60) {
251 msg("YOU DOUBLE SKUNKED ME!");
254 if (glimit
- cscore
> 30) {
255 msg("YOU SKUNKED ME!");
263 if (glimit
- pscore
> 60) {
264 msg("I DOUBLE SKUNKED YOU!");
267 if (glimit
- pscore
> 30) {
268 msg("I SKUNKED YOU!");
281 * Do up one hand of the game
284 playhand(bool mycrib
)
291 deckpos
= deal(mycrib
);
292 sorthand(chand
, FULLHAND
);
293 sorthand(phand
, FULLHAND
);
294 makeknown(chand
, FULLHAND
);
295 prhand(phand
, FULLHAND
, Playwin
, false);
297 if (cut(mycrib
, deckpos
))
309 * deal cards to both players from deck
316 for (i
= j
= 0; i
< FULLHAND
; i
++) {
318 phand
[i
] = deck
[j
++];
319 chand
[i
] = deck
[j
++];
321 chand
[i
] = deck
[j
++];
322 phand
[i
] = deck
[j
++];
330 * Handle players discarding into the crib...
331 * Note: we call cdiscard() after printing first message so player doesn't wait
339 prcrib(mycrib
, true);
340 prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
341 cdiscard(mycrib
); /* puts best discard at end */
342 crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
343 cremove(crd
, phand
, FULLHAND
);
344 prhand(phand
, FULLHAND
, Playwin
, false);
347 /* Next four lines same as last four except for cdiscard(). */
348 crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
349 cremove(crd
, phand
, FULLHAND
- 1);
350 prhand(phand
, FULLHAND
, Playwin
, false);
354 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
359 * Cut the deck and set turnover. Actually, we only ASK the
360 * player what card to turn. We do a random one, anyway.
363 cut(bool mycrib
, int pos
)
370 if (!rflag
) { /* random cut */
371 msg(quiet
? "Cut the deck? " :
372 "How many cards down do you wish to cut the deck? ");
375 i
= random() % (CARDS
- pos
);
376 turnover
= deck
[i
+ pos
];
377 addmsg(quiet
? "You cut " : "You cut the ");
378 msgcard(turnover
, false);
380 if (turnover
.rank
== JACK
) {
381 msg("I get two for his heels");
382 win
= chkscr(&cscore
, 2);
385 i
= random() % (CARDS
- pos
) + pos
;
387 addmsg(quiet
? "I cut " : "I cut the ");
388 msgcard(turnover
, false);
390 if (turnover
.rank
== JACK
) {
391 msg("You get two for his heels");
392 win
= chkscr(&pscore
, 2);
395 makeknown(&turnover
, 1);
396 prcrib(mycrib
, false);
402 * Print out the turnover card with crib indicator
405 prcrib(bool mycrib
, bool blank
)
414 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
415 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
422 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
423 mvaddstr(y
, cardx
, " ");
428 * Handle all the pegging...
430 static CARD Table
[14];
436 static CARD ch
[CINHAND
], ph
[CINHAND
];
440 bool myturn
, mego
, ugo
, last
, played
;
443 cnum
= pnum
= CINHAND
;
444 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
448 Tcnt
= 0; /* index to table of cards played */
449 sum
= 0; /* sum of cards played */
450 played
= mego
= ugo
= false;
453 last
= true; /* enable last flag */
454 prhand(ph
, pnum
, Playwin
, false);
455 prhand(ch
, cnum
, Compwin
, true);
457 if (myturn
) { /* my tyrn to play */
458 if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
459 if (!mego
&& cnum
) { /* go for comp? */
463 /* can player move? */
464 if (anymove(ph
, pnum
, sum
))
466 else { /* give him his point */
467 msg(quiet
? "You get one" :
468 "You get one point");
469 if (chkscr(&pscore
, 1))
480 for (i
= 0; i
< cnum
; i
++) {
481 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
487 if (j
< 0) /* if nothing scores */
488 j
= cchose(ch
, cnum
, sum
);
490 cremove(crd
, ch
, cnum
--);
491 sum
+= VAL(crd
.rank
);
494 addmsg(quiet
? "I get %d playing " :
495 "I get %d points playing ", k
);
498 if (chkscr(&cscore
, k
))
504 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
505 if (!ugo
&& pnum
) { /* go for player */
506 msg("You have a GO");
509 /* can computer play? */
510 if (anymove(ch
, cnum
, sum
))
513 msg(quiet
? "I get one" :
516 if (chkscr(&cscore
, 1))
522 } else { /* player plays */
526 msg("You play your last card");
530 pnum
, Playwin
, false);
532 pnum
, "Your play: ")];
533 if (sum
+ VAL(crd
.rank
) <= 31)
536 msg("Total > 31 -- try again");
539 cremove(crd
, ph
, pnum
--);
540 i
= pegscore(crd
, Table
, Tcnt
, sum
);
541 sum
+= VAL(crd
.rank
);
544 msg(quiet
? "You got %d" :
545 "You got %d points", i
);
546 if (chkscr(&pscore
, i
))
558 last
= false; /* disable last flag */
561 break; /* both done */
563 prhand(ph
, pnum
, Playwin
, false);
564 prhand(ch
, cnum
, Compwin
, true);
568 msg(quiet
? "I get one for last" :
569 "I get one point for last");
571 if (chkscr(&cscore
, 1))
574 msg(quiet
? "You get one for last" :
575 "You get one point for last");
576 if (chkscr(&pscore
, 1))
585 * Print out the table with the current score
590 prhand(Table
, Tcnt
, Tablewin
, false);
591 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", score
);
597 * Handle the scoring of the hands
602 sorthand(crib
, CINHAND
);
604 if (plyrhand(phand
, "hand"))
606 if (comphand(chand
, "hand"))
609 if (comphand(crib
, "crib"))
612 if (comphand(chand
, "hand"))
614 if (plyrhand(phand
, "hand"))
616 if (plyrhand(crib
, "crib"))