Changeset ebe70f1 in mainline for uspace/app/tetris/tetris.c


Ignore:
Timestamp:
2009-06-09T11:10:31Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f3afd24
Parents:
080ad7f
Message:

slightly cleanup the horrible mess of tetris
introduce colors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tetris/tetris.c

    r080ad7f rebe70f1  
    3737
    3838/** @addtogroup tetris Tetris
    39  * @brief       Tetris ported from OpenBSD
    40  * @{ 
     39 * @brief Tetris ported from OpenBSD
     40 * @{
    4141 */
    4242/** @file
    4343 */
    4444
    45 #ifndef lint
    4645static 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";
    5448
    5549#include <sys/time.h>
    5650#include <sys/types.h>
    57 
    5851#include <err.h>
    5952#include <stdio.h>
     
    6154#include <string.h>
    6255#include <unistd.h>
     56#include <getopt.h>
    6357
    6458#include "input.h"
     
    6761#include "tetris.h"
    6862
    69 cell    board[B_SIZE];
    70 int     Rows, Cols;
     63cell board[B_SIZE];
     64
     65int Rows;
     66int Cols;
     67
    7168const struct shape *curshape;
    7269const 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
     71long fallrate;
     72int score;
     73char key_msg[100];
     74int showpreview;
     75int classic;
     76
     77static void elide(void);
     78static void setup_board(void);
     79static const struct shape *randshape(void);
     80
     81static void usage(void);
     82
     83static int firstgame = 1;
    8484
    8585/*
    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
     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
    8888 * right edges are set.
    8989 */
    90 static void
    91 setup_board(void)
     90static void setup_board(void)
    9291{
    9392        int i;
    94         cell *p;
    95 
    96         p = board;
     93        cell *p = board;
     94       
    9795        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;
    9997}
    10098
     
    102100 * Elide any full active rows.
    103101 */
    104 static void
    105 elide(void)
     102static void elide(void)
    106103{
    107104        int rows = 0;
    108         int i, j, base;
     105        int i;
     106        int j;
     107        int base;
    109108        cell *p;
    110 
     109       
    111110        for (i = A_FIRST; i < A_LAST; i++) {
    112111                base = i * B_COLS + 1;
     
    114113                for (j = B_COLS - 2; *p++ != 0;) {
    115114                        if (--j <= 0) {
    116                                 /* this row is to be elided */
     115                                /* This row is to be elided */
    117116                                rows++;
    118                                 memset(&board[base], 0, B_COLS - 2);
     117                                memset(&board[base], 0, sizeof(cell) * (B_COLS - 2));
     118                               
    119119                                scr_update();
    120120                                tsleep();
     121                               
    121122                                while (--base != 0)
    122123                                        board[base + B_COLS] = board[base];
     124                               
    123125                                scr_update();
    124126                                tsleep();
     127                               
    125128                                break;
    126129                        }
    127130                }
    128131        }
     132       
    129133        switch (rows) {
    130134        case 1:
     
    145149}
    146150
    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;
     151const struct shape *randshape(void)
     152{
     153        const struct shape *tmp = &shapes[random() % 7];
     154        int i;
     155        int j = random() % 4;
     156       
    155157        for (i = 0; i < j; i++)
    156                 tmp = &shapes[classic? tmp->rotc : tmp->rot];
     158                tmp = &shapes[classic ? tmp->rotc : tmp->rot];
     159       
    157160        return (tmp);
    158161}
     
    161164{
    162165        struct timeval tv;
    163 
     166       
    164167        gettimeofday(&tv, NULL);
    165168        srandom(tv.tv_sec + tv.tv_usec / 100000);
     
    168171static void tetris_menu_draw(int level)
    169172{
    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
     193static int tetris_menu(int *level)
     194{
    209195        tetris_menu_draw(*level);
    210196        while (1) {
    211        
    212                 i = getchar();
     197                int i = getchar();
    213198               
    214199                switch(i) {
    215200                        case 'p':
    216201                                showpreview = !showpreview;
    217                                 moveto(9,21);
     202                                moveto(9, 21);
    218203                                if (showpreview)
    219204                                        printf("on ");
    220205                                else
    221206                                        printf("off");
    222                                        
    223207                                break;
    224208                        case 'h':
     
    236220                        case '4':
    237221                        case '5':
    238                         case '6':               
     222                        case '6':
    239223                        case '7':
    240224                        case '8':
    241225                        case '9':
    242226                                *level = i - '0';
    243                                 moveto(8,18);
     227                                moveto(8, 18);
    244228                                printf("%d", *level);
    245229                                break;
    246230                }
    247231        }
    248        
    249 }
    250 
    251 int
    252 main(int argc, char *argv[])
    253 {
    254         int pos, c;
     232}
     233
     234int main(int argc, char *argv[])
     235{
     236        int pos;
     237        int c;
    255238        char *keys;
    256239        int level = 2;
    257240        char key_write[6][10];
    258         int i, j;
    259 
     241        int i;
     242        int j;
     243        int ch;
     244       
    260245        keys = "jkl pq";
    261 
    262 //      gid = getgid();
    263 //      egid = getegid();
    264 //      setegid(gid);
    265 
     246       
    266247        classic = 0;
    267248        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       
    308280        for (i = 0; i <= 5; i++) {
    309                 for (j = i+1; j <= 5; j++) {
     281                for (j = i + 1; j <= 5; j++) {
    310282                        if (keys[i] == keys[j])
    311283                                errx(1, "duplicate command keys specified.");
    312284                }
     285               
    313286                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>");
    315288                else {
    316289                        key_write[i][0] = keys[i];
     
    318291                }
    319292        }
    320 
    321         snprintf(key_msg, sizeof key_msg,
    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],
    324                 key_write[4], key_write[5]);
    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       
    326299        scr_init();
    327300        initscores();
     
    331304                scr_clear();
    332305                setup_board();
    333        
     306               
    334307                srandomdev();
    335308                scr_set();
    336        
    337                 pos = A_FIRST*B_COLS + (B_COLS/2)-1;
     309               
     310                pos = A_FIRST * B_COLS + (B_COLS / 2) - 1;
    338311                nextshape = randshape();
    339312                curshape = randshape();
    340        
     313               
    341314                scr_msg(key_msg, 1);
    342        
    343                 for (;;) {
     315               
     316                while (1) {
    344317                        place(curshape, pos, 1);
    345318                        scr_update();
     
    354327                                        continue;
    355328                                }
    356        
     329                               
    357330                                /*
    358331                                 * Put up the current shape `permanently',
     
    362335                                score++;
    363336                                elide();
    364        
     337                               
    365338                                /*
    366339                                 * Choose a new shape.  If it does not fit,
     
    369342                                curshape = nextshape;
    370343                                nextshape = randshape();
    371                                 pos = A_FIRST*B_COLS + (B_COLS/2)-1;
     344                                pos = A_FIRST * B_COLS + (B_COLS / 2) - 1;
     345                               
    372346                                if (!fits_in(curshape, pos))
    373347                                        break;
    374                                 continue;
    375                         }
    376        
     348                               
     349                                continue;
     350                        }
     351                       
    377352                        /*
    378353                         * Handle command keys.
     
    382357                                break;
    383358                        }
     359                       
    384360                        if (c == keys[4]) {
    385361                                static char msg[] =
    386362                                    "paused - press RETURN to continue";
    387        
     363                               
    388364                                place(curshape, pos, 1);
    389365                                do {
     
    392368                                        scr_msg(msg, 1);
    393369                                        (void) fflush(stdout);
    394                                 } while (rwait((struct timeval *)NULL) == -1);
     370                                } while (rwait((struct timeval *) NULL) == -1);
     371                               
    395372                                scr_msg(msg, 0);
    396373                                scr_msg(key_msg, 1);
     
    398375                                continue;
    399376                        }
     377                       
    400378                        if (c == keys[0]) {
    401379                                /* move left */
     
    404382                                continue;
    405383                        }
     384                       
    406385                        if (c == keys[1]) {
    407386                                /* 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                               
    411390                                if (fits_in(new, pos))
    412391                                        curshape = new;
    413392                                continue;
    414393                        }
     394                       
    415395                        if (c == keys[2]) {
    416396                                /* move right */
     
    419399                                continue;
    420400                        }
     401                       
    421402                        if (c == keys[3]) {
    422403                                /* move to bottom */
     
    427408                                continue;
    428409                        }
     410                       
    429411                        if (c == '\f') {
    430412                                scr_clear();
     
    435417                scr_clear();
    436418                insertscore(score, level);
    437                 score=0;
     419                score = 0;
    438420        }
    439421       
    440422        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");
    447424        scr_end();
    448 
     425       
    449426        return 0;
    450427}
    451428
    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");
     429void usage(void)
     430{
     431        fprintf(stderr, "usage: tetris [-ps] [-k keys]\n");
    464432        exit(1);
    465433}
     
    467435/** @}
    468436 */
    469 
Note: See TracChangeset for help on using the changeset viewer.