2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)subs.c 8.1 (Berkeley) 5/31/93
30 * $FreeBSD: src/games/backgammon/common_source/subs.c,v 1.12 1999/11/30 03:48:27 billf Exp $
31 * $DragonFly: src/games/backgammon/common_source/subs.c,v 1.3 2006/08/08 16:36:11 pavalos Exp $
42 static const char plred
[] = "Player is red, computer is white.";
43 static const char plwhite
[] = "Player is white, computer is red.";
44 static const char nocomp
[] = "(No computer play.)";
46 const char *const descr
[] = {
47 "Usage: backgammon [-h n r w b pr pw pb tterm sfile]\n",
48 "\t-h\tgets this list\n\t-n\tdon't ask for rules or instructions",
49 "\t-r\tplayer is red (implies -n)\n\t-w\tplayer is white (implies -n)",
50 "\t-b\ttwo players, red and white (implies -n)",
51 "\t-pr\tprint the board before red's turn",
52 "\t-pw\tprint the board before white's turn",
53 "\t-pb\tprint the board before both player's turn",
54 "\t-tterm\tterminal is a term",
55 "\t-sfile\trecover saved game from file",
60 errexit(const char *s
)
71 if (buffnum
== BUFSIZ
) {
72 if (write(1, outbuff
, BUFSIZ
) != BUFSIZ
)
73 errexit("addbuf (write):");
86 if (write(1, outbuff
, buffnum
) != buffnum
)
87 errexit("buflush (write):");
101 if (read(0, &c
, 1) != 1)
103 #ifdef WHY_IS_THIS_HARDWIRED_IN_HERE
107 if (c
== '\033' || c
== '\015')
113 if (c
>= 'a' && c
<= 'z')
128 writel(const char *l
)
134 trace
= fopen("bgtrace", "w");
136 fprintf(trace
, "writel: \"");
137 for (s
= l
; *s
; s
++) {
138 if (*s
< ' ' || *s
== '\177')
139 fprintf(trace
, "^%c", (*s
) ^ 0100);
143 fprintf(trace
, "\"\n");
157 writel("Red's roll: ");
159 writel("White's roll: ");
172 for (i
= 4; i
> 0; i
--) {
174 for (j
= 0; j
< i
; j
++)
177 writec((n
/ t
) % 10 + '0');
179 writec(n
% 10 + '0');
194 writel("Game value: ");
201 writel(" doubled last.");
204 case -1: /* player is red */
207 case 0: /* player is both colors */
210 case 1: /* player is white */
215 if (rscore
|| wscore
) {
233 writel("Are you sure you want to quit?");
236 writel("Would you like to save this game?");
253 while ((c
= readc()) != 'Y' && c
!= 'N') {
254 if (special
&& c
== special
)
258 writel(" (Y, N, or ");
279 writel("Blot hit on ");
291 c
= cturn
/ abs(cturn
);
303 getarg(int argc
, char **argv
)
308 /* process arguments here. dashes are ignored, nbrw are ignored if
309 * the game is being recovered */
311 while ((ch
= getopt(argc
, argv
, "nbrwp:t:s:h")) != -1) {
313 /* don't ask if rules or instructions needed */
318 args
[acnt
++] = strdup("-n");
321 /* player is both red and white */
327 args
[acnt
++] = strdup("-b");
336 args
[acnt
++] = strdup("-r");
339 /* player is white */
345 args
[acnt
++] = strdup("-w");
348 /* print board after move according to following character */
350 if (optarg
[0] != 'r' && optarg
[0] != 'w' &&
353 args
[acnt
] = strdup("-p ");
354 args
[acnt
++][2] = optarg
[0];
355 if (optarg
[0] == 'r')
357 if (optarg
[0] == 'w')
359 if (optarg
[0] == 'b')
364 tflag
= getcaps(optarg
);
372 for (i
= 0; descr
[i
] != 0; i
++)
379 if (argc
&& argv
[0][0] != '\0')
388 for (i
= 0; i
< 26; )
391 board
[6] = board
[13] = -5;
393 board
[12] = board
[19] = 5;
396 off
[0] = off
[1] = -15;
422 if (tcsetattr(0, TCSANOW
, &tty
) < 0)
429 /* go to bottom of screen */
436 /* fix terminal status */
458 while (c
< '1' || c
> '6')
464 while (c
< '1' || c
> '6')