void chkwinner(int player, const int *hand) { int cb, i, ub; for (i = 0; i < RANKS; ++i) if (hand[i] > 0 && hand[i] < CARDS) return; printplayer(player); (void)printf("don't have any more cards!\n"); (void)printf("My books:"); cb = countbooks(comphand); (void)printf("Your books:"); ub = countbooks(userhand); (void)printf("\nI have %d, you have %d.\n", cb, ub); if (ub > cb) { (void)printf("\nYou win!!!\n"); if (nrandom(1024) == 0723) (void)printf("Cheater, cheater, pumpkin eater!\n"); } else if (cb > ub) { (void)printf("\nI win!!!\n"); if (nrandom(1024) == 0723) (void)printf("Hah! Stupid peasant!\n"); } else (void)printf("\nTie!\n"); exit(0); }
int usermove(void) { int n; const char *const *p; char buf[256]; (void)printf("\nYour hand is:"); printhand(userhand); for (;;) { (void)printf("You ask me for: "); (void)fflush(stdout); if (fgets(buf, sizeof(buf), stdin) == NULL) exit(0); if (buf[0] == '\0') continue; if (buf[0] == '\n') { (void)printf("%d cards in my hand, %d in the pool.\n", countcards(comphand), curcard); (void)printf("My books:"); (void)countbooks(comphand); continue; } buf[strlen(buf) - 1] = '\0'; if (!strcasecmp(buf, "p") && !promode) { promode = 1; (void)printf("Entering pro mode.\n"); continue; } if (!strcasecmp(buf, "quit")) exit(0); for (p = cards; *p; ++p) if (!strcasecmp(*p, buf)) break; if (!*p) { (void)printf("I don't understand!\n"); continue; } n = p - cards; if (userhand[n]) { userasked[n] = 1; return(n); } if (nrandom(3) == 1) (void)printf("You don't have any of those!\n"); else (void)printf("You don't have any %s's!\n", cards[n]); if (nrandom(4) == 1) (void)printf("No cheating!\n"); (void)printf("Guess again.\n"); } /* NOTREACHED */ }
int main(int argc, char *argv[]) { char *str, flg; int leng = LENG, seed = SEED, i; long next = SEED; double p, mean = MEAN, sdev = SDEV; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) { if (*(str = *++argv) == '-') { flg = *++str; if (*++str == '\0') { str = *++argv; argc--; } switch (flg) { case 'l': leng = atoi(str); break; case 's': seed = atoi(str); break; case 'm': mean = atof(str); break; case 'v': sdev = atof(str); sdev = sqrt(sdev); break; case 'd': sdev = atof(str); break; case 'h': default: usage(); } } else usage(); } if (seed != 1) next = srnd((unsigned int) seed); for (i = 0;; i++) { p = (double) nrandom(&next); p = mean + sdev * p; fwritef(&p, sizeof(p), 1, stdout); if (i == leng - 1) break; } return (0); }
int promove(void) { int i, max; for (i = 0; i < RANKS; ++i) if (userasked[i] && comphand[i] > 0 && comphand[i] < CARDS) { userasked[i] = 0; return(i); } if (nrandom(3) == 1) { for (i = 0;; ++i) if (comphand[i] && comphand[i] != CARDS) { max = i; break; } while (++i < RANKS) if (comphand[i] != CARDS && comphand[i] > comphand[max]) max = i; return(max); } if (nrandom(1024) == 0723) { for (i = 0; i < RANKS; ++i) if (userhand[i] && comphand[i]) return(i); } for (;;) { for (i = 0; i < RANKS; ++i) if (comphand[i] && comphand[i] != CARDS && !asked[i]) return(i); for (i = 0; i < RANKS; ++i) asked[i] = 0; } /* NOTREACHED */ }
int main(int argc, char *argv[]) { int ch, move; while ((ch = getopt(argc, argv, "ph")) != -1) switch(ch) { case 'p': promode = 1; break; case '?': case 'h': default: usage(); } srandomdev(); instructions(); init(); if (nrandom(2) == 1) { printplayer(COMPUTER); (void)printf("get to start.\n"); goto istart; } printplayer(USER); (void)printf("get to start.\n"); for (;;) { move = usermove(); if (!comphand[move]) { if (gofish(move, USER, userhand)) continue; } else { goodmove(USER, move, userhand, comphand); continue; } istart: for (;;) { move = compmove(); if (!userhand[move]) { if (!gofish(move, COMPUTER, comphand)) break; } else goodmove(COMPUTER, move, comphand, userhand); } } /* NOTREACHED */ }
void init(void) { int i, j, temp; curcard = TOTCARDS; for (i = 0; i < TOTCARDS; ++i) deck[i] = i % RANKS; for (i = 0; i < TOTCARDS - 1; ++i) { j = nrandom(TOTCARDS-i); if (j == 0) continue; temp = deck[i]; deck[i] = deck[i+j]; deck[i+j] = temp; } for (i = 0; i < HANDSIZE; ++i) { ++userhand[deck[--curcard]]; ++comphand[deck[--curcard]]; } }
void lbg(double *x, const int l, const int tnum, double *icb, int icbsize, double *cb, const int ecbsize, const int iter, const int mintnum, const int seed, const int centup, const double delta, const double end) { int i, j, k, it, maxindex, tnum1, tnum2; static int *cntcb, *tindex, size, sizex, sizecb; unsigned long next = SEED; double d0, d1, dl, err, tmp, rand; static double *cb1 = NULL; double *p, *q, *r; if (cb1 == NULL) { cb1 = dgetmem(ecbsize * l); tindex = (int *) dgetmem(tnum); cntcb = (int *) dgetmem(ecbsize); size = l; sizex = tnum; sizecb = ecbsize; } if (l > size) { free(cb1); cb1 = dgetmem(ecbsize * l); size = l; } if (tnum > sizex) { free(tindex); tindex = (int *) dgetmem(tnum); sizex = tnum; } if (sizecb > ecbsize) { free(cb1); free(cntcb); cb1 = dgetmem(ecbsize * l); cntcb = (int *) dgetmem(ecbsize); } movem(icb, cb, sizeof(*icb), icbsize * l); if (seed != 1) next = srnd((unsigned int) seed); for (; icbsize * 2 <= ecbsize;) { q = cb; r = cb + icbsize * l; for (i = 0; i < icbsize; i++) { for (j = 0; j < l; j++) { dl = delta * nrandom(&next); *r = *q - dl; r++; *q = *q + dl; q++; } } icbsize *= 2; d0 = MAXVALUE; for (it = 1; it <= iter; it++) { fillz((double *) cntcb, sizeof(*cntcb), icbsize); d1 = 0.0; p = x; for (i = 0; i < tnum; i++, p += l) { tindex[i] = vq(p, cb, l, icbsize); cntcb[tindex[i]]++; q = cb + tindex[i] * l; d1 += edist(p, q, l); } d1 /= tnum; err = abs((d0 - d1) / d1); if (err < end) break; d0 = d1; fillz(cb1, sizeof(*cb), icbsize * l); p = x; for (i = 0; i < tnum; i++) { q = cb1 + tindex[i] * l; for (j = 0; j < l; j++) *q++ += *p++; } k = maxindex = 0; for (i = 0; i < icbsize; i++) if (cntcb[i] > k) { k = cntcb[i]; maxindex = i; } q = cb; r = cb1; for (i = 0; i < icbsize; i++, r += l, q += l) if (cntcb[i] >= mintnum) for (j = 0; j < l; j++) q[j] = r[j] / (double) cntcb[i]; else { if (centup == 1) { p = cb + maxindex * l; for (j = 0; j < l; j++) { rand = nrandom(&next); q[j] = p[j] + delta * rand; p[j] = p[j] - delta * rand; } } else if (centup == 2) { if (i < icbsize / 2) { p = q + icbsize / 2 * l; tnum1 = cntcb[i]; tnum2 = cntcb[i + icbsize / 2]; for (j = 0; j < l; j++) { tmp = (tnum2 * q[j] + tnum1 * p[j]) / (tnum1 + tnum2); rand = nrandom(&next); q[j] = tmp + delta * rand; p[j] = tmp - delta * rand; } } else { p = q - icbsize / 2 * l; tnum1 = cntcb[i]; tnum2 = cntcb[i - icbsize / 2]; for (j = 0; j < l; j++) { tmp = (tnum2 * q[j] + tnum1 * p[j]) / (tnum1 + tnum2); rand = nrandom(&next); q[j] = tmp + delta * rand; p[j] = tmp - delta * rand; } } } } } if (icbsize == ecbsize) break; } return; }
int main(int argc, char **argv) { int fprd = FPERIOD, iprd = IPERIOD, i, j, seed = SEED; long next = SEED; FILE *fp = stdin; double x, p1, p2, inc, pc; Boolean gauss = GAUSS; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'p': fprd = atoi(*++argv); --argc; break; case 'i': iprd = atoi(*++argv); --argc; break; case 'n': gauss = TR; break; case 's': seed = atoi(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); if (gauss & (seed != 1)) next = srnd((unsigned int) seed); if (freadf(&p1, sizeof(p1), 1, fp) != 1) return (1); pc = p1; for (;;) { if (freadf(&p2, sizeof(p2), 1, fp) != 1) return (0); if ((p1 != 0.0) && (p2 != 0.0)) inc = (p2 - p1) * (double) iprd / (double) fprd; else { inc = 0.0; pc = p2; p1 = 0.0; } for (j = fprd, i = (iprd + 1) / 2; j--;) { if (p1 == 0.0) { if (gauss) x = (double) nrandom(&next); else x = mseq(); } else { if ((pc += 1.0) >= p1) { x = sqrt(p1); pc = pc - p1; } else x = 0.0; } fwritef(&x, sizeof(x), 1, stdout); if (!--i) { p1 += inc; i = iprd; } } p1 = p2; } return 0; }