Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/sample/tetris/tetris.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


   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  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions
  10  * are met:
  11  * 1. Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  * 2. Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in the
  15  *    documentation and/or other materials provided with the distribution.
  16  * 3. Neither the name of the University nor the names of its contributors
  17  *    may be used to endorse or promote products derived from this software
  18  *    without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30  * SUCH DAMAGE.
  31  *
  32  *      @(#)tetris.h    8.1 (Berkeley) 5/31/93
  33  */
  34 
  35 /*
  36  * Definitions for Tetris.
  37  */
  38 
  39 /*
  40  * The display (`board') is composed of 23 rows of 12 columns of characters
  41  * (numbered 0..22 and 0..11), stored in a single array for convenience.
  42  * Columns 1 to 10 of rows 1 to 20 are the actual playing area, where
  43  * shapes appear.  Columns 0 and 11 are always occupied, as are all
  44  * columns of rows 21 and 22.  Rows 0 and 22 exist as boundary areas
  45  * so that regions `outside' the visible area can be examined without
  46  * worrying about addressing problems.
  47  */
  48 
  49 #if defined(__gba__)
  50         /* the board */
  51 #define B_COLS  12
  52 #define B_ROWS  22
  53 
  54         /* the displayed area (rows) */
  55 #define D_FIRST 1
  56 #define D_LAST  21
  57 
  58         /* the active area (rows) */
  59 #define A_FIRST 1
  60 #define A_LAST  20
  61 
  62 #else /* !__gba__ */
  63 
  64         /* the board */
  65 #define B_COLS  12
  66 #define B_ROWS  23
  67 
  68         /* the displayed area (rows) */
  69 #define D_FIRST 1
  70 #define D_LAST  22
  71 
  72         /* the active area (rows) */
  73 #define A_FIRST 1
  74 #define A_LAST  21
  75 #endif
  76 
  77 
  78 
  79 #define B_SIZE  (B_ROWS * B_COLS)
  80 
  81 typedef unsigned char cell;
  82 extern cell     board[B_SIZE];  /* 1 => occupied, 0 => empty */
  83 
  84 extern int      Rows, Cols;     /* current screen size */
  85 
  86 /*
  87  * Translations from board coordinates to display coordinates.
  88  * As with board coordinates, display coordiates are zero origin.
  89  */
  90 #define RTOD(x) ((x) - 1)
  91 #define CTOD(x) ((x) * 2 + (((Cols - 2 * B_COLS) >> 1) - 1 + 3))
  92 
  93 /*
  94  * A `shape' is the fundamental thing that makes up the game.  There
  95  * are 7 basic shapes, each consisting of four `blots':
  96  *
  97  *      X.X       X.X           X.X
  98  *        X.X   X.X     X.X.X   X.X     X.X.X   X.X.X   X.X.X.X
  99  *                        X             X           X
 100  *
 101  *        0       1       2       3       4       5       6
 102  *
 103  * Except for 3 and 6, the center of each shape is one of the blots.
 104  * This blot is designated (0,0).  The other three blots can then be
 105  * described as offsets from the center.  Shape 3 is the same under
 106  * rotation, so its center is effectively irrelevant; it has been chosen
 107  * so that it `sticks out' upward and leftward.  Except for shape 6,
 108  * all the blots are contained in a box going from (-1,-1) to (+1,+1);
 109  * shape 6's center `wobbles' as it rotates, so that while it `sticks out'
 110  * rightward, its rotation---a vertical line---`sticks out' downward.
 111  * The containment box has to include the offset (2,0), making the overall
 112  * containment box range from offset (-1,-1) to (+2,+1).  (This is why
 113  * there is only one row above, but two rows below, the display area.)
 114  *
 115  * The game works by choosing one of these shapes at random and putting
 116  * its center at the middle of the first display row (row 1, column 5).
 117  * The shape is moved steadily downward until it collides with something:
 118  * either  another shape, or the bottom of the board.  When the shape can
 119  * no longer be moved downwards, it is merged into the current board.
 120  * At this time, any completely filled rows are elided, and blots above
 121  * these rows move down to make more room.  A new random shape is again
 122  * introduced at the top of the board, and the whole process repeats.
 123  * The game ends when the new shape will not fit at (1,5).
 124  *
 125  * While the shapes are falling, the user can rotate them counterclockwise
 126  * 90 degrees (in addition to moving them left or right), provided that the
 127  * rotation puts the blots in empty spaces.  The table of shapes is set up
 128  * so that each shape contains the index of the new shape obtained by
 129  * rotating the current shape.  Due to symmetry, each shape has exactly
 130  * 1, 2, or 4 rotations total; the first 7 entries in the table represent
 131  * the primary shapes, and the remaining 12 represent their various
 132  * rotated forms.
 133  */
 134 struct shape {
 135         int     rot;    /* index of rotated version of this shape */
 136         int     off[3]; /* offsets to other blots if center is at (0,0) */
 137 };
 138 
 139 extern const struct shape shapes[];
 140 #define randshape() (&shapes[random() % 7])
 141 
 142 /*
 143  * Shapes fall at a rate faster than once per second.
 144  *
 145  * The initial rate is determined by dividing 1 million microseconds
 146  * by the game `level'.  (This is at most 1 million, or one second.)
 147  * Each time the fall-rate is used, it is decreased a little bit,
 148  * depending on its current value, via the `faster' macro below.
 149  * The value eventually reaches a limit, and things stop going faster,
 150  * but by then the game is utterly impossible.
 151  */
 152 extern long     fallrate;       /* less than 1 million; smaller => faster */
 153 #define faster() (fallrate -= fallrate / 3000)
 154 
 155 /*
 156  * Game level must be between 1 and 9.  This controls the initial fall rate
 157  * and affects scoring.
 158  */
 159 #define MINLEVEL        1
 160 #define MAXLEVEL        9
 161 
 162 /*
 163  * Scoring is as follows:
 164  *
 165  * When the shape comes to rest, and is integrated into the board,
 166  * we score one point.  If the shape is high up (at a low-numbered row),
 167  * and the user hits the space bar, the shape plummets all the way down,
 168  * and we score a point for each row it falls (plus one more as soon as
 169  * we find that it is at rest and integrate it---until then, it can
 170  * still be moved or rotated).
 171  */
 172 extern int      score;          /* the obvious thing */
 173 extern char     key_msg[100];
 174 
 175 int     fits_in(const struct shape *, int);
 176 void    place(const struct shape *, int, cell);
 177 void    stop(char *);

/* [<][>][^][v][top][bottom][index][help] */