Subversion Repositories HelenOS

Rev

Rev 4668 | 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
4537 trochtova 39
 * @brief Tetris ported from OpenBSD
40
 * @{
1653 cejka 41
 */
42
/** @file
43
 */
44
 
1419 jermar 45
static const char copyright[] =
4537 trochtova 46
    "@(#) Copyright (c) 1992, 1993\n"
47
    "\tThe Regents of the University of California.  All rights reserved.\n";
1419 jermar 48
 
49
#include <sys/time.h>
50
#include <sys/types.h>
51
#include <err.h>
4668 trochtova 52
#include <errno.h>
1419 jermar 53
#include <stdio.h>
54
#include <stdlib.h>
55
#include <string.h>
56
#include <unistd.h>
4537 trochtova 57
#include <getopt.h>
1419 jermar 58
 
59
#include "input.h"
60
#include "scores.h"
61
#include "screen.h"
62
#include "tetris.h"
63
 
4537 trochtova 64
cell board[B_SIZE];
65
 
66
int Rows;
67
int Cols;
68
 
1419 jermar 69
const struct shape *curshape;
70
const struct shape *nextshape;
71
 
4537 trochtova 72
long fallrate;
73
int score;
74
char key_msg[100];
75
int showpreview;
76
int classic;
1419 jermar 77
 
4537 trochtova 78
static void elide(void);
79
static void setup_board(void);
80
static const struct shape *randshape(void);
81
 
82
static void usage(void);
83
 
84
static int firstgame = 1;
85
 
1419 jermar 86
/*
4537 trochtova 87
 * Set up the initial board. The bottom display row is completely set,
88
 * along with another (hidden) row underneath that. Also, the left and
1419 jermar 89
 * right edges are set.
90
 */
4537 trochtova 91
static void setup_board(void)
1419 jermar 92
{
93
    int i;
4537 trochtova 94
    cell *p = board;
95
 
1419 jermar 96
    for (i = B_SIZE; i; i--)
4537 trochtova 97
        *p++ = (i <= (2 * B_COLS) || (i % B_COLS) < 2) ? 0x0000ff : 0x000000;
1419 jermar 98
}
99
 
100
/*
101
 * Elide any full active rows.
102
 */
4537 trochtova 103
static void elide(void)
1419 jermar 104
{
105
    int rows = 0;
4537 trochtova 106
    int i;
107
    int j;
108
    int base;
1419 jermar 109
    cell *p;
4537 trochtova 110
 
1419 jermar 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) {
4537 trochtova 116
                /* This row is to be elided */
1419 jermar 117
                rows++;
4537 trochtova 118
                memset(&board[base], 0, sizeof(cell) * (B_COLS - 2));
119
 
1419 jermar 120
                scr_update();
121
                tsleep();
4537 trochtova 122
 
1419 jermar 123
                while (--base != 0)
124
                    board[base + B_COLS] = board[base];
4537 trochtova 125
 
1419 jermar 126
                scr_update();
127
                tsleep();
4537 trochtova 128
 
1419 jermar 129
                break;
130
            }
131
        }
132
    }
4537 trochtova 133
 
1419 jermar 134
    switch (rows) {
135
    case 1:
136
        score += 10;
137
        break;
138
    case 2:
139
        score += 30;
140
        break;
141
    case 3:
142
        score += 70;
143
        break;
144
    case 4:
145
        score += 150;
146
        break;
147
    default:
148
        break;
149
    }
150
}
151
 
4537 trochtova 152
const struct shape *randshape(void)
1419 jermar 153
{
4537 trochtova 154
    const struct shape *tmp = &shapes[random() % 7];
155
    int i;
156
    int j = random() % 4;
157
 
1419 jermar 158
    for (i = 0; i < j; i++)
4537 trochtova 159
        tmp = &shapes[classic ? tmp->rotc : tmp->rot];
160
 
1419 jermar 161
    return (tmp);
162
}
163
 
1472 palkovsky 164
static void srandomdev(void)
165
{
166
    struct timeval tv;
4537 trochtova 167
 
1472 palkovsky 168
    gettimeofday(&tv, NULL);
169
    srandom(tv.tv_sec + tv.tv_usec / 100000);
170
}
171
 
