Subversion Repositories HelenOS

Rev

Rev 2479 | Rev 2799 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1419 jermar 1
/*  $OpenBSD: tetris.c,v 1.21 2006/04/20 03:24:12 ray Exp $ */
2
/*  $NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd Exp $   */
3
 
4
/*-
5
 * Copyright (c) 1992, 1993
6
 *  The Regents of the University of California.  All rights reserved.
7
 *
8
 * This code is derived from software contributed to Berkeley by
9
 * Chris Torek and Darren F. Provine.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
34
 *
35
 *  @(#)tetris.c    8.1 (Berkeley) 5/31/93
36
 */
37
 
1653 cejka 38
/** @addtogroup tetris Tetris
39
 * @brief   Tetris ported from OpenBSD
40
 * @{
41
 */
42
/** @file
43
 */
44
 
1419 jermar 45
#ifndef lint
46
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
 */
54
 
55
#include <sys/time.h>
56
#include <sys/types.h>
57
 
58
#include <err.h>
59
#include <stdio.h>
60
#include <stdlib.h>
61
#include <string.h>
62
#include <unistd.h>
63
 
64
#include "input.h"
65
#include "scores.h"
66
#include "screen.h"
67
#include "tetris.h"
68
 
69
cell    board[B_SIZE];
70
int Rows, Cols;
71
const struct shape *curshape;
72
const struct shape *nextshape;
73
long    fallrate;
74
int score;
1449 palkovsky 75
//gid_t gid, egid;
1419 jermar 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);
84
 
85
/*
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
 * right edges are set.
89
 */
90
static void
91
setup_board(void)
92
{
93
    int i;
94
    cell *p;
95
 
96
    p = board;
97
    for (i = B_SIZE; i; i--)
98
        *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
99
}
100
 
101
/*
102
 * Elide any full active rows.
103
 */
104
static void
105
elide(void)
106
{
107
    int rows = 0;
108
    int i, j, base;
109
    cell *p;
110
 
111
    for (i = A_FIRST; i < A_LAST; i++) {
112
        base = i * B_COLS + 1;
113
        p = &board[base];
114
        for (j = B_COLS - 2; *p++ != 0;) {
115
            if (--j <= 0) {
116
                /* this row is to be elided */
117
                rows++;
118
                memset(&board[base], 0, B_COLS - 2);
119
                scr_update();
120
                tsleep();
121
                while (--base != 0)
122
                    board[base + B_COLS] = board[base];
123
                scr_update();
124
                tsleep();
125
                break;
126
            }
127
        }
128
    }
129
    switch (rows) {
130
    case 1:
131
        score += 10;
132
        break;
133
    case 2:
134
        score += 30;
135
        break;
136
    case 3:
137
        score += 70;
138
        break;
139
    case 4:
140
        score += 150;
141
        break;
142
    default:
143
        break;
144
    }
145
}
146
 
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;
155
    for (i = 0; i < j; i++)
156
        tmp = &shapes[classic? tmp->rotc : tmp->rot];
157
    return (tmp);
158
}
159
 
1472 palkovsky 160
static void srandomdev(void)
161
{
162
    struct timeval tv;
163
 
164
    gettimeofday(&tv, NULL);
165
    srandom(tv.tv_sec + tv.tv_usec / 100000);
166
}
167
 
1590 cejka 168
static void tetris_menu_draw(int level)
169
{
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
*/
209
    tetris_menu_draw(*level);
210
    while (1) {
211
 
212
        i = getchar();
213
 
214
        switch(i) {
215
            case 'p':
216
                showpreview = !showpreview;
217
                moveto(9,21);
218
                if (showpreview)
219
                    printf("on ");
220
                else
221
                    printf("off");
222
 
223
                break;
224
            case 'h':
2787 decky 225
                *(int *)0 = 0; /* generate a page fault */
1590 cejka 226
                showscores(firstgame);
227
                tetris_menu_draw(*level);
228
                break;
229
            case 's':
230
                firstgame = 0;
231
                return 1;
232
            case 'q':
233
                return 0;
234
            case '1':
235
            case '2':
236
            case '3':
237
            case '4':
238
            case '5':
239
            case '6':      
240
            case '7':
241
            case '8':
242
            case '9':
243
                *level = i - '0';
244
                moveto(8,18);
245
                printf("%d", *level);
246
                break;
247
        }
248
    }
249
 
250
}
251
 
