Subversion Repositories HelenOS

Rev

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