xref: /openbsd-src/games/backgammon/common_source/board.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: board.c,v 1.8 2009/10/27 23:59:23 deraadt 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 "back.h"
33 
34 static int i, j, k;
35 static char ln[60];
36 
37 void
38 wrboard()
39 {
40 	int     l;
41 	static const char bl[] =
42 	"|                       |   |                       |\n";
43 	static const char sv[] =
44 	"|                       |   |                       |    \n";
45 
46 	clear();
47 
48 	fboard();
49 	goto lastline;
50 	addstr("_____________________________________________________\n");
51 	addstr(bl);
52 	strlcpy(ln, bl, sizeof ln);
53 	for (j = 1; j < 50; j += 4) {
54 		k = j / 4 + (j > 24 ? 12 : 13);
55 		ln[j + 1] = k % 10 + '0';
56 		ln[j]     = k / 10 + '0';
57 		if (j == 21)
58 			j += 4;
59 	}
60 	addstr(ln);
61 	for (i = 0; i < 5; i++) {
62 		strlcpy(ln, sv, sizeof ln);
63 		for (j = 1; j < 50; j += 4) {
64 			k = j / 4 + (j > 24 ? 12 : 13);
65 			wrbsub();
66 			if (j == 21)
67 				j += 4;
68 		}
69 		if (-board[25] > i)
70 			ln[26] = 'w';
71 		if (-board[25] > i + 5)
72 			ln[25] = 'w';
73 		if (-board[25] > i + 10)
74 			ln[27] = 'w';
75 		l = 53;
76 		if (off[1] > i || (off[1] < 0 && off[1] + 15 > i)) {
77 			ln[54] = 'r';
78 			l = 55;
79 		}
80 		if (off[1] > i + 5 || (off[1] < 0 && off[1] + 15 > i + 5)) {
81 			ln[55] = 'r';
82 			l = 56;
83 		}
84 		if (off[1] > i + 10 || (off[1] < 0 && off[1] + 15 > i + 10)) {
85 			ln[56] = 'r';
86 			l = 57;
87 		}
88 		ln[l++] = '\n';
89 		ln[l] = '\0';
90 		addstr(ln);
91 	}
92 	strlcpy(ln, bl, sizeof ln);
93 	ln[25] = 'B';
94 	ln[26] = 'A';
95 	ln[27] = 'R';
96 	addstr(ln);
97 	strlcpy(ln, sv, sizeof ln);
98 	for (i = 4; i > -1; i--) {
99 		for (j = 1; j < 50; j += 4) {
100 			k = ((j > 24 ? 53 : 49) - j) / 4;
101 			wrbsub();
102 			if (j == 21)
103 				j += 4;
104 		}
105 		if (board[0] > i)
106 			ln[26] = 'r';
107 		if (board[0] > i + 5)
108 			ln[25] = 'r';
109 		if (board[0] > i + 10)
110 			ln[27] = 'r';
111 		l = 53;
112 		if (off[0] > i || (off[0] < 0 && off[0] + 15 > i)) {
113 			ln[54] = 'w';
114 			l = 55;
115 		}
116 		if (off[0] > i + 5 || (off[0] < 0 && off[0] + 15 > i + 5)) {
117 			ln[55] = 'w';
118 			l = 56;
119 		}
120 		if (off[0] > i + 10 || (off[0] < 0 && off[0] + 15 > i + 10)) {
121 			ln[56] = 'w';
122 			l = 57;
123 		}
124 		ln[l++] = '\n';
125 		ln[l] = '\0';
126 		addstr(ln);
127 	}
128 	strlcpy(ln, bl, sizeof ln);
129 	for (j = 1; j < 50; j += 4) {
130 		k = ((j > 24 ? 53 : 49) - j) / 4;
131 		ln[j + 1] = k % 10 + '0';
132 		if (k > 9)
133 			ln[j] = k / 10 + '0';
134 		if (j == 21)
135 			j += 4;
136 	}
137 	addstr(ln);
138 	addstr("|_______________________|___|_______________________|\n");
139 
140 lastline:
141 	gwrite();
142 	move(18, 0);
143 }
144 
145 void
146 wrbsub()
147 {
148 	int     m;
149 	char    d;
150 
151 	if (board[k] > 0) {
152 		m = board[k];
153 		d = 'r';
154 	} else {
155 		m = -board[k];
156 		d = 'w';
157 	}
158 	if (m > i)
159 		ln[j + 1] = d;
160 	if (m > i + 5)
161 		ln[j] = d;
162 	if (m > i + 10)
163 		ln[j + 2] = d;
164 }
165