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 $
32 * $DragonFly: src/games/backgammon/backgammon/main.c,v 1.3 2006/08/08 16:36:11 pavalos Exp $
43 #define MVPAUSE 5 /* time to sleep when stuck */
44 #define MAXUSERS 35 /* maximum number of users */
46 extern const char *const instr[]; /* text of instructions */
47 extern const char *const message[]; /* update message */
49 const char *helpm[] = { /* help message */
50 "Enter a space or newline to roll, or",
51 " R to reprint the board\tD to double",
52 " S to save the game\tQ to quit",
56 const char *contin[] = { /* pause message */
57 "(Type a newline to continue.)",
62 static const char rules[] = "\nDo you want the rules of the game?";
63 static const char noteach[] = "Teachgammon not available!\n\a";
64 static const char need[] = "Do you need instructions for this program?";
65 static const char askcol[] =
66 "Enter 'r' to play red, 'w' to play white, 'b' to play both:";
67 static const char rollr[] = "Red rolls a ";
68 static const char rollw[] = ". White rolls a ";
69 static const char rstart[] = ". Red starts.\n";
70 static const char wstart[] = ". White starts.\n";
71 static const char toobad1[] = "Too bad, ";
72 static const char unable[] = " is unable to use that roll.\n";
73 static const char toobad2[] = ". Too bad, ";
74 static const char cantmv[] = " can't move.\n";
75 static const char bgammon[] = "Backgammon! ";
76 static const char gammon[] = "Gammon! ";
77 static const char again[] = ".\nWould you like to play again?";
78 static const char svpromt[] = "Would you like to save this game?";
80 static const char password[] = "losfurng";
84 main(int argc, char **argv)
93 bflag = 2; /* default no board */
94 acnt = 1; /* Number of args */
95 signal(SIGINT, (sig_t)getout); /* trap interrupts */
96 if (tcgetattr(0, &tty) == -1) /* get old tty mode */
97 errexit("backgammon(tcgetattr)");
99 raw = ((noech = old & ~ECHO) & ~ICANON); /* set up modes */
100 ospeed = cfgetospeed(&tty); /* for termlib */
102 /* get terminal capabilities, and decide if it can cursor address */
103 tflag = getcaps(getenv("TERM"));
104 /* use whole screen for text */
111 if (tflag) { /* clear screen */
112 noech &= ~(ICRNL | OXTABS);
113 raw &= ~(ICRNL | OXTABS);
116 fixtty(raw); /* go into raw mode */
118 /* check if restored game and save flag for later */
119 if ((rfl = rflag) != 0) {
120 text(message); /* print message */
122 wrboard(); /* print board */
123 /* if new game, pretend
124 * to be a non-restored game
129 rscore = wscore = 0; /* zero score */
130 text(message); /* update message
133 if (aflag) { /* print rules */
136 fixtty(old); /* restore tty */
137 args[0] = strdup("teachgammon");
140 tflag = 0; /* error! */
143 } else { /* if not rules, then
147 if (yorn(0)) { /* print instructions */
154 for (i = 0; i < acnt; i++)
157 init(); /* initialize board */
159 if (pnum == 2) { /* ask for color(s) */
169 case 'W': /* white */
185 signal(SIGALRM, (sig_t)getout);
188 for (i = 0; i < 10; i++) {
194 while (readc() != '\n');
199 for (i = 0; i < 9; i++)
200 if (pbuf[i] != password[i])
216 /* pause to read message */
219 wrboard(); /* print board */
226 /* limit text to bottom of screen */
230 for (;;) { /* begin game! */
231 /* initial roll if needed */
232 if ((!rflag) || raflag)
235 /* perform ritual of first roll */
239 while (D0 == D1) /* no doubles */
248 /* winner goes first */
258 /* initialize variables according to whose turn it is */
259 if (cturn == 1) { /* red */
266 Colorptr = &color[1];
267 colorptr = &color[3];
276 Colorptr = &color[0];
277 colorptr = &color[2];
281 /* do first move (special case) */
282 if (!(rflag && raflag)) {
283 if (cturn == pnum) /* computer's move */
285 else { /* player's move */
289 curmove(cturn == -1 ? 18 : 19, 0);
291 getmove(); /* get player's move */
300 /* no longer any difference between normal and recovered game. */
303 /* move as long as it's someone's turn */
304 while (cturn == 1 || cturn == -1) {
305 /* board maintenance */
307 refresh(); /* fix board */
309 /* redo board if -p */
310 if (cturn == bflag || bflag == 0)
313 /* do computer's move */
317 /* see if double refused */
318 if (cturn == -2 || cturn == 2)
321 /* check for winning move */
329 /* (player's move) */
331 /* clean screen if safe */
332 if (tflag && hflag) {
338 /* if allowed, give him a chance to double */
339 if (dlast != cturn && gvalue < 64) {
341 curmove(cturn == -1 ? 18 : 19, 0);
345 /* character cases */
347 case 'R': /* reprint board */
351 case 'S': /* save game */
360 case 'D': /* double */
373 /* see if he can move */
374 if ((mvlim = movallow()) == 0) {
392 /* okay to clean screen */
396 /* invalid character */
399 /* print help message */
406 curmove(cturn == -1 ? 18 : 19,
414 } else { /* couldn't double */
418 curmove(cturn == -1 ? 18 : 19, 0);
422 if ((mvlim = movallow()) == 0) {
438 /* don't worry about who won if quit */
442 /* fix cturn = winner */
445 /* final board pos. */
452 for (i = bar; i != l; i += cturn)
453 if (board[i] * cturn)
456 /* compute game value */
470 /* report situation */
475 writel("White wins ");
487 /* see if he wants another game */
489 if ((i = yorn('S')) == 0)
499 /* yes, reset game */
503 /* give him a chance to save if game was recovered */
507 /* re-initialize for recovery */
514 /* leave peacefully */