1*6766Srrh static char sccsid[] = "	subs.c	1.1	82/05/11	";
2*6766Srrh 
3*6766Srrh #include <stdio.h>
4*6766Srrh #include "back.h"
5*6766Srrh 
6*6766Srrh int	buffnum;
7*6766Srrh char	outbuff[BUFSIZ];
8*6766Srrh 
9*6766Srrh static char	plred[] = "Player is red, computer is white.";
10*6766Srrh static char	plwhite[] = "Player is white, computer is red.";
11*6766Srrh static char	nocomp[] = "(No computer play.)";
12*6766Srrh 
13*6766Srrh char  *descr[] = {
14*6766Srrh 	"Usage:  backgammon [-] [n r w b pr pw pb t3a]\n",
15*6766Srrh 	"\t-\tgets this list\n\tn\tdon't ask for rules or instructions",
16*6766Srrh 	"\tr\tplayer is red (implies n)\n\tw\tplayer is white (implies n)",
17*6766Srrh 	"\tb\ttwo players, red and white (implies n)",
18*6766Srrh 	"\tpr\tprint the board before red's turn",
19*6766Srrh 	"\tpw\tprint the board before white's turn",
20*6766Srrh 	"\tpb\tprint the board before both player's turn",
21*6766Srrh 	"\tterm\tterminal is a term",
22*6766Srrh 	"\tsfile\trecover saved game from file",
23*6766Srrh 	0
24*6766Srrh };
25*6766Srrh 
26*6766Srrh errexit (s)
27*6766Srrh register char	*s;
28*6766Srrh {
29*6766Srrh 	write (2,"\n",1);
30*6766Srrh 	perror (s);
31*6766Srrh 	getout();
32*6766Srrh }
33*6766Srrh 
34*6766Srrh strset (s1,s2)
35*6766Srrh register char	*s1, *s2;
36*6766Srrh {
37*6766Srrh 	while ( (*s1++ = *s2++) != '\0');
38*6766Srrh }
39*6766Srrh 
40*6766Srrh addbuf (c)
41*6766Srrh register char	c;
42*6766Srrh 
43*6766Srrh {
44*6766Srrh 	buffnum++;
45*6766Srrh 	if (buffnum == BUFSIZ)  {
46*6766Srrh 		if (write(1,outbuff,BUFSIZ) != BUFSIZ)
47*6766Srrh 			errexit ("addbuf (write):");
48*6766Srrh 		buffnum = 0;
49*6766Srrh 	}
50*6766Srrh 	outbuff[buffnum] = c;
51*6766Srrh }
52*6766Srrh 
53*6766Srrh buflush ()  {
54*6766Srrh 	if (buffnum < 0)
55*6766Srrh 		return;
56*6766Srrh 	buffnum++;
57*6766Srrh 	if (write (1,outbuff,buffnum) != buffnum)
58*6766Srrh 		errexit ("buflush (write):");
59*6766Srrh 	buffnum = -1;
60*6766Srrh }
61*6766Srrh 
62*6766Srrh readc () {
63*6766Srrh 	char	c;
64*6766Srrh 
65*6766Srrh 	if (tflag)  {
66*6766Srrh 		cline();
67*6766Srrh 		newpos();
68*6766Srrh 	}
69*6766Srrh 	buflush();
70*6766Srrh 	if (read(0,&c,1) != 1)
71*6766Srrh 		errexit ("readc");
72*6766Srrh 	if (c == '\177')
73*6766Srrh 		getout();
74*6766Srrh 	if (c == '\033' || c == '\015')
75*6766Srrh 		return ('\n');
76*6766Srrh 	if (cflag)
77*6766Srrh 		return (c);
78*6766Srrh 	if (c == '\014')
79*6766Srrh 		return ('R');
80*6766Srrh 	if (c >= 'a' && c <= 'z')
81*6766Srrh 		return (c & 0137);
82*6766Srrh 	return (c);
83*6766Srrh }
84*6766Srrh 
85*6766Srrh writec (c)
86*6766Srrh char	c;
87*6766Srrh {
88*6766Srrh 	if (tflag)
89*6766Srrh 		fancyc (c);
90*6766Srrh 	else
91*6766Srrh 		addbuf (c);
92*6766Srrh }
93*6766Srrh 
94*6766Srrh writel (l)
95*6766Srrh register char	*l;
96*6766Srrh {
97*6766Srrh #ifdef DEBUG
98*6766Srrh 	register char	*s;
99*6766Srrh 
100*6766Srrh 	if (trace == NULL)
101*6766Srrh 		trace = fopen ("bgtrace","w");
102*6766Srrh 
103*6766Srrh 	fprintf (trace,"writel: \"");
104*6766Srrh 	for (s = l; *s; s++) {
105*6766Srrh 		if (*s < ' ' || *s == '\177')
106*6766Srrh 			fprintf (trace,"^%c",(*s)^0100);
107*6766Srrh 		else
108*6766Srrh 			putc (*s,trace);
109*6766Srrh 	}
110*6766Srrh 	fprintf (trace,"\"\n");
111*6766Srrh 	fflush (trace);
112*6766Srrh #endif
113*6766Srrh 
114*6766Srrh 	while (*l)
115*6766Srrh 		writec (*l++);
116*6766Srrh }
117*6766Srrh 
118*6766Srrh proll ()   {
119*6766Srrh 	if (d0)
120*6766Srrh 		swap;
121*6766Srrh 	if (cturn == 1)
122*6766Srrh 		writel ("Red's roll:  ");
123*6766Srrh 	else
124*6766Srrh 		writel ("White's roll:  ");
125*6766Srrh 	writec (D0+'0');
126*6766Srrh 	writec ('\040');
127*6766Srrh 	writec (D1+'0');
128*6766Srrh 	if (tflag)
129*6766Srrh 		cline();
130*6766Srrh }
131*6766Srrh 
132*6766Srrh wrint (n)
133*6766Srrh int	n;
134*6766Srrh {
135*6766Srrh 	register int	i, j, t;
136*6766Srrh 
137*6766Srrh 	for (i = 4; i > 0; i--)  {
138*6766Srrh 		t = 1;
139*6766Srrh 		for (j = 0; j<i; j++)
140*6766Srrh 			t *= 10;
141*6766Srrh 		if (n > t-1)
142*6766Srrh 			writec ((n/t)%10+'0');
143*6766Srrh 	}
144*6766Srrh 	writec (n%10+'0');
145*6766Srrh }
146*6766Srrh 
147*6766Srrh gwrite()  {
148*6766Srrh 	register int	r, c;
149*6766Srrh 
150*6766Srrh 	if (tflag)  {
151*6766Srrh 		r = curr;
152*6766Srrh 		c = curc;
153*6766Srrh 		curmove (16,0);
154*6766Srrh 	}
155*6766Srrh 
156*6766Srrh 	if (gvalue > 1)  {
157*6766Srrh 		writel ("Game value:  ");
158*6766Srrh 		wrint (gvalue);
159*6766Srrh 		writel (".  ");
160*6766Srrh 		if (dlast == -1)
161*6766Srrh 			writel (color[0]);
162*6766Srrh 		else
163*6766Srrh 			writel (color[1]);
164*6766Srrh 		writel (" doubled last.");
165*6766Srrh 	} else  {
166*6766Srrh 		switch (pnum)  {
167*6766Srrh 		case -1:			    /* player is red */
168*6766Srrh 			writel (plred);
169*6766Srrh 			break;
170*6766Srrh 		case 0:				    /* player is both colors */
171*6766Srrh 			writel (nocomp);
172*6766Srrh 			break;
173*6766Srrh 		case 1:				    /* player is white */
174*6766Srrh 			writel (plwhite);
175*6766Srrh 		}
176*6766Srrh 	}
177*6766Srrh 
178*6766Srrh 	if (rscore || wscore)  {
179*6766Srrh 		writel ("  ");
180*6766Srrh 		wrscore();
181*6766Srrh 	}
182*6766Srrh 
183*6766Srrh 	if (tflag)  {
184*6766Srrh 		cline();
185*6766Srrh 		curmove (r,c);
186*6766Srrh 	}
187*6766Srrh }
188*6766Srrh 
189*6766Srrh quit ()  {
190*6766Srrh 	register int	i;
191*6766Srrh 
192*6766Srrh 	if (tflag)  {
193*6766Srrh 		curmove (20,0);
194*6766Srrh 		clend();
195*6766Srrh 	} else
196*6766Srrh 		writec ('\n');
197*6766Srrh 	writel ("Are you sure you want to quit?");
198*6766Srrh 	if (yorn (0))  {
199*6766Srrh 		if (rfl)  {
200*6766Srrh 			writel ("Would you like to save this game?");
201*6766Srrh 			if (yorn(0))
202*6766Srrh 				save(0);
203*6766Srrh 		}
204*6766Srrh 		cturn = 0;
205*6766Srrh 		return (1);
206*6766Srrh 	}
207*6766Srrh 	return (0);
208*6766Srrh }
209*6766Srrh 
210*6766Srrh yorn (special)
211*6766Srrh register char	special;			/* special response */
212*6766Srrh {
213*6766Srrh 	register char	c;
214*6766Srrh 	register int	i;
215*6766Srrh 
216*6766Srrh 	i = 1;
217*6766Srrh 	while ( (c = readc()) != 'Y' && c != 'N')  {
218*6766Srrh 		if (special && c == special)
219*6766Srrh 			return (2);
220*6766Srrh 		if (i)  {
221*6766Srrh 			if (special)  {
222*6766Srrh 				writel ("  (Y, N, or ");
223*6766Srrh 				writec (special);
224*6766Srrh 				writec (')');
225*6766Srrh 			} else
226*6766Srrh 				writel ("  (Y or N)");
227*6766Srrh 			i = 0;
228*6766Srrh 		} else
229*6766Srrh 			writec ('\007');
230*6766Srrh 	}
231*6766Srrh 	if (c == 'Y')
232*6766Srrh 		writel ("  Yes.\n");
233*6766Srrh 	else
234*6766Srrh 		writel ("  No.\n");
235*6766Srrh 	if (tflag)
236*6766Srrh 		buflush();
237*6766Srrh 	return (c == 'Y');
238*6766Srrh }
239*6766Srrh 
240*6766Srrh wrhit (i)
241*6766Srrh register int	i;
242*6766Srrh {
243*6766Srrh 	writel ("Blot hit on ");
244*6766Srrh 	wrint (i);
245*6766Srrh 	writec ('.');
246*6766Srrh 	writec ('\n');
247*6766Srrh }
248*6766Srrh 
249*6766Srrh nexturn ()  {
250*6766Srrh 	register int	c;
251*6766Srrh 
252*6766Srrh 	cturn = -cturn;
253*6766Srrh 	c = cturn/abs(cturn);
254*6766Srrh 	home = bar;
255*6766Srrh 	bar = 25-bar;
256*6766Srrh 	offptr += c;
257*6766Srrh 	offopp -= c;
258*6766Srrh 	inptr += c;
259*6766Srrh 	inopp -= c;
260*6766Srrh 	Colorptr += c;
261*6766Srrh 	colorptr += c;
262*6766Srrh }
263*6766Srrh 
264*6766Srrh getarg (arg)
265*6766Srrh register char	***arg;
266*6766Srrh 
267*6766Srrh {
268*6766Srrh 	register char	*s;
269*6766Srrh 
270*6766Srrh 	/* process arguments here.  dashes are ignored, nbrw are ignored
271*6766Srrh 	   if the game is being recovered */
272*6766Srrh 
273*6766Srrh 	s = **arg;
274*6766Srrh 	if (*s == '-' && s[1] == '\0')  {
275*6766Srrh 		tflag = 0;
276*6766Srrh 		text (descr);
277*6766Srrh 		getout ();
278*6766Srrh 	}
279*6766Srrh 	for (; *s != '\0'; s++)  {
280*6766Srrh 		switch (*s)  {
281*6766Srrh 
282*6766Srrh 		/* ignore dashes */
283*6766Srrh 		case '-':
284*6766Srrh 			break;
285*6766Srrh 
286*6766Srrh 		/* don't ask if rules or instructions needed */
287*6766Srrh 		case 'n':
288*6766Srrh 			if (rflag)
289*6766Srrh 				break;
290*6766Srrh 			aflag = 0;
291*6766Srrh 			args[acnt++] = *s;
292*6766Srrh 			break;
293*6766Srrh 
294*6766Srrh 		/* player is both read and white */
295*6766Srrh 		case 'b':
296*6766Srrh 			if (rflag)
297*6766Srrh 				break;
298*6766Srrh 			pnum = 0;
299*6766Srrh 			aflag = 0;
300*6766Srrh 			args[acnt++] = *s;
301*6766Srrh 			break;
302*6766Srrh 
303*6766Srrh 		/* player is red */
304*6766Srrh 		case 'r':
305*6766Srrh 			if (rflag)
306*6766Srrh 				break;
307*6766Srrh 			pnum = -1;
308*6766Srrh 			aflag = 0;
309*6766Srrh 			args[acnt++] = *s;
310*6766Srrh 			break;
311*6766Srrh 
312*6766Srrh 		/* player is white */
313*6766Srrh 		case 'w':
314*6766Srrh 			if (rflag)
315*6766Srrh 				break;
316*6766Srrh 			pnum = 1;
317*6766Srrh 			aflag = 0;
318*6766Srrh 			args[acnt++] = *s;
319*6766Srrh 			break;
320*6766Srrh 
321*6766Srrh 		/* print board after move according to following character */
322*6766Srrh 		case 'p':
323*6766Srrh 			s++;
324*6766Srrh 			if (*s != 'r' && *s != 'w' && *s != 'b')
325*6766Srrh 				break;
326*6766Srrh 			args[acnt++] = 'p';
327*6766Srrh 			args[acnt++] = *s;
328*6766Srrh 			if (*s == 'r')
329*6766Srrh 				bflag = 1;
330*6766Srrh 			if (*s == 'w')
331*6766Srrh 				bflag = -1;
332*6766Srrh 			if (*s == 'b')
333*6766Srrh 				bflag = 0;
334*6766Srrh 			break;
335*6766Srrh 
336*6766Srrh 		case 't':
337*6766Srrh 			if (*++s == '\0')	/* get terminal caps */
338*6766Srrh 				tflag = getcaps (*(++*arg));
339*6766Srrh 			else
340*6766Srrh 				tflag = getcaps (s);
341*6766Srrh 			return;
342*6766Srrh 
343*6766Srrh 		case 's':
344*6766Srrh 			if (*++s == '\0')	/* recover file */
345*6766Srrh 				recover (*(++*arg));
346*6766Srrh 			else
347*6766Srrh 				recover (s);
348*6766Srrh 			return;
349*6766Srrh 		}
350*6766Srrh 	}
351*6766Srrh }
352*6766Srrh 
353*6766Srrh init ()  {
354*6766Srrh 	register int	i;
355*6766Srrh 	for (i = 0; i < 26;)
356*6766Srrh 		board[i++] = 0;
357*6766Srrh 	board[1] = 2;
358*6766Srrh 	board[6] = board[13] = -5;
359*6766Srrh 	board[8] = -3;
360*6766Srrh 	board[12] = board[19] = 5;
361*6766Srrh 	board[17] = 3;
362*6766Srrh 	board[24] = -2;
363*6766Srrh 	off[0] = off[1] = -15;
364*6766Srrh 	in[0] = in[1] = 5;
365*6766Srrh 	gvalue = 1;
366*6766Srrh 	dlast = 0;
367*6766Srrh }
368*6766Srrh 
369*6766Srrh wrscore ()  {
370*6766Srrh 	writel ("Score:  ");
371*6766Srrh 	writel (color[1]);
372*6766Srrh 	writec (' ');
373*6766Srrh 	wrint (rscore);
374*6766Srrh 	writel (", ");
375*6766Srrh 	writel (color[0]);
376*6766Srrh 	writec (' ');
377*6766Srrh 	wrint (wscore);
378*6766Srrh }
379*6766Srrh 
380*6766Srrh fixtty (mode)
381*6766Srrh int	mode;
382*6766Srrh {
383*6766Srrh 	if (tflag)
384*6766Srrh 		newpos();
385*6766Srrh 	buflush();
386*6766Srrh 	tty.sg_flags = mode;
387*6766Srrh 	if (stty (0,&tty) < 0)
388*6766Srrh 		errexit("fixtty");
389*6766Srrh }
390*6766Srrh 
391*6766Srrh getout ()  {
392*6766Srrh 	/* go to bottom of screen */
393*6766Srrh 	if (tflag)  {
394*6766Srrh 		curmove (23,0);
395*6766Srrh 		cline();
396*6766Srrh 	} else
397*6766Srrh 		writec ('\n');
398*6766Srrh 
399*6766Srrh 	/* fix terminal status */
400*6766Srrh 	fixtty (old);
401*6766Srrh 	exit();
402*6766Srrh }
403*6766Srrh roll ()  {
404*6766Srrh 	register char	c;
405*6766Srrh 	register int	row;
406*6766Srrh 	register int	col;
407*6766Srrh 
408*6766Srrh 	if (iroll)  {
409*6766Srrh 		if (tflag)  {
410*6766Srrh 			row = curr;
411*6766Srrh 			col = curc;
412*6766Srrh 			curmove (17,0);
413*6766Srrh 		} else
414*6766Srrh 			writec ('\n');
415*6766Srrh 		writel ("ROLL: ");
416*6766Srrh 		c = readc();
417*6766Srrh 		if (c != '\n')  {
418*6766Srrh 			while (c < '1' || c > '6')
419*6766Srrh 				c = readc();
420*6766Srrh 			D0 = c-'0';
421*6766Srrh 			writec (' ');
422*6766Srrh 			writec (c);
423*6766Srrh 			c = readc();
424*6766Srrh 			while (c < '1' || c > '6')
425*6766Srrh 				c = readc();
426*6766Srrh 			D1 = c-'0';
427*6766Srrh 			writec (' ');
428*6766Srrh 			writec (c);
429*6766Srrh 			if (tflag)  {
430*6766Srrh 				curmove (17,0);
431*6766Srrh 				cline();
432*6766Srrh 				curmove (row,col);
433*6766Srrh 			} else
434*6766Srrh 				writec ('\n');
435*6766Srrh 			return;
436*6766Srrh 		}
437*6766Srrh 		if (tflag)  {
438*6766Srrh 			curmove (17,0);
439*6766Srrh 			cline();
440*6766Srrh 			curmove (row,col);
441*6766Srrh 		} else
442*6766Srrh 			writec ('\n');
443*6766Srrh 	}
444*6766Srrh 	D0 = rnum(6)+1;
445*6766Srrh 	D1 = rnum(6)+1;
446*6766Srrh 	d0 = 0;
447*6766Srrh }
448