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