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.2 2003/06/17 04:25:22 dillon Exp $
46 #define MVPAUSE 5 /* time to sleep when stuck */
47 #define MAXUSERS 35 /* maximum number of users */
49 extern const char *const instr[]; /* text of instructions */
50 extern const char *const message[]; /* update message */
51 char ospeed; /* tty output speed */
53 const char *helpm[] = { /* help message */
54 "Enter a space or newline to roll, or",
55 " R to reprint the board\tD to double",
56 " S to save the game\tQ to quit",
60 const char *contin[] = { /* pause message */
61 "(Type a newline to continue.)",
66 static const char rules[] = "\nDo you want the rules of the game?";
67 static const char noteach[] = "Teachgammon not available!\n\007";
68 static const char need[] = "Do you need instructions for this program?";
69 static const char askcol[] =
70 "Enter 'r' to play red, 'w' to play white, 'b' to play both:";
71 static const char rollr[] = "Red rolls a ";
72 static const char rollw[] = ". White rolls a ";
73 static const char rstart[] = ". Red starts.\n";
74 static const char wstart[] = ". White starts.\n";
75 static const char toobad1[] = "Too bad, ";
76 static const char unable[] = " is unable to use that roll.\n";
77 static const char toobad2[] = ". Too bad, ";
78 static const char cantmv[] = " can't move.\n";
79 static const char bgammon[] = "Backgammon! ";
80 static const char gammon[] = "Gammon! ";
81 static const char again[] = ".\nWould you like to play again?";
82 static const char svpromt[] = "Would you like to save this game?";
84 static const char password[] = "losfurng";
92 int i; /* non-descript index */
93 int l; /* non-descript index */
94 char c; /* non-descript character storage */
100 bflag = 2; /* default no board */
101 acnt = 1; /* Nuber of args */
102 signal (SIGINT,getout); /* trap interrupts */
103 if (gtty (0,&tty) == -1) /* get old tty mode */
104 errexit ("backgammon(gtty)");
107 raw = ((noech = old & ~ECHO) | CBREAK); /* set up modes */
109 raw = ((noech = old & ~ECHO) | RAW); /* set up modes */
111 ospeed = tty.sg_ospeed; /* for termlib */
117 tflag = getcaps (getenv ("TERM"));
126 if (tflag) { /* clear screen */
127 noech &= ~(CRMOD|XTABS);
128 raw &= ~(CRMOD|XTABS);
131 fixtty (raw); /* go into raw mode */
137 text (message); /* print message */
139 wrboard(); /* print board */
140 /* if new game, pretend
141 * to be a non-restored
146 rscore = wscore = 0; /* zero score */
147 text (message); /* update message
150 if (aflag) { /* print rules */
154 fixtty (old); /* restore tty */
155 args[0] = strdup ("teachgammon");
158 tflag = 0; /* error! */
161 } else { /* if not rules, then
164 if (yorn(0)) { /* print instructions */
171 for (i = 0; i < acnt; i++)
174 init(); /* initialize board */
176 if (pnum == 2) { /* ask for color(s) */
187 case 'W': /* white */
202 writel ("Password:");
203 signal (SIGALRM,getout);
206 for (i = 0; i < 10; i++) {
212 while (readc() != '\n');
217 for (i = 0; i < 9; i++)
218 if (pbuf[i] != password[i])
237 wrboard(); /* print board */
244 /* limit text to bottom
249 for (;;) { /* begin game! */
252 if ((! rflag) || raflag)
260 while (D0 == D1) /* no doubles */
269 /* winner goes first */
279 /* initalize variables
283 if (cturn == 1) { /* red */
290 Colorptr = &color[1];
291 colorptr = &color[3];
300 Colorptr = &color[0];
301 colorptr = &color[2];
307 if (! (rflag && raflag)) {
308 if (cturn == pnum) /* computer's move */
310 else { /* player's move */
314 curmove (cturn == -1? 18: 19,0);
316 getmove(); /* get player's move */
325 /* no longer any diff-
326 * erence between normal
331 /* move as long as it's
333 while (cturn == 1 || cturn == -1) {
335 /* board maintainence */
337 refresh(); /* fix board */
339 /* redo board if -p */
340 if (cturn == bflag || bflag == 0)
343 /* do computer's move */
349 if (cturn == -2 || cturn == 2)
362 /* (player's move) */
366 if (tflag && hflag) {
372 /* if allowed, give him
373 * a chance to double */
374 if (dlast != cturn && gvalue < 64) {
376 curmove (cturn == -1? 18: 19,0);
380 /* character cases */
414 /* see if he can move */
415 if ( (mvlim = movallow()) == 0) {
439 /* invalid character */
442 /* print help message */
449 curmove (cturn == -1? 18: 19,0);
456 } else { /* couldn't double */
461 curmove (cturn == -1? 18: 19,0);
465 if ((mvlim = movallow()) == 0) {
482 /* don't worry about who
487 /* fix cturn = winner */
490 /* final board pos. */
497 for (i = bar; i != l; i += cturn)
498 if (board[i]*cturn) mflag++;
500 /* compute game value */
508 else if (*offptr <= 0) {
514 /* report situation */
516 writel ("Red wins ");
519 writel ("White wins ");
534 if ((i = yorn ('S')) == 0)
544 /* yes, reset game */
548 /* give him a chance to save if game was recovered */
560 /* leave peacefully */