/* * plyrhand: * Evaluate and score a player hand or crib */ int plyrhand(CARD hand[], char *s) { static char prompt[BUFSIZ]; int i, j; bool win; prhand(hand, CINHAND, Playwin, FALSE); (void) snprintf(prompt, sizeof prompt, "Your %s scores ", s); i = scorehand(hand, turnover, CINHAND, strcmp(s, "crib") == 0, explain); if ((j = number(0, 29, prompt)) == 19) j = 0; if (i != j) { if (i < j) { win = chkscr(&pscore, i); if (!win) { msg("It's really only %d points; I get %d", i, 2); win = chkscr(&cscore, 2); } else msg("It's really only %d points.", i); } else { win = chkscr(&pscore, j); msg("You should have taken %d, not %d!", i, j); if (!win && muggins) { msg("Muggins! I score %d", i - j); win = chkscr(&cscore, i - j); } } if (explain) msg("Explanation: %s", expl_string); do_wait(); } else win = chkscr(&pscore, i); return (win); }
/* * cut: * Cut the deck and set turnover. Actually, we only ASK the * player what card to turn. We do a random one, anyway. */ int cut(bool mycrib, int pos) { int i; bool win; win = FALSE; if (mycrib) { if (!rflag) { /* random cut */ char *foo; /* This is silly, but we should parse user input, * even if we're not actually going to use it. */ do { msg(quiet ? "Cut the deck? " : "How many cards down do you wish to cut the deck? "); foo = get_line(); if (*foo != '\0' && ((i = atoi(foo)) < 4 || i > 36)) msg("Invalid cut"); else *foo = '\0'; } while (*foo != '\0'); } i = arc4random_uniform(CARDS - pos); turnover = deck[i + pos]; addmsg(quiet ? "You cut " : "You cut the "); msgcard(turnover, FALSE); endmsg(); prcrib(mycrib, FALSE); if (turnover.rank == JACK) { msg("I get two for his heels"); win = chkscr(&cscore, 2); } } else { i = arc4random_uniform(CARDS - pos) + pos; turnover = deck[i]; addmsg(quiet ? "I cut " : "I cut the "); msgcard(turnover, FALSE); endmsg(); prcrib(mycrib, FALSE); if (turnover.rank == JACK) { msg("You get two for his heels"); win = chkscr(&pscore, 2); } } makeknown(&turnover, 1); return (win); }
/* * comphand: * Handle scoring and displaying the computers hand */ int comphand(CARD h[], char *s) { int j; j = scorehand(h, turnover, CINHAND, strcmp(s, "crib") == 0, FALSE); prhand(h, CINHAND, Compwin, FALSE); msg("My %s scores %d", s, (j == 0 ? 19 : j)); return (chkscr(&cscore, j)); }
int peg(bool mycrib) { static CARD ch[CINHAND], ph[CINHAND]; int i, j, k; int l; int cnum, pnum, sum; bool myturn, mego, ugo, last, played; CARD crd; played = FALSE; cnum = pnum = CINHAND; for (i = 0; i < CINHAND; i++) { /* make copies of hands */ ch[i] = chand[i]; ph[i] = phand[i]; } Tcnt = 0; /* index to table of cards played */ sum = 0; /* sum of cards played */ mego = ugo = FALSE; myturn = !mycrib; for (;;) { last = TRUE; /* enable last flag */ prhand(ph, pnum, Playwin, FALSE); prhand(ch, cnum, Compwin, TRUE); prtable(sum); if (myturn) { if (!anymove(ch, cnum, sum)) { /* if no card to play */ if (!mego && cnum) { /* go for comp? */ msg("GO"); mego = TRUE; } /* can player move? */ if (anymove(ph, pnum, sum)) myturn = !myturn; else { /* give him his point */ msg(quiet ? "You get one" : "You get one point"); do_wait(); if (chkscr(&pscore, 1)) return TRUE; sum = 0; mego = ugo = FALSE; Tcnt = 0; } } else { played = TRUE; j = -1; k = 0; /* maximize score */ for (i = 0; i < cnum; i++) { l = pegscore(ch[i], Table, Tcnt, sum); if (l > k) { k = l; j = i; } } if (j < 0) /* if nothing scores */ j = cchose(ch, cnum, sum); crd = ch[j]; cremove(crd, ch, cnum--); sum += VAL(crd.rank); Table[Tcnt++] = crd; if (k > 0) { addmsg(quiet ? "I get %d playing " : "I get %d points playing ", k); msgcard(crd, FALSE); endmsg(); prhand(ph, pnum, Playwin, FALSE); prhand(ch, cnum, Compwin, TRUE); prtable(sum); if (chkscr(&cscore, k)) return TRUE; } myturn = !myturn; } } else { if (!anymove(ph, pnum, sum)) { /* can player move? */ if (!ugo && pnum) { /* go for player */ msg("You have a GO"); ugo = TRUE; } /* can computer play? */ if (anymove(ch, cnum, sum)) myturn = !myturn; else { msg(quiet ? "I get one" : "I get one point"); do_wait(); prhand(ph, pnum, Playwin, FALSE); prhand(ch, cnum, Compwin, TRUE); prtable(sum); if (chkscr(&cscore, 1)) return TRUE; sum = 0; mego = ugo = FALSE; Tcnt = 0; } } else { /* player plays */ played = FALSE; if (pnum == 1) { crd = ph[0]; msg("You play your last card"); } else for (;;) { prhand(ph, pnum, Playwin, FALSE); crd = ph[infrom(ph, pnum, "Your play: ")]; if (sum + VAL(crd.rank) <= 31) break; else msg("Total > 31 -- try again"); } makeknown(&crd, 1); cremove(crd, ph, pnum--); i = pegscore(crd, Table, Tcnt, sum); sum += VAL(crd.rank); Table[Tcnt++] = crd; if (i > 0) { msg(quiet ? "You got %d" : "You got %d points", i); if (pnum == 0) do_wait(); prhand(ph, pnum, Playwin, FALSE); prhand(ch, cnum, Compwin, TRUE); prtable(sum); if (chkscr(&pscore, i)) return TRUE; } myturn = !myturn; } } if (sum >= 31) { if (!myturn) do_wait(); sum = 0; mego = ugo = FALSE; Tcnt = 0; last = FALSE; /* disable last flag */ } if (!pnum && !cnum) break; /* both done */ } prhand(ph, pnum, Playwin, FALSE); prhand(ch, cnum, Compwin, TRUE); prtable(sum); if (last) { if (played) { msg(quiet ? "I get one for last" : "I get one point for last"); do_wait(); if (chkscr(&cscore, 1)) return TRUE; } else { msg(quiet ? "You get one for last" : "You get one point for last"); do_wait(); if (chkscr(&pscore, 1)) return TRUE; } } return (FALSE); }