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