1419 jermar 252
int
253
main(int argc, char *argv[])
254
{
255
    int pos, c;
256
    char *keys;
257
    int level = 2;
258
    char key_write[6][10];
1721 palkovsky 259
    int i, j;
1419 jermar 260
 
261
    keys = "jkl pq";
262
 
1449 palkovsky 263
//  gid = getgid();
264
//  egid = getegid();
265
//  setegid(gid);
1419 jermar 266
 
1590 cejka 267
    classic = 0;
268
    showpreview = 1;
1419 jermar 269
 
1449 palkovsky 270
/*  while ((ch = getopt(argc, argv, "ck:l:ps")) != -1) */
271
/*      switch(ch) { */
272
/*      case 'c': */
273
/*          /\* */
274
/*           * this means: */
275
/*           *  - rotate the other way; */
276
/*           *  - no reverse video. */
277
/*           *\/ */
278
/*          classic = 1; */
279
/*          break; */
280
/*      case 'k': */
281
/*          if (strlen(keys = optarg) != 6) */
282
/*              usage(); */
283
/*          break; */
284
/*      case 'l': */
285
/*          level = (int)strtonum(optarg, MINLEVEL, MAXLEVEL, */
286
/*              &errstr); */
287
/*          if (errstr) */
288
/*              errx(1, "level must be from %d to %d", */
289
/*                  MINLEVEL, MAXLEVEL); */
290
/*          break; */
291
/*      case 'p': */
292
/*          showpreview = 1; */
293
/*          break; */
294
/*      case 's': */
295
/*          showscores(0); */
296
/*          exit(0); */
297
/*      default: */
298
/*          usage(); */
299
/*      } */
1419 jermar 300
 
1449 palkovsky 301
/*  argc -= optind; */
302
/*  argv += optind; */
1419 jermar 303
 
1449 palkovsky 304
/*  if (argc) */
305
/*      usage(); */
306
 
1688 jermar 307
 
1419 jermar 308
 
309
    for (i = 0; i <= 5; i++) {
310
        for (j = i+1; j <= 5; j++) {
311
            if (keys[i] == keys[j])
312
                errx(1, "duplicate command keys specified.");
313
        }
314
        if (keys[i] == ' ')
1472 palkovsky 315
            strncpy(key_write[i], "<space>", sizeof key_write[i]);
1419 jermar 316
        else {
317
            key_write[i][0] = keys[i];
318
            key_write[i][1] = '\0';
319
        }
320
    }
321
 
322
    snprintf(key_msg, sizeof key_msg,
323
"%s - left   %s - rotate   %s - right   %s - drop   %s - pause   %s - quit",
324
        key_write[0], key_write[1], key_write[2], key_write[3],
325
        key_write[4], key_write[5]);
1590 cejka 326
 
1419 jermar 327
    scr_init();
1590 cejka 328
    initscores();
329
    while (tetris_menu(&level)) {
1688 jermar 330
        fallrate = 1000000 / level;
331
 
1590 cejka 332
        scr_clear();
333
        setup_board();
334
 
335
        srandomdev();
336
        scr_set();
337
 
338
        pos = A_FIRST*B_COLS + (B_COLS/2)-1;
339
        nextshape = randshape();
340
        curshape = randshape();
341
 
342
        scr_msg(key_msg, 1);
343
 
344
        for (;;) {
345
            place(curshape, pos, 1);
346
            scr_update();
347
            place(curshape, pos, 0);
348
            c = tgetchar();
349
            if (c < 0) {
350
                /*
351
                 * Timeout.  Move down if possible.
352
                 */
353
                if (fits_in(curshape, pos + B_COLS)) {
354
                    pos += B_COLS;
355
                    continue;
356
                }
357
 
358
                /*
359
                 * Put up the current shape `permanently',
360
                 * bump score, and elide any full rows.
361
                 */
362
                place(curshape, pos, 1);
363
                score++;
364
                elide();
365
 
366
                /*
367
                 * Choose a new shape.  If it does not fit,
368
                 * the game is over.
369
                 */
370
                curshape = nextshape;
371
                nextshape = randshape();
372
                pos = A_FIRST*B_COLS + (B_COLS/2)-1;
373
                if (!fits_in(curshape, pos))
374
                    break;
1419 jermar 375
                continue;
376
            }
1590 cejka 377
 
1419 jermar 378
            /*
1590 cejka 379
             * Handle command keys.
1419 jermar 380
             */
1590 cejka 381
            if (c == keys[5]) {
382
                /* quit */
1419 jermar 383
                break;
384
            }
1590 cejka 385
            if (c == keys[4]) {
386
                static char msg[] =
387
                    "paused - press RETURN to continue";
1584 cejka 388
 
1590 cejka 389
                place(curshape, pos, 1);
390
                do {
391
                    scr_update();
392
                    scr_msg(key_msg, 0);
393
                    scr_msg(msg, 1);
394
    //              (void) fflush(stdout);
395
                } while (rwait((struct timeval *)NULL) == -1);
396
                scr_msg(msg, 0);
397
                scr_msg(key_msg, 1);
398
                place(curshape, pos, 0);
399
                continue;
400
            }
401
            if (c == keys[0]) {
402
                /* move left */
403
                if (fits_in(curshape, pos - 1))
404
                    pos--;
405
                continue;
406
            }
407
            if (c == keys[1]) {
408
                /* turn */
409
                const struct shape *new = &shapes[
410
                    classic? curshape->rotc : curshape->rot];
1584 cejka 411
 
1590 cejka 412
                if (fits_in(new, pos))
413
                    curshape = new;
414
                continue;
415
            }
416
            if (c == keys[2]) {
417
                /* move right */
418
                if (fits_in(curshape, pos + 1))
419
                    pos++;
420
                continue;
421
            }
422
            if (c == keys[3]) {
423
                /* move to bottom */
424
                while (fits_in(curshape, pos + B_COLS)) {
425
                    pos += B_COLS;
426
                    score++;
427
                }
428
                continue;
429
            }
430
            if (c == '\f') {
431
                scr_clear();
432
                scr_msg(key_msg, 1);
433
            }
434
        }
435
 
436
        scr_clear();
437
        insertscore(score, level);
1710 vana 438
        score=0;
1584 cejka 439
    }
440
 
441
    scr_clear();
442
    printf("\n\n\n\t\tGame over.\n");
443
/* 
1419 jermar 444
    while ((i = getchar()) != '\n')
445
        if (i == EOF)
1584 cejka 446
            break
447
*/
1590 cejka 448
    scr_end();
1721 palkovsky 449
 
450
    return 0;
1419 jermar 451
}
452
 
1449 palkovsky 453
/* void */
454
/* onintr(int signo) */
455
/* { */
456
/*  scr_clear();        /\* XXX signal race *\/ */
457
/*  scr_end();      /\* XXX signal race *\/ */
458
/*  _exit(0); */
459
/* } */
1419 jermar 460
 
461
void
462
usage(void)
463
{
464
    (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n");
465
    exit(1);
466
}
1653 cejka 467
 
468
/** @}
469
 */
470