/* * 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); }
/* * prtable: * Print out the table with the current score */ void prtable(int score) { prhand(Table, Tcnt, Tablewin, FALSE); mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score); wrefresh(Tablewin); }
/* * playhand: * Do up one hand of the game */ int playhand(bool mycrib) { int deckpos; werase(Compwin); wrefresh(Compwin); werase(Tablewin); wrefresh(Tablewin); knownum = 0; deckpos = deal(mycrib); sorthand(chand, FULLHAND); sorthand(phand, FULLHAND); makeknown(chand, FULLHAND); prhand(phand, FULLHAND, Playwin, FALSE); discard(mycrib); if (cut(mycrib, deckpos)) return TRUE; if (peg(mycrib)) return TRUE; werase(Tablewin); wrefresh(Tablewin); if (score(mycrib)) return TRUE; return FALSE; }
/* * 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)); }
/* * discard: * Handle players discarding into the crib... * Note: we call cdiscard() after prining first message so player doesn't wait */ void discard(bool mycrib) { char *prompt; CARD crd; prcrib(mycrib, TRUE); 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); crib[0] = crd; /* 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); crib[1] = crd; crib[2] = chand[4]; crib[3] = chand[5]; chand[4].rank = chand[4].suit = chand[5].rank = chand[5].suit = EMPTY; }
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); }