1590 cejka 172
static void tetris_menu_draw(int level)
173
{
4537 trochtova 174
    clear_screen();
175
    moveto(5, 10);
176
    puts("Tetris\n\n");
177
 
178
    moveto(8, 10);
179
    printf("Level = %d (press keys 1 - 9 to change)", level);
180
    moveto(9, 10);
181
    printf("Preview is %s (press 'p' to change)", (showpreview ? "on ": "off"));
182
    moveto(12, 10);
183
    printf("Press 'h' to show hiscore table.");
184
    moveto(13, 10);
185
    printf("Press 's' to start game.");
186
    moveto(14, 10);
187
    printf("Press 'q' to quit game.");
188
    moveto(20, 10);
189
    printf("In game controls:");
190
    moveto(21, 0);
191
    puts(key_msg);
1590 cejka 192
}
193
 
4537 trochtova 194
static int tetris_menu(int *level)
1590 cejka 195
{
196
    tetris_menu_draw(*level);
197
    while (1) {
4537 trochtova 198
        int i = getchar();
1590 cejka 199
 
200
        switch(i) {
201
            case 'p':
202
                showpreview = !showpreview;
4537 trochtova 203
                moveto(9, 21);
1590 cejka 204
                if (showpreview)
205
                    printf("on ");
206
                else
207
                    printf("off");
208
                break;
209
            case 'h':
4668 trochtova 210
                loadscores();
1590 cejka 211
                showscores(firstgame);
212
                tetris_menu_draw(*level);
213
                break;
214
            case 's':
215
                firstgame = 0;
216
                return 1;
217
            case 'q':
218
                return 0;
219
            case '1':
220
            case '2':
221
            case '3':
222
            case '4':
223
            case '5':
4537 trochtova 224
            case '6':
1590 cejka 225
            case '7':
226
            case '8':
227
            case '9':
228
                *level = i - '0';
4537 trochtova 229
                moveto(8, 18);
1590 cejka 230
                printf("%d", *level);
231
                break;
232
        }
233
    }
234
}
235
 
