]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - backgammon/backgammon/main.c
1 /* $NetBSD: main.c,v 1.26 2009/08/12 04:51:11 dholland Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: main.c,v 1.26 2009/08/12 04:51:11 dholland Exp $");
49 #include "backlocal.h"
51 #define MVPAUSE 5 /* time to sleep when stuck */
53 extern const char *const instr
[]; /* text of instructions */
54 extern const char *const message
[]; /* update message */
55 extern short ospeed
; /* tty output speed */
57 const char *const helpm
[] = { /* help message */
58 "Enter a space or newline to roll, or",
59 " R to reprint the board\tD to double",
60 " S to save the game\tQ to quit",
64 const char *const contin
[] = { /* pause message */
65 "(Type a newline to continue.)",
69 static const char rules
[] = "\nDo you want the rules of the game?";
70 static const char noteach
[] = "Teachgammon not available!\n\a";
71 static const char need
[] = "Do you need instructions for this program?";
72 static const char askcol
[] =
73 "Enter 'r' to play red, 'w' to play white, 'b' to play both:";
74 static const char rollr
[] = "Red rolls a ";
75 static const char rollw
[] = ". White rolls a ";
76 static const char rstart
[] = ". Red starts.\n";
77 static const char wstart
[] = ". White starts.\n";
78 static const char toobad1
[] = "Too bad, ";
79 static const char unable
[] = " is unable to use that roll.\n";
80 static const char toobad2
[] = ". Too bad, ";
81 static const char cantmv
[] = " can't move.\n";
82 static const char bgammon
[] = "Backgammon! ";
83 static const char gammon
[] = "Gammon! ";
84 static const char again
[] = ".\nWould you like to play again?";
85 static const char svpromt
[] = "Would you like to save this game?";
87 static const char password
[] = "losfurng";
91 main(int argc __unused
, char **argv
)
93 int i
; /* non-descript index */
94 int l
; /* non-descript index */
95 char c
; /* non-descript character storage */
96 time_t t
; /* time for random num generator */
98 /* revoke setgid privileges */
102 bflag
= 2; /* default no board */
103 signal(SIGINT
, getout
); /* trap interrupts */
104 if (tcgetattr(0, &old
) == -1) /* get old tty mode */
105 errexit("backgammon(gtty)");
107 noech
.c_lflag
&= ~ECHO
;
109 raw
.c_lflag
&= ~ICANON
; /* set up modes */
110 ospeed
= cfgetospeed(&old
); /* for termlib */
112 /* get terminal capabilities, and decide if it can cursor address */
113 tflag
= getcaps(getenv("TERM"));
114 /* use whole screen for text */
118 srandom(t
); /* 'random' seed */
121 while (*++argv
!= 0) /* process arguments */
123 while (*++argv
!= -1) /* process arguments */
127 if (tflag
) { /* clear screen */
128 noech
.c_oflag
&= ~(ONLCR
| OXTABS
);
129 raw
.c_oflag
&= ~(ONLCR
| OXTABS
);
132 fixtty(&raw
); /* go into raw mode */
134 /* check if restored game and save flag for later */
135 if ((rfl
= rflag
) != 0) {
136 text(message
); /* print message */
138 wrboard(); /* print board */
139 /* if new game, pretend to be a non-restored game */
143 rscore
= wscore
= 0; /* zero score */
144 text(message
); /* update message without pausing */
146 if (aflag
) { /* print rules */
150 fixtty(&old
); /* restore tty */
151 execl(TEACH
, "teachgammon", args
[0]?args
:0,
154 tflag
= 0; /* error! */
157 } else {/* if not rules, then instructions */
159 if (yorn(0)) { /* print instructions */
165 init(); /* initialize board */
167 if (pnum
== 2) {/* ask for color(s) */
178 case 'W': /* white */
194 signal(SIGALRM
, getout
);
197 for (i
= 0; i
< 10; i
++) {
203 while (readc() != '\n');
208 for (i
= 0; i
< 9; i
++)
209 if (pbuf
[i
] != password
[i
])
225 /* pause to read message */
228 wrboard(); /* print board */
235 /* limit text to bottom of screen */
239 for (;;) { /* begin game! */
240 /* initial roll if needed */
241 if ((!rflag
) || raflag
)
244 /* perform ritual of first roll */
248 while (D0
== D1
) /* no doubles */
257 /* winner goes first */
266 /* initialize variables according to whose turn it is */
268 if (cturn
== 1) { /* red */
275 Colorptr
= &color
[1];
276 colorptr
= &color
[3];
285 Colorptr
= &color
[0];
286 colorptr
= &color
[2];
290 /* do first move (special case) */
291 if (!(rflag
&& raflag
)) {
292 if (cturn
== pnum
) /* computer's move */
294 else { /* player's move */
298 curmove(cturn
== -1 ? 18 : 19, 0);
300 getmove(); /* get player's move */
308 /* no longer any diff- erence between normal game and
312 /* move as long as it's someone's turn */
313 while (cturn
== 1 || cturn
== -1) {
315 /* board maintainence */
317 refresh(); /* fix board */
319 /* redo board if -p */
320 if (cturn
== bflag
|| bflag
== 0)
323 /* do computer's move */
327 /* see if double refused */
328 if (cturn
== -2 || cturn
== 2)
331 /* check for winning move */
339 /* (player's move) */
341 /* clean screen if safe */
342 if (tflag
&& hflag
) {
347 /* if allowed, give him a chance to double */
348 if (dlast
!= cturn
&& gvalue
< 64) {
350 curmove(cturn
== -1 ? 18 : 19, 0);
354 /* character cases */
388 /* see if he can move */
389 if ((mvlim
= movallow()) == 0) {
407 /* okay to clean screen */
411 /* invalid character */
414 /* print help message */
421 curmove(cturn
== -1 ?
429 } else {/* couldn't double */
434 curmove(cturn
== -1 ? 18 : 19, 0);
438 if ((mvlim
= movallow()) == 0) {
454 /* don't worry about who won if quit */
458 /* fix cturn = winner */
461 /* final board pos. */
468 for (i
= bar
; i
!= l
; i
+= cturn
)
469 if (board
[i
] * cturn
)
472 /* compute game value */
475 if (*offopp
== 15 && (*offptr
== 0 || *offptr
== -15)) {
484 /* report situation */
489 writel("White wins ");
501 /* see if he wants another game */
503 if ((i
= yorn('S')) == 0)
512 /* yes, reset game */
516 /* give him a chance to save if game was recovered */
520 /* re-initialize for recovery */
526 /* leave peacefully */