* Copyright (c) 1980 Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)crib.c 5.6 (Berkeley) 2/28/91";
char *s
; /* for reading arguments */
while ((ch
= getopt(argc
, argv
, "eqr")) != EOF
)
(void) fprintf(stderr
, "usage: cribbage [-eqr]\n");
Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
msg("Do you need instructions for cribbage? ");
mvcur(0, COLS
- 1, LINES
- 1, 0);
msg("For the rules of this program, do \"man cribbage\"");
msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
playing
= (getuchar() == 'Y');
if (f
= fopen(_PATH_LOG
, "a")) {
(void)fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
getlogin(), cgames
, pgames
);
(void)fprintf(stderr
, "\ncribbage: can't open %s.\n",
* Print out the initial board on the screen
mvaddstr(SCORE_Y
+ 0, SCORE_X
, "+---------------------------------------+");
mvaddstr(SCORE_Y
+ 1, SCORE_X
, "| Score: 0 YOU |");
mvaddstr(SCORE_Y
+ 2, SCORE_X
, "| *.....:.....:.....:.....:.....:..... |");
mvaddstr(SCORE_Y
+ 3, SCORE_X
, "| *.....:.....:.....:.....:.....:..... |");
mvaddstr(SCORE_Y
+ 4, SCORE_X
, "| |");
mvaddstr(SCORE_Y
+ 5, SCORE_X
, "| *.....:.....:.....:.....:.....:..... |");
mvaddstr(SCORE_Y
+ 6, SCORE_X
, "| *.....:.....:.....:.....:.....:..... |");
mvaddstr(SCORE_Y
+ 7, SCORE_X
, "| Score: 0 ME |");
mvaddstr(SCORE_Y
+ 8, SCORE_X
, "+---------------------------------------+");
* Print out the current game score
mvprintw(SCORE_Y
+ 1, SCORE_X
+ 28, "Games: %3d", pgames
);
mvprintw(SCORE_Y
+ 7, SCORE_X
+ 28, "Games: %3d", cgames
);
* Play one game up to glimit points. Actually, we only ASK the
* player what card to turn. We do a random one, anyway.
if (!rflag
) { /* player cuts deck */
msg(quiet
? "Cut for crib? " :
"Cut to see whose crib it is -- low card wins? ");
i
= (rand() >> 4) % CARDS
; /* random cut */
do { /* comp cuts deck */
j
= (rand() >> 4) % CARDS
;
addmsg(quiet
? "You cut " : "You cut the ");
addmsg(quiet
? "I cut " : "I cut the ");
flag
= (deck
[i
].rank
== deck
[j
].rank
);
msg(quiet
? "We tied..." :
"We tied and have to try again...");
compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
flag
= !playhand(compcrib
);
if (glimit
- cscore
> 60) {
msg("YOU DOUBLE SKUNKED ME!");
else if (glimit
- cscore
> 30) {
if (glimit
- pscore
> 60) {
msg("I DOUBLE SKUNKED YOU!");
else if (glimit
- pscore
> 30) {
* Do up one hand of the game
sorthand(chand
, FULLHAND
);
sorthand(phand
, FULLHAND
);
makeknown(chand
, FULLHAND
);
prhand(phand
, FULLHAND
, Playwin
, FALSE
);
if (cut(mycrib
, deckpos
))
* deal cards to both players from deck
for( i
= 0; i
< FULLHAND
; i
++ ) {
* Handle players discarding into the crib...
* Note: we call cdiscard() after prining first message so player doesn't wait
prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
cdiscard(mycrib
); /* puts best discard at end */
crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
cremove(crd
, phand
, FULLHAND
);
prhand(phand
, FULLHAND
, Playwin
, FALSE
);
/* next four lines same as last four except for cdiscard() */
crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
cremove(crd
, phand
, FULLHAND
- 1);
prhand(phand
, FULLHAND
, Playwin
, FALSE
);
chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
* Cut the deck and set turnover. Actually, we only ASK the
* player what card to turn. We do a random one, anyway.
if (!rflag
) { /* random cut */
msg(quiet
? "Cut the deck? " :
"How many cards down do you wish to cut the deck? ");
i
= (rand() >> 4) % (CARDS
- pos
);
turnover
= deck
[i
+ pos
];
addmsg(quiet
? "You cut " : "You cut the ");
msgcard(turnover
, FALSE
);
if (turnover
.rank
== JACK
) {
msg("I get two for his heels");
win
= chkscr(&cscore
,2 );
i
= (rand() >> 4) % (CARDS
- pos
) + pos
;
addmsg(quiet
? "I cut " : "I cut the ");
msgcard(turnover
, FALSE
);
if (turnover
.rank
== JACK
) {
msg("You get two for his heels");
win
= chkscr(&pscore
, 2);
* Print out the turnover card with crib indicator
mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
* Handle all the pegging...
static CARD ch
[CINHAND
], ph
[CINHAND
];
register int cnum
, pnum
, sum
;
register BOOLEAN myturn
, mego
, ugo
, last
, played
;
for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
Tcnt
= 0; /* index to table of cards played */
sum
= 0; /* sum of cards played */
last
= TRUE
; /* enable last flag */
prhand(ph
, pnum
, Playwin
, FALSE
);
prhand(ch
, cnum
, Compwin
, TRUE
);
if (myturn
) { /* my tyrn to play */
if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
if (!mego
&& cnum
) { /* go for comp? */
if (anymove(ph
, pnum
, sum
)) /* can player move? */
else { /* give him his point */
msg(quiet
? "You get one" : "You get one point");
for (i
= 0; i
< cnum
; i
++) { /* maximize score */
l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
if (j
< 0) /* if nothing scores */
j
= cchose(ch
, cnum
, sum
);
cremove(crd
, ch
, cnum
--);
addmsg(quiet
? "I get %d playing " :
"I get %d points playing ", k
);
if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
if (!ugo
&& pnum
) { /* go for player */
if (anymove(ch
, cnum
, sum
)) /* can computer play? */
msg(quiet
? "I get one" : "I get one point");
else { /* player plays */
msg("You play your last card");
prhand(ph
, pnum
, Playwin
, FALSE
);
crd
= ph
[infrom(ph
, pnum
, "Your play: ")];
if (sum
+ VAL(crd
.rank
) <= 31)
msg("Total > 31 -- try again");
cremove(crd
, ph
, pnum
--);
i
= pegscore(crd
, Table
, Tcnt
, sum
);
msg(quiet
? "You got %d" : "You got %d points", i
);
last
= FALSE
; /* disable last flag */
prhand(ph
, pnum
, Playwin
, FALSE
);
prhand(ch
, cnum
, Compwin
, TRUE
);
msg(quiet
? "I get one for last" : "I get one point for last");
msg(quiet
? "You get one for last" :
"You get one point for last");
* Print out the table with the current score
prhand(Table
, Tcnt
, Tablewin
, FALSE
);
mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", score
);
* Handle the scoring of the hands
if (plyrhand(phand
, "hand"))
if (comphand(chand
, "hand"))
if (comphand(crib
, "crib"))
if (comphand(chand
, "hand"))
if (plyrhand(phand
, "hand"))
if (plyrhand(crib
, "crib"))