Changeset 9996ed5 in mainline


Ignore:
Timestamp:
2006-06-05T20:40:44Z (19 years ago)
Author:
Josef Cejka <malyzelenyhnus@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e9073f2
Parents:
0aa024b1
Message:

Tetris has now a new menu.
Hiscore table added.

Location:
tetris
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • tetris/scores.c

    r0aa024b1 r9996ed5  
    4747/* #include <fcntl.h> */
    4848/* #include <pwd.h> */
    49 /* #include <stdio.h> */
     49 #include <stdio.h>
    5050/* #include <stdlib.h> */
    51 /* #include <string.h> */
     51#include <string.h>
    5252/* #include <time.h> */
    5353/* #include <term.h> */
     
    7777/* static int gotscores; */
    7878/* static struct highscore scores[NUMSPOTS]; */
     79static struct highscore scores[NUMSPOTS];
    7980
    8081/* static int checkscores(struct highscore *, int); */
     
    8384/* static void printem(int, int, struct highscore *, int, const char *); */
    8485/* static char *thisuser(void); */
     86
     87void showscores(int firstgame)
     88{
     89        int i;
     90       
     91        clear_screen();
     92        moveto(10, 0);
     93        printf("\tRank \tLevel \tName\t                     points\n");
     94        printf("\t========================================================\n");
     95        for (i = 0; i < NUMSPOTS - 1; i++) {
     96                printf("\t%6d %6d %-16s %20d\n", i+1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score);
     97        }
     98        if (!firstgame) {
     99                printf("\t========================================================\n");
     100                printf("\t  Last %6d %-16s %20d\n", scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
     101        }
     102        printf("\n\n\n\n\tPress any key to return to main menu.");
     103        getchar();
     104}
     105
     106/** Copy from hiscore table score with index src to dest
     107 *
     108 */
     109static void copyhiscore(int dest, int src)
     110{
     111        strcpy(scores[dest].hs_name, scores[src].hs_name);
     112        scores[dest].hs_score = scores[src].hs_score;   
     113        scores[dest].hs_level = scores[src].hs_level;
     114}
     115
     116void insertscore(int score, int level)
     117{
     118        int i,j;
     119        int key;
     120
     121       
     122        clear_screen();
     123        moveto(10 , 10);
     124        puts("Insert your name: ");
     125        strncpy(scores[NUMSPOTS - 1].hs_name, "Player", MAXLOGNAME);
     126        i = 6;
     127
     128        moveto(10 , 28);
     129        printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................");
     130        key = getchar();       
     131        while(key != '\n') {
     132                if (key == '\b') {
     133                        if (i > 0)
     134                                scores[NUMSPOTS - 1].hs_name[--i] = '\0';       
     135                } else {
     136                        if (i < (MAXLOGNAME - 1))
     137                                scores[NUMSPOTS - 1].hs_name[i++] = key;       
     138                                scores[NUMSPOTS - 1].hs_name[i] = '\0';
     139                }
     140               
     141                moveto(10 , 28);
     142                printf("%s%.*s",scores[NUMSPOTS - 1].hs_name,MAXLOGNAME-i,"........................................");
     143       
     144                key = getchar();       
     145        }
     146       
     147        scores[NUMSPOTS - 1].hs_score = score; 
     148        scores[NUMSPOTS - 1].hs_level = level;
     149       
     150        i = NUMSPOTS-1;
     151        while ((i > 0) && (scores[i - 1].hs_score < score))
     152                i--;
     153
     154        for (j = NUMSPOTS - 2; j > i; j--) {
     155                copyhiscore(j,j-1);
     156        }
     157        copyhiscore(i, NUMSPOTS - 1);   
     158}
     159
     160void initscores(void)
     161{
     162        int i;
     163        for(i = 0; i < NUMSPOTS; i++) {
     164                strncpy(scores[i].hs_name, "HelenOS Team", MAXLOGNAME);
     165                scores[i].hs_score = (NUMSPOTS - i) * 200;     
     166                scores[i].hs_level = (i + 1 > MAXLEVEL?MAXLEVEL:i + 1);
     167        }
     168}
    85169
    86170/*
     
    345429 *   before it can be shown anyway.
    346430 */
     431/*
    347432void
    348433showscores(int level)
     
    350435        return;
    351436}
    352 
     437*/
    353438/*      struct highscore *sp; */
    354439/*      int i, n, c; */
  • tetris/scores.h

    r0aa024b1 r9996ed5  
    4040 */
    4141#include <sys/time.h>
    42 #define MAXLOGNAME 10
     42#define MAXLOGNAME 16
    4343struct highscore {
    44         char    hs_name[MAXLOGNAME];    /* login name */
     44        char    hs_name[MAXLOGNAME + 1];        /* login name */
    4545        int     hs_score;       /* raw score */
    4646        int     hs_level;       /* play level */
    47         time_t  hs_time;        /* time at game end */
     47//      time_t  hs_time;        /* time at game end */
    4848};
    4949
    50 #define MAXHISCORES     80
    51 #define MAXSCORES       9       /* maximum high score entries per person */
    52 #define EXPIRATION      (5L * 365 * 24 * 60 * 60)
     50#define MAXHISCORES     10
     51//#define MAXSCORES     9       /* maximum high score entries per person */
     52//#define       EXPIRATION      (5L * 365 * 24 * 60 * 60)
    5353
    5454void savescore(int);
    5555void showscores(int);
     56void insertscore(int score, int level);
     57void initscores(void);
  • tetris/screen.c

    r0aa024b1 r9996ed5  
    9494}
    9595
     96
     97void clear_screen(void)
     98{
     99        send_call(con_phone, CONSOLE_CLEAR, 0);
     100        moveto(0,0);
     101}
     102
    96103/*
    97104 * Clear the screen, forgetting the current contents in the process.
     
    119126}
    120127
    121 static void moveto(int r, int c)
     128void moveto(int r, int c)
    122129{
    123130        send_call_2(con_phone, CONSOLE_GOTO, r, c);
     
    129136}
    130137
    131 struct winsize {
    132         ipcarg_t ws_row;
    133         ipcarg_t ws_col;
    134 };
    135 
    136 static int get_display_size(struct winsize *ws)
     138winsize_t winsize;
     139
     140static int get_display_size(winsize_t *ws)
    137141{
    138142        return sync_send_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col);
     
    154158scr_set(void)
    155159{
    156         struct winsize ws;
     160        winsize_t ws;
    157161
    158162        Rows = 0, Cols = 0;
  • tetris/screen.h

    r0aa024b1 r9996ed5  
    4141#define putpad(s)       tputs(s, 1, put)
    4242
     43#include <async.h>
     44
     45typedef struct {
     46        ipcarg_t ws_row;
     47        ipcarg_t ws_col;
     48} winsize_t;
     49
     50extern winsize_t winsize;
     51
     52void moveto(int r, int c);
     53void clear_screen(void);
     54
    4355int     put(int);                       /* just calls putchar; for tputs */
    4456void    scr_clear(void);
  • tetris/tetris.c

    r0aa024b1 r9996ed5  
    159159}
    160160
     161static void tetris_scores(int firstgame)
     162{
     163}
     164
     165static 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
     187static 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
    161248int
    162249main(int argc, char *argv[])
     
    175262//      setegid(gid);
    176263
    177         classic = showpreview = 0;
     264        classic = 0;
     265        showpreview = 1;
    178266
    179267/*      while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */
     
    233321                key_write[0], key_write[1], key_write[2], key_write[3],
    234322                key_write[4], key_write[5]);
    235 newgame:
     323
    236324        scr_init();
    237         setup_board();
    238 
    239         srandomdev();
    240         scr_set();
    241 
    242         pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    243         nextshape = randshape();
    244         curshape = randshape();
    245 
    246         scr_msg(key_msg, 1);
    247 
    248         for (;;) {
    249                 place(curshape, pos, 1);
    250                 scr_update();
    251                 place(curshape, pos, 0);
    252                 c = tgetchar();
    253                 if (c < 0) {
     325        initscores();
     326        while (tetris_menu(&level)) {
     327
     328
     329                scr_clear();
     330                setup_board();
     331       
     332                srandomdev();
     333                scr_set();
     334       
     335                pos = A_FIRST*B_COLS + (B_COLS/2)-1;
     336                nextshape = randshape();
     337                curshape = randshape();
     338       
     339                scr_msg(key_msg, 1);
     340       
     341                for (;;) {
     342                        place(curshape, pos, 1);
     343                        scr_update();
     344                        place(curshape, pos, 0);
     345                        c = tgetchar();
     346                        if (c < 0) {
     347                                /*
     348                                 * Timeout.  Move down if possible.
     349                                 */
     350                                if (fits_in(curshape, 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;
     372                                continue;
     373                        }
     374       
    254375                        /*
    255                          * Timeout.  Move down if possible.
     376                         * Handle command keys.
    256377                         */
    257                         if (fits_in(curshape, pos + B_COLS)) {
    258                                 pos += B_COLS;
    259                                 continue;
    260                         }
    261 
    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                         /*
    271                          * Choose a new shape.  If it does not fit,
    272                          * the game is over.
    273                          */
    274                         curshape = nextshape;
    275                         nextshape = randshape();
    276                         pos = A_FIRST*B_COLS + (B_COLS/2)-1;
    277                         if (!fits_in(curshape, pos))
     378                        if (c == keys[5]) {
     379                                /* quit */
    278380                                break;
    279                         continue;
     381                        }
     382                        if (c == keys[4]) {
     383                                static char msg[] =
     384                                    "paused - press RETURN to continue";
     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);
     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;
     403                        }
     404                        if (c == keys[1]) {
     405                                /* turn */
     406                                const struct shape *new = &shapes[
     407                                    classic? curshape->rotc : curshape->rot];
     408       
     409                                if (fits_in(new, pos))
     410                                        curshape = new;
     411                                continue;
     412                        }
     413                        if (c == keys[2]) {
     414                                /* move right */
     415                                if (fits_in(curshape, pos + 1))
     416                                        pos++;
     417                                continue;
     418                        }
     419                        if (c == keys[3]) {
     420                                /* move to bottom */
     421                                while (fits_in(curshape, pos + B_COLS)) {
     422                                        pos += B_COLS;
     423                                        score++;
     424                                }
     425                                continue;
     426                        }
     427                        if (c == '\f') {
     428                                scr_clear();
     429                                scr_msg(key_msg, 1);
     430                        }
    280431                }
    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                         }
    332                         continue;
    333                 }
    334                 if (c == '\f') {
    335                         scr_clear();
    336                         scr_msg(key_msg, 1);
    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)); */
    350 /*              score = score * PRE_PENALTY; */
    351         }
    352         savescore(level);
    353 
    354         showscores(level);
    355        
    356         printf("\nHit 's' to new game, 'q' to quit.\n");
    357 
    358        
    359         while (i = getchar()) {
    360                 if (i == 's')
    361                         goto newgame;
    362                 if (i == 'q')
    363                         break;
     432               
     433                scr_clear();
     434                insertscore(score, level);
    364435        }
    365436       
     
    371442                        break
    372443*/
     444        scr_end();
    373445        exit(0);
    374446}
Note: See TracChangeset for help on using the changeset viewer.