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