]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/tetris.c
63265bc6b7541c435dca4f326ffbd1fd64edcdec
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek and Darren F. Provine.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)tetris.c 8.1 (Berkeley) 5/31/93
40 static char copyright
[] =
41 "@(#) Copyright (c) 1992, 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
46 * Tetris (or however it is spelled).
62 void onintr
__P((int));
63 void usage
__P((void));
66 * Set up the initial board. The bottom display row is completely set,
67 * along with another (hidden) row underneath that. Also, the left and
68 * right edges are set.
77 for (i
= B_SIZE
; i
; i
--)
79 *p
++ = i
<= (2 * B_COLS
) || (i
% B_COLS
) < 2;
80 #else /* work around compiler bug */
81 *p
++ = i
<= (2 * B_COLS
) || (i
% B_COLS
) < 2 ? 1 : 0;
86 * Elide any full active rows.
91 register int i
, j
, base
;
94 for (i
= A_FIRST
; i
< A_LAST
; i
++) {
95 base
= i
* B_COLS
+ 1;
97 for (j
= B_COLS
- 2; *p
++ != 0;) {
99 /* this row is to be elided */
100 bzero(&board
[base
], B_COLS
- 2);
104 board
[base
+ B_COLS
] = board
[base
];
119 register struct shape
*curshape
;
121 register int level
= 2;
122 char key_write
[6][10];
127 while ((ch
= getopt(argc
, argv
, "k:l:s")) != EOF
)
130 if (strlen(keys
= optarg
) != 6)
134 level
= atoi(optarg
);
135 if (level
< MINLEVEL
|| level
> MAXLEVEL
) {
136 (void)fprintf(stderr
,
137 "tetris: level must be from %d to %d",
156 fallrate
= 1000000 / level
;
158 for (i
= 0; i
<= 5; i
++) {
159 for (j
= i
+1; j
<= 5; j
++) {
160 if (keys
[i
] == keys
[j
]) {
161 (void)fprintf(stderr
,
162 "%s: Duplicate command keys specified.\n",
168 strcpy(key_write
[i
], "<space>");
170 key_write
[i
][0] = keys
[i
];
171 key_write
[i
][1] = '\0';
176 "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
177 key_write
[0], key_write
[1], key_write
[2], key_write
[3],
178 key_write
[4], key_write
[5]);
180 (void)signal(SIGINT
, onintr
);
187 pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
188 curshape
= randshape();
193 place(curshape
, pos
, 1);
195 place(curshape
, pos
, 0);
199 * Timeout. Move down if possible.
201 if (fits_in(curshape
, pos
+ B_COLS
)) {
207 * Put up the current shape `permanently',
208 * bump score, and elide any full rows.
210 place(curshape
, pos
, 1);
215 * Choose a new shape. If it does not fit,
218 curshape
= randshape();
219 pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
220 if (!fits_in(curshape
, pos
))
226 * Handle command keys.
234 "paused - press RETURN to continue";
236 place(curshape
, pos
, 1);
241 (void) fflush(stdout
);
242 } while (rwait((struct timeval
*)NULL
) == -1);
245 place(curshape
, pos
, 0);
250 if (fits_in(curshape
, pos
- 1))
256 struct shape
*new = &shapes
[curshape
->rot
];
258 if (fits_in(new, pos
))
264 if (fits_in(curshape
, pos
+ 1))
270 while (fits_in(curshape
, pos
+ B_COLS
)) {
283 (void)printf("Your score: %d point%s x level %d = %d\n",
284 score
, score
== 1 ? "" : "s", level
, score
* level
);
287 printf("\nHit RETURN to see high scores, ^C to skip.\n");
289 while ((i
= getchar()) != '\n')
310 (void)fprintf(stderr
, "usage: tetris [-s] [-l level] [-keys]\n");