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 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)main.c 8.1 (Berkeley) 5/31/93
31 * $FreeBSD: src/games/backgammon/backgammon/main.c,v 1.13 1999/11/30 03:48:22 billf Exp $
42 #define MVPAUSE 5 /* time to sleep when stuck */
43 #define MAXUSERS 35 /* maximum number of users */
45 extern const char *const instr
[]; /* text of instructions */
46 extern const char *const message
[]; /* update message */
48 const char *helpm
[] = { /* help message */
49 "Enter a space or newline to roll, or",
50 " R to reprint the board\tD to double",
51 " S to save the game\tQ to quit",
55 const char *contin
[] = { /* pause message */
56 "(Type a newline to continue.)",
61 static const char rules
[] = "\nDo you want the rules of the game?";
62 static const char noteach
[] = "Teachgammon not available!\n\a";
63 static const char need
[] = "Do you need instructions for this program?";
64 static const char askcol
[] =
65 "Enter 'r' to play red, 'w' to play white, 'b' to play both:";
66 static const char rollr
[] = "Red rolls a ";
67 static const char rollw
[] = ". White rolls a ";
68 static const char rstart
[] = ". Red starts.\n";
69 static const char wstart
[] = ". White starts.\n";
70 static const char toobad1
[] = "Too bad, ";
71 static const char unable
[] = " is unable to use that roll.\n";
72 static const char toobad2
[] = ". Too bad, ";
73 static const char cantmv
[] = " can't move.\n";
74 static const char bgammon
[] = "Backgammon! ";
75 static const char gammon
[] = "Gammon! ";
76 static const char again
[] = ".\nWould you like to play again?";
77 static const char svpromt
[] = "Would you like to save this game?";
79 static const char password
[] = "losfurng";
83 main(int argc
, char **argv
)
92 bflag
= 2; /* default no board */
93 acnt
= 1; /* Number of args */
94 signal(SIGINT
, getout
); /* trap interrupts */
95 if (tcgetattr(0, &tty
) == -1) /* get old tty mode */
96 errexit("backgammon(tcgetattr)");
98 bgraw
= ((noech
= old
& ~ECHO
) & ~ICANON
); /* set up modes */
99 ospeed
= cfgetospeed(&tty
); /* for termlib */
101 /* get terminal capabilities, and decide if it can cursor address */
102 tflag
= getcaps(getenv("TERM"));
103 /* use whole screen for text */
110 if (tflag
) { /* clear screen */
111 noech
&= ~(ICRNL
| OXTABS
);
112 bgraw
&= ~(ICRNL
| OXTABS
);
115 fixtty(bgraw
); /* go into raw mode */
117 /* check if restored game and save flag for later */
118 if ((rfl
= rflag
) != 0) {
119 text(message
); /* print message */
121 wrboard(); /* print board */
122 /* if new game, pretend
123 * to be a non-restored game
128 rscore
= wscore
= 0; /* zero score */
129 text(message
); /* update message
132 if (aflag
) { /* print rules */
135 fixtty(old
); /* restore tty */
136 args
[0] = strdup("teachgammon");
139 tflag
= 0; /* error! */
142 } else { /* if not rules, then
146 if (yorn(0)) { /* print instructions */
153 for (i
= 0; i
< acnt
; i
++)
156 init(); /* initialize board */
158 if (pnum
== 2) { /* ask for color(s) */
168 case 'W': /* white */
184 signal(SIGALRM
, getout
);
187 for (i
= 0; i
< 10; i
++) {
193 while (readc() != '\n');
198 for (i
= 0; i
< 9; i
++)
199 if (pbuf
[i
] != password
[i
])
215 /* pause to read message */
218 wrboard(); /* print board */
225 /* limit text to bottom of screen */
229 for (;;) { /* begin game! */
230 /* initial roll if needed */
231 if ((!rflag
) || raflag
)
234 /* perform ritual of first roll */
238 while (D0
== D1
) /* no doubles */
247 /* winner goes first */
257 /* initialize variables according to whose turn it is */
258 if (cturn
== 1) { /* red */
265 Colorptr
= &color
[1];
266 colorptr
= &color
[3];
275 Colorptr
= &color
[0];
276 colorptr
= &color
[2];
280 /* do first move (special case) */
281 if (!(rflag
&& raflag
)) {
282 if (cturn
== pnum
) /* computer's move */
284 else { /* player's move */
288 curmove(cturn
== -1 ? 18 : 19, 0);
290 getmove(); /* get player's move */
299 /* no longer any difference between normal and recovered game. */
302 /* move as long as it's someone's turn */
303 while (cturn
== 1 || cturn
== -1) {
304 /* board maintenance */
306 refresh(); /* fix board */
308 /* redo board if -p */
309 if (cturn
== bflag
|| bflag
== 0)
312 /* do computer's move */
316 /* see if double refused */
317 if (cturn
== -2 || cturn
== 2)
320 /* check for winning move */
328 /* (player's move) */
330 /* clean screen if safe */
331 if (tflag
&& hflag
) {
337 /* if allowed, give him a chance to double */
338 if (dlast
!= cturn
&& gvalue
< 64) {
340 curmove(cturn
== -1 ? 18 : 19, 0);
344 /* character cases */
346 case 'R': /* reprint board */
350 case 'S': /* save game */
359 case 'D': /* double */
372 /* see if he can move */
373 if ((mvlim
= movallow()) == 0) {
391 /* okay to clean screen */
395 /* invalid character */
398 /* print help message */
405 curmove(cturn
== -1 ? 18 : 19,
413 } else { /* couldn't double */
417 curmove(cturn
== -1 ? 18 : 19, 0);
421 if ((mvlim
= movallow()) == 0) {
437 /* don't worry about who won if quit */
441 /* fix cturn = winner */
444 /* final board pos. */
451 for (i
= bar
; i
!= l
; i
+= cturn
)
452 if (board
[i
] && cturn
)
455 /* compute game value */
469 /* report situation */
474 writel("White wins ");
486 /* see if he wants another game */
488 if ((i
= yorn('S')) == 0)
498 /* yes, reset game */
502 /* give him a chance to save if game was recovered */
506 /* re-initialize for recovery */
513 /* leave peacefully */