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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
44 static const char rcsid[] =
45 "$FreeBSD: src/games/backgammon/backgammon/main.c,v 1.13 1999/11/30 03:48:22 billf Exp $";
55 #define MVPAUSE 5 /* time to sleep when stuck */
56 #define MAXUSERS 35 /* maximum number of users */
58 extern const char *const instr[]; /* text of instructions */
59 extern const char *const message[]; /* update message */
60 char ospeed; /* tty output speed */
62 const char *helpm[] = { /* help message */
63 "Enter a space or newline to roll, or",
64 " R to reprint the board\tD to double",
65 " S to save the game\tQ to quit",
69 const char *contin[] = { /* pause message */
70 "(Type a newline to continue.)",
75 static const char rules[] = "\nDo you want the rules of the game?";
76 static const char noteach[] = "Teachgammon not available!\n\007";
77 static const char need[] = "Do you need instructions for this program?";
78 static const char askcol[] =
79 "Enter 'r' to play red, 'w' to play white, 'b' to play both:";
80 static const char rollr[] = "Red rolls a ";
81 static const char rollw[] = ". White rolls a ";
82 static const char rstart[] = ". Red starts.\n";
83 static const char wstart[] = ". White starts.\n";
84 static const char toobad1[] = "Too bad, ";
85 static const char unable[] = " is unable to use that roll.\n";
86 static const char toobad2[] = ". Too bad, ";
87 static const char cantmv[] = " can't move.\n";
88 static const char bgammon[] = "Backgammon! ";
89 static const char gammon[] = "Gammon! ";
90 static const char again[] = ".\nWould you like to play again?";
91 static const char svpromt[] = "Would you like to save this game?";
93 static const char password[] = "losfurng";
101 int i; /* non-descript index */
102 int l; /* non-descript index */
103 char c; /* non-descript character storage */
109 bflag = 2; /* default no board */
110 acnt = 1; /* Nuber of args */
111 signal (SIGINT,getout); /* trap interrupts */
112 if (gtty (0,&tty) == -1) /* get old tty mode */
113 errexit ("backgammon(gtty)");
116 raw = ((noech = old & ~ECHO) | CBREAK); /* set up modes */
118 raw = ((noech = old & ~ECHO) | RAW); /* set up modes */
120 ospeed = tty.sg_ospeed; /* for termlib */
126 tflag = getcaps (getenv ("TERM"));
135 if (tflag) { /* clear screen */
136 noech &= ~(CRMOD|XTABS);
137 raw &= ~(CRMOD|XTABS);
140 fixtty (raw); /* go into raw mode */
146 text (message); /* print message */
148 wrboard(); /* print board */
149 /* if new game, pretend
150 * to be a non-restored
155 rscore = wscore = 0; /* zero score */
156 text (message); /* update message
159 if (aflag) { /* print rules */
163 fixtty (old); /* restore tty */
164 args[0] = strdup ("teachgammon");
167 tflag = 0; /* error! */
170 } else { /* if not rules, then
173 if (yorn(0)) { /* print instructions */
180 for (i = 0; i < acnt; i++)
183 init(); /* initialize board */
185 if (pnum == 2) { /* ask for color(s) */
196 case 'W': /* white */
211 writel ("Password:");
212 signal (SIGALRM,getout);
215 for (i = 0; i < 10; i++) {
221 while (readc() != '\n');
226 for (i = 0; i < 9; i++)
227 if (pbuf[i] != password[i])
246 wrboard(); /* print board */
253 /* limit text to bottom
258 for (;;) { /* begin game! */
261 if ((! rflag) || raflag)
269 while (D0 == D1) /* no doubles */
278 /* winner goes first */
288 /* initalize variables
292 if (cturn == 1) { /* red */
299 Colorptr = &color[1];
300 colorptr = &color[3];
309 Colorptr = &color[0];
310 colorptr = &color[2];
316 if (! (rflag && raflag)) {
317 if (cturn == pnum) /* computer's move */
319 else { /* player's move */
323 curmove (cturn == -1? 18: 19,0);
325 getmove(); /* get player's move */
334 /* no longer any diff-
335 * erence between normal
340 /* move as long as it's
342 while (cturn == 1 || cturn == -1) {
344 /* board maintainence */
346 refresh(); /* fix board */
348 /* redo board if -p */
349 if (cturn == bflag || bflag == 0)
352 /* do computer's move */
358 if (cturn == -2 || cturn == 2)
371 /* (player's move) */
375 if (tflag && hflag) {
381 /* if allowed, give him
382 * a chance to double */
383 if (dlast != cturn && gvalue < 64) {
385 curmove (cturn == -1? 18: 19,0);
389 /* character cases */
423 /* see if he can move */
424 if ( (mvlim = movallow()) == 0) {
448 /* invalid character */
451 /* print help message */
458 curmove (cturn == -1? 18: 19,0);
465 } else { /* couldn't double */
470 curmove (cturn == -1? 18: 19,0);
474 if ((mvlim = movallow()) == 0) {
491 /* don't worry about who
496 /* fix cturn = winner */
499 /* final board pos. */
506 for (i = bar; i != l; i += cturn)
507 if (board[i]*cturn) mflag++;
509 /* compute game value */
517 else if (*offptr <= 0) {
523 /* report situation */
525 writel ("Red wins ");
528 writel ("White wins ");
543 if ((i = yorn ('S')) == 0)
553 /* yes, reset game */
557 /* give him a chance to save if game was recovered */
569 /* leave peacefully */