Subversion Repositories HelenOS-historic

Rev

Rev 1584 | Rev 1653 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1584 Rev 1590
Line 156... Line 156...
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;
Line 172... Line 259...
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
/*          /\* */
Line 230... Line 318...
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) */