1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  *	@(#)back.h	8.1 (Berkeley) 5/31/93
30  * $DragonFly: src/games/backgammon/common_source/back.h,v 1.2 2006/08/08 16:36:11 pavalos Exp $
31  */
32 
33 #include <termios.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 
37 #define rnum(r)	(random()%r)
38 #define D0	dice[0]
39 #define D1	dice[1]
40 #define swap	{D0 ^= D1; D1 ^= D0; D0 ^= D1; d0 = 1-d0;}
41 
42 /*
43  *
44  * Some numerical conventions:
45  *
46  *	Arrays have white's value in [0], red in [1].
47  *	Numeric values which are one color or the other use
48  *	-1 for white, 1 for red.
49  *	Hence, white will be negative values, red positive one.
50  *	This makes a lot of sense since white is going in descending
51  *	order around the board, and red is ascending.
52  *
53  */
54 
55 extern const char	EXEC[];	/* object for main program */
56 extern const char	TEACH[];/* object for tutorial program */
57 
58 extern int	pnum;		/* color of player:
59 					-1 = white
60 					 1 = red
61 					 0 = both
62 					 2 = not yet init'ed */
63 extern char	*args[16];	/* args passed to teachgammon and back */
64 extern int	acnt;		/* number of args */
65 extern int	aflag;		/* flag to ask for rules or instructions */
66 extern int	bflag;		/* flag for automatic board printing */
67 extern int	cflag;		/* case conversion flag */
68 extern int	hflag;		/* flag for cleaning screen */
69 extern int	mflag;		/* backgammon flag */
70 extern int	raflag;		/* 'roll again' flag for recovered game */
71 extern int	rflag;		/* recovered game flag */
72 extern int	tflag;		/* cursor addressing flag */
73 extern int	rfl;		/* saved value of rflag */
74 extern int	iroll;		/* special flag for inputting rolls */
75 extern int	board[26];	/* board:  negative values are white,
76 				   positive are red */
77 extern int	dice[2];	/* value of dice */
78 extern int	mvlim;		/* 'move limit':  max. number of moves */
79 extern int	mvl;		/* working copy of mvlim */
80 extern int	p[5];		/* starting position of moves */
81 extern int	g[5];		/* ending position of moves (goals) */
82 extern int	h[4];		/* flag for each move if a man was hit */
83 extern int	cturn;		/* whose turn it currently is:
84 					-1 = white
85 					 1 = red
86 					 0 = just quitted
87 					-2 = white just lost
88 					 2 = red just lost */
89 extern int	d0;		/* flag if dice have been reversed from
90 				   original position */
91 extern int	table[6][6];	/* odds table for possible rolls */
92 extern int	rscore;		/* red's score */
93 extern int	wscore;		/* white's score */
94 extern int	gvalue;		/* value of game (64 max.) */
95 extern int	dlast;		/* who doubled last (0 = neither) */
96 extern int	bar;		/* position of bar for current player */
97 extern int	home;		/* position of home for current player */
98 extern int	off[2];		/* number of men off board */
99 extern int	*offptr;	/* pointer to off for current player */
100 extern int	*offopp;	/* pointer to off for opponent */
101 extern int	in[2];		/* number of men in inner table */
102 extern int	*inptr;		/* pointer to in for current player */
103 extern int	*inopp;		/* pointer to in for opponent */
104 
105 extern int	ncin;		/* number of characters in cin */
106 extern char	cin[100];	/* input line of current move
107 				   (used for reconstructing input after
108 				   a backspace) */
109 
110 extern const char	*const color[];
111 				/* colors as strings */
112 extern const char *const *colorptr;	/* color of current player */
113 extern const char *const *Colorptr;	/* color of current player, capitalized */
114 extern int	colen;		/* length of color of current player */
115 
116 extern struct termios	tty;	/* tty information buffer */
117 extern int		old;	/* original tty status */
118 extern int		noech;	/* original tty status without echo */
119 extern int		raw;	/* raw tty status, no echo */
120 
121 extern int	curr;		/* row position of cursor */
122 extern int	curc;		/* column position of cursor */
123 extern int	begscr;		/* 'beginning' of screen
124 				   (not including board) */
125 
126 int	addbuf(int);
127 void	backone(int);
128 void	buflush(void);
129 int	canhit(int, int);
130 int	checkmove(int);
131 void	clear(void);
132 void	clend(void);
133 void	cline(void);
134 int	count(void);
135 void	curmove(int, int);
136 void	dble(void);
137 int	dblgood(void);
138 void	errexit(const char *);
139 void	fancyc(char);
140 void	fboard(void);
141 void	fixtty(int);
142 int	freemen(int);
143 void	getarg(int, char **);
144 int	getcaps(const char *);
145 void	getmove(void);
146 void	getout(void);		/* function to exit backgammon cleanly */
147 void	gwrite(void);
148 void	init(void);
149 int	makmove(int);
150 int	movallow(void);
151 void	movback(int);
152 void	move(int);
153 void	moverr(int);
154 int	movokay(int);
155 void	newpos(void);
156 void	nexturn(void);
157 void	odds(int, int, int);
158 void	proll(void);
159 int	quit(void);
160 char	readc(void);
161 void	recover(const char *);
162 void	refresh(void);
163 void	roll(void);
164 void	save(int);
165 #ifdef	TEACHGAMMON_TEXT
166 int	text(const char *const *);
167 #else
168 void	text(const char *const *);
169 #endif
170 int	trapped(int, int);
171 void	wrboard(void);
172 void	wrhit(int);
173 void	wrint(int);
174 void	writec(char);
175 void	writel(const char *);
176 void	wrscore(void);
177 int	yorn(char);
178