# define LOGFILE "/usr/games/lib/criblog"
# define INSTRCMD "ul /usr/games/lib/crib.instr | more -f"
char *s
; /* for reading arguments */
char bust
; /* flag for arg reader */
if ((*++argv
)[0] != '-') {
fprintf(stderr
, "\n\ncribbage: usage is 'cribbage [-eqr]'\n");
for (s
= argv
[0] + 1; *s
!= NULL
; s
++) {
fprintf(stderr
, "\n\ncribbage: usage is 'cribbage [-eqr]'\n");
Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, SCORE_SZ
);
Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
+ SCORE_SZ
);
Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
+ SCORE_SZ
);
msg("Do you need instructions for cribbage? ");
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(LOGFILE
, "a")) != NULL
) {
fprintf(f
, "Won %5.5d, Lost %5.5d\n", cgames
, pgames
);
* Leave the program, cleaning things up as we go.
mvcur(0, COLS
- 1, LINES
- 1, 0);
* 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
mvaddstr(LINES
- 1, 0, Msgbuf
);
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
)];
remove(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
)];
remove(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
);
* 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
);
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.");
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"))