xref: /netbsd-src/games/backgammon/teachgammon/tutor.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: tutor.c,v 1.7 2005/07/01 01:12:39 jmc 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 <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)tutor.c	8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: tutor.c,v 1.7 2005/07/01 01:12:39 jmc Exp $");
38 #endif
39 #endif				/* not lint */
40 
41 #include "back.h"
42 #include "tutor.h"
43 
44 static const char better[] =
45 	"That is a legal move, but there is a better one.\n";
46 
47 void
48 tutor(void)
49 {
50 	int     i, j;
51 
52 	i = 0;
53 	begscr = 18;
54 	cturn = -1;
55 	home = 0;
56 	bar = 25;
57 	inptr = &in[0];
58 	inopp = &in[1];
59 	offptr = &off[0];
60 	offopp = &off[1];
61 	Colorptr = &color[0];
62 	colorptr = &color[2];
63 	colen = 5;
64 	wrboard();
65 
66 	while (1) {
67 		if (!brdeq(test[i].brd, board)) {
68 			if (tflag && curr == 23)
69 				curmove(18, 0);
70 			writel(better);
71 			nexturn();
72 			movback(mvlim);
73 			if (tflag) {
74 				refresh();
75 				clrest();
76 			}
77 			if ((!tflag) || curr == 19) {
78 				proll();
79 				writec('\t');
80 			} else
81 				curmove(curr > 19 ? curr - 2 : curr + 4, 25);
82 			getmove();
83 			if (cturn == 0)
84 				leave();
85 			continue;
86 		}
87 		if (tflag)
88 			curmove(18, 0);
89 		text(*test[i].com);
90 		if (!tflag)
91 			writec('\n');
92 		if (i == maxmoves)
93 			break;
94 		D0 = test[i].roll1;
95 		D1 = test[i].roll2;
96 		d0 = 0;
97 		mvlim = 0;
98 		for (j = 0; j < 4; j++) {
99 			if (test[i].mp[j] == test[i].mg[j])
100 				break;
101 			p[j] = test[i].mp[j];
102 			g[j] = test[i].mg[j];
103 			mvlim++;
104 		}
105 		if (mvlim)
106 			for (j = 0; j < mvlim; j++)
107 				if (makmove(j))
108 					writel("AARGH!!!\n");
109 		if (tflag)
110 			refresh();
111 		nexturn();
112 		D0 = test[i].new1;
113 		D1 = test[i].new2;
114 		d0 = 0;
115 		i++;
116 		mvlim = movallow();
117 		if (mvlim) {
118 			if (tflag)
119 				clrest();
120 			proll();
121 			writec('\t');
122 			getmove();
123 			if (tflag)
124 				refresh();
125 			if (cturn == 0)
126 				leave();
127 		}
128 	}
129 	leave();
130 }
131 
132 void
133 clrest(void)
134 {
135 	int     r, c, j;
136 
137 	r = curr;
138 	c = curc;
139 	for (j = r + 1; j < 24; j++) {
140 		curmove(j, 0);
141 		cline();
142 	}
143 	curmove(r, c);
144 }
145 
146 int
147 brdeq(const int *b1, const int *b2)
148 {
149 	const int    *e;
150 
151 	e = b1 + 26;
152 	while (b1 < e)
153 		if (*b1++ != *b2++)
154 			return (0);
155 	return (1);
156 }
157