int yorn(int special) { char c; int i; i = 1; while ((c = readc()) != 'Y' && c != 'N') { if (special && c == special) return (2); if (i) { if (special) { writel(" (Y, N, or "); writec(special); writec(')'); } else writel(" (Y or N)"); i = 0; } else writec('\007'); } if (c == 'Y') writel(" Yes.\n"); else writel(" No.\n"); if (tflag) buflush(); return (c == 'Y'); }
int readc(void) { char c; if (tflag) { cline(); newpos(); } buflush(); if (read(0, &c, 1) != 1) errexit("readc"); #ifdef WHY_IS_THIS_HARDWIRED_IN_HERE if (c == '\177') getout(0); #endif if (c == '\033' || c == '\015') return ('\n'); if (cflag) return (c); if (c == '\014') return ('R'); if (c >= 'a' && c <= 'z') return (c & 0137); return (c); }
void fixtty(struct termios *t) { if (tflag) newpos(); buflush(); if (tcsetattr(0, TCSADRAIN, t) < 0) errexit("fixtty"); }
/* * dble() * Have the current player double and ask opponent to accept. */ void dble () { register int resp; /* response to y/n */ for (;;) { writel (" doubles."); /* indicate double */ if (cturn == -pnum) { /* see if computer accepts */ if (dblgood()) { /* guess not */ writel (" Declined.\n"); nexturn(); cturn *= -2; /* indicate loss */ return; } else { /* computer accepts */ writel (" Accepted.\n"); gvalue *= 2; /* double game value */ dlast = cturn; if (tflag) gwrite(); return; } } /* ask if player accepts */ writel (" Does "); writel (cturn == 1? color[2]: color[3]); writel (" accept?"); /* get response from yorn, * a "2" means he said "p" * for print board. */ if ((resp = yorn ('R')) == 2) { writel (" Reprint.\n"); buflush(); wrboard(); writel (*Colorptr); continue; } /* check response */ if (resp) { /* accepted */ gvalue *= 2; dlast = cturn; if (tflag) gwrite(); return; } nexturn (); /* declined */ cturn *= -2; return; } }
void refresh(void) { int i, r, c; r = curr; /* save current position */ c = curc; for (i = 12; i > 6; i--)/* fix positions 12-7 */ if (board[i] != oldb[i]) { fixpos(oldb[i], board[i], 13, 1 + (12 - i) * 4, -1); oldb[i] = board[i]; } if (board[0] != oldb[0]) { /* fix red men on bar */ fixpos(oldb[0], board[0], 13, 25, -1); oldb[0] = board[0]; } for (i = 6; i > 0; i--) /* fix positions 6-1 */ if (board[i] != oldb[i]) { fixpos(oldb[i], board[i], 13, 29 + (6 - i) * 4, -1); oldb[i] = board[i]; } i = -(off[0] < 0 ? off[0] + 15 : off[0]); /* fix white's home */ if (oldw != i) { fixpos(oldw, i, 13, 54, -1); oldw = i; } for (i = 13; i < 19; i++) /* fix positions 13-18 */ if (board[i] != oldb[i]) { fixpos(oldb[i], board[i], 3, 1 + (i - 13) * 4, 1); oldb[i] = board[i]; } if (board[25] != oldb[25]) { /* fix white men on bar */ fixpos(oldb[25], board[25], 3, 25, 1); oldb[25] = board[25]; } for (i = 19; i < 25; i++) /* fix positions 19-24 */ if (board[i] != oldb[i]) { fixpos(oldb[i], board[i], 3, 29 + (i - 19) * 4, 1); oldb[i] = board[i]; } i = (off[1] < 0 ? off[1] + 15 : off[1]); /* fix red's home */ if (oldr != i) { fixpos(oldr, i, 3, 54, 1); oldr = i; } curmove(r, c); /* return to saved position */ newpos(); buflush(); }
/* zero if first move */ void move(int okay) { int i; /* index */ int l; /* last man */ l = 0; if (okay) { /* see if comp should double */ if (gvalue < 64 && dlast != cturn && dblgood()) { writel(*Colorptr); dble(); /* double */ /* return if declined */ if (cturn != 1 && cturn != -1) return; } roll(); } race = 0; for (i = 0; i < 26; i++) { if (board[i] < 0) l = i; } for (i = 0; i < l; i++) { if (board[i] > 0) break; } if (i == l) race = 1; /* print roll */ if (tflag) curmove(cturn == -1 ? 18 : 19, 0); writel(*Colorptr); writel(" rolls "); writec(D0 + '0'); writec(' '); writec(D1 + '0'); /* make tty interruptable while thinking */ if (tflag) cline(); fixtty(noech); /* find out how many moves */ mvlim = movallow(); if (mvlim == 0) { writel(" but cannot use it.\n"); nexturn(); fixtty(raw); return; } /* initialize */ for (i = 0; i < 4; i++) cp[i] = cg[i] = 0; /* strategize */ trymove(0, 0); pickmove(); /* print move */ writel(" and moves "); for (i = 0; i < mvlim; i++) { if (i > 0) writec(','); wrint(p[i] = cp[i]); writec('-'); wrint(g[i] = cg[i]); makmove(i); } writec('.'); /* print blots hit */ if (tflag) curmove(20, 0); else writec('\n'); for (i = 0; i < mvlim; i++) if (h[i]) wrhit(g[i]); /* get ready for next move */ nexturn(); if (!okay) { buflush(); sleep(3); } fixtty(raw); /* no more tty interrupt */ }
/* zero if first move */ void move(struct move *mm, int okay) { int i; /* index */ int l; /* last man */ l = 0; if (okay) { /* see if comp should double */ if (gvalue < 64 && dlast != cturn && dblgood()) { writel(*Colorptr); dble(); /* double */ /* return if declined */ if (cturn != 1 && cturn != -1) return; } roll(mm); } race = 0; for (i = 0; i < 26; i++) { if (board[i] < 0) l = i; } for (i = 0; i < l; i++) { if (board[i] > 0) break; } if (i == l) race = 1; /* print roll */ if (tflag) curmove(cturn == -1 ? 18 : 19, 0); writel(*Colorptr); writel(" rolls "); writec(mm->D0 + '0'); writec(' '); writec(mm->D1 + '0'); /* make tty interruptable while thinking */ if (tflag) cline(); fixtty(&noech); /* find out how many moves */ mm->mvlim = movallow(mm); if (mm->mvlim == 0) { writel(" but cannot use it.\n"); nexturn(); fixtty(&raw); return; } /* initialize */ for (i = 0; i < 4; i++) cp[i] = cg[i] = 0; /* strategize */ trymove(mm, 0, 0); pickmove(mm); /* print move */ writel(" and moves "); for (i = 0; i < mm->mvlim; i++) { if (i > 0) writec(','); wrint(mm->p[i] = cp[i]); writec('-'); wrint(mm->g[i] = cg[i]); makmove(mm, i); /* * This assertion persuades gcc 4.5 that the loop * doesn't result in signed overflow of i. mvlim * isn't, or at least shouldn't be, changed by makmove * at all. */ assert(mm->mvlim >= 0 && mm->mvlim <= 5); } writec('.'); /* print blots hit */ if (tflag) curmove(20, 0); else writec('\n'); for (i = 0; i < mm->mvlim; i++) if (mm->h[i]) wrhit(mm->g[i]); /* get ready for next move */ nexturn(); if (!okay) { buflush(); sleep(3); } fixtty(&raw); /* no more tty interrupt */ }