]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - cribbage/crib.c
2 * Copyright (c) 1980 Regents of the University of California.
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
36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
37 All rights reserved.\n";
41 static char sccsid
[] = "@(#)crib.c 5.6 (Berkeley) 2/28/91";
44 # include <sys/signal.h>
47 # include "cribbage.h"
49 # include "pathnames.h"
60 char *s
; /* for reading arguments */
63 char *getline(), *getlogin();
66 while ((ch
= getopt(argc
, argv
, "eqr")) != EOF
)
79 (void) fprintf(stderr
, "usage: cribbage [-eqr]\n");
87 Playwin
= subwin(stdscr
, PLAY_Y
, PLAY_X
, 0, 0);
88 Tablewin
= subwin(stdscr
, TABLE_Y
, TABLE_X
, 0, PLAY_X
);
89 Compwin
= subwin(stdscr
, COMP_Y
, COMP_X
, 0, TABLE_X
+ PLAY_X
);
90 Msgwin
= subwin(stdscr
, MSG_Y
, MSG_X
, Y_MSG_START
, SCORE_X
+ 1);
91 leaveok(Playwin
, TRUE
);
92 leaveok(Tablewin
, TRUE
);
93 leaveok(Compwin
, TRUE
);
94 clearok(stdscr
, FALSE
);
97 msg("Do you need instructions for cribbage? ");
98 if (getuchar() == 'Y') {
101 mvcur(0, COLS
- 1, LINES
- 1, 0);
108 msg("For the rules of this program, do \"man cribbage\"");
114 msg(quiet
? "L or S? " : "Long (to 121) or Short (to 61)? ");
116 glimit
= (getuchar() == 'L' ? LGAME
: SGAME
);
118 glimit
= (getuchar() == 'S' ? SGAME
: LGAME
);
120 msg("Another game? ");
121 playing
= (getuchar() == 'Y');
124 if (f
= fopen(_PATH_LOG
, "a")) {
125 (void)fprintf(f
, "%s: won %5.5d, lost %5.5d\n",
126 getlogin(), cgames
, pgames
);
131 (void)fprintf(stderr
, "\ncribbage: can't open %s.\n",
140 * Print out the initial board on the screen
144 mvaddstr(SCORE_Y
+ 0, SCORE_X
, "+---------------------------------------+");
145 mvaddstr(SCORE_Y
+ 1, SCORE_X
, "| Score: 0 YOU |");
146 mvaddstr(SCORE_Y
+ 2, SCORE_X
, "| *.....:.....:.....:.....:.....:..... |");
147 mvaddstr(SCORE_Y
+ 3, SCORE_X
, "| *.....:.....:.....:.....:.....:..... |");
148 mvaddstr(SCORE_Y
+ 4, SCORE_X
, "| |");
149 mvaddstr(SCORE_Y
+ 5, SCORE_X
, "| *.....:.....:.....:.....:.....:..... |");
150 mvaddstr(SCORE_Y
+ 6, SCORE_X
, "| *.....:.....:.....:.....:.....:..... |");
151 mvaddstr(SCORE_Y
+ 7, SCORE_X
, "| Score: 0 ME |");
152 mvaddstr(SCORE_Y
+ 8, SCORE_X
, "+---------------------------------------+");
158 * Print out the current game score
162 extern int Lastscore
[];
164 if (pgames
|| cgames
) {
165 mvprintw(SCORE_Y
+ 1, SCORE_X
+ 28, "Games: %3d", pgames
);
166 mvprintw(SCORE_Y
+ 7, SCORE_X
+ 28, "Games: %3d", cgames
);
174 * Play one game up to glimit points. Actually, we only ASK the
175 * player what card to turn. We do a random one, anyway.
187 if (gamecount
== 0) {
190 if (!rflag
) { /* player cuts deck */
191 msg(quiet
? "Cut for crib? " :
192 "Cut to see whose crib it is -- low card wins? ");
195 i
= (rand() >> 4) % CARDS
; /* random cut */
196 do { /* comp cuts deck */
197 j
= (rand() >> 4) % CARDS
;
199 addmsg(quiet
? "You cut " : "You cut the ");
200 msgcard(deck
[i
], FALSE
);
202 addmsg(quiet
? "I cut " : "I cut the ");
203 msgcard(deck
[j
], FALSE
);
205 flag
= (deck
[i
].rank
== deck
[j
].rank
);
207 msg(quiet
? "We tied..." :
208 "We tied and have to try again...");
213 compcrib
= (deck
[i
].rank
> deck
[j
].rank
);
221 msg("Loser (%s) gets first crib", (iwon
? "you" : "me"));
229 flag
= !playhand(compcrib
);
230 compcrib
= !compcrib
;
233 if (cscore
< pscore
) {
234 if (glimit
- cscore
> 60) {
235 msg("YOU DOUBLE SKUNKED ME!");
238 else if (glimit
- cscore
> 30) {
239 msg("YOU SKUNKED ME!");
249 if (glimit
- pscore
> 60) {
250 msg("I DOUBLE SKUNKED YOU!");
253 else if (glimit
- pscore
> 30) {
254 msg("I SKUNKED YOU!");
268 * Do up one hand of the game
273 register int deckpos
;
274 extern char Msgbuf
[];
279 deckpos
= deal(mycrib
);
280 sorthand(chand
, FULLHAND
);
281 sorthand(phand
, FULLHAND
);
282 makeknown(chand
, FULLHAND
);
283 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
285 if (cut(mycrib
, deckpos
))
299 * deal cards to both players from deck
307 for( i
= 0; i
< FULLHAND
; i
++ ) {
309 phand
[i
] = deck
[j
++];
310 chand
[i
] = deck
[j
++];
313 chand
[i
] = deck
[j
++];
314 phand
[i
] = deck
[j
++];
322 * Handle players discarding into the crib...
323 * Note: we call cdiscard() after prining first message so player doesn't wait
328 register char *prompt
;
331 prcrib(mycrib
, TRUE
);
332 prompt
= (quiet
? "Discard --> " : "Discard a card --> ");
333 cdiscard(mycrib
); /* puts best discard at end */
334 crd
= phand
[infrom(phand
, FULLHAND
, prompt
)];
335 cremove(crd
, phand
, FULLHAND
);
336 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
338 /* next four lines same as last four except for cdiscard() */
339 crd
= phand
[infrom(phand
, FULLHAND
- 1, prompt
)];
340 cremove(crd
, phand
, FULLHAND
- 1);
341 prhand(phand
, FULLHAND
, Playwin
, FALSE
);
345 chand
[4].rank
= chand
[4].suit
= chand
[5].rank
= chand
[5].suit
= EMPTY
;
350 * Cut the deck and set turnover. Actually, we only ASK the
351 * player what card to turn. We do a random one, anyway.
357 register int i
, cardx
;
361 if (!rflag
) { /* random cut */
362 msg(quiet
? "Cut the deck? " :
363 "How many cards down do you wish to cut the deck? ");
366 i
= (rand() >> 4) % (CARDS
- pos
);
367 turnover
= deck
[i
+ pos
];
368 addmsg(quiet
? "You cut " : "You cut the ");
369 msgcard(turnover
, FALSE
);
371 if (turnover
.rank
== JACK
) {
372 msg("I get two for his heels");
373 win
= chkscr(&cscore
,2 );
377 i
= (rand() >> 4) % (CARDS
- pos
) + pos
;
379 addmsg(quiet
? "I cut " : "I cut the ");
380 msgcard(turnover
, FALSE
);
382 if (turnover
.rank
== JACK
) {
383 msg("You get two for his heels");
384 win
= chkscr(&pscore
, 2);
387 makeknown(&turnover
, 1);
388 prcrib(mycrib
, FALSE
);
394 * Print out the turnover card with crib indicator
396 prcrib(mycrib
, blank
)
397 BOOLEAN mycrib
, blank
;
399 register int y
, cardx
;
406 mvaddstr(CRIB_Y
, cardx
+ 1, "CRIB");
407 prcard(stdscr
, CRIB_Y
+ 1, cardx
, turnover
, blank
);
414 for (y
= CRIB_Y
; y
<= CRIB_Y
+ 5; y
++)
415 mvaddstr(y
, cardx
, " ");
420 * Handle all the pegging...
423 static CARD Table
[14];
430 static CARD ch
[CINHAND
], ph
[CINHAND
];
432 register int i
, j
, k
;
434 register int cnum
, pnum
, sum
;
435 register BOOLEAN myturn
, mego
, ugo
, last
, played
;
437 cnum
= pnum
= CINHAND
;
438 for (i
= 0; i
< CINHAND
; i
++) { /* make copies of hands */
442 Tcnt
= 0; /* index to table of cards played */
443 sum
= 0; /* sum of cards played */
447 last
= TRUE
; /* enable last flag */
448 prhand(ph
, pnum
, Playwin
, FALSE
);
449 prhand(ch
, cnum
, Compwin
, TRUE
);
451 if (myturn
) { /* my tyrn to play */
452 if (!anymove(ch
, cnum
, sum
)) { /* if no card to play */
453 if (!mego
&& cnum
) { /* go for comp? */
457 if (anymove(ph
, pnum
, sum
)) /* can player move? */
459 else { /* give him his point */
460 msg(quiet
? "You get one" : "You get one point");
461 if (chkscr(&pscore
, 1))
472 for (i
= 0; i
< cnum
; i
++) { /* maximize score */
473 l
= pegscore(ch
[i
], Table
, Tcnt
, sum
);
479 if (j
< 0) /* if nothing scores */
480 j
= cchose(ch
, cnum
, sum
);
482 cremove(crd
, ch
, cnum
--);
483 sum
+= VAL(crd
.rank
);
486 addmsg(quiet
? "I get %d playing " :
487 "I get %d points playing ", k
);
490 if (chkscr(&cscore
, k
))
497 if (!anymove(ph
, pnum
, sum
)) { /* can player move? */
498 if (!ugo
&& pnum
) { /* go for player */
499 msg("You have a GO");
502 if (anymove(ch
, cnum
, sum
)) /* can computer play? */
505 msg(quiet
? "I get one" : "I get one point");
507 if (chkscr(&cscore
, 1))
514 else { /* player plays */
518 msg("You play your last card");
522 prhand(ph
, pnum
, Playwin
, FALSE
);
523 crd
= ph
[infrom(ph
, pnum
, "Your play: ")];
524 if (sum
+ VAL(crd
.rank
) <= 31)
527 msg("Total > 31 -- try again");
530 cremove(crd
, ph
, pnum
--);
531 i
= pegscore(crd
, Table
, Tcnt
, sum
);
532 sum
+= VAL(crd
.rank
);
535 msg(quiet
? "You got %d" : "You got %d points", i
);
536 if (chkscr(&pscore
, i
))
548 last
= FALSE
; /* disable last flag */
551 break; /* both done */
553 prhand(ph
, pnum
, Playwin
, FALSE
);
554 prhand(ch
, cnum
, Compwin
, TRUE
);
558 msg(quiet
? "I get one for last" : "I get one point for last");
560 if (chkscr(&cscore
, 1))
564 msg(quiet
? "You get one for last" :
565 "You get one point for last");
566 if (chkscr(&pscore
, 1))
574 * Print out the table with the current score
579 prhand(Table
, Tcnt
, Tablewin
, FALSE
);
580 mvwprintw(Tablewin
, (Tcnt
+ 2) * 2, Tcnt
+ 1, "%2d", score
);
586 * Handle the scoring of the hands
591 sorthand(crib
, CINHAND
);
593 if (plyrhand(phand
, "hand"))
595 if (comphand(chand
, "hand"))
598 if (comphand(crib
, "crib"))
602 if (comphand(chand
, "hand"))
604 if (plyrhand(phand
, "hand"))
606 if (plyrhand(crib
, "crib"))