// Display the player's word list, the list of words not found, and the running stats void results(void) { int col, row; int denom1, denom2; move(LIST_LINE, LIST_COL); clrtobot(); printw("Words you found (%d):", npwords); refresh(); move(LIST_LINE + 1, LIST_COL); prtable(pword, npwords, 0, ncols, prword, prwidth); getyx(stdscr, row, col); move(row + 1, col); printw("Words you missed (%d):", nmwords); refresh(); move(row + 2, col); prtable(mword, nmwords, 0, ncols, prword, prwidth); denom1 = npwords + nmwords; denom2 = tnpwords + tnmwords; move(SCORE_LINE, SCORE_COL); printw("Percentage: %0.2f%% (%0.2f%% over %d game%s)\n", denom1 ? (100.0 * npwords) / (double) (npwords + nmwords) : 0.0, denom2 ? (100.0 * tnpwords) / (double) (tnpwords + tnmwords) : 0.0, ngames, ngames > 1 ? "s" : ""); }
void mktables(state_t *state, const char *defvalue, int useprefix) { char prefix[1024]; int char_count; int i; /* make the character map */ map = (int *)malloc(sizeof(int) * 256); for (i = 0; i < 256; i++) map[i] = 0; char_count = mkmap(state); prtable("int", "map", map, 256); printf("\n"); /* make the accept state table */ accepttbl = (const char **)malloc(sizeof(const char *) * state_count); for (i = 0; i < state_count; i++) accepttbl[i] = NULL; mkaccept(state, defvalue); /* print the accept state table */ printf(" static int accepttbl[%d] =\n", state_count); printf(" {\n"); for (i = 0; i < state_count; i++) printf(" %s,\n", accepttbl[i]); printf(" };\n\n"); /* make the prefix table */ if (useprefix) { prefixtbl = (char **)malloc(sizeof(char *) * state_count); for (i = 0; i < state_count; i++) prefixtbl[i] = NULL; mkprefix(state, prefix, 0); /* print the prefix table */ printf(" static const char *prefixtbl[%d] =\n", state_count); printf(" {\n"); for (i = 0; i < state_count; i++) printf(" \"%s\",\n", prefixtbl[i]); printf(" };\n\n"); } /* make the state transition tables */ basetbl = (int *)malloc(sizeof(int) * state_count); nexttbl = (int *)malloc(sizeof(int) * (state_count + char_count)); checktbl = (int *)malloc(sizeof(int) * (state_count + char_count)); for (i = 0; i < state_count; i++) { basetbl[i] = -1; } for (i = 0; i < (state_count + char_count); i++) { nexttbl[i] = 0; checktbl[i] = -1; } mktranstables(state); prtable("int", "basetbl", basetbl, state_count); printf("\n"); prtable("int", "nexttbl", nexttbl, state_count + char_count); printf("\n"); prtable("int", "checktbl", checktbl, state_count + char_count); }
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); }