]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/tetris.c
1 /* $NetBSD: tetris.c,v 1.18 2007/12/15 19:44:43 perry Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)tetris.c 8.1 (Berkeley) 5/31/93
37 #include <sys/cdefs.h>
39 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
40 The Regents of the University of California. All rights reserved.\n");
44 * Tetris (or however it is spelled).
62 cell board
[B_SIZE
]; /* 1 => occupied, 0 => empty */
64 int Rows
, Cols
; /* current screen size */
66 const struct shape
*curshape
;
67 const struct shape
*nextshape
;
69 long fallrate
; /* less than 1 million; smaller => faster */
71 int score
; /* the obvious thing */
77 static void elide(void);
78 static void setup_board(void);
79 int main(int, char **);
80 void onintr(int) __dead
;
81 void usage(void) __dead
;
84 * Set up the initial board. The bottom display row is completely set,
85 * along with another (hidden) row underneath that. Also, the left and
86 * right edges are set.
95 for (i
= B_SIZE
; i
; i
--)
96 *p
++ = i
<= (2 * B_COLS
) || (i
% B_COLS
) < 2;
100 * Elide any full active rows.
108 for (i
= A_FIRST
; i
< A_LAST
; i
++) {
109 base
= i
* B_COLS
+ 1;
111 for (j
= B_COLS
- 2; *p
++ != 0;) {
113 /* this row is to be elided */
114 memset(&board
[base
], 0, B_COLS
- 2);
118 board
[base
+ B_COLS
] = board
[base
];
135 char key_write
[6][10];
143 fd
= open("/dev/null", O_RDONLY
);
150 while ((ch
= getopt(argc
, argv
, "k:l:ps")) != -1)
153 if (strlen(keys
= optarg
) != 6)
157 level
= atoi(optarg
);
158 if (level
< MINLEVEL
|| level
> MAXLEVEL
) {
159 errx(1, "level must be from %d to %d",
180 fallrate
= 1000000 / level
;
182 for (i
= 0; i
<= 5; i
++) {
183 for (j
= i
+1; j
<= 5; j
++) {
184 if (keys
[i
] == keys
[j
]) {
185 errx(1, "duplicate command keys specified.");
189 strcpy(key_write
[i
], "<space>");
191 key_write
[i
][0] = keys
[i
];
192 key_write
[i
][1] = '\0';
197 "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
198 key_write
[0], key_write
[1], key_write
[2], key_write
[3],
199 key_write
[4], key_write
[5]);
201 (void)signal(SIGINT
, onintr
);
208 pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
209 nextshape
= randshape();
210 curshape
= randshape();
215 place(curshape
, pos
, 1);
217 place(curshape
, pos
, 0);
221 * Timeout. Move down if possible.
223 if (fits_in(curshape
, pos
+ B_COLS
)) {
229 * Put up the current shape `permanently',
230 * bump score, and elide any full rows.
232 place(curshape
, pos
, 1);
237 * Choose a new shape. If it does not fit,
240 curshape
= nextshape
;
241 nextshape
= randshape();
242 pos
= A_FIRST
*B_COLS
+ (B_COLS
/2)-1;
243 if (!fits_in(curshape
, pos
))
249 * Handle command keys.
257 "paused - press RETURN to continue";
259 place(curshape
, pos
, 1);
264 (void) fflush(stdout
);
265 } while (rwait((struct timeval
*)NULL
) == -1);
268 place(curshape
, pos
, 0);
273 if (fits_in(curshape
, pos
- 1))
279 const struct shape
*new = &shapes
[curshape
->rot
];
281 if (fits_in(new, pos
))
287 if (fits_in(curshape
, pos
+ 1))
293 while (fits_in(curshape
, pos
+ B_COLS
)) {
308 (void)printf("Your score: %d point%s x level %d = %d\n",
309 score
, score
== 1 ? "" : "s", level
, score
* level
);
312 printf("\nHit RETURN to see high scores, ^C to skip.\n");
314 while ((i
= getchar()) != '\n')
335 (void)fprintf(stderr
, "usage: tetris [-ps] [-k keys] [-l level]\n");