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.3 2006/08/08 16:36:11 pavalos Exp $
36  */
37 
38 #include <string.h>
39 #include "back.h"
40 
41 static void	wrbsub(void);
42 
43 static int	i, j, k;
44 static char	ln[60];
45 
46 void
47 wrboard(void)
48 {
49 	int	l;
50 	static const char	bl[] =
51 		"|                       |   |                       |\n";
52 	static const char	sv[] =
53 		"|                       |   |                       |    \n";
54 
55 	fixtty (noech);
56 	clear();
57 
58 	if (tflag)  {
59 		fboard();
60 		goto lastline;
61 	}
62 
63 	writel ("_____________________________________________________\n");
64 	writel (bl);
65 	strcpy (ln,bl);
66 	for (j = 1; j < 50; j += 4) {
67 		k = j/4+(j > 24? 12: 13);
68 		ln[j+1] = k%10+'0';
69 		ln[j] = k/10+'0';
70 		if (j == 21)
71 			j += 4;
72 	}
73 	writel (ln);
74 	for (i = 0; i < 5; i++) {
75 		strcpy (ln,sv);
76 		for (j = 1; j < 50; j += 4) {
77 			k = j/4+(j > 24? 12: 13);
78 			wrbsub ();
79 			if (j == 21)
80 				j += 4;
81 		}
82 		if (-board[25] > i)
83 			ln[26] = 'w';
84 		if (-board[25] > i+5)
85 			ln[25] = 'w';
86 		if (-board[25] > i+10)
87 			ln[27] = 'w';
88 		l = 53;
89 		if (off[1] > i || (off[1] < 0 && off[1]+15 > i))  {
90 			ln[54] = 'r';
91 			l = 55;
92 		}
93 		if (off[1] > i+5 || (off[1] < 0 && off[1]+15 > i+5))  {
94 			ln[55] = 'r';
95 			l = 56;
96 		}
97 		if (off[1] > i+10 || (off[1] < 0 && off[1]+15 > i+10))  {
98 			ln[56] = 'r';
99 			l = 57;
100 		}
101 		ln[l++] = '\n';
102 		ln[l] = '\0';
103 		writel (ln);
104 	}
105 	strcpy (ln,bl);
106 	ln[25] = 'B';
107 	ln[26] = 'A';
108 	ln[27] = 'R';
109 	writel (ln);
110 	strcpy (ln,sv);
111 	for (i = 4; i > -1; i--) {
112 		for (j = 1; j < 50; j += 4) {
113 			k = ((j > 24? 53: 49)-j)/4;
114 			wrbsub();
115 			if (j == 21)
116 				j += 4;
117 		}
118 		if (board[0] > i)
119 			ln[26] = 'r';
120 		if (board[0] > i+5)
121 			ln[25] = 'r';
122 		if (board[0] > i+10)
123 			ln[27] = 'r';
124 		l = 53;
125 		if (off[0] > i || (off[0] < 0 && off[0]+15 > i))  {
126 			ln[54] = 'w';
127 			l = 55;
128 		}
129 		if (off[0] > i+5 || (off[0] < 0 && off[0]+15 > i+5))  {
130 			ln[55] = 'w';
131 			l = 56;
132 		}
133 		if (off[0] > i+10 || (off[0] < 0 && off[0]+15 > i+10))  {
134 			ln[56] = 'w';
135 			l = 57;
136 		}
137 		ln[l++] = '\n';
138 		ln[l] = '\0';
139 		writel (ln);
140 	}
141 	strcpy (ln,bl);
142 	for (j = 1; j < 50; j += 4) {
143 		k = ((j > 24? 53: 49)-j)/4;
144 		ln[j+1] = k%10+'0';
145 		if (k > 9)
146 			ln[j] = k/10+'0';
147 		if (j == 21)
148 			j += 4;
149 	}
150 	writel (ln);
151 	writel ("|_______________________|___|_______________________|\n");
152 
153 lastline:
154 	gwrite ();
155 	if (tflag)
156 		curmove (18,0);
157 	else  {
158 		writec ('\n');
159 		writec ('\n');
160 	}
161 	fixtty(raw);
162 }
163 
164 static void
165 wrbsub(void)
166 {
167 	int		m;
168 	char		d;
169 
170 	if (board[k] > 0)  {
171 		m = board[k];
172 		d = 'r';
173 	} else {
174 		m = -board[k];
175 		d = 'w';
176 	}
177 	if (m>i)
178 		ln[j+1] = d;
179 	if (m>i+5)
180 		ln[j] = d;
181 	if (m>i+10)
182 		ln[j+2] = d;
183 }
184