121218Sdist /*
2*60746Sbostic * Copyright (c) 1980, 1993
3*60746Sbostic * The Regents of the University of California. All rights reserved.
433487Sbostic *
542565Sbostic * %sccs.include.redist.c%
621218Sdist */
76767Srrh
821218Sdist #ifndef lint
9*60746Sbostic static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 05/31/93";
1033487Sbostic #endif /* not lint */
1121218Sdist
126767Srrh #include "back.h"
136767Srrh
146767Srrh char *help2[] = {
156767Srrh " Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
166767Srrh "position, <f> is the finishing position, and <r> is the roll.",
176767Srrh "Remember, each die roll must be moved separately.",
186767Srrh 0
196767Srrh };
206767Srrh
216767Srrh struct state {
226767Srrh char ch;
236767Srrh int fcode;
246767Srrh int newst;
256767Srrh };
266767Srrh
276767Srrh struct state atmata[] = {
286767Srrh
296767Srrh 'R', 1, 0, '?', 7, 0, 'Q', 0, -3, 'B', 8, 25,
306767Srrh '9', 2, 25, '8', 2, 25, '7', 2, 25, '6', 2, 25,
316767Srrh '5', 2, 25, '4', 2, 25, '3', 2, 25, '2', 2, 19,
326767Srrh '1', 2, 15, '0', 2, 25, '.', 0, 0, '9', 2, 25,
336767Srrh '8', 2, 25, '7', 2, 25, '6', 2, 25, '5', 2, 25,
346767Srrh
356767Srrh '4', 2, 25, '3', 2, 25, '2', 2, 25, '1', 2, 25,
366767Srrh '0', 2, 25, '/', 0, 32, '-', 0, 39, '.', 0, 0,
376767Srrh '/', 5, 32, ' ', 6, 3, ',', 6, 3, '\n', 0, -1,
386767Srrh '6', 3, 28, '5', 3, 28, '4', 3, 28, '3', 3, 28,
396767Srrh '2', 3, 28, '1', 3, 28, '.', 0, 0, 'H', 9, 61,
406767Srrh
416767Srrh '9', 4, 61, '8', 4, 61, '7', 4, 61, '6', 4, 61,
426767Srrh '5', 4, 61, '4', 4, 61, '3', 4, 61, '2', 4, 53,
436767Srrh '1', 4, 51, '0', 4, 61, '.', 0, 0, '9', 4, 61,
446767Srrh '8', 4, 61, '7', 4, 61, '6', 4, 61, '5', 4, 61,
456767Srrh '4', 4, 61, '3', 4, 61, '2', 4, 61, '1', 4, 61,
466767Srrh
476767Srrh '0', 4, 61, ' ', 6, 3, ',', 6, 3, '-', 5, 39,
486767Srrh '\n', 0, -1, '.', 0, 0
496767Srrh };
506767Srrh
checkmove(ist)516767Srrh checkmove (ist)
526767Srrh
536767Srrh int ist;
546767Srrh
556767Srrh {
566767Srrh register int j, n;
576767Srrh register char c;
586767Srrh char a;
596767Srrh
606767Srrh domove:
616767Srrh if (ist == 0) {
626767Srrh if (tflag)
636767Srrh curmove (curr,32);
646767Srrh else
656767Srrh writel ("\t\t");
666767Srrh writel ("Move: ");
676767Srrh }
686767Srrh ist = mvl = ncin = 0;
696767Srrh for (j = 0; j < 5; j++)
706767Srrh p[j] = g[j] = -1;
716767Srrh
726767Srrh dochar:
736767Srrh c = readc();
746767Srrh
756767Srrh if (c == 'S') {
766767Srrh raflag = 0;
776767Srrh save (1);
786767Srrh if (tflag) {
796767Srrh curmove (cturn == -1? 18: 19,39);
806767Srrh ist = -1;
816767Srrh goto domove;
826767Srrh } else {
836767Srrh proll ();
846767Srrh ist = 0;
856767Srrh goto domove;
866767Srrh }
876767Srrh }
886767Srrh
896767Srrh if (c == tty.sg_erase && ncin > 0) {
906767Srrh if (tflag)
916767Srrh curmove (curr,curc-1);
926767Srrh else {
936767Srrh if (tty.sg_erase == '\010')
946767Srrh writel ("\010 \010");
956767Srrh else
966767Srrh writec (cin[ncin-1]);
976767Srrh }
986767Srrh ncin--;
996767Srrh n = rsetbrd();
1006767Srrh if (n == 0) {
1016767Srrh n = -1;
1026767Srrh if (tflag)
1036767Srrh refresh();
1046767Srrh }
1056767Srrh if ((ist = n) > 0)
1066767Srrh goto dochar;
1076767Srrh goto domove;
1086767Srrh }
1096767Srrh
1106767Srrh if (c == tty.sg_kill && ncin > 0) {
1116767Srrh if (tflag) {
1126767Srrh refresh();
1136767Srrh curmove (curr,39);
1146767Srrh ist = -1;
1156767Srrh goto domove;
1166767Srrh } else if (tty.sg_erase == '\010') {
1176767Srrh for (j = 0; j < ncin; j++)
1186767Srrh writel ("\010 \010");
1196767Srrh ist = -1;
1206767Srrh goto domove;
1216767Srrh } else {
1226767Srrh writec ('\\');
1236767Srrh writec ('\n');
1246767Srrh proll ();
1256767Srrh ist = 0;
1266767Srrh goto domove;
1276767Srrh }
1286767Srrh }
1296767Srrh
1306767Srrh n = dotable(c,ist);
1316767Srrh if (n >= 0) {
1326767Srrh cin[ncin++] = c;
1336767Srrh if (n > 2)
1346767Srrh if ((! tflag) || c != '\n')
1356767Srrh writec (c);
1366767Srrh ist = n;
1376767Srrh if (n)
1386767Srrh goto dochar;
1396767Srrh else
1406767Srrh goto domove;
1416767Srrh }
1426767Srrh
1436767Srrh if (n == -1 && mvl >= mvlim)
1446767Srrh return(0);
1456767Srrh if (n == -1 && mvl < mvlim-1)
1466767Srrh return(-4);
1476767Srrh
1486767Srrh if (n == -6) {
1496767Srrh if (! tflag) {
1506767Srrh if (movokay(mvl+1)) {
1516767Srrh wrboard();
1526767Srrh movback (mvl+1);
1536767Srrh }
1546767Srrh proll ();
1556767Srrh writel ("\t\tMove: ");
1566767Srrh for (j = 0; j < ncin;)
1576767Srrh writec (cin[j++]);
1586767Srrh } else {
1596767Srrh if (movokay(mvl+1)) {
1606767Srrh refresh();
1616767Srrh movback (mvl+1);
1626767Srrh } else
1636767Srrh curmove (cturn == -1? 18:19,ncin+39);
1646767Srrh }
1656767Srrh ist = n = rsetbrd();
1666767Srrh goto dochar;
1676767Srrh }
1686767Srrh
1696767Srrh if (n != -5)
1706767Srrh return(n);
1716767Srrh writec ('\007');
1726767Srrh goto dochar;
1736767Srrh }
1746767Srrh
dotable(c,i)1756767Srrh dotable (c,i)
1766767Srrh char c;
1776767Srrh register int i;
1786767Srrh
1796767Srrh {
1806767Srrh register int a, j;
1816767Srrh int test;
1826767Srrh
1836767Srrh test = (c == 'R');
1846767Srrh
1856767Srrh while ( (a = atmata[i].ch) != '.') {
1866767Srrh if (a == c || (test && a == '\n')) {
1876767Srrh switch (atmata[i].fcode) {
1886767Srrh
1896767Srrh case 1:
1906767Srrh wrboard();
1916767Srrh if (tflag) {
1926767Srrh curmove (cturn == -1? 18: 19,0);
1936767Srrh proll ();
1946767Srrh writel ("\t\t");
1956767Srrh } else
1966767Srrh proll ();
1976767Srrh break;
1986767Srrh
1996767Srrh case 2:
2006767Srrh if (p[mvl] == -1)
2016767Srrh p[mvl] = c-'0';
2026767Srrh else
2036767Srrh p[mvl] = p[mvl]*10+c-'0';
2046767Srrh break;
2056767Srrh
2066767Srrh case 3:
2076767Srrh if (g[mvl] != -1) {
2086767Srrh if (mvl < mvlim)
2096767Srrh mvl++;
2106767Srrh p[mvl] = p[mvl-1];
2116767Srrh }
2126767Srrh g[mvl] = p[mvl]+cturn*(c-'0');
2136767Srrh if (g[mvl] < 0)
2146767Srrh g[mvl] = 0;
2156767Srrh if (g[mvl] > 25)
2166767Srrh g[mvl] = 25;
2176767Srrh break;
2186767Srrh
2196767Srrh case 4:
2206767Srrh if (g[mvl] == -1)
2216767Srrh g[mvl] = c-'0';
2226767Srrh else
2236767Srrh g[mvl] = g[mvl]*10+c-'0';
2246767Srrh break;
2256767Srrh
2266767Srrh case 5:
2276767Srrh if (mvl < mvlim)
2286767Srrh mvl++;
2296767Srrh p[mvl] = g[mvl-1];
2306767Srrh break;
2316767Srrh
2326767Srrh case 6:
2336767Srrh if (mvl < mvlim)
2346767Srrh mvl++;
2356767Srrh break;
2366767Srrh
2376767Srrh case 7:
2386767Srrh if (tflag)
2396767Srrh curmove (20,0);
2406767Srrh else
2416767Srrh writec ('\n');
2426767Srrh text (help2);
2436767Srrh if (tflag) {
2446767Srrh curmove (cturn == -1? 18: 19,39);
2456767Srrh } else {
2466767Srrh writec ('\n');
2476767Srrh proll();
2486767Srrh writel ("\t\tMove: ");
2496767Srrh }
2506767Srrh break;
2516767Srrh
2526767Srrh case 8:
2536767Srrh p[mvl] = bar;
2546767Srrh break;
2556767Srrh
2566767Srrh case 9:
2576767Srrh g[mvl] = home;
2586767Srrh }
2596767Srrh
2606767Srrh if (! test || a != '\n')
2616767Srrh return (atmata[i].newst);
2626767Srrh else
2636767Srrh return (-6);
2646767Srrh }
2656767Srrh
2666767Srrh i++;
2676767Srrh }
2686767Srrh
2696767Srrh return (-5);
2706767Srrh }
2716767Srrh
rsetbrd()2726767Srrh rsetbrd () {
2736767Srrh register int i, j, n;
2746767Srrh
2756767Srrh n = 0;
2766767Srrh mvl = 0;
2776767Srrh for (i = 0; i < 4; i++)
2786767Srrh p[i] = g[i] = -1;
2796767Srrh for (j = 0; j < ncin; j++)
2806767Srrh n = dotable (cin[j],n);
2816767Srrh return (n);
2826767Srrh }
283