xref: /csrg-svn/games/tetris/tetris.h (revision 57277)
1*57277Sbostic /*
2*57277Sbostic  * Definitions for Tetris.
3*57277Sbostic  */
4*57277Sbostic 
5*57277Sbostic /*
6*57277Sbostic  * The display (`board') is composed of 23 rows of 12 columns of characters
7*57277Sbostic  * (numbered 0..22 and 0..11), stored in a single array for convenience.
8*57277Sbostic  * Columns 1 to 10 of rows 1 to 20 are the actual playing area, where
9*57277Sbostic  * shapes appear.  Columns 0 and 11 are always occupied, as are all
10*57277Sbostic  * columns of rows 21 and 22.  Rows 0 and 22 exist as boundary areas
11*57277Sbostic  * so that regions `outside' the visible area can be examined without
12*57277Sbostic  * worrying about addressing problems.
13*57277Sbostic  */
14*57277Sbostic 
15*57277Sbostic 	/* the board */
16*57277Sbostic #define	B_COLS	12
17*57277Sbostic #define	B_ROWS	23
18*57277Sbostic #define	B_SIZE	(B_ROWS * B_COLS)
19*57277Sbostic 
20*57277Sbostic typedef unsigned char cell;
21*57277Sbostic cell	board[B_SIZE];		/* 1 => occupied, 0 => empty */
22*57277Sbostic 
23*57277Sbostic 	/* the displayed area (rows) */
24*57277Sbostic #define	D_FIRST	1
25*57277Sbostic #define	D_LAST	22
26*57277Sbostic 
27*57277Sbostic 	/* the active area (rows) */
28*57277Sbostic #define	A_FIRST	1
29*57277Sbostic #define	A_LAST	21
30*57277Sbostic 
31*57277Sbostic /*
32*57277Sbostic  * Minimum display size.
33*57277Sbostic  */
34*57277Sbostic #define	MINROWS	23
35*57277Sbostic #define	MINCOLS	40
36*57277Sbostic 
37*57277Sbostic int	Rows, Cols;		/* current screen size */
38*57277Sbostic 
39*57277Sbostic /*
40*57277Sbostic  * Translations from board coordinates to display coordinates.
41*57277Sbostic  * As with board coordinates, display coordiates are zero origin.
42*57277Sbostic  */
43*57277Sbostic #define	RTOD(x)	((x) - 1)
44*57277Sbostic #define	CTOD(x)	((x) * 2 + (((Cols - 2 * B_COLS) >> 1) - 1))
45*57277Sbostic 
46*57277Sbostic /*
47*57277Sbostic  * A `shape' is the fundamental thing that makes up the game.  There
48*57277Sbostic  * are 7 basic shapes, each consisting of four `blots':
49*57277Sbostic  *
50*57277Sbostic  *	X.X	  X.X		X.X
51*57277Sbostic  *	  X.X	X.X	X.X.X	X.X	X.X.X	X.X.X	X.X.X.X
52*57277Sbostic  *			  X		X	    X
53*57277Sbostic  *
54*57277Sbostic  *	  0	  1	  2	  3	  4	  5	  6
55*57277Sbostic  *
56*57277Sbostic  * Except for 3 and 6, the center of each shape is one of the blots.
57*57277Sbostic  * This blot is designated (0,0).  The other three blots can then be
58*57277Sbostic  * described as offsets from the center.  Shape 3 is the same under
59*57277Sbostic  * rotation, so its center is effectively irrelevant; it has been chosen
60*57277Sbostic  * so that it `sticks out' upward and leftward.  Except for shape 6,
61*57277Sbostic  * all the blots are contained in a box going from (-1,-1) to (+1,+1);
62*57277Sbostic  * shape 6's center `wobbles' as it rotates, so that while it `sticks out'
63*57277Sbostic  * rightward, its rotation---a vertical line---`sticks out' downward.
64*57277Sbostic  * The containment box has to include the offset (2,0), making the overall
65*57277Sbostic  * containment box range from offset (-1,-1) to (+2,+1).  (This is why
66*57277Sbostic  * there is only one row above, but two rows below, the display area.)
67*57277Sbostic  *
68*57277Sbostic  * The game works by choosing one of these shapes at random and putting
69*57277Sbostic  * its center at the middle of the first display row (row 1, column 5).
70*57277Sbostic  * The shape is moved steadily downward until it collides with something:
71*57277Sbostic  * either  another shape, or the bottom of the board.  When the shape can
72*57277Sbostic  * no longer be moved downwards, it is merged into the current board.
73*57277Sbostic  * At this time, any completely filled rows are elided, and blots above
74*57277Sbostic  * these rows move down to make more room.  A new random shape is again
75*57277Sbostic  * introduced at the top of the board, and the whole process repeats.
76*57277Sbostic  * The game ends when the new shape will not fit at (1,5).
77*57277Sbostic  *
78*57277Sbostic  * While the shapes are falling, the user can rotate them counterclockwise
79*57277Sbostic  * 90 degrees (in addition to moving them left or right), provided that the
80*57277Sbostic  * rotation puts the blots in empty spaces.  The table of shapes is set up
81*57277Sbostic  * so that each shape contains the index of the new shape obtained by
82*57277Sbostic  * rotating the current shape.  Due to symmetry, each shape has exactly
83*57277Sbostic  * 1, 2, or 4 rotations total; the first 7 entries in the table represent
84*57277Sbostic  * the primary shapes, and the remaining 12 represent their various
85*57277Sbostic  * rotated forms.
86*57277Sbostic  */
87*57277Sbostic struct shape {
88*57277Sbostic 	int	rot;	/* index of rotated version of this shape */
89*57277Sbostic 	int	off[3];	/* offsets to other blots if center is at (0,0) */
90*57277Sbostic };
91*57277Sbostic 
92*57277Sbostic extern struct shape shapes[];
93*57277Sbostic #define	randshape() (&shapes[random() % 7])
94*57277Sbostic 
95*57277Sbostic /*
96*57277Sbostic  * Shapes fall at a rate faster than once per second.
97*57277Sbostic  *
98*57277Sbostic  * The initial rate is determined by dividing 1 million microseconds
99*57277Sbostic  * by the game `level'.  (This is at most 1 million, or one second.)
100*57277Sbostic  * Each time the fall-rate is used, it is decreased a little bit,
101*57277Sbostic  * depending on its current value, via the `faster' macro below.
102*57277Sbostic  * The value eventually reaches a limit, and things stop going faster,
103*57277Sbostic  * but by then the game is utterly impossible.
104*57277Sbostic  */
105*57277Sbostic long	fallrate;		/* less than 1 million; smaller => faster */
106*57277Sbostic #define	faster() (fallrate -= fallrate / 3000)
107*57277Sbostic 
108*57277Sbostic /*
109*57277Sbostic  * Game level must be between 1 and 9.  This controls the initial fall rate
110*57277Sbostic  * and affects scoring.
111*57277Sbostic  */
112*57277Sbostic #define	MINLEVEL	1
113*57277Sbostic #define	MAXLEVEL	9
114*57277Sbostic 
115*57277Sbostic /*
116*57277Sbostic  * Scoring is as follows:
117*57277Sbostic  *
118*57277Sbostic  * When the shape comes to rest, and is integrated into the board,
119*57277Sbostic  * we score one point.  If the shape is high up (at a low-numbered row),
120*57277Sbostic  * and the user hits the space bar, the shape plummets all the way down,
121*57277Sbostic  * and we score a point for each row it falls (plus one more as soon as
122*57277Sbostic  * we find that it is at rest and integrate it---until then, it can
123*57277Sbostic  * still be moved or rotated).
124*57277Sbostic  */
125*57277Sbostic int	score;			/* the obvious thing */
126*57277Sbostic 
127*57277Sbostic char	key_msg[100];
128*57277Sbostic 
129*57277Sbostic int	fits_in __P((struct shape *, int));
130*57277Sbostic void	place __P((struct shape *, int, int));
131*57277Sbostic void	stop __P((char *));
132