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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)crib.c 8.1 (Berkeley) 5/31/93
31 * $FreeBSD: src/games/cribbage/crib.c,v 1.10 1999/12/12 03:04:14 billf Exp $
32 * $DragonFly: src/games/cribbage/crib.c,v 1.3 2005/08/03 13:31:00 eirikn Exp $
44 #include "pathnames.h"
46 static bool cut(bool, int);
47 static int deal(bool);
48 static void discard(bool);
49 static void game(void);
50 static void gamescore(void);
51 static void makeboard(void);
52 static bool peg(bool);
53 static bool playhand(bool);
54 static void prcrib(bool, bool);
55 static void prtable(int);
56 static bool scoreh(bool);
59 main(int argc
, char *argv
[])
65 f
= fopen(_PATH_LOG
, "a");
70 while ((ch
= getopt(argc
, argv
, "eqr")) != -1)
83 fprintf(stderr
, "usage: cribbage [-eqr]\n");
92 Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
93 Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
94 Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
95 Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
96 leaveok(Playwin
, TRUE
);
97 leaveok(Tablewin
, TRUE
);
98 leaveok(Compwin
, TRUE
);
99 clearok(stdscr
, FALSE
);
102 msg("Do you need instructions for cribbage? ");
103 if (getuchar() == 'Y') {
106 mvcur(0, COLS
- 1, LINES
- 1, 0);
113 msg("For cribbage rules, use \"man cribbage\"");
119 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
121 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
123 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
125 msg("Another game? ");
126 playing
= (getuchar() == 'Y');
130 fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
131 getlogin(), cgames
, pgames
);
136 fprintf(stderr
, "\ncribbage: can't open %s.\n", _PATH_LOG
);
144 * Print out the initial board on the screen
149 mvaddstr(SCORE_Y
+ 0, SCORE_X
,
150 "+---------------------------------------+");
151 mvaddstr(SCORE_Y
+ 1, SCORE_X
,
153 mvaddstr(SCORE_Y
+ 2, SCORE_X
,
154 "| *.....:.....:.....:.....:.....:..... |");
155 mvaddstr(SCORE_Y
+ 3, SCORE_X
,
156 "| *.....:.....:.....:.....:.....:..... |");
157 mvaddstr(SCORE_Y
+ 4, SCORE_X
,
159 mvaddstr(SCORE_Y
+ 5, SCORE_X
,
160 "| *.....:.....:.....:.....:.....:..... |");
161 mvaddstr(SCORE_Y
+ 6, SCORE_X
,
162 "| *.....:.....:.....:.....:.....:..... |");
163 mvaddstr(SCORE_Y
+ 7, SCORE_X
,
165 mvaddstr(SCORE_Y
+ 8, SCORE_X
,
166 "+---------------------------------------+");
172 * Print out the current game score
178 if (pgames
|| cgames
) {
179 mvprintw(SCORE_Y
+ 1, SCORE_X
+ 28, "Games: %3d", pgames
);
180 mvprintw(SCORE_Y
+ 7, SCORE_X
+ 28, "Games: %3d", cgames
);
188 * Play one game up to glimit points. Actually, we only ASK the
189 * player what card to turn. We do a random one, anyway.
200 if (gamecount
== 0) {
203 if (!rflag
) { /* player cuts deck */
204 msg(quiet
? "Cut for crib? " :
205 "Cut to see whose crib it is -- low card wins? ");
208 i
= random() % CARDS
; /* random cut */
209 do { /* comp cuts deck */
210 j
= random() % CARDS
;
212 addmsg(quiet
? "You cut " : "You cut the ");
213 msgcard(deck
[i
], false);
215 addmsg(quiet
? "I cut " : "I cut the ");
216 msgcard(deck
[j
], false);
218 flag
= (deck
[i
].rank
== deck
[j
].rank
);
220 msg(quiet
? "We tied..." :
221 "We tied and have to try again...");
225 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
235 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
243 flag
= !playhand(compcrib
);
244 compcrib
= !compcrib
;
247 if (cscore
< pscore
) {
248 if (glimit
- cscore
> 60) {
249 msg("YOU DOUBLE SKUNKED ME!");
252 if (glimit
- cscore
> 30) {
253 msg("YOU SKUNKED ME!");
261 if (glimit
- pscore
> 60) {
262 msg("I DOUBLE SKUNKED YOU!");
265 if (glimit
- pscore
> 30) {
266 msg("I SKUNKED YOU!");
279 * Do up one hand of the game
282 playhand(bool mycrib
)
289 deckpos
= deal(mycrib
);
290 sorthand(chand
, FULLHAND
);
291 sorthand(phand
, FULLHAND
);
292 makeknown(chand
, FULLHAND
);
293 prhand(phand
, FULLHAND
, Playwin
, false);
295 if (cut(mycrib
, deckpos
))
307 * deal cards to both players from deck
314 for (i
= j
= 0; i
< FULLHAND
; i
++) {
316 phand
[i
] = deck
[j
++];
317 chand
[i
] = deck
[j
++];
319 chand
[i
] = deck
[j
++];
320 phand
[i
] = deck
[j
++];
328 * Handle players discarding into the crib...
329 * Note: we call cdiscard() after printing first message so player doesn't wait
337 prcrib(mycrib
, true);
338 prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
339 cdiscard(mycrib
); /* puts best discard at end */
340 crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
341 cremove(crd
, phand
, FULLHAND
);
342 prhand(phand
, FULLHAND
, Playwin
, false);
345 /* Next four lines same as last four except for cdiscard(). */
346 crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
347 cremove(crd
, phand
, FULLHAND
- 1);
348 prhand(phand
, FULLHAND
, Playwin
, false);
352 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
357 * Cut the deck and set turnover. Actually, we only ASK the
358 * player what card to turn. We do a random one, anyway.
361 cut(bool mycrib
, int pos
)
368 if (!rflag
) { /* random cut */
369 msg(quiet
? "Cut the deck? " :
370 "How many cards down do you wish to cut the deck? ");
373 i
= random() % (CARDS
- pos
);
374 turnover
= deck
[i
+ pos
];
375 addmsg(quiet
? "You cut " : "You cut the ");
376 msgcard(turnover
, false);
378 if (turnover
.rank
== JACK
) {
379 msg("I get two for his heels");
380 win
= chkscr(&cscore
, 2);
383 i
= random() % (CARDS
- pos
) + pos
;
385 addmsg(quiet
? "I cut " : "I cut the ");
386 msgcard(turnover
, false);
388 if (turnover
.rank
== JACK
) {
389 msg("You get two for his heels");
390 win
= chkscr(&pscore
, 2);
393 makeknown(&turnover
, 1);
394 prcrib(mycrib
, false);
400 * Print out the turnover card with crib indicator
403 prcrib(bool mycrib
, bool blank
)
412 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
413 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
420 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
421 mvaddstr(y
, cardx
, " ");
426 * Handle all the pegging...
428 static CARD Table
[14];
434 static CARD ch
[CINHAND
], ph
[CINHAND
];
438 bool myturn
, mego
, ugo
, last
, played
;
441 cnum
= pnum
= CINHAND
;
442 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
446 Tcnt
= 0; /* index to table of cards played */
447 sum
= 0; /* sum of cards played */
448 played
= mego
= ugo
= false;
451 last
= true; /* enable last flag */
452 prhand(ph
, pnum
, Playwin
, false);
453 prhand(ch
, cnum
, Compwin
, true);
455 if (myturn
) { /* my tyrn to play */
456 if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
457 if (!mego
&& cnum
) { /* go for comp? */
461 /* can player move? */
462 if (anymove(ph
, pnum
, sum
))
464 else { /* give him his point */
465 msg(quiet
? "You get one" :
466 "You get one point");
467 if (chkscr(&pscore
, 1))
478 for (i
= 0; i
< cnum
; i
++) {
479 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
485 if (j
< 0) /* if nothing scores */
486 j
= cchose(ch
, cnum
, sum
);
488 cremove(crd
, ch
, cnum
--);
489 sum
+= VAL(crd
.rank
);
492 addmsg(quiet
? "I get %d playing " :
493 "I get %d points playing ", k
);
496 if (chkscr(&cscore
, k
))
502 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
503 if (!ugo
&& pnum
) { /* go for player */
504 msg("You have a GO");
507 /* can computer play? */
508 if (anymove(ch
, cnum
, sum
))
511 msg(quiet
? "I get one" :
514 if (chkscr(&cscore
, 1))
520 } else { /* player plays */
524 msg("You play your last card");
528 pnum
, Playwin
, false);
530 pnum
, "Your play: ")];
531 if (sum
+ VAL(crd
.rank
) <= 31)
534 msg("Total > 31 -- try again");
537 cremove(crd
, ph
, pnum
--);
538 i
= pegscore(crd
, Table
, Tcnt
, sum
);
539 sum
+= VAL(crd
.rank
);
542 msg(quiet
? "You got %d" :
543 "You got %d points", i
);
544 if (chkscr(&pscore
, i
))
556 last
= false; /* disable last flag */
559 break; /* both done */
561 prhand(ph
, pnum
, Playwin
, false);
562 prhand(ch
, cnum
, Compwin
, true);
566 msg(quiet
? "I get one for last" :
567 "I get one point for last");
569 if (chkscr(&cscore
, 1))
572 msg(quiet
? "You get one for last" :
573 "You get one point for last");
574 if (chkscr(&pscore
, 1))
583 * Print out the table with the current score
588 prhand(Table
, Tcnt
, Tablewin
, false);
589 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", score
);
595 * Handle the scoring of the hands
600 sorthand(crib
, CINHAND
);
602 if (plyrhand(phand
, "hand"))
604 if (comphand(chand
, "hand"))
607 if (comphand(crib
, "crib"))
610 if (comphand(chand
, "hand"))
612 if (plyrhand(phand
, "hand"))
614 if (plyrhand(crib
, "crib"))