subs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* $NetBSD: subs.c,v 1.14 2003/08/07 09:36:57 agc Exp $ */
  2. /*
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)subs.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: subs.c,v 1.14 2003/08/07 09:36:57 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include "back.h"
  39. int buffnum = -1;
  40. char outbuff[BUFSIZ];
  41. static const char plred[] = "Player is red, computer is white.";
  42. static const char plwhite[] = "Player is white, computer is red.";
  43. static const char nocomp[] = "(No computer play.)";
  44. const char *const descr[] = {
  45. "Usage: backgammon [-] [n r w b pr pw pb t3a]\n",
  46. "\t-\tgets this list\n\tn\tdon't ask for rules or instructions",
  47. "\tr\tplayer is red (implies n)\n\tw\tplayer is white (implies n)",
  48. "\tb\ttwo players, red and white (implies n)",
  49. "\tpr\tprint the board before red's turn",
  50. "\tpw\tprint the board before white's turn",
  51. "\tpb\tprint the board before both player's turn",
  52. "\tterm\tterminal is a term",
  53. "\tsfile\trecover saved game from file",
  54. 0
  55. };
  56. void
  57. errexit(s)
  58. const char *s;
  59. {
  60. write(2, "\n", 1);
  61. perror(s);
  62. getout(0);
  63. }
  64. int
  65. addbuf(c)
  66. int c;
  67. {
  68. buffnum++;
  69. if (buffnum == BUFSIZ) {
  70. if (write(1, outbuff, BUFSIZ) != BUFSIZ)
  71. errexit("addbuf (write):");
  72. buffnum = 0;
  73. }
  74. outbuff[buffnum] = c;
  75. return (0);
  76. }
  77. void
  78. buflush()
  79. {
  80. if (buffnum < 0)
  81. return;
  82. buffnum++;
  83. if (write(1, outbuff, buffnum) != buffnum)
  84. errexit("buflush (write):");
  85. buffnum = -1;
  86. }
  87. int
  88. readc()
  89. {
  90. char c;
  91. if (tflag) {
  92. cline();
  93. newpos();
  94. }
  95. buflush();
  96. if (read(0, &c, 1) != 1)
  97. errexit("readc");
  98. #ifdef WHY_IS_THIS_HARDWIRED_IN_HERE
  99. if (c == '\177')
  100. getout(0);
  101. #endif
  102. if (c == '\033' || c == '\015')
  103. return ('\n');
  104. if (cflag)
  105. return (c);
  106. if (c == '\014')
  107. return ('R');
  108. if (c >= 'a' && c <= 'z')
  109. return (c & 0137);
  110. return (c);
  111. }
  112. void
  113. writec(c)
  114. char c;
  115. {
  116. if (tflag)
  117. fancyc(c);
  118. else
  119. addbuf(c);
  120. }
  121. void
  122. writel(l)
  123. const char *l;
  124. {
  125. #ifdef DEBUG
  126. const char *s;
  127. if (trace == NULL)
  128. trace = fopen("bgtrace", "w");
  129. fprintf(trace, "writel: \"");
  130. for (s = l; *s; s++) {
  131. if (*s < ' ' || *s == '\177')
  132. fprintf(trace, "^%c", (*s) ^ 0100);
  133. else
  134. putc(*s, trace);
  135. }
  136. fprintf(trace, "\"\n");
  137. fflush(trace);
  138. #endif
  139. while (*l)
  140. writec(*l++);
  141. }
  142. void
  143. proll()
  144. {
  145. if (d0)
  146. swap;
  147. if (cturn == 1)
  148. writel("Red's roll: ");
  149. else
  150. writel("White's roll: ");
  151. writec(D0 + '0');
  152. writec('\040');
  153. writec(D1 + '0');
  154. if (tflag)
  155. cline();
  156. }
  157. void
  158. wrint(n)
  159. int n;
  160. {
  161. int i, j, t;
  162. for (i = 4; i > 0; i--) {
  163. t = 1;
  164. for (j = 0; j < i; j++)
  165. t *= 10;
  166. if (n > t - 1)
  167. writec((n / t) % 10 + '0');
  168. }
  169. writec(n % 10 + '0');
  170. }
  171. void
  172. gwrite()
  173. {
  174. int r, c;
  175. r = c = 0;
  176. if (tflag) {
  177. r = curr;
  178. c = curc;
  179. curmove(16, 0);
  180. }
  181. if (gvalue > 1) {
  182. writel("Game value: ");
  183. wrint(gvalue);
  184. writel(". ");
  185. if (dlast == -1)
  186. writel(color[0]);
  187. else
  188. writel(color[1]);
  189. writel(" doubled last.");
  190. } else {
  191. switch (pnum) {
  192. case -1: /* player is red */
  193. writel(plred);
  194. break;
  195. case 0: /* player is both colors */
  196. writel(nocomp);
  197. break;
  198. case 1: /* player is white */
  199. writel(plwhite);
  200. }
  201. }
  202. if (rscore || wscore) {
  203. writel(" ");
  204. wrscore();
  205. }
  206. if (tflag) {
  207. cline();
  208. curmove(r, c);
  209. }
  210. }
  211. int
  212. quit()
  213. {
  214. if (tflag) {
  215. curmove(20, 0);
  216. clend();
  217. } else
  218. writec('\n');
  219. writel("Are you sure you want to quit?");
  220. if (yorn(0)) {
  221. if (rfl) {
  222. writel("Would you like to save this game?");
  223. if (yorn(0))
  224. save(0);
  225. }
  226. cturn = 0;
  227. return (1);
  228. }
  229. return (0);
  230. }
  231. int
  232. yorn(special)
  233. char special; /* special response */
  234. {
  235. char c;
  236. int i;
  237. i = 1;
  238. while ((c = readc()) != 'Y' && c != 'N') {
  239. if (special && c == special)
  240. return (2);
  241. if (i) {
  242. if (special) {
  243. writel(" (Y, N, or ");
  244. writec(special);
  245. writec(')');
  246. } else
  247. writel(" (Y or N)");
  248. i = 0;
  249. } else
  250. writec('\007');
  251. }
  252. if (c == 'Y')
  253. writel(" Yes.\n");
  254. else
  255. writel(" No.\n");
  256. if (tflag)
  257. buflush();
  258. return (c == 'Y');
  259. }
  260. void
  261. wrhit(i)
  262. int i;
  263. {
  264. writel("Blot hit on ");
  265. wrint(i);
  266. writec('.');
  267. writec('\n');
  268. }
  269. void
  270. nexturn()
  271. {
  272. int c;
  273. cturn = -cturn;
  274. c = cturn / abs(cturn);
  275. home = bar;
  276. bar = 25 - bar;
  277. offptr += c;
  278. offopp -= c;
  279. inptr += c;
  280. inopp -= c;
  281. Colorptr += c;
  282. colorptr += c;
  283. }
  284. void
  285. getarg(arg)
  286. char ***arg;
  287. {
  288. char **s;
  289. /* process arguments here. dashes are ignored, nbrw are ignored if
  290. * the game is being recovered */
  291. s = *arg;
  292. while (*s && s[0][0] == '-') {
  293. switch (s[0][1]) {
  294. /* don't ask if rules or instructions needed */
  295. case 'n':
  296. if (rflag)
  297. break;
  298. aflag = 0;
  299. args[acnt++] = 'n';
  300. break;
  301. /* player is both red and white */
  302. case 'b':
  303. if (rflag)
  304. break;
  305. pnum = 0;
  306. aflag = 0;
  307. args[acnt++] = 'b';
  308. break;
  309. /* player is red */
  310. case 'r':
  311. if (rflag)
  312. break;
  313. pnum = -1;
  314. aflag = 0;
  315. args[acnt++] = 'r';
  316. break;
  317. /* player is white */
  318. case 'w':
  319. if (rflag)
  320. break;
  321. pnum = 1;
  322. aflag = 0;
  323. args[acnt++] = 'w';
  324. break;
  325. /* print board after move according to following
  326. * character */
  327. case 'p':
  328. if (s[0][2] != 'r' && s[0][2] != 'w' && s[0][2] != 'b')
  329. break;
  330. args[acnt++] = 'p';
  331. args[acnt++] = s[0][2];
  332. if (s[0][2] == 'r')
  333. bflag = 1;
  334. if (s[0][2] == 'w')
  335. bflag = -1;
  336. if (s[0][2] == 'b')
  337. bflag = 0;
  338. break;
  339. case 't':
  340. if (s[0][2] == '\0') { /* get terminal caps */
  341. s++;
  342. tflag = getcaps(*s);
  343. } else
  344. tflag = getcaps(&s[0][2]);
  345. break;
  346. case 's':
  347. s++;
  348. /* recover file */
  349. if (s[0] == NULL) {
  350. writel("No save file named\n");
  351. getout(0);
  352. } else
  353. recover(s[0]);
  354. break;
  355. }
  356. s++;
  357. }
  358. if (s[0] != 0)
  359. recover(s[0]);
  360. }
  361. void
  362. init()
  363. {
  364. int i;
  365. for (i = 0; i < 26;)
  366. board[i++] = 0;
  367. board[1] = 2;
  368. board[6] = board[13] = -5;
  369. board[8] = -3;
  370. board[12] = board[19] = 5;
  371. board[17] = 3;
  372. board[24] = -2;
  373. off[0] = off[1] = -15;
  374. in[0] = in[1] = 5;
  375. gvalue = 1;
  376. dlast = 0;
  377. }
  378. void
  379. wrscore()
  380. {
  381. writel("Score: ");
  382. writel(color[1]);
  383. writec(' ');
  384. wrint(rscore);
  385. writel(", ");
  386. writel(color[0]);
  387. writec(' ');
  388. wrint(wscore);
  389. }
  390. void
  391. fixtty(t)
  392. struct termios *t;
  393. {
  394. if (tflag)
  395. newpos();
  396. buflush();
  397. if (tcsetattr(0, TCSADRAIN, t) < 0)
  398. errexit("fixtty");
  399. }
  400. void
  401. getout(dummy)
  402. int dummy __attribute__((__unused__));
  403. {
  404. /* go to bottom of screen */
  405. if (tflag) {
  406. curmove(23, 0);
  407. cline();
  408. } else
  409. writec('\n');
  410. /* fix terminal status */
  411. fixtty(&old);
  412. exit(0);
  413. }
  414. void
  415. roll()
  416. {
  417. char c;
  418. int row;
  419. int col;
  420. row = col = 0;
  421. if (iroll) {
  422. if (tflag) {
  423. row = curr;
  424. col = curc;
  425. curmove(17, 0);
  426. } else
  427. writec('\n');
  428. writel("ROLL: ");
  429. c = readc();
  430. if (c != '\n') {
  431. while (c < '1' || c > '6')
  432. c = readc();
  433. D0 = c - '0';
  434. writec(' ');
  435. writec(c);
  436. c = readc();
  437. while (c < '1' || c > '6')
  438. c = readc();
  439. D1 = c - '0';
  440. writec(' ');
  441. writec(c);
  442. if (tflag) {
  443. curmove(17, 0);
  444. cline();
  445. curmove(row, col);
  446. } else
  447. writec('\n');
  448. return;
  449. }
  450. if (tflag) {
  451. curmove(17, 0);
  452. cline();
  453. curmove(row, col);
  454. } else
  455. writec('\n');
  456. }
  457. D0 = rnum(6) + 1;
  458. D1 = rnum(6) + 1;
  459. d0 = 0;
  460. }