]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/backgammon/main.c
0aab499d3d57be2f62e5697d948fc1edd6e4bcad
1 /* $NetBSD: main.c,v 1.15 1999/09/17 17:07:11 jsm Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
46 __RCSID("$NetBSD: main.c,v 1.15 1999/09/17 17:07:11 jsm Exp $");
53 #include "backlocal.h"
55 #define MVPAUSE 5 /* time to sleep when stuck */
57 extern const char *const instr
[]; /* text of instructions */
58 extern const char *const message
[]; /* update message */
59 short ospeed
; /* tty output speed */
61 const char *const helpm
[] = { /* help message */
62 "Enter a space or newline to roll, or",
63 " R to reprint the board\tD to double",
64 " S to save the game\tQ to quit",
68 const char *const contin
[] = { /* pause message */
69 "(Type a newline to continue.)",
73 static const char rules
[] = "\nDo you want the rules of the game?";
74 static const char noteach
[] = "Teachgammon not available!\n\a";
75 static const char need
[] = "Do you need instructions for this program?";
76 static const char askcol
[] =
77 "Enter 'r' to play red, 'w' to play white, 'b' to play both:";
78 static const char rollr
[] = "Red rolls a ";
79 static const char rollw
[] = ". White rolls a ";
80 static const char rstart
[] = ". Red starts.\n";
81 static const char wstart
[] = ". White starts.\n";
82 static const char toobad1
[] = "Too bad, ";
83 static const char unable
[] = " is unable to use that roll.\n";
84 static const char toobad2
[] = ". Too bad, ";
85 static const char cantmv
[] = " can't move.\n";
86 static const char bgammon
[] = "Backgammon! ";
87 static const char gammon
[] = "Gammon! ";
88 static const char again
[] = ".\nWould you like to play again?";
89 static const char svpromt
[] = "Would you like to save this game?";
91 static const char password
[] = "losfurng";
99 int i
; /* non-descript index */
100 int l
; /* non-descript index */
101 char c
; /* non-descript character storage */
102 long t
; /* time for random num generator */
104 /* revoke setgid privileges */
105 setregid(getgid(), getgid());
108 bflag
= 2; /* default no board */
109 signal(SIGINT
, getout
); /* trap interrupts */
110 if (tcgetattr(0, &old
) == -1) /* get old tty mode */
111 errexit("backgammon(gtty)");
113 noech
.c_lflag
&= ~ECHO
;
115 raw
.c_lflag
&= ~ICANON
; /* set up modes */
116 ospeed
= cfgetospeed(&old
); /* for termlib */
118 /* get terminal capabilities, and decide if it can cursor address */
119 tflag
= getcaps(getenv("TERM"));
120 /* use whole screen for text */
124 srandom(t
); /* 'random' seed */
127 while (*++argv
!= 0) /* process arguments */
129 while (*++argv
!= -1) /* process arguments */
133 if (tflag
) { /* clear screen */
134 noech
.c_oflag
&= ~(ONLCR
| OXTABS
);
135 raw
.c_oflag
&= ~(ONLCR
| OXTABS
);
138 fixtty(&raw
); /* go into raw mode */
140 /* check if restored game and save flag for later */
141 if ((rfl
= rflag
) != 0) {
142 text(message
); /* print message */
144 wrboard(); /* print board */
145 /* if new game, pretend to be a non-restored game */
149 rscore
= wscore
= 0; /* zero score */
150 text(message
); /* update message without pausing */
152 if (aflag
) { /* print rules */
156 fixtty(&old
); /* restore tty */
157 execl(TEACH
, "teachgammon", args
[0]?args
:0, 0);
159 tflag
= 0; /* error! */
162 } else {/* if not rules, then instructions */
164 if (yorn(0)) { /* print instructions */
170 init(); /* initialize board */
172 if (pnum
== 2) {/* ask for color(s) */
183 case 'W': /* white */
199 signal(SIGALRM
, getout
);
202 for (i
= 0; i
< 10; i
++) {
208 while (readc() != '\n');
213 for (i
= 0; i
< 9; i
++)
214 if (pbuf
[i
] != password
[i
])
230 /* pause to read message */
233 wrboard(); /* print board */
240 /* limit text to bottom of screen */
244 for (;;) { /* begin game! */
245 /* initial roll if needed */
246 if ((!rflag
) || raflag
)
249 /* perform ritual of first roll */
253 while (D0
== D1
) /* no doubles */
262 /* winner goes first */
271 /* initalize variables according to whose turn it is */
273 if (cturn
== 1) { /* red */
280 Colorptr
= &color
[1];
281 colorptr
= &color
[3];
290 Colorptr
= &color
[0];
291 colorptr
= &color
[2];
295 /* do first move (special case) */
296 if (!(rflag
&& raflag
)) {
297 if (cturn
== pnum
) /* computer's move */
299 else { /* player's move */
303 curmove(cturn
== -1 ? 18 : 19, 0);
305 getmove(); /* get player's move */
313 /* no longer any diff- erence between normal game and
317 /* move as long as it's someone's turn */
318 while (cturn
== 1 || cturn
== -1) {
320 /* board maintainence */
322 refresh(); /* fix board */
324 /* redo board if -p */
325 if (cturn
== bflag
|| bflag
== 0)
328 /* do computer's move */
332 /* see if double refused */
333 if (cturn
== -2 || cturn
== 2)
336 /* check for winning move */
344 /* (player's move) */
346 /* clean screen if safe */
347 if (tflag
&& hflag
) {
352 /* if allowed, give him a chance to double */
353 if (dlast
!= cturn
&& gvalue
< 64) {
355 curmove(cturn
== -1 ? 18 : 19, 0);
359 /* character cases */
393 /* see if he can move */
394 if ((mvlim
= movallow()) == 0) {
412 /* okay to clean screen */
416 /* invalid character */
419 /* print help message */
426 curmove(cturn
== -1 ? 18 : 19, 0);
433 } else {/* couldn't double */
438 curmove(cturn
== -1 ? 18 : 19, 0);
442 if ((mvlim
= movallow()) == 0) {
458 /* don't worry about who won if quit */
462 /* fix cturn = winner */
465 /* final board pos. */
472 for (i
= bar
; i
!= l
; i
+= cturn
)
473 if (board
[i
] * cturn
)
476 /* compute game value */
479 if (*offopp
== 15 && *offptr
<= 0) {
488 /* report situation */
493 writel("White wins ");
505 /* see if he wants another game */
507 if ((i
= yorn('S')) == 0)
516 /* yes, reset game */
520 /* give him a chance to save if game was recovered */
524 /* re-initialize for recovery */
530 /* leave peacefully */