1 /* $OpenBSD: subs.c,v 1.22 2015/12/02 20:05:01 tb Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include "back.h" 33 34 __dead void usage(void); 35 36 int buffnum; 37 char outbuff[BUFSIZ]; 38 39 static const char plred[] = "Player is red, computer is white."; 40 static const char plwhite[] = "Player is white, computer is red."; 41 static const char nocomp[] = "(No computer play.)"; 42 43 void 44 errexit(const char *s) 45 { 46 write(STDERR_FILENO, "\n", 1); 47 perror(s); 48 getout(0); 49 } 50 51 int 52 readc(void) 53 { 54 int c; 55 56 clrtoeol(); 57 refresh(); 58 c = getch(); 59 if (c == '\004' || c == ERR) /* ^D or failure */ 60 getout(0); 61 if (c == '\033' || c == '\015') 62 return('\n'); 63 if (cflag) 64 return(c); 65 if (c == '\014') 66 return('R'); 67 if (c >= 'a' && c <= 'z') 68 return(c & 0137); /* upper case */ 69 return(c); 70 } 71 72 void 73 proll(void) 74 { 75 if (d0) 76 swap; 77 if (cturn == 1) 78 printw("Red's roll: "); 79 else 80 printw("White's roll: "); 81 printw("%d,%d", D0, D1); 82 clrtoeol(); 83 } 84 85 void 86 gwrite(void) 87 { 88 int r, c; 89 90 getyx(stdscr, r, c); 91 move(16, 0); 92 if (gvalue > 1) { 93 printw("Game value: %d. ", gvalue); 94 if (dlast == -1) 95 addstr(color[0]); 96 else 97 addstr(color[1]); 98 addstr(" doubled last."); 99 } else { 100 if (!dflag) 101 printw("[No doubling.] "); 102 switch (pnum) { 103 case -1: /* player is red */ 104 addstr(plred); 105 break; 106 case 0: /* player is both colors */ 107 addstr(nocomp); 108 break; 109 case 1: /* player is white */ 110 addstr(plwhite); 111 } 112 } 113 if (rscore || wscore) { 114 addstr(" "); 115 wrscore(); 116 } 117 clrtoeol(); 118 move(r, c); 119 } 120 121 int 122 quit(void) 123 { 124 move(20, 0); 125 clrtobot(); 126 addstr("Are you sure you want to quit?"); 127 if (yorn(0)) { 128 if (rfl) { 129 addstr("Would you like to save this game?"); 130 if (yorn(0)) 131 save(0); 132 } 133 cturn = 0; 134 return(1); 135 } 136 return(0); 137 } 138 139 int 140 yorn(char special) 141 { 142 char c; 143 int i; 144 145 i = 1; 146 while ((c = readc()) != 'Y' && c != 'N') { 147 if (special && c == special) 148 return(2); 149 if (i) { 150 if (special) 151 printw(" (Y, N, or %c)", special); 152 else 153 printw(" (Y or N)"); 154 i = 0; 155 } else 156 beep(); 157 } 158 if (c == 'Y') 159 addstr(" Yes.\n"); 160 else 161 addstr(" No.\n"); 162 refresh(); 163 return(c == 'Y'); 164 } 165 166 void 167 wrhit(int i) 168 { 169 printw("Blot hit on %d.\n", i); 170 } 171 172 void 173 nexturn(void) 174 { 175 int c; 176 177 cturn = -cturn; 178 c = cturn / abs(cturn); 179 home = bar; 180 bar = 25 - bar; 181 offptr += c; 182 offopp -= c; 183 inptr += c; 184 inopp -= c; 185 Colorptr += c; 186 colorptr += c; 187 } 188 189 void 190 getarg(int argc, char **argv) 191 { 192 int ch; 193 194 while ((ch = getopt(argc, argv, "bdnrs:w")) != -1) 195 switch(ch) { 196 case 'n': /* don't ask if rules or instructions needed */ 197 if (rflag) 198 break; 199 aflag = 0; 200 break; 201 202 case 'b': /* player is both red and white */ 203 if (rflag) 204 break; 205 pnum = 0; 206 aflag = 0; 207 break; 208 209 case 'r': /* player is red */ 210 if (rflag) 211 break; 212 pnum = -1; 213 aflag = 0; 214 break; 215 216 case 'w': /* player is white */ 217 if (rflag) 218 break; 219 pnum = 1; 220 aflag = 0; 221 break; 222 223 case 's': /* restore saved game */ 224 recover(optarg); 225 break; 226 227 case 'd': /* disable doubling */ 228 dflag = 0; 229 aflag = 0; 230 break; 231 232 default: /* print cmdline options */ 233 usage(); 234 } /* end switch */ 235 } 236 237 void 238 usage(void) 239 { 240 extern char *__progname; 241 242 fprintf(stderr, "usage: %s [-bdnrw] [-s file]\n", __progname); 243 exit(1); 244 } 245 246 void 247 init(void) 248 { 249 int i; 250 251 for (i = 0; i < 26;) 252 board[i++] = 0; 253 board[1] = 2; 254 board[6] = board[13] = -5; 255 board[8] = -3; 256 board[12] = board[19] = 5; 257 board[17] = 3; 258 board[24] = -2; 259 off[0] = off[1] = -15; 260 in[0] = in[1] = 5; 261 gvalue = 1; 262 dlast = 0; 263 } 264 265 void 266 wrscore(void) 267 { 268 printw("Score: %s %d, %s %d", color[1], rscore, color[0], wscore); 269 } 270 271 272 void 273 getout(int dummy) 274 { 275 /* go to bottom of screen */ 276 move(23, 0); 277 clrtoeol(); 278 279 endwin(); 280 exit(0); 281 } 282 283 void 284 roll(void) 285 { 286 char c; 287 int row; 288 int col; 289 290 if (iroll) { 291 getyx(stdscr, row, col); 292 mvprintw(17, 0, "ROLL: "); 293 c = readc(); 294 if (c != '\n') { 295 while (c < '1' || c > '6') 296 c = readc(); 297 D0 = c - '0'; 298 printw(" %c", c); 299 c = readc(); 300 while (c < '1' || c > '6') 301 c = readc(); 302 D1 = c - '0'; 303 printw(" %c", c); 304 move(17, 0); 305 clrtoeol(); 306 move(row, col); 307 return; 308 } 309 move(17, 0); 310 clrtoeol(); 311 move(row, col); 312 } 313 D0 = rnum(6) + 1; 314 D1 = rnum(6) + 1; 315 d0 = 0; 316 } 317