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