1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)board.c	8.1 (Berkeley) 5/31/93
34  * $FreeBSD: src/games/backgammon/common_source/board.c,v 1.6 1999/11/30 03:48:25 billf Exp $
35  * $DragonFly: src/games/backgammon/common_source/board.c,v 1.2 2003/06/17 04:25:22 dillon Exp $
36  */
37 
38 #include <string.h>
39 #include "back.h"
40 
41 static int	i, j, k;
42 static char	ln[60];
43 
44 wrboard ()  {
45 	int	l;
46 	static const char	bl[] =
47 		"|                       |   |                       |\n";
48 	static const char	sv[] =
49 		"|                       |   |                       |    \n";
50 
51 	fixtty (noech);
52 	clear();
53 
54 	if (tflag)  {
55 		fboard();
56 		goto lastline;
57 	}
58 
59 	writel ("_____________________________________________________\n");
60 	writel (bl);
61 	strcpy (ln,bl);
62 	for (j = 1; j < 50; j += 4) {
63 		k = j/4+(j > 24? 12: 13);
64 		ln[j+1] = k%10+'0';
65 		ln[j] = k/10+'0';
66 		if (j == 21)
67 			j += 4;
68 	}
69 	writel (ln);
70 	for (i = 0; i < 5; i++) {
71 		strcpy (ln,sv);
72 		for (j = 1; j < 50; j += 4) {
73 			k = j/4+(j > 24? 12: 13);
74 			wrbsub ();
75 			if (j == 21)
76 				j += 4;
77 		}
78 		if (-board[25] > i)
79 			ln[26] = 'w';
80 		if (-board[25] > i+5)
81 			ln[25] = 'w';
82 		if (-board[25] > i+10)
83 			ln[27] = 'w';
84 		l = 53;
85 		if (off[1] > i || (off[1] < 0 && off[1]+15 > i))  {
86 			ln[54] = 'r';
87 			l = 55;
88 		}
89 		if (off[1] > i+5 || (off[1] < 0 && off[1]+15 > i+5))  {
90 			ln[55] = 'r';
91 			l = 56;
92 		}
93 		if (off[1] > i+10 || (off[1] < 0 && off[1]+15 > i+10))  {
94 			ln[56] = 'r';
95 			l = 57;
96 		}
97 		ln[l++] = '\n';
98 		ln[l] = '\0';
99 		writel (ln);
100 	}
101 	strcpy (ln,bl);
102 	ln[25] = 'B';
103 	ln[26] = 'A';
104 	ln[27] = 'R';
105 	writel (ln);
106 	strcpy (ln,sv);
107 	for (i = 4; i > -1; i--) {
108 		for (j = 1; j < 50; j += 4) {
109 			k = ((j > 24? 53: 49)-j)/4;
110 			wrbsub();
111 			if (j == 21)
112 				j += 4;
113 		}
114 		if (board[0] > i)
115 			ln[26] = 'r';
116 		if (board[0] > i+5)
117 			ln[25] = 'r';
118 		if (board[0] > i+10)
119 			ln[27] = 'r';
120 		l = 53;
121 		if (off[0] > i || (off[0] < 0 && off[0]+15 > i))  {
122 			ln[54] = 'w';
123 			l = 55;
124 		}
125 		if (off[0] > i+5 || (off[0] < 0 && off[0]+15 > i+5))  {
126 			ln[55] = 'w';
127 			l = 56;
128 		}
129 		if (off[0] > i+10 || (off[0] < 0 && off[0]+15 > i+10))  {
130 			ln[56] = 'w';
131 			l = 57;
132 		}
133 		ln[l++] = '\n';
134 		ln[l] = '\0';
135 		writel (ln);
136 	}
137 	strcpy (ln,bl);
138 	for (j = 1; j < 50; j += 4) {
139 		k = ((j > 24? 53: 49)-j)/4;
140 		ln[j+1] = k%10+'0';
141 		if (k > 9)
142 			ln[j] = k/10+'0';
143 		if (j == 21)
144 			j += 4;
145 	}
146 	writel (ln);
147 	writel ("|_______________________|___|_______________________|\n");
148 
149 lastline:
150 	gwrite ();
151 	if (tflag)
152 		curmove (18,0);
153 	else  {
154 		writec ('\n');
155 		writec ('\n');
156 	}
157 	fixtty(raw);
158 }
159 
160 wrbsub () {
161 	int		m;
162 	char		d;
163 
164 	if (board[k] > 0)  {
165 		m = board[k];
166 		d = 'r';
167 	} else {
168 		m = -board[k];
169 		d = 'w';
170 	}
171 	if (m>i)
172 		ln[j+1] = d;
173 	if (m>i+5)
174 		ln[j] = d;
175 	if (m>i+10)
176 		ln[j+2] = d;
177 }
178