ANSIfication; bug report 4.3BSD/bin/223
[unix-history] / usr / src / games / cribbage / crib.c
CommitLineData
76944aef
KM
1/*
2 * Copyright (c) 1980 Regents of the University of California.
bf870064
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b8c620d6
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
76944aef
KM
16 */
17
18#ifndef lint
19char copyright[] =
20"@(#) Copyright (c) 1980 Regents of the University of California.\n\
21 All rights reserved.\n";
bf870064 22#endif /* not lint */
76944aef
KM
23
24#ifndef lint
b8c620d6 25static char sccsid[] = "@(#)crib.c 5.3 (Berkeley) %G%";
bf870064 26#endif /* not lint */
9a6fbd6d 27
5836528f
KA
28# include <curses.h>
29# include <signal.h>
30# include "deck.h"
31# include "cribbage.h"
32# include "cribcur.h"
7bbc756f 33
7bbc756f 34
5836528f
KA
35# define LOGFILE "/usr/games/lib/criblog"
36# define INSTRCMD "ul /usr/games/lib/crib.instr | more -f"
7bbc756f 37
7bbc756f 38
5836528f
KA
39main(argc, argv)
40int argc;
41char *argv[];
7bbc756f 42{
7bbc756f
KA
43 register char *p;
44 BOOLEAN playing;
45 char *s; /* for reading arguments */
46 char bust; /* flag for arg reader */
5836528f
KA
47 FILE *f;
48 FILE *fopen();
49 char *getline();
50 int bye();
7bbc756f 51
5836528f
KA
52 while (--argc > 0) {
53 if ((*++argv)[0] != '-') {
54 fprintf(stderr, "\n\ncribbage: usage is 'cribbage [-eqr]'\n");
55 exit(1);
7bbc756f
KA
56 }
57 bust = FALSE;
5836528f
KA
58 for (s = argv[0] + 1; *s != NULL; s++) {
59 switch (*s) {
60 case 'e':
7bbc756f
KA
61 explain = TRUE;
62 break;
5836528f 63 case 'q':
7bbc756f
KA
64 quiet = TRUE;
65 break;
5836528f 66 case 'r':
7bbc756f
KA
67 rflag = TRUE;
68 break;
7bbc756f 69 default:
5836528f
KA
70 fprintf(stderr, "\n\ncribbage: usage is 'cribbage [-eqr]'\n");
71 exit(2);
7bbc756f
KA
72 break;
73 }
5836528f
KA
74 if (bust)
75 break;
7bbc756f
KA
76 }
77 }
5836528f
KA
78
79 initscr();
80 signal(SIGINT, bye);
81 crmode();
82 noecho();
f6867294
KA
83 Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, 0);
84 Tablewin = subwin(stdscr, TABLE_Y, TABLE_X, 0, PLAY_X);
85 Compwin = subwin(stdscr, COMP_Y, COMP_X, 0, TABLE_X + PLAY_X);
86 Msgwin = subwin(stdscr, MSG_Y, MSG_X, Y_MSG_START, SCORE_X + 1);
5836528f
KA
87 leaveok(Playwin, TRUE);
88 leaveok(Tablewin, TRUE);
89 leaveok(Compwin, TRUE);
39911be8 90 clearok(stdscr, FALSE);
5836528f
KA
91
92 if (!quiet) {
93 msg("Do you need instructions for cribbage? ");
94 if (getuchar() == 'Y') {
d1b41579
KA
95 endwin();
96 fflush(stdout);
5836528f 97 system(INSTRCMD);
d1b41579
KA
98 crmode();
99 noecho();
100 clear();
101 refresh();
5836528f 102 msg("For the rules of this program, do \"man cribbage\"");
7bbc756f
KA
103 }
104 }
105 playing = TRUE;
5836528f 106 do {
f6867294 107 wclrtobot(Msgwin);
5836528f 108 msg(quiet ? "L or S? " : "Long (to 121) or Short (to 61)? ");
b6b51343
KA
109 if (glimit == SGAME)
110 glimit = (getuchar() == 'L' ? LGAME : SGAME);
111 else
112 glimit = (getuchar() == 'S' ? SGAME : LGAME);
7bbc756f 113 game();
5836528f
KA
114 msg("Another game? ");
115 playing = (getuchar() == 'Y');
116 } while (playing);
117
118 if ((f = fopen(LOGFILE, "a")) != NULL) {
119 fprintf(f, "Won %5.5d, Lost %5.5d\n", cgames, pgames);
120 fclose(f);
7bbc756f 121 }
7bbc756f 122
5836528f
KA
123 bye();
124}
7bbc756f 125
7bbc756f 126/*
5836528f
KA
127 * makeboard:
128 * Print out the initial board on the screen
7bbc756f 129 */
5836528f
KA
130makeboard()
131{
5836528f 132 mvaddstr(SCORE_Y + 0, SCORE_X, "+---------------------------------------+");
608476cd 133 mvaddstr(SCORE_Y + 1, SCORE_X, "| Score: 0 YOU |");
a410e8bc
KA
134 mvaddstr(SCORE_Y + 2, SCORE_X, "| *.....:.....:.....:.....:.....:..... |");
135 mvaddstr(SCORE_Y + 3, SCORE_X, "| *.....:.....:.....:.....:.....:..... |");
5836528f 136 mvaddstr(SCORE_Y + 4, SCORE_X, "| |");
a410e8bc
KA
137 mvaddstr(SCORE_Y + 5, SCORE_X, "| *.....:.....:.....:.....:.....:..... |");
138 mvaddstr(SCORE_Y + 6, SCORE_X, "| *.....:.....:.....:.....:.....:..... |");
608476cd 139 mvaddstr(SCORE_Y + 7, SCORE_X, "| Score: 0 ME |");
5836528f 140 mvaddstr(SCORE_Y + 8, SCORE_X, "+---------------------------------------+");
b6b51343
KA
141 gamescore();
142}
143
144/*
145 * gamescore:
146 * Print out the current game score
147 */
148gamescore()
149{
150 extern int Lastscore[];
151
86410cf9
KA
152 if (pgames || cgames) {
153 mvprintw(SCORE_Y + 1, SCORE_X + 28, "Games: %3d", pgames);
154 mvprintw(SCORE_Y + 7, SCORE_X + 28, "Games: %3d", cgames);
155 }
a410e8bc
KA
156 Lastscore[0] = -1;
157 Lastscore[1] = -1;
5836528f 158}
7bbc756f 159
5836528f
KA
160/*
161 * game:
d864f6e8
KA
162 * Play one game up to glimit points. Actually, we only ASK the
163 * player what card to turn. We do a random one, anyway.
5836528f 164 */
7bbc756f
KA
165game()
166{
5836528f 167 register int i, j;
7bbc756f
KA
168 BOOLEAN flag;
169 BOOLEAN compcrib;
170
b6b51343 171 makeboard();
39911be8 172 refresh();
5836528f
KA
173 makedeck(deck);
174 shuffle(deck);
175 if (gamecount == 0) {
7bbc756f 176 flag = TRUE;
5836528f 177 do {
d864f6e8 178 if (!rflag) { /* player cuts deck */
5836528f
KA
179 msg(quiet ? "Cut for crib? " :
180 "Cut to see whose crib it is -- low card wins? ");
d864f6e8 181 getline();
7bbc756f 182 }
d864f6e8 183 i = (rand() >> 4) % CARDS; /* random cut */
5836528f
KA
184 do { /* comp cuts deck */
185 j = (rand() >> 4) % CARDS;
186 } while (j == i);
187 addmsg(quiet ? "You cut " : "You cut the ");
188 msgcard(deck[i], FALSE);
189 endmsg();
190 addmsg(quiet ? "I cut " : "I cut the ");
191 msgcard(deck[j], FALSE);
192 endmsg();
193 flag = (deck[i].rank == deck[j].rank);
194 if (flag) {
195 msg(quiet ? "We tied..." :
196 "We tied and have to try again...");
197 shuffle(deck);
7bbc756f
KA
198 continue;
199 }
5836528f
KA
200 else
201 compcrib = (deck[i].rank > deck[j].rank);
202 } while (flag);
7bbc756f 203 }
5836528f 204 else {
b6b51343
KA
205 werase(Tablewin);
206 wrefresh(Tablewin);
207 werase(Compwin);
208 wrefresh(Compwin);
f6867294 209 msg("Loser (%s) gets first crib", (iwon ? "you" : "me"));
7bbc756f
KA
210 compcrib = !iwon;
211 }
5836528f 212
7bbc756f
KA
213 pscore = cscore = 0;
214 flag = TRUE;
5836528f
KA
215 do {
216 shuffle(deck);
217 flag = !playhand(compcrib);
7bbc756f 218 compcrib = !compcrib;
5836528f 219 } while (flag);
7bbc756f 220 ++gamecount;
5836528f
KA
221 if (cscore < pscore) {
222 if (glimit - cscore > 60) {
223 msg("YOU DOUBLE SKUNKED ME!");
224 pgames += 4;
7bbc756f 225 }
5836528f
KA
226 else if (glimit - cscore > 30) {
227 msg("YOU SKUNKED ME!");
228 pgames += 2;
229 }
230 else {
231 msg("YOU WON!");
7bbc756f
KA
232 ++pgames;
233 }
234 iwon = FALSE;
235 }
5836528f
KA
236 else {
237 if (glimit - pscore > 60) {
238 msg("I DOUBLE SKUNKED YOU!");
239 cgames += 4;
7bbc756f 240 }
5836528f
KA
241 else if (glimit - pscore > 30) {
242 msg("I SKUNKED YOU!");
243 cgames += 2;
244 }
245 else {
246 msg("I WON!");
7bbc756f
KA
247 ++cgames;
248 }
249 iwon = TRUE;
250 }
b6b51343 251 gamescore();
7bbc756f
KA
252}
253
7bbc756f 254/*
5836528f
KA
255 * playhand:
256 * Do up one hand of the game
7bbc756f 257 */
5836528f
KA
258playhand(mycrib)
259BOOLEAN mycrib;
7bbc756f 260{
5836528f
KA
261 register int deckpos;
262 extern char Msgbuf[];
263
264 werase(Compwin);
7bbc756f
KA
265
266 knownum = 0;
5836528f
KA
267 deckpos = deal(mycrib);
268 sorthand(chand, FULLHAND);
269 sorthand(phand, FULLHAND);
270 makeknown(chand, FULLHAND);
5dc185f5 271 prhand(phand, FULLHAND, Playwin, FALSE);
5836528f
KA
272 discard(mycrib);
273 if (cut(mycrib, deckpos))
274 return TRUE;
275 if (peg(mycrib))
276 return TRUE;
277 werase(Tablewin);
278 wrefresh(Tablewin);
279 if (score(mycrib))
280 return TRUE;
281 return FALSE;
7bbc756f
KA
282}
283
284
285
286/*
287 * deal cards to both players from deck
288 */
289
290deal( mycrib )
291{
292 register int i, j;
293
294 j = 0;
295 for( i = 0; i < FULLHAND; i++ ) {
296 if( mycrib ) {
297 phand[i] = deck[j++];
298 chand[i] = deck[j++];
299 }
300 else {
301 chand[i] = deck[j++];
302 phand[i] = deck[j++];
303 }
304 }
305 return( j );
306}
307
7bbc756f 308/*
5836528f
KA
309 * discard:
310 * Handle players discarding into the crib...
311 * Note: we call cdiscard() after prining first message so player doesn't wait
7bbc756f 312 */
5836528f
KA
313discard(mycrib)
314BOOLEAN mycrib;
7bbc756f 315{
5836528f
KA
316 register char *prompt;
317 CARD crd;
318
f62fed8c 319 prcrib(mycrib, TRUE);
5836528f 320 prompt = (quiet ? "Discard --> " : "Discard a card --> ");
5836528f
KA
321 cdiscard(mycrib); /* puts best discard at end */
322 crd = phand[infrom(phand, FULLHAND, prompt)];
323 remove(crd, phand, FULLHAND);
5dc185f5 324 prhand(phand, FULLHAND, Playwin, FALSE);
7bbc756f 325 crib[0] = crd;
5836528f 326/* next four lines same as last four except for cdiscard() */
5836528f
KA
327 crd = phand[infrom(phand, FULLHAND - 1, prompt)];
328 remove(crd, phand, FULLHAND - 1);
5dc185f5 329 prhand(phand, FULLHAND, Playwin, FALSE);
7bbc756f
KA
330 crib[1] = crd;
331 crib[2] = chand[4];
332 crib[3] = chand[5];
5836528f 333 chand[4].rank = chand[4].suit = chand[5].rank = chand[5].suit = EMPTY;
7bbc756f
KA
334}
335
7bbc756f 336/*
5836528f 337 * cut:
d864f6e8
KA
338 * Cut the deck and set turnover. Actually, we only ASK the
339 * player what card to turn. We do a random one, anyway.
7bbc756f 340 */
5836528f
KA
341cut(mycrib, pos)
342BOOLEAN mycrib;
343int pos;
7bbc756f 344{
5836528f 345 register int i, cardx;
7bbc756f
KA
346 BOOLEAN win = FALSE;
347
5836528f 348 if (mycrib) {
d864f6e8 349 if (!rflag) { /* random cut */
5836528f
KA
350 msg(quiet ? "Cut the deck? " :
351 "How many cards down do you wish to cut the deck? ");
d864f6e8 352 getline();
7bbc756f 353 }
d864f6e8 354 i = (rand() >> 4) % (CARDS - pos);
7bbc756f 355 turnover = deck[i + pos];
5836528f
KA
356 addmsg(quiet ? "You cut " : "You cut the ");
357 msgcard(turnover, FALSE);
358 endmsg();
359 if (turnover.rank == JACK) {
f6867294 360 msg("I get two for his heels");
5836528f 361 win = chkscr(&cscore,2 );
7bbc756f
KA
362 }
363 }
5836528f
KA
364 else {
365 i = (rand() >> 4) % (CARDS - pos) + pos;
7bbc756f 366 turnover = deck[i];
5836528f
KA
367 addmsg(quiet ? "I cut " : "I cut the ");
368 msgcard(turnover, FALSE);
369 endmsg();
370 if (turnover.rank == JACK) {
f6867294 371 msg("You get two for his heels");
5836528f 372 win = chkscr(&pscore, 2);
7bbc756f
KA
373 }
374 }
5836528f 375 makeknown(&turnover, 1);
f62fed8c 376 prcrib(mycrib, FALSE);
5836528f 377 return win;
7bbc756f
KA
378}
379
f62fed8c
KA
380/*
381 * prcrib:
382 * Print out the turnover card with crib indicator
383 */
384prcrib(mycrib, blank)
385BOOLEAN mycrib, blank;
386{
39911be8 387 register int y, cardx;
f62fed8c
KA
388
389 if (mycrib)
390 cardx = CRIB_X;
391 else
f6867294 392 cardx = 0;
f62fed8c
KA
393
394 mvaddstr(CRIB_Y, cardx + 1, "CRIB");
395 prcard(stdscr, CRIB_Y + 1, cardx, turnover, blank);
39911be8
KA
396
397 if (mycrib)
f6867294 398 cardx = 0;
39911be8
KA
399 else
400 cardx = CRIB_X;
401
402 for (y = CRIB_Y; y <= CRIB_Y + 5; y++)
403 mvaddstr(y, cardx, " ");
f62fed8c
KA
404}
405
7bbc756f 406/*
5836528f
KA
407 * peg:
408 * Handle all the pegging...
7bbc756f
KA
409 */
410
5836528f
KA
411static CARD Table[14];
412
413static int Tcnt;
7bbc756f 414
5836528f
KA
415peg(mycrib)
416BOOLEAN mycrib;
7bbc756f 417{
5836528f 418 static CARD ch[CINHAND], ph[CINHAND];
7bbc756f 419 CARD crd;
5836528f 420 register int i, j, k;
fe4cdc5a
KA
421 register int l;
422 register int cnum, pnum, sum;
423 register BOOLEAN myturn, mego, ugo, last, played;
7bbc756f
KA
424
425 cnum = pnum = CINHAND;
5836528f 426 for (i = 0; i < CINHAND; i++) { /* make copies of hands */
7bbc756f
KA
427 ch[i] = chand[i];
428 ph[i] = phand[i];
429 }
5836528f 430 Tcnt = 0; /* index to table of cards played */
7bbc756f
KA
431 sum = 0; /* sum of cards played */
432 mego = ugo = FALSE;
433 myturn = !mycrib;
5836528f 434 for (;;) {
7bbc756f 435 last = TRUE; /* enable last flag */
5dc185f5
KA
436 prhand(ph, pnum, Playwin, FALSE);
437 prhand(ch, cnum, Compwin, TRUE);
fe4cdc5a 438 prtable(sum);
5836528f
KA
439 if (myturn) { /* my tyrn to play */
440 if (!anymove(ch, cnum, sum)) { /* if no card to play */
441 if (!mego && cnum) { /* go for comp? */
f6867294 442 msg("GO");
7bbc756f
KA
443 mego = TRUE;
444 }
5836528f 445 if (anymove(ph, pnum, sum)) /* can player move? */
7bbc756f 446 myturn = !myturn;
5836528f 447 else { /* give him his point */
f6867294 448 msg(quiet ? "You get one" : "You get one point");
5836528f
KA
449 if (chkscr(&pscore, 1))
450 return TRUE;
7bbc756f
KA
451 sum = 0;
452 mego = ugo = FALSE;
5836528f 453 Tcnt = 0;
7bbc756f
KA
454 }
455 }
5836528f 456 else {
7bbc756f
KA
457 played = TRUE;
458 j = -1;
459 k = 0;
5836528f
KA
460 for (i = 0; i < cnum; i++) { /* maximize score */
461 l = pegscore(ch[i], Table, Tcnt, sum);
462 if (l > k) {
7bbc756f
KA
463 k = l;
464 j = i;
465 }
466 }
5836528f
KA
467 if (j < 0) /* if nothing scores */
468 j = cchose(ch, cnum, sum);
7bbc756f 469 crd = ch[j];
5836528f
KA
470 remove(crd, ch, cnum--);
471 sum += VAL(crd.rank);
472 Table[Tcnt++] = crd;
473 if (k > 0) {
86410cf9
KA
474 addmsg(quiet ? "I get %d playing " :
475 "I get %d points playing ", k);
5836528f
KA
476 msgcard(crd, FALSE);
477 endmsg();
478 if (chkscr(&cscore, k))
479 return TRUE;
7bbc756f 480 }
7bbc756f
KA
481 myturn = !myturn;
482 }
483 }
5836528f
KA
484 else {
485 if (!anymove(ph, pnum, sum)) { /* can player move? */
486 if (!ugo && pnum) { /* go for player */
f6867294 487 msg("You have a GO");
7bbc756f
KA
488 ugo = TRUE;
489 }
5836528f 490 if (anymove(ch, cnum, sum)) /* can computer play? */
7bbc756f 491 myturn = !myturn;
5836528f 492 else {
f6867294
KA
493 msg(quiet ? "I get one" : "I get one point");
494 do_wait();
5836528f
KA
495 if (chkscr(&cscore, 1))
496 return TRUE;
7bbc756f
KA
497 sum = 0;
498 mego = ugo = FALSE;
5836528f 499 Tcnt = 0;
7bbc756f
KA
500 }
501 }
5836528f 502 else { /* player plays */
7bbc756f 503 played = FALSE;
5836528f 504 if (pnum == 1) {
7bbc756f 505 crd = ph[0];
5836528f 506 msg("You play your last card");
7bbc756f 507 }
5836528f
KA
508 else
509 for (;;) {
5dc185f5 510 prhand(ph, pnum, Playwin, FALSE);
5836528f
KA
511 crd = ph[infrom(ph, pnum, "Your play: ")];
512 if (sum + VAL(crd.rank) <= 31)
7bbc756f 513 break;
5836528f 514 else
f6867294 515 msg("Total > 31 -- try again");
5836528f
KA
516 }
517 makeknown(&crd, 1);
518 remove(crd, ph, pnum--);
519 i = pegscore(crd, Table, Tcnt, sum);
520 sum += VAL(crd.rank);
521 Table[Tcnt++] = crd;
522 if (i > 0) {
523 msg(quiet ? "You got %d" : "You got %d points", i);
524 if (chkscr(&pscore, i))
525 return TRUE;
7bbc756f 526 }
7bbc756f
KA
527 myturn = !myturn;
528 }
529 }
5836528f 530 if (sum >= 31) {
f6867294
KA
531 if (!myturn)
532 do_wait();
7bbc756f
KA
533 sum = 0;
534 mego = ugo = FALSE;
5836528f 535 Tcnt = 0;
7bbc756f
KA
536 last = FALSE; /* disable last flag */
537 }
5836528f
KA
538 if (!pnum && !cnum)
539 break; /* both done */
540 }
5dc185f5
KA
541 prhand(ph, pnum, Playwin, FALSE);
542 prhand(ch, cnum, Compwin, TRUE);
fe4cdc5a 543 prtable(sum);
5836528f
KA
544 if (last)
545 if (played) {
546 msg(quiet ? "I get one for last" : "I get one point for last");
f6867294 547 do_wait();
5836528f
KA
548 if (chkscr(&cscore, 1))
549 return TRUE;
7bbc756f 550 }
5836528f
KA
551 else {
552 msg(quiet ? "You get one for last" :
553 "You get one point for last");
554 if (chkscr(&pscore, 1))
555 return TRUE;
7bbc756f 556 }
5836528f 557 return FALSE;
7bbc756f
KA
558}
559
7bbc756f 560/*
5836528f
KA
561 * prtable:
562 * Print out the table with the current score
7bbc756f 563 */
5836528f
KA
564prtable(score)
565int score;
566{
5dc185f5 567 prhand(Table, Tcnt, Tablewin, FALSE);
5836528f
KA
568 mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score);
569 wrefresh(Tablewin);
570}
7bbc756f 571
5836528f
KA
572/*
573 * score:
574 * Handle the scoring of the hands
575 */
576score(mycrib)
577BOOLEAN mycrib;
7bbc756f 578{
5836528f
KA
579 sorthand(crib, CINHAND);
580 if (mycrib) {
581 if (plyrhand(phand, "hand"))
582 return TRUE;
583 if (comphand(chand, "hand"))
584 return TRUE;
f6867294 585 do_wait();
5836528f
KA
586 if (comphand(crib, "crib"))
587 return TRUE;
7bbc756f 588 }
5836528f
KA
589 else {
590 if (comphand(chand, "hand"))
591 return TRUE;
592 if (plyrhand(phand, "hand"))
593 return TRUE;
594 if (plyrhand(crib, "crib"))
595 return TRUE;
7bbc756f 596 }
5836528f 597 return FALSE;
7bbc756f 598}