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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)main.c 8.1 (Berkeley) 5/31/93
35 * $FreeBSD: src/games/backgammon/backgammon/main.c,v 1.13 1999/11/30 03:48:22 billf Exp $
36 * $DragonFly: src/games/backgammon/backgammon/main.c,v 1.3 2006/08/08 16:36:11 pavalos Exp $
44 #define MVPAUSE 5 /* time to sleep when stuck */
45 #define MAXUSERS 35 /* maximum number of users */
47 extern const char *const instr
[]; /* text of instructions */
48 extern const char *const message
[]; /* update message */
50 const char *helpm
[] = { /* help message */
51 "Enter a space or newline to roll, or",
52 " R to reprint the board\tD to double",
53 " S to save the game\tQ to quit",
57 const char *contin
[] = { /* pause message */
58 "(Type a newline to continue.)",
63 static const char rules
[] = "\nDo you want the rules of the game?";
64 static const char noteach
[] = "Teachgammon not available!\n\007";
65 static const char need
[] = "Do you need instructions for this program?";
66 static const char askcol
[] =
67 "Enter 'r' to play red, 'w' to play white, 'b' to play both:";
68 static const char rollr
[] = "Red rolls a ";
69 static const char rollw
[] = ". White rolls a ";
70 static const char rstart
[] = ". Red starts.\n";
71 static const char wstart
[] = ". White starts.\n";
72 static const char toobad1
[] = "Too bad, ";
73 static const char unable
[] = " is unable to use that roll.\n";
74 static const char toobad2
[] = ". Too bad, ";
75 static const char cantmv
[] = " can't move.\n";
76 static const char bgammon
[] = "Backgammon! ";
77 static const char gammon
[] = "Gammon! ";
78 static const char again
[] = ".\nWould you like to play again?";
79 static const char svpromt
[] = "Would you like to save this game?";
81 static const char password
[] = "losfurng";
85 main(int argc
, char **argv
)
87 int i
; /* non-descript index */
88 int l
; /* non-descript index */
89 char c
; /* non-descript character storage */
95 bflag
= 2; /* default no board */
96 acnt
= 1; /* Nuber of args */
97 signal (SIGINT
,(sig_t
)getout
); /* trap interrupts */
98 if (ioctl(0, TIOCGETP
, &tty
) == -1) /* get old tty mode */
99 errexit ("backgammon(gtty)");
102 raw
= ((noech
= old
& ~ECHO
) | CBREAK
); /* set up modes */
104 raw
= ((noech
= old
& ~ECHO
) | RAW
); /* set up modes */
111 tflag
= getcaps (getenv ("TERM"));
120 if (tflag
) { /* clear screen */
121 noech
&= ~(CRMOD
|XTABS
);
122 raw
&= ~(CRMOD
|XTABS
);
125 fixtty (raw
); /* go into raw mode */
130 if ((rfl
= rflag
) != 0) {
131 text (message
); /* print message */
133 wrboard(); /* print board */
134 /* if new game, pretend
135 * to be a non-restored
140 rscore
= wscore
= 0; /* zero score */
141 text (message
); /* update message
144 if (aflag
) { /* print rules */
148 fixtty (old
); /* restore tty */
149 args
[0] = strdup ("teachgammon");
152 tflag
= 0; /* error! */
155 } else { /* if not rules, then
158 if (yorn(0)) { /* print instructions */
165 for (i
= 0; i
< acnt
; i
++)
168 init(); /* initialize board */
170 if (pnum
== 2) { /* ask for color(s) */
181 case 'W': /* white */
196 writel ("Password:");
197 signal (SIGALRM
,(sig_t
)getout
);
200 for (i
= 0; i
< 10; i
++) {
206 while (readc() != '\n');
211 for (i
= 0; i
< 9; i
++)
212 if (pbuf
[i
] != password
[i
])
231 wrboard(); /* print board */
238 /* limit text to bottom
243 for (;;) { /* begin game! */
246 if ((! rflag
) || raflag
)
254 while (D0
== D1
) /* no doubles */
263 /* winner goes first */
273 /* initalize variables
277 if (cturn
== 1) { /* red */
284 Colorptr
= &color
[1];
285 colorptr
= &color
[3];
294 Colorptr
= &color
[0];
295 colorptr
= &color
[2];
301 if (! (rflag
&& raflag
)) {
302 if (cturn
== pnum
) /* computer's move */
304 else { /* player's move */
308 curmove (cturn
== -1? 18: 19,0);
310 getmove(); /* get player's move */
319 /* no longer any diff-
320 * erence between normal
325 /* move as long as it's
327 while (cturn
== 1 || cturn
== -1) {
329 /* board maintainence */
331 refresh(); /* fix board */
333 /* redo board if -p */
334 if (cturn
== bflag
|| bflag
== 0)
337 /* do computer's move */
343 if (cturn
== -2 || cturn
== 2)
356 /* (player's move) */
360 if (tflag
&& hflag
) {
366 /* if allowed, give him
367 * a chance to double */
368 if (dlast
!= cturn
&& gvalue
< 64) {
370 curmove (cturn
== -1? 18: 19,0);
374 /* character cases */
408 /* see if he can move */
409 if ( (mvlim
= movallow()) == 0) {
433 /* invalid character */
436 /* print help message */
443 curmove (cturn
== -1? 18: 19,0);
450 } else { /* couldn't double */
455 curmove (cturn
== -1? 18: 19,0);
459 if ((mvlim
= movallow()) == 0) {
476 /* don't worry about who
481 /* fix cturn = winner */
484 /* final board pos. */
491 for (i
= bar
; i
!= l
; i
+= cturn
)
492 if (board
[i
]*cturn
) mflag
++;
494 /* compute game value */
502 else if (*offptr
<= 0) {
508 /* report situation */
510 writel ("Red wins ");
513 writel ("White wins ");
528 if ((i
= yorn ('S')) == 0)
538 /* yes, reset game */
542 /* give him a chance to save if game was recovered */
554 /* leave peacefully */