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