Changeset 9996ed5 in mainline
- Timestamp:
- 2006-06-05T20:40:44Z (19 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- e9073f2
- Parents:
- 0aa024b1
- Location:
- tetris
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
tetris/scores.c
r0aa024b1 r9996ed5 47 47 /* #include <fcntl.h> */ 48 48 /* #include <pwd.h> */ 49 /* #include <stdio.h> */ 49 #include <stdio.h> 50 50 /* #include <stdlib.h> */ 51 /* #include <string.h> */ 51 #include <string.h> 52 52 /* #include <time.h> */ 53 53 /* #include <term.h> */ … … 77 77 /* static int gotscores; */ 78 78 /* static struct highscore scores[NUMSPOTS]; */ 79 static struct highscore scores[NUMSPOTS]; 79 80 80 81 /* static int checkscores(struct highscore *, int); */ … … 83 84 /* static void printem(int, int, struct highscore *, int, const char *); */ 84 85 /* static char *thisuser(void); */ 86 87 void 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 */ 109 static 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 116 void 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 160 void 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 } 85 169 86 170 /* … … 345 429 * before it can be shown anyway. 346 430 */ 431 /* 347 432 void 348 433 showscores(int level) … … 350 435 return; 351 436 } 352 437 */ 353 438 /* struct highscore *sp; */ 354 439 /* int i, n, c; */ -
tetris/scores.h
r0aa024b1 r9996ed5 40 40 */ 41 41 #include <sys/time.h> 42 #define MAXLOGNAME 1 042 #define MAXLOGNAME 16 43 43 struct highscore { 44 char hs_name[MAXLOGNAME ]; /* login name */44 char hs_name[MAXLOGNAME + 1]; /* login name */ 45 45 int hs_score; /* raw score */ 46 46 int hs_level; /* play level */ 47 time_t hs_time; /* time at game end */47 // time_t hs_time; /* time at game end */ 48 48 }; 49 49 50 #define MAXHISCORES 8051 #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) 53 53 54 54 void savescore(int); 55 55 void showscores(int); 56 void insertscore(int score, int level); 57 void initscores(void); -
tetris/screen.c
r0aa024b1 r9996ed5 94 94 } 95 95 96 97 void clear_screen(void) 98 { 99 send_call(con_phone, CONSOLE_CLEAR, 0); 100 moveto(0,0); 101 } 102 96 103 /* 97 104 * Clear the screen, forgetting the current contents in the process. … … 119 126 } 120 127 121 staticvoid moveto(int r, int c)128 void moveto(int r, int c) 122 129 { 123 130 send_call_2(con_phone, CONSOLE_GOTO, r, c); … … 129 136 } 130 137 131 struct winsize { 132 ipcarg_t ws_row; 133 ipcarg_t ws_col; 134 }; 135 136 static int get_display_size(struct winsize *ws) 138 winsize_t winsize; 139 140 static int get_display_size(winsize_t *ws) 137 141 { 138 142 return sync_send_2(con_phone, CONSOLE_GETSIZE, 0, 0, &ws->ws_row, &ws->ws_col); … … 154 158 scr_set(void) 155 159 { 156 struct winsizews;160 winsize_t ws; 157 161 158 162 Rows = 0, Cols = 0; -
tetris/screen.h
r0aa024b1 r9996ed5 41 41 #define putpad(s) tputs(s, 1, put) 42 42 43 #include <async.h> 44 45 typedef struct { 46 ipcarg_t ws_row; 47 ipcarg_t ws_col; 48 } winsize_t; 49 50 extern winsize_t winsize; 51 52 void moveto(int r, int c); 53 void clear_screen(void); 54 43 55 int put(int); /* just calls putchar; for tputs */ 44 56 void scr_clear(void); -
tetris/tetris.c
r0aa024b1 r9996ed5 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 248 int 162 249 main(int argc, char *argv[]) … … 175 262 // setegid(gid); 176 263 177 classic = showpreview = 0; 264 classic = 0; 265 showpreview = 1; 178 266 179 267 /* while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */ … … 233 321 key_write[0], key_write[1], key_write[2], key_write[3], 234 322 key_write[4], key_write[5]); 235 newgame: 323 236 324 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 254 375 /* 255 * Timeout. Move down if possible.376 * Handle command keys. 256 377 */ 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 */ 278 380 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 } 280 431 } 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); 364 435 } 365 436 … … 371 442 break 372 443 */ 444 scr_end(); 373 445 exit(0); 374 446 }
Note:
See TracChangeset
for help on using the changeset viewer.