Rev 1590 | Rev 1688 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1590 | Rev 1653 | ||
---|---|---|---|
1 | /* $OpenBSD: tetris.c,v 1.21 2006/04/20 03:24:12 ray Exp $ */ |
1 | /* $OpenBSD: tetris.c,v 1.21 2006/04/20 03:24:12 ray Exp $ */ |
2 | /* $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $ */ |
2 | /* $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $ */ |
3 | 3 | ||
4 | /*- |
4 | /*- |
5 | * Copyright (c) 1992, 1993 |
5 | * Copyright (c) 1992, 1993 |
6 | * The Regents of the University of California. All rights reserved. |
6 | * The Regents of the University of California. All rights reserved. |
7 | * |
7 | * |
8 | * This code is derived from software contributed to Berkeley by |
8 | * This code is derived from software contributed to Berkeley by |
9 | * Chris Torek and Darren F. Provine. |
9 | * Chris Torek and Darren F. Provine. |
10 | * |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright |
14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the |
17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. |
18 | * documentation and/or other materials provided with the distribution. |
19 | * 3. Neither the name of the University nor the names of its contributors |
19 | * 3. Neither the name of the University nor the names of its contributors |
20 | * may be used to endorse or promote products derived from this software |
20 | * may be used to endorse or promote products derived from this software |
21 | * without specific prior written permission. |
21 | * without specific prior written permission. |
22 | * |
22 | * |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
33 | * SUCH DAMAGE. |
33 | * SUCH DAMAGE. |
34 | * |
34 | * |
35 | * @(#)tetris.c 8.1 (Berkeley) 5/31/93 |
35 | * @(#)tetris.c 8.1 (Berkeley) 5/31/93 |
36 | */ |
36 | */ |
37 | 37 | ||
- | 38 | /** @addtogroup tetris Tetris |
|
- | 39 | * @brief Tetris ported from OpenBSD |
|
- | 40 | * @{ |
|
- | 41 | */ |
|
- | 42 | /** @file |
|
- | 43 | */ |
|
- | 44 | ||
38 | #ifndef lint |
45 | #ifndef lint |
39 | static const char copyright[] = |
46 | static const char copyright[] = |
40 | "@(#) Copyright (c) 1992, 1993\n\ |
47 | "@(#) Copyright (c) 1992, 1993\n\ |
41 | The Regents of the University of California. All rights reserved.\n"; |
48 | The Regents of the University of California. All rights reserved.\n"; |
42 | #endif /* not lint */ |
49 | #endif /* not lint */ |
43 | 50 | ||
44 | /* |
51 | /* |
45 | * Tetris (or however it is spelled). |
52 | * Tetris (or however it is spelled). |
46 | */ |
53 | */ |
47 | 54 | ||
48 | #include <sys/time.h> |
55 | #include <sys/time.h> |
49 | #include <sys/types.h> |
56 | #include <sys/types.h> |
50 | 57 | ||
51 | #include <err.h> |
58 | #include <err.h> |
52 | #include <stdio.h> |
59 | #include <stdio.h> |
53 | #include <stdlib.h> |
60 | #include <stdlib.h> |
54 | #include <string.h> |
61 | #include <string.h> |
55 | #include <unistd.h> |
62 | #include <unistd.h> |
56 | 63 | ||
57 | #include "input.h" |
64 | #include "input.h" |
58 | #include "scores.h" |
65 | #include "scores.h" |
59 | #include "screen.h" |
66 | #include "screen.h" |
60 | #include "tetris.h" |
67 | #include "tetris.h" |
61 | 68 | ||
62 | cell board[B_SIZE]; |
69 | cell board[B_SIZE]; |
63 | int Rows, Cols; |
70 | int Rows, Cols; |
64 | const struct shape *curshape; |
71 | const struct shape *curshape; |
65 | const struct shape *nextshape; |
72 | const struct shape *nextshape; |
66 | long fallrate; |
73 | long fallrate; |
67 | int score; |
74 | int score; |
68 | //gid_t gid, egid; |
75 | //gid_t gid, egid; |
69 | char key_msg[100]; |
76 | char key_msg[100]; |
70 | int showpreview, classic; |
77 | int showpreview, classic; |
71 | 78 | ||
72 | static void elide(void); |
79 | static void elide(void); |
73 | static void setup_board(void); |
80 | static void setup_board(void); |
74 | const struct shape *randshape(void); |
81 | const struct shape *randshape(void); |
75 | void onintr(int); |
82 | void onintr(int); |
76 | void usage(void); |
83 | void usage(void); |
77 | 84 | ||
78 | /* |
85 | /* |
79 | * Set up the initial board. The bottom display row is completely set, |
86 | * Set up the initial board. The bottom display row is completely set, |
80 | * along with another (hidden) row underneath that. Also, the left and |
87 | * along with another (hidden) row underneath that. Also, the left and |
81 | * right edges are set. |
88 | * right edges are set. |
82 | */ |
89 | */ |
83 | static void |
90 | static void |
84 | setup_board(void) |
91 | setup_board(void) |
85 | { |
92 | { |
86 | int i; |
93 | int i; |
87 | cell *p; |
94 | cell *p; |
88 | 95 | ||
89 | p = board; |
96 | p = board; |
90 | for (i = B_SIZE; i; i--) |
97 | for (i = B_SIZE; i; i--) |
91 | *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2; |
98 | *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2; |
92 | } |
99 | } |
93 | 100 | ||
94 | /* |
101 | /* |
95 | * Elide any full active rows. |
102 | * Elide any full active rows. |
96 | */ |
103 | */ |
97 | static void |
104 | static void |
98 | elide(void) |
105 | elide(void) |
99 | { |
106 | { |
100 | int rows = 0; |
107 | int rows = 0; |
101 | int i, j, base; |
108 | int i, j, base; |
102 | cell *p; |
109 | cell *p; |
103 | 110 | ||
104 | for (i = A_FIRST; i < A_LAST; i++) { |
111 | for (i = A_FIRST; i < A_LAST; i++) { |
105 | base = i * B_COLS + 1; |
112 | base = i * B_COLS + 1; |
106 | p = &board[base]; |
113 | p = &board[base]; |
107 | for (j = B_COLS - 2; *p++ != 0;) { |
114 | for (j = B_COLS - 2; *p++ != 0;) { |
108 | if (--j <= 0) { |
115 | if (--j <= 0) { |
109 | /* this row is to be elided */ |
116 | /* this row is to be elided */ |
110 | rows++; |
117 | rows++; |
111 | memset(&board[base], 0, B_COLS - 2); |
118 | memset(&board[base], 0, B_COLS - 2); |
112 | scr_update(); |
119 | scr_update(); |
113 | tsleep(); |
120 | tsleep(); |
114 | while (--base != 0) |
121 | while (--base != 0) |
115 | board[base + B_COLS] = board[base]; |
122 | board[base + B_COLS] = board[base]; |
116 | scr_update(); |
123 | scr_update(); |
117 | tsleep(); |
124 | tsleep(); |
118 | break; |
125 | break; |
119 | } |
126 | } |
120 | } |
127 | } |
121 | } |
128 | } |
122 | switch (rows) { |
129 | switch (rows) { |
123 | case 1: |
130 | case 1: |
124 | score += 10; |
131 | score += 10; |
125 | break; |
132 | break; |
126 | case 2: |
133 | case 2: |
127 | score += 30; |
134 | score += 30; |
128 | break; |
135 | break; |
129 | case 3: |
136 | case 3: |
130 | score += 70; |
137 | score += 70; |
131 | break; |
138 | break; |
132 | case 4: |
139 | case 4: |
133 | score += 150; |
140 | score += 150; |
134 | break; |
141 | break; |
135 | default: |
142 | default: |
136 | break; |
143 | break; |
137 | } |
144 | } |
138 | } |
145 | } |
139 | 146 | ||
140 | const struct shape * |
147 | const struct shape * |
141 | randshape(void) |
148 | randshape(void) |
142 | { |
149 | { |
143 | const struct shape *tmp; |
150 | const struct shape *tmp; |
144 | int i, j; |
151 | int i, j; |
145 | 152 | ||
146 | tmp = &shapes[random() % 7]; |
153 | tmp = &shapes[random() % 7]; |
147 | j = random() % 4; |
154 | j = random() % 4; |
148 | for (i = 0; i < j; i++) |
155 | for (i = 0; i < j; i++) |
149 | tmp = &shapes[classic? tmp->rotc : tmp->rot]; |
156 | tmp = &shapes[classic? tmp->rotc : tmp->rot]; |
150 | return (tmp); |
157 | return (tmp); |
151 | } |
158 | } |
152 | 159 | ||
153 | static void srandomdev(void) |
160 | static void srandomdev(void) |
154 | { |
161 | { |
155 | struct timeval tv; |
162 | struct timeval tv; |
156 | 163 | ||
157 | gettimeofday(&tv, NULL); |
164 | gettimeofday(&tv, NULL); |
158 | srandom(tv.tv_sec + tv.tv_usec / 100000); |
165 | srandom(tv.tv_sec + tv.tv_usec / 100000); |
159 | } |
166 | } |
160 | 167 | ||
161 | static void tetris_scores(int firstgame) |
168 | static void tetris_scores(int firstgame) |
162 | { |
169 | { |
163 | } |
170 | } |
164 | 171 | ||
165 | static void tetris_menu_draw(int level) |
172 | static void tetris_menu_draw(int level) |
166 | { |
173 | { |
167 | clear_screen(); |
174 | clear_screen(); |
168 | moveto(5,10); |
175 | moveto(5,10); |
169 | puts("Tetris\n\n"); |
176 | puts("Tetris\n\n"); |
170 | 177 | ||
171 | moveto(8,10); |
178 | moveto(8,10); |
172 | printf("Level = %d (press keys 1 - 9 to change)",level); |
179 | printf("Level = %d (press keys 1 - 9 to change)",level); |
173 | moveto(9,10); |
180 | moveto(9,10); |
174 | printf("Preview is %s (press 'p' to change)", (showpreview?"on ":"off")); |
181 | printf("Preview is %s (press 'p' to change)", (showpreview?"on ":"off")); |
175 | moveto(12,10); |
182 | moveto(12,10); |
176 | printf("Press 'h' to show hiscore table."); |
183 | printf("Press 'h' to show hiscore table."); |
177 | moveto(13,10); |
184 | moveto(13,10); |
178 | printf("Press 's' to start game."); |
185 | printf("Press 's' to start game."); |
179 | moveto(14,10); |
186 | moveto(14,10); |
180 | printf("Press 'q' to quit game."); |
187 | printf("Press 'q' to quit game."); |
181 | moveto(20,10); |
188 | moveto(20,10); |
182 | printf("In game controls:"); |
189 | printf("In game controls:"); |
183 | moveto(21,0); |
190 | moveto(21,0); |
184 | puts(key_msg); |
191 | puts(key_msg); |
185 | } |
192 | } |
186 | 193 | ||
187 | static int tetris_menu(int *level) |
194 | static int tetris_menu(int *level) |
188 | { |
195 | { |
189 | static int firstgame = 1; |
196 | static int firstgame = 1; |
190 | int i; |
197 | int i; |
191 | /* if (showpreview == 0) |
198 | /* if (showpreview == 0) |
192 | (void)printf("Your score: %d point%s x level %d = %d\n", |
199 | (void)printf("Your score: %d point%s x level %d = %d\n", |
193 | score, score == 1 ? "" : "s", level, score * level); |
200 | score, score == 1 ? "" : "s", level, score * level); |
194 | else { |
201 | else { |
195 | (void)printf("Your score: %d point%s x level %d x preview penalty %0.3f = %d\n", |
202 | (void)printf("Your score: %d point%s x level %d x preview penalty %0.3f = %d\n", |
196 | score, score == 1 ? "" : "s", level, (double)PRE_PENALTY, |
203 | score, score == 1 ? "" : "s", level, (double)PRE_PENALTY, |
197 | (int)(score * level * PRE_PENALTY)); |
204 | (int)(score * level * PRE_PENALTY)); |
198 | score = score * PRE_PENALTY; |
205 | score = score * PRE_PENALTY; |
199 | } |
206 | } |
200 | savescore(level); |
207 | savescore(level); |
201 | 208 | ||
202 | showscores(level); |
209 | showscores(level); |
203 | |
210 | |
204 | printf("\nHit 's' to new game, 'q' to quit.\n"); |
211 | printf("\nHit 's' to new game, 'q' to quit.\n"); |
205 | */ |
212 | */ |
206 | tetris_menu_draw(*level); |
213 | tetris_menu_draw(*level); |
207 | while (1) { |
214 | while (1) { |
208 | 215 | ||
209 | i = getchar(); |
216 | i = getchar(); |
210 | 217 | ||
211 | switch(i) { |
218 | switch(i) { |
212 | case 'p': |
219 | case 'p': |
213 | showpreview = !showpreview; |
220 | showpreview = !showpreview; |
214 | moveto(9,21); |
221 | moveto(9,21); |
215 | if (showpreview) |
222 | if (showpreview) |
216 | printf("on "); |
223 | printf("on "); |
217 | else |
224 | else |
218 | printf("off"); |
225 | printf("off"); |
219 | 226 | ||
220 | break; |
227 | break; |
221 | case 'h': |
228 | case 'h': |
222 | showscores(firstgame); |
229 | showscores(firstgame); |
223 | tetris_menu_draw(*level); |
230 | tetris_menu_draw(*level); |
224 | break; |
231 | break; |
225 | case 's': |
232 | case 's': |
226 | firstgame = 0; |
233 | firstgame = 0; |
227 | return 1; |
234 | return 1; |
228 | case 'q': |
235 | case 'q': |
229 | return 0; |
236 | return 0; |
230 | case '1': |
237 | case '1': |
231 | case '2': |
238 | case '2': |
232 | case '3': |
239 | case '3': |
233 | case '4': |
240 | case '4': |
234 | case '5': |
241 | case '5': |
235 | case '6': |
242 | case '6': |
236 | case '7': |
243 | case '7': |
237 | case '8': |
244 | case '8': |
238 | case '9': |
245 | case '9': |
239 | *level = i - '0'; |
246 | *level = i - '0'; |
240 | moveto(8,18); |
247 | moveto(8,18); |
241 | printf("%d", *level); |
248 | printf("%d", *level); |
242 | break; |
249 | break; |
243 | } |
250 | } |
244 | } |
251 | } |
245 | 252 | ||
246 | } |
253 | } |
247 | 254 | ||
248 | int |
255 | int |
249 | main(int argc, char *argv[]) |
256 | main(int argc, char *argv[]) |
250 | { |
257 | { |
251 | int pos, c; |
258 | int pos, c; |
252 | char *keys; |
259 | char *keys; |
253 | int level = 2; |
260 | int level = 2; |
254 | char key_write[6][10]; |
261 | char key_write[6][10]; |
255 | const char *errstr; |
262 | const char *errstr; |
256 | int ch, i, j; |
263 | int ch, i, j; |
257 | 264 | ||
258 | keys = "jkl pq"; |
265 | keys = "jkl pq"; |
259 | 266 | ||
260 | // gid = getgid(); |
267 | // gid = getgid(); |
261 | // egid = getegid(); |
268 | // egid = getegid(); |
262 | // setegid(gid); |
269 | // setegid(gid); |
263 | 270 | ||
264 | classic = 0; |
271 | classic = 0; |
265 | showpreview = 1; |
272 | showpreview = 1; |
266 | 273 | ||
267 | /* while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */ |
274 | /* while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */ |
268 | /* switch(ch) { */ |
275 | /* switch(ch) { */ |
269 | /* case 'c': */ |
276 | /* case 'c': */ |
270 | /* /\* */ |
277 | /* /\* */ |
271 | /* * this means: */ |
278 | /* * this means: */ |
272 | /* * - rotate the other way; */ |
279 | /* * - rotate the other way; */ |
273 | /* * - no reverse video. */ |
280 | /* * - no reverse video. */ |
274 | /* *\/ */ |
281 | /* *\/ */ |
275 | /* classic = 1; */ |
282 | /* classic = 1; */ |
276 | /* break; */ |
283 | /* break; */ |
277 | /* case 'k': */ |
284 | /* case 'k': */ |
278 | /* if (strlen(keys = optarg) != 6) */ |
285 | /* if (strlen(keys = optarg) != 6) */ |
279 | /* usage(); */ |
286 | /* usage(); */ |
280 | /* break; */ |
287 | /* break; */ |
281 | /* case 'l': */ |
288 | /* case 'l': */ |
282 | /* level = (int)strtonum(optarg, MINLEVEL, MAXLEVEL, */ |
289 | /* level = (int)strtonum(optarg, MINLEVEL, MAXLEVEL, */ |
283 | /* &errstr); */ |
290 | /* &errstr); */ |
284 | /* if (errstr) */ |
291 | /* if (errstr) */ |
285 | /* errx(1, "level must be from %d to %d", */ |
292 | /* errx(1, "level must be from %d to %d", */ |
286 | /* MINLEVEL, MAXLEVEL); */ |
293 | /* MINLEVEL, MAXLEVEL); */ |
287 | /* break; */ |
294 | /* break; */ |
288 | /* case 'p': */ |
295 | /* case 'p': */ |
289 | /* showpreview = 1; */ |
296 | /* showpreview = 1; */ |
290 | /* break; */ |
297 | /* break; */ |
291 | /* case 's': */ |
298 | /* case 's': */ |
292 | /* showscores(0); */ |
299 | /* showscores(0); */ |
293 | /* exit(0); */ |
300 | /* exit(0); */ |
294 | /* default: */ |
301 | /* default: */ |
295 | /* usage(); */ |
302 | /* usage(); */ |
296 | /* } */ |
303 | /* } */ |
297 | 304 | ||
298 | /* argc -= optind; */ |
305 | /* argc -= optind; */ |
299 | /* argv += optind; */ |
306 | /* argv += optind; */ |
300 | 307 | ||
301 | /* if (argc) */ |
308 | /* if (argc) */ |
302 | /* usage(); */ |
309 | /* usage(); */ |
303 | 310 | ||
304 | fallrate = 1000000 / level; |
311 | fallrate = 1000000 / level; |
305 | 312 | ||
306 | for (i = 0; i <= 5; i++) { |
313 | for (i = 0; i <= 5; i++) { |
307 | for (j = i+1; j <= 5; j++) { |
314 | for (j = i+1; j <= 5; j++) { |
308 | if (keys[i] == keys[j]) |
315 | if (keys[i] == keys[j]) |
309 | errx(1, "duplicate command keys specified."); |
316 | errx(1, "duplicate command keys specified."); |
310 | } |
317 | } |
311 | if (keys[i] == ' ') |
318 | if (keys[i] == ' ') |
312 | strncpy(key_write[i], "<space>", sizeof key_write[i]); |
319 | strncpy(key_write[i], "<space>", sizeof key_write[i]); |
313 | else { |
320 | else { |
314 | key_write[i][0] = keys[i]; |
321 | key_write[i][0] = keys[i]; |
315 | key_write[i][1] = '\0'; |
322 | key_write[i][1] = '\0'; |
316 | } |
323 | } |
317 | } |
324 | } |
318 | 325 | ||
319 | snprintf(key_msg, sizeof key_msg, |
326 | snprintf(key_msg, sizeof key_msg, |
320 | "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit", |
327 | "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit", |
321 | key_write[0], key_write[1], key_write[2], key_write[3], |
328 | key_write[0], key_write[1], key_write[2], key_write[3], |
322 | key_write[4], key_write[5]); |
329 | key_write[4], key_write[5]); |
323 | 330 | ||
324 | scr_init(); |
331 | scr_init(); |
325 | initscores(); |
332 | initscores(); |
326 | while (tetris_menu(&level)) { |
333 | while (tetris_menu(&level)) { |
327 | 334 | ||
328 | 335 | ||
329 | scr_clear(); |
336 | scr_clear(); |
330 | setup_board(); |
337 | setup_board(); |
331 | 338 | ||
332 | srandomdev(); |
339 | srandomdev(); |
333 | scr_set(); |
340 | scr_set(); |
334 | 341 | ||
335 | pos = A_FIRST*B_COLS + (B_COLS/2)-1; |
342 | pos = A_FIRST*B_COLS + (B_COLS/2)-1; |
336 | nextshape = randshape(); |
343 | nextshape = randshape(); |
337 | curshape = randshape(); |
344 | curshape = randshape(); |
338 | 345 | ||
339 | scr_msg(key_msg, 1); |
346 | scr_msg(key_msg, 1); |
340 | 347 | ||
341 | for (;;) { |
348 | for (;;) { |
342 | place(curshape, pos, 1); |
349 | place(curshape, pos, 1); |
343 | scr_update(); |
350 | scr_update(); |
344 | place(curshape, pos, 0); |
351 | place(curshape, pos, 0); |
345 | c = tgetchar(); |
352 | c = tgetchar(); |
346 | if (c < 0) { |
353 | if (c < 0) { |
347 | /* |
354 | /* |
348 | * Timeout. Move down if possible. |
355 | * Timeout. Move down if possible. |
349 | */ |
356 | */ |
350 | if (fits_in(curshape, pos + B_COLS)) { |
357 | if (fits_in(curshape, pos + B_COLS)) { |
351 | pos += B_COLS; |
358 | pos += B_COLS; |
352 | continue; |
359 | continue; |
353 | } |
360 | } |
354 | 361 | ||
355 | /* |
362 | /* |
356 | * Put up the current shape `permanently', |
363 | * Put up the current shape `permanently', |
357 | * bump score, and elide any full rows. |
364 | * bump score, and elide any full rows. |
358 | */ |
365 | */ |
359 | place(curshape, pos, 1); |
366 | place(curshape, pos, 1); |
360 | score++; |
367 | score++; |
361 | elide(); |
368 | elide(); |
362 | 369 | ||
363 | /* |
370 | /* |
364 | * Choose a new shape. If it does not fit, |
371 | * Choose a new shape. If it does not fit, |
365 | * the game is over. |
372 | * the game is over. |
366 | */ |
373 | */ |
367 | curshape = nextshape; |
374 | curshape = nextshape; |
368 | nextshape = randshape(); |
375 | nextshape = randshape(); |
369 | pos = A_FIRST*B_COLS + (B_COLS/2)-1; |
376 | pos = A_FIRST*B_COLS + (B_COLS/2)-1; |
370 | if (!fits_in(curshape, pos)) |
377 | if (!fits_in(curshape, pos)) |
371 | break; |
378 | break; |
372 | continue; |
379 | continue; |
373 | } |
380 | } |
374 | 381 | ||
375 | /* |
382 | /* |
376 | * Handle command keys. |
383 | * Handle command keys. |
377 | */ |
384 | */ |
378 | if (c == keys[5]) { |
385 | if (c == keys[5]) { |
379 | /* quit */ |
386 | /* quit */ |
380 | break; |
387 | break; |
381 | } |
388 | } |
382 | if (c == keys[4]) { |
389 | if (c == keys[4]) { |
383 | static char msg[] = |
390 | static char msg[] = |
384 | "paused - press RETURN to continue"; |
391 | "paused - press RETURN to continue"; |
385 | 392 | ||
386 | place(curshape, pos, 1); |
393 | place(curshape, pos, 1); |
387 | do { |
394 | do { |
388 | scr_update(); |
395 | scr_update(); |
389 | scr_msg(key_msg, 0); |
396 | scr_msg(key_msg, 0); |
390 | scr_msg(msg, 1); |
397 | scr_msg(msg, 1); |
391 | // (void) fflush(stdout); |
398 | // (void) fflush(stdout); |
392 | } while (rwait((struct timeval *)NULL) == -1); |
399 | } while (rwait((struct timeval *)NULL) == -1); |
393 | scr_msg(msg, 0); |
400 | scr_msg(msg, 0); |
394 | scr_msg(key_msg, 1); |
401 | scr_msg(key_msg, 1); |
395 | place(curshape, pos, 0); |
402 | place(curshape, pos, 0); |
396 | continue; |
403 | continue; |
397 | } |
404 | } |
398 | if (c == keys[0]) { |
405 | if (c == keys[0]) { |
399 | /* move left */ |
406 | /* move left */ |
400 | if (fits_in(curshape, pos - 1)) |
407 | if (fits_in(curshape, pos - 1)) |
401 | pos--; |
408 | pos--; |
402 | continue; |
409 | continue; |
403 | } |
410 | } |
404 | if (c == keys[1]) { |
411 | if (c == keys[1]) { |
405 | /* turn */ |
412 | /* turn */ |
406 | const struct shape *new = &shapes[ |
413 | const struct shape *new = &shapes[ |
407 | classic? curshape->rotc : curshape->rot]; |
414 | classic? curshape->rotc : curshape->rot]; |
408 | 415 | ||
409 | if (fits_in(new, pos)) |
416 | if (fits_in(new, pos)) |
410 | curshape = new; |
417 | curshape = new; |
411 | continue; |
418 | continue; |
412 | } |
419 | } |
413 | if (c == keys[2]) { |
420 | if (c == keys[2]) { |
414 | /* move right */ |
421 | /* move right */ |
415 | if (fits_in(curshape, pos + 1)) |
422 | if (fits_in(curshape, pos + 1)) |
416 | pos++; |
423 | pos++; |
417 | continue; |
424 | continue; |
418 | } |
425 | } |
419 | if (c == keys[3]) { |
426 | if (c == keys[3]) { |
420 | /* move to bottom */ |
427 | /* move to bottom */ |
421 | while (fits_in(curshape, pos + B_COLS)) { |
428 | while (fits_in(curshape, pos + B_COLS)) { |
422 | pos += B_COLS; |
429 | pos += B_COLS; |
423 | score++; |
430 | score++; |
424 | } |
431 | } |
425 | continue; |
432 | continue; |
426 | } |
433 | } |
427 | if (c == '\f') { |
434 | if (c == '\f') { |
428 | scr_clear(); |
435 | scr_clear(); |
429 | scr_msg(key_msg, 1); |
436 | scr_msg(key_msg, 1); |
430 | } |
437 | } |
431 | } |
438 | } |
432 | 439 | ||
433 | scr_clear(); |
440 | scr_clear(); |
434 | insertscore(score, level); |
441 | insertscore(score, level); |
435 | } |
442 | } |
436 | 443 | ||
437 | scr_clear(); |
444 | scr_clear(); |
438 | printf("\n\n\n\t\tGame over.\n"); |
445 | printf("\n\n\n\t\tGame over.\n"); |
439 | /* |
446 | /* |
440 | while ((i = getchar()) != '\n') |
447 | while ((i = getchar()) != '\n') |
441 | if (i == EOF) |
448 | if (i == EOF) |
442 | break |
449 | break |
443 | */ |
450 | */ |
444 | scr_end(); |
451 | scr_end(); |
445 | exit(0); |
452 | exit(0); |
446 | } |
453 | } |
447 | 454 | ||
448 | /* void */ |
455 | /* void */ |
449 | /* onintr(int signo) */ |
456 | /* onintr(int signo) */ |
450 | /* { */ |
457 | /* { */ |
451 | /* scr_clear(); /\* XXX signal race *\/ */ |
458 | /* scr_clear(); /\* XXX signal race *\/ */ |
452 | /* scr_end(); /\* XXX signal race *\/ */ |
459 | /* scr_end(); /\* XXX signal race *\/ */ |
453 | /* _exit(0); */ |
460 | /* _exit(0); */ |
454 | /* } */ |
461 | /* } */ |
455 | 462 | ||
456 | void |
463 | void |
457 | usage(void) |
464 | usage(void) |
458 | { |
465 | { |
459 | (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n"); |
466 | (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n"); |
460 | exit(1); |
467 | exit(1); |
461 | } |
468 | } |
- | 469 | ||
- | 470 | /** @} |
|
- | 471 | */ |
|
- | 472 | ||
462 | 473 |