summaryrefslogtreecommitdiff
path: root/games/backgammon/common_source/subs.c
blob: c6557e006622d7dfe71e8efe596665fa81782bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*	$OpenBSD: subs.c,v 1.11 2001/06/23 23:50:04 pjanzen Exp $	*/

/*
 * Copyright (c) 1980, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
#if 0
static char sccsid[] = "@(#)subs.c	8.1 (Berkeley) 5/31/93";
#else
static char rcsid[] = "$OpenBSD: subs.c,v 1.11 2001/06/23 23:50:04 pjanzen Exp $";
#endif
#endif /* not lint */

#include "back.h"

int     buffnum;
char    outbuff[BUFSIZ];

static const char plred[] = "Player is red, computer is white.";
static const char plwhite[] = "Player is white, computer is red.";
static const char nocomp[] = "(No computer play.)";

const char   *const descr[] = {
	"Usage:  backgammon [-] [-nrwb] [-s <file>]\n",
	"\t-h\tget this list\n\t-n\tdon't ask for rules or instructions",
	"\t-r\tplayer is red (implies n)\n\t-w\tplayer is white (implies n)",
	"\t-b\ttwo players, red and white (implies n)",
	"\t-s file\trecover previously saved game from file",
	0
};

void
errexit(s)
	const char *s;
{
	write(STDERR_FILENO, "\n", 1);
	perror(s);
	getout(0);
}

int
readc()
{
	int    c;

	clrtoeol();
	refresh();
	c = getch();
	if (c == '\004' || c == ERR)	/* ^D or failure	*/
		getout(0);
	if (c == '\033' || c == '\015')
		return('\n');
	if (cflag)
		return(c);
	if (c == '\014')
		return('R');
	if (c >= 'a' && c <= 'z')
		return(c & 0137);	/* upper case */
	return(c);
}

void
proll()
{
	if (d0)
		swap;
	if (cturn == 1)
		printw("Red's roll:  ");
	else
		printw("White's roll:  ");
	printw("%d,%d", D0, D1);
	clrtoeol();
}

void
gwrite()
{
	int     r, c;

	getyx(stdscr, r, c);
	move(16, 0);
	if (gvalue > 1) {
		printw("Game value:  %d.  ", gvalue);
		if (dlast == -1)
			addstr(color[0]);
		else
			addstr(color[1]);
		addstr(" doubled last.");
	} else {
		switch (pnum) {
		case -1:	/* player is red */
			addstr(plred);
			break;
		case 0:	/* player is both colors */
			addstr(nocomp);
			break;
		case 1:	/* player is white */
			addstr(plwhite);
		}
	}
	if (rscore || wscore) {
		addstr("  ");
		wrscore();
	}
	clrtoeol();
	move(r, c);
}

int
quit()
{
	move(20, 0);
	clrtobot();
	addstr("Are you sure you want to quit?");
	if (yorn(0)) {
		if (rfl) {
			addstr("Would you like to save this game?");
			if (yorn(0))
				save(0);
		}
		cturn = 0;
		return(1);
	}
	return(0);
}

int
yorn(special)
	char    special;	/* special response */
{
	char    c;
	int     i;

	i = 1;
	while ((c = readc()) != 'Y' && c != 'N') {
		if (special && c == special)
			return(2);
		if (i) {
			if (special)
				printw("  (Y, N, or %c)", special);
			else
				printw("  (Y or N)");
			i = 0;
		} else
			beep();
	}
	if (c == 'Y')
		addstr("  Yes.\n");
	else
		addstr("  No.\n");
	refresh();
	return(c == 'Y');
}

void
wrhit(i)
	int     i;
{
	printw("Blot hit on %d.\n", i);
}

void
nexturn()
{
	int     c;

	cturn = -cturn;
	c = cturn / abs(cturn);
	home = bar;
	bar = 25 - bar;
	offptr += c;
	offopp -= c;
	inptr += c;
	inopp -= c;
	Colorptr += c;
	colorptr += c;
}

void
getarg(argc,argv)
	int     argc;
	char  **argv;
{
	int     ch;
	int     j;

	while ((ch = getopt(argc, argv, "bhnp:rs:t:w")) != -1)
		switch((char)ch) {
		case 'n':	/* don't ask if rules or instructions needed */
			if (rflag)
				break;
			aflag = 0;
			args[acnt++] = 'n';
			break;

		case 'b':	/* player is both red and white */
			if (rflag)
				break;
			pnum = 0;
			aflag = 0;
			args[acnt++] = 'b';
			break;

		case 'r':	/* player is red */
			if (rflag)
				break;
			pnum = -1;
			aflag = 0;
			args[acnt++] = 'r';
			break;

		case 'w':	/* player is white */
			if (rflag)
				break;
			pnum = 1;
			aflag = 0;
			args[acnt++] = 'w';
			break;

		case 's':	/* restore saved game */
			recover(optarg);
			break;

		default:	/* print cmdline options */
		case 'h':
			for (j = 0; descr[j] != NULL; j++)
				printf("%s\n", descr[j]);
			exit(0);
			break;
	} /* end switch */
}

void
init()
{
	int     i;

	for (i = 0; i < 26;)
		board[i++] = 0;
	board[1] = 2;
	board[6] = board[13] = -5;
	board[8] = -3;
	board[12] = board[19] = 5;
	board[17] = 3;
	board[24] = -2;
	off[0] = off[1] = -15;
	in[0] = in[1] = 5;
	gvalue = 1;
	dlast = 0;
}

void
wrscore()
{
	printw("Score:  %s %d, %s %d", color[1], rscore, color[0], wscore);
}


void
getout(dummy)
	int     dummy;
{
	/* go to bottom of screen */
	move(23, 0);
	clrtoeol();

	endwin();
	exit(0);
}

void
roll()
{
	char    c;
	int     row;
	int     col;

	if (iroll) {
		getyx(stdscr, row, col);
		mvprintw(17, 0, "ROLL: ");
		c = readc();
		if (c != '\n') {
			while (c < '1' || c > '6')
				c = readc();
			D0 = c - '0';
			printw(" %c", c);
			c = readc();
			while (c < '1' || c > '6')
				c = readc();
			D1 = c - '0';
			printw(" %c", c);
			move(17, 0);
			clrtoeol();
			move(row, col);
			return;
		}
		move(17, 0);
		clrtoeol();
		move(row, col);
	}
	D0 = rnum(6) + 1;
	D1 = rnum(6) + 1;
	d0 = 0;
}