4537 trochtova 236
int main(int argc, char *argv[])
1419 jermar 237
{
4537 trochtova 238
    int pos;
239
    int c;
4688 trochtova 240
    const char *keys;
1419 jermar 241
    int level = 2;
242
    char key_write[6][10];
4537 trochtova 243
    int i;
244
    int j;
245
    int ch;
246
 
1419 jermar 247
    keys = "jkl pq";
4537 trochtova 248
 
1590 cejka 249
    classic = 0;
250
    showpreview = 1;
1688 jermar 251
 
4537 trochtova 252
    while ((ch = getopt(argc, argv, "ck:ps")) != -1)
253
        switch(ch) {
254
        case 'c':
255
            /*
256
             * this means:
257
             *  - rotate the other way
258
             *  - no reverse video
259
             */
260
            classic = 1;
261
            break;
262
        case 'k':
263
            if (str_size(keys = optarg) != 6)
264
                usage();
265
            break;
266
        case 'p':
267
            showpreview = 1;
268
            break;
269
        case 's':
270
            showscores(0);
271
            exit(0);
272
        default:
273
            usage();
274
        }
275
 
276
    argc -= optind;
277
    argv += optind;
278
 
279
    if (argc)
280
        usage();
281
 
1419 jermar 282
    for (i = 0; i <= 5; i++) {
4537 trochtova 283
        for (j = i + 1; j <= 5; j++) {
1419 jermar 284
            if (keys[i] == keys[j])
285
                errx(1, "duplicate command keys specified.");
286
        }
4537 trochtova 287
 
1419 jermar 288
        if (keys[i] == ' ')
4537 trochtova 289
            str_cpy(key_write[i], sizeof(key_write[i]), "<space>");
1419 jermar 290
        else {
291
            key_write[i][0] = keys[i];
292
            key_write[i][1] = '\0';
293
        }
294
    }
4537 trochtova 295
 
296
    snprintf(key_msg, sizeof(key_msg),
297
        "%s - left   %s - rotate   %s - right   %s - drop   %s - pause   %s - quit",
298
        key_write[0], key_write[1], key_write[2], key_write[3],
299
        key_write[4], key_write[5]);
300
 
1419 jermar 301
    scr_init();
4668 trochtova 302
    if (loadscores() != EOK)
303
        initscores();
304
 
1590 cejka 305
    while (tetris_menu(&level)) {
1688 jermar 306
        fallrate = 1000000 / level;
307
 
1590 cejka 308
        scr_clear();
309
        setup_board();
4537 trochtova 310
 
1590 cejka 311
        srandomdev();
312
        scr_set();
4537 trochtova 313
 
314
        pos = A_FIRST * B_COLS + (B_COLS / 2) - 1;
1590 cejka 315
        nextshape = randshape();
316
        curshape = randshape();
4537 trochtova 317
 
1590 cejka 318
        scr_msg(key_msg, 1);
4537 trochtova 319
 
320
        while (1) {
1590 cejka 321
            place(curshape, pos, 1);
322
            scr_update();
323
            place(curshape, pos, 0);
324
            c = tgetchar();
325
            if (c < 0) {
326
                /*
327
                 * Timeout.  Move down if possible.
328
                 */
329
                if (fits_in(curshape, pos + B_COLS)) {
330
                    pos += B_COLS;
331
                    continue;
332
                }
4537 trochtova 333
 
1590 cejka 334
                /*
335
                 * Put up the current shape `permanently',
336
                 * bump score, and elide any full rows.
337
                 */
338
                place(curshape, pos, 1);
339
                score++;
340
                elide();
4537 trochtova 341
 
1590 cejka 342
                /*
343
                 * Choose a new shape.  If it does not fit,
344
                 * the game is over.
345
                 */
346
                curshape = nextshape;
347
                nextshape = randshape();
4537 trochtova 348
                pos = A_FIRST * B_COLS + (B_COLS / 2) - 1;
349
 
1590 cejka 350
                if (!fits_in(curshape, pos))
351
                    break;
4537 trochtova 352
 
1419 jermar 353
                continue;
354
            }
4537 trochtova 355
 
1419 jermar 356
            /*
1590 cejka 357
             * Handle command keys.
1419 jermar 358
             */
1590 cejka 359
            if (c == keys[5]) {
360
                /* quit */
1419 jermar 361
                break;
362
            }
4537 trochtova 363
 
1590 cejka 364
            if (c == keys[4]) {
365
                static char msg[] =
366
                    "paused - press RETURN to continue";
4537 trochtova 367
 
1590 cejka 368
                place(curshape, pos, 1);
369
                do {
370
                    scr_update();
371
                    scr_msg(key_msg, 0);
372
                    scr_msg(msg, 1);
4201 trochtova 373
                    (void) fflush(stdout);
4537 trochtova 374
                } while (rwait((struct timeval *) NULL) == -1);
375
 
1590 cejka 376
                scr_msg(msg, 0);
377
                scr_msg(key_msg, 1);
378
                place(curshape, pos, 0);
379
                continue;
380
            }
4537 trochtova 381
 
1590 cejka 382
            if (c == keys[0]) {
383
                /* move left */
384
                if (fits_in(curshape, pos - 1))
385
                    pos--;
386
                continue;
387
            }
4537 trochtova 388
 
1590 cejka 389
            if (c == keys[1]) {
390
                /* turn */
4537 trochtova 391
                const struct shape *new =
392
                    &shapes[classic ? curshape->rotc : curshape->rot];
393
 
1590 cejka 394
                if (fits_in(new, pos))
395
                    curshape = new;
396
                continue;
397
            }
4537 trochtova 398
 
1590 cejka 399
            if (c == keys[2]) {
400
                /* move right */
401
                if (fits_in(curshape, pos + 1))
402
                    pos++;
403
                continue;
404
            }
4537 trochtova 405
 
1590 cejka 406
            if (c == keys[3]) {
407
                /* move to bottom */
408
                while (fits_in(curshape, pos + B_COLS)) {
409
                    pos += B_COLS;
410
                    score++;
411
                }
412
                continue;
413
            }
4537 trochtova 414
 
1590 cejka 415
            if (c == '\f') {
416
                scr_clear();
417
                scr_msg(key_msg, 1);
418
            }
419
        }
420
 
421
        scr_clear();
4668 trochtova 422
        loadscores();
1590 cejka 423
        insertscore(score, level);
4668 trochtova 424
        savescores();
4537 trochtova 425
        score = 0;
1584 cejka 426
    }
427
 
428
    scr_clear();
4537 trochtova 429
    printf("\nGame over.\n");
1590 cejka 430
    scr_end();
4537 trochtova 431
 
1721 palkovsky 432
    return 0;
1419 jermar 433
}
434
 
4537 trochtova 435
void usage(void)
1419 jermar 436
{
4537 trochtova 437
    fprintf(stderr, "usage: tetris [-ps] [-k keys]\n");
1419 jermar 438
    exit(1);
439
}
1653 cejka 440
 
441
/** @}
442
 */