xref: /netbsd-src/games/backgammon/common_source/board.c (revision 9795f61a5af380f665d92198f5c50f2cfdc80167)
1*9795f61aSdholland /*	$NetBSD: board.c,v 1.9 2009/08/12 05:17:57 dholland Exp $	*/
2101657d1Scgd 
361f28255Scgd /*
4101657d1Scgd  * Copyright (c) 1980, 1993
5101657d1Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3216641b76Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34101657d1Scgd #if 0
35101657d1Scgd static char sccsid[] = "@(#)board.c	8.1 (Berkeley) 5/31/93";
36101657d1Scgd #else
37*9795f61aSdholland __RCSID("$NetBSD: board.c,v 1.9 2009/08/12 05:17:57 dholland Exp $");
38101657d1Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #include "back.h"
4261f28255Scgd 
4361f28255Scgd static int i, j, k;
4461f28255Scgd static char ln[60];
4561f28255Scgd 
46*9795f61aSdholland static void wrbsub(void);
47*9795f61aSdholland 
4816641b76Slukem void
wrboard(void)49fcb317eeSjmc wrboard(void)
5016641b76Slukem {
5116641b76Slukem 	int     l;
5292932dfcShubertf 	static const char bl[] =
5361f28255Scgd 	"|                       |   |                       |\n";
5492932dfcShubertf 	static const char sv[] =
5561f28255Scgd 	"|                       |   |                       |    \n";
5661f28255Scgd 
57eab5a765Smycroft 	fixtty(&noech);
5861f28255Scgd 	clear();
5961f28255Scgd 
6061f28255Scgd 	if (tflag) {
6161f28255Scgd 		fboard();
6261f28255Scgd 		goto lastline;
6361f28255Scgd 	}
6461f28255Scgd 	writel("_____________________________________________________\n");
6561f28255Scgd 	writel(bl);
6661f28255Scgd 	strcpy(ln, bl);
6761f28255Scgd 	for (j = 1; j < 50; j += 4) {
6861f28255Scgd 		k = j / 4 + (j > 24 ? 12 : 13);
6961f28255Scgd 		ln[j + 1] = k % 10 + '0';
7061f28255Scgd 		ln[j] = k / 10 + '0';
7161f28255Scgd 		if (j == 21)
7261f28255Scgd 			j += 4;
7361f28255Scgd 	}
7461f28255Scgd 	writel(ln);
7561f28255Scgd 	for (i = 0; i < 5; i++) {
7661f28255Scgd 		strcpy(ln, sv);
7761f28255Scgd 		for (j = 1; j < 50; j += 4) {
7861f28255Scgd 			k = j / 4 + (j > 24 ? 12 : 13);
7961f28255Scgd 			wrbsub();
8061f28255Scgd 			if (j == 21)
8161f28255Scgd 				j += 4;
8261f28255Scgd 		}
8361f28255Scgd 		if (-board[25] > i)
8461f28255Scgd 			ln[26] = 'w';
8561f28255Scgd 		if (-board[25] > i + 5)
8661f28255Scgd 			ln[25] = 'w';
8761f28255Scgd 		if (-board[25] > i + 10)
8861f28255Scgd 			ln[27] = 'w';
8961f28255Scgd 		l = 53;
9061f28255Scgd 		if (off[1] > i || (off[1] < 0 && off[1] + 15 > i)) {
9161f28255Scgd 			ln[54] = 'r';
9261f28255Scgd 			l = 55;
9361f28255Scgd 		}
9461f28255Scgd 		if (off[1] > i + 5 || (off[1] < 0 && off[1] + 15 > i + 5)) {
9561f28255Scgd 			ln[55] = 'r';
9661f28255Scgd 			l = 56;
9761f28255Scgd 		}
9861f28255Scgd 		if (off[1] > i + 10 || (off[1] < 0 && off[1] + 15 > i + 10)) {
9961f28255Scgd 			ln[56] = 'r';
10061f28255Scgd 			l = 57;
10161f28255Scgd 		}
10261f28255Scgd 		ln[l++] = '\n';
10361f28255Scgd 		ln[l] = '\0';
10461f28255Scgd 		writel(ln);
10561f28255Scgd 	}
10661f28255Scgd 	strcpy(ln, bl);
10761f28255Scgd 	ln[25] = 'B';
10861f28255Scgd 	ln[26] = 'A';
10961f28255Scgd 	ln[27] = 'R';
11061f28255Scgd 	writel(ln);
11161f28255Scgd 	strcpy(ln, sv);
11261f28255Scgd 	for (i = 4; i > -1; i--) {
11361f28255Scgd 		for (j = 1; j < 50; j += 4) {
11461f28255Scgd 			k = ((j > 24 ? 53 : 49) - j) / 4;
11561f28255Scgd 			wrbsub();
11661f28255Scgd 			if (j == 21)
11761f28255Scgd 				j += 4;
11861f28255Scgd 		}
11961f28255Scgd 		if (board[0] > i)
12061f28255Scgd 			ln[26] = 'r';
12161f28255Scgd 		if (board[0] > i + 5)
12261f28255Scgd 			ln[25] = 'r';
12361f28255Scgd 		if (board[0] > i + 10)
12461f28255Scgd 			ln[27] = 'r';
12561f28255Scgd 		l = 53;
12661f28255Scgd 		if (off[0] > i || (off[0] < 0 && off[0] + 15 > i)) {
12761f28255Scgd 			ln[54] = 'w';
12861f28255Scgd 			l = 55;
12961f28255Scgd 		}
13061f28255Scgd 		if (off[0] > i + 5 || (off[0] < 0 && off[0] + 15 > i + 5)) {
13161f28255Scgd 			ln[55] = 'w';
13261f28255Scgd 			l = 56;
13361f28255Scgd 		}
13461f28255Scgd 		if (off[0] > i + 10 || (off[0] < 0 && off[0] + 15 > i + 10)) {
13561f28255Scgd 			ln[56] = 'w';
13661f28255Scgd 			l = 57;
13761f28255Scgd 		}
13861f28255Scgd 		ln[l++] = '\n';
13961f28255Scgd 		ln[l] = '\0';
14061f28255Scgd 		writel(ln);
14161f28255Scgd 	}
14261f28255Scgd 	strcpy(ln, bl);
14361f28255Scgd 	for (j = 1; j < 50; j += 4) {
14461f28255Scgd 		k = ((j > 24 ? 53 : 49) - j) / 4;
14561f28255Scgd 		ln[j + 1] = k % 10 + '0';
14661f28255Scgd 		if (k > 9)
14761f28255Scgd 			ln[j] = k / 10 + '0';
14861f28255Scgd 		if (j == 21)
14961f28255Scgd 			j += 4;
15061f28255Scgd 	}
15161f28255Scgd 	writel(ln);
15261f28255Scgd 	writel("|_______________________|___|_______________________|\n");
15361f28255Scgd 
15461f28255Scgd lastline:
15561f28255Scgd 	gwrite();
15661f28255Scgd 	if (tflag)
15761f28255Scgd 		curmove(18, 0);
15861f28255Scgd 	else {
15961f28255Scgd 		writec('\n');
16061f28255Scgd 		writec('\n');
16161f28255Scgd 	}
162eab5a765Smycroft 	fixtty(&raw);
16361f28255Scgd }
16416641b76Slukem 
165*9795f61aSdholland static void
wrbsub(void)166fcb317eeSjmc wrbsub(void)
16716641b76Slukem {
16816641b76Slukem 	int     m;
16916641b76Slukem 	char    d;
17061f28255Scgd 
17161f28255Scgd 	if (board[k] > 0) {
17261f28255Scgd 		m = board[k];
17361f28255Scgd 		d = 'r';
17461f28255Scgd 	} else {
17561f28255Scgd 		m = -board[k];
17661f28255Scgd 		d = 'w';
17761f28255Scgd 	}
17861f28255Scgd 	if (m > i)
17961f28255Scgd 		ln[j + 1] = d;
18061f28255Scgd 	if (m > i + 5)
18161f28255Scgd 		ln[j] = d;
18261f28255Scgd 	if (m > i + 10)
18361f28255Scgd 		ln[j + 2] = d;
18461f28255Scgd }
185