Tetris

Check-in [b915664999]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add "n" to move downward one step.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:b9156649998c747e7d286569f82bc4f46e20b9463be1d175034a237e22efd061
User & Date: ajv-899-334-8894@vsta.org 2008-10-29 00:31:24
Context
2008-10-29
00:31
Add doc for -h and "n" key. check-in: a1af86b053 user: ajv-899-334-8894@vsta.org tags: trunk
00:31
Add "n" to move downward one step. check-in: b915664999 user: ajv-899-334-8894@vsta.org tags: trunk
2008-10-28
02:37
Add "shadow" option -h to show where piece would land if dropped check-in: 25baf927f6 user: ajv-899-334-8894@vsta.org tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to tetris.c.

73
74
75
76
77
78
79

80
81
82
83
84
85
86
...
158
159
160
161
162
163
164
165


166
167
168
169
170
171
172
...
188
189
190
191
192
193
194
195
196
197
198
199
200
201


202
203
204
205
206
207
208
...
211
212
213
214
215
216
217


218
219
220
221
222
223


224
225
226
227
228
229
230
231


232
233
234
235
236
237


238
239
240
241
242
243
244
245


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262


263
264
265
266
267
268
269

270
271
272
273
274
275
276
277
278

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

301
302
303
304
305
306

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

372
373
static	void	elide(void);
static	void	setup_board(void);
	int	main(int, char **);
	void	onintr(int) __attribute__((__noreturn__));
	void	usage(void) __attribute__((__noreturn__));

static const char *keys;


/*
 * Set up the initial board.  The bottom display row is completely set,
 * along with another (hidden) row underneath that.  Also, the left and
 * right edges are set.
 */
static void
................................................................................
	/* Remove shadow (if any), and piece image */
	if (showshadow) {
	    place(curshape, pos + B_COLS*c, 0);
	}
	place(curshape, pos, 0);

	c = tgetchar();
	if (c < 0) {


	    /*
	     * Timeout.  Move down if possible.
	     */
	    if (fits_in(curshape, pos + B_COLS)) {
		pos += B_COLS;
		continue;
	    }
................................................................................
	    pos = A_FIRST*B_COLS + (B_COLS/2)-1;
	    if (!fits_in(curshape, pos)) {
		break;
	    }
	    continue;
	}

	/*
	 * Handle command keys.
	 */
	if (c == keys[5]) {
	    /* quit */
	    break;
	}


	if (c == keys[4]) {
	    static char msg[] =
		"paused - press RETURN to continue";

	    place(curshape, pos, 2);
	    do {
		scr_update();
................................................................................
		(void) fflush(stdout);
	    } while (rwait((struct timeval *)NULL) == -1);
	    scr_msg(msg, 0);
	    scr_msg(key_msg, 1);
	    place(curshape, pos, 0);
	    continue;
	}


	if (c == keys[0]) {
	    /* move left */
	    if (fits_in(curshape, pos - 1))
		pos--;
	    continue;
	}


	if (c == keys[1]) {
	    /* turn */
	    const struct shape *new = &shapes[curshape->rot];

	    if (fits_in(new, pos))
		curshape = new;
	    continue;
	}


	if (c == keys[2]) {
	    /* move right */
	    if (fits_in(curshape, pos + 1))
		pos++;
	    continue;
	}


	if (c == keys[3]) {
	    /* move to bottom */
	    while (fits_in(curshape, pos + B_COLS)) {
		pos += B_COLS;
		score++;
	    }
	    continue;
	}


	if (c == '\f') {
	    scr_clear();
	    scr_msg(key_msg, 1);
	}
    }
}

int
main(argc, argv)
	int argc;
	char *argv[];
{
	int level = 2;
	char key_write[6][10];
	int ch, i, j;
	int fd;



	gid = getgid();
	egid = getegid();
	setegid(gid);

	fd = open("/dev/null", O_RDONLY);
	if (fd < 3)
		exit(1);

	close(fd);

	keys = "jkl pq";

	while ((ch = getopt(argc, argv, "k:l:psh")) != -1)
		switch(ch) {
		case 'k':
			if (strlen(keys = optarg) != 6)
				usage();

			break;
		case 'l':
			level = atoi(optarg);
			if (level < MINLEVEL || level > MAXLEVEL) {
				errx(1, "level must be from %d to %d",
				     MINLEVEL, MAXLEVEL);
			}
			break;
		case 'p':
			showpreview = 1;
			break;
		case 'h':
			showshadow = 1;
			break;
		case 's':
			showscores(0);
			exit(0);
		case '?':
		default:
			usage();
		}


	argc -= optind;
	argv += optind;

	if (argc)
		usage();


	fallrate = 1000000 / level;

	for (i = 0; i <= 5; i++) {
		for (j = i+1; j <= 5; j++) {
			if (keys[i] == keys[j]) {
				errx(1, "duplicate command keys specified.");
			}
		}
		if (keys[i] == ' ')
			strcpy(key_write[i], "<space>");
		else {
			key_write[i][0] = keys[i];
			key_write[i][1] = '\0';
		}
	}

	sprintf(key_msg,
"%s - left   %s - rotate   %s - right   %s - drop   %s - pause   %s - quit",


		key_write[0], key_write[1], key_write[2], key_write[3],
		key_write[4], key_write[5]);

	(void)signal(SIGINT, onintr);
	scr_init();
	setup_board();
	init_random();
	scr_set();

	nextshape = randshape();
	curshape = randshape();

	scr_msg(key_msg, 1);

	play_game();

	scr_clear();
	scr_end();

	(void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
	    score, score == 1 ? "" : "s", level, score * level);
	savescore(level);

	printf("\nHit RETURN to see high scores, ^C to skip.\n");

	while ((i = getchar()) != '\n')
		if (i == EOF)
			break;



	showscores(level);

	exit(0);
}

void
onintr(signo)
	int signo __attribute__((__unused__));
{
	scr_clear();
	scr_end();
	exit(0);
}

void
usage()
{
	(void)fprintf(stderr, "usage: tetris-bsd [-ps] [-k keys] [-l level]\n");

	exit(1);
}







>







 







|
>
>







 







|
<
<

<


>
>







 







>
>

<




>
>

<






>
>

<




>
>

<






>
>








<
|
<

|
|
|
|

>
>
|
|
|

|
|
|
>
|

|

|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|

|
|
|
>
|

|
|
|
|
|
|
|
|
|
|
|
|
|

|
<
>
>
|
|

|
|
|
|
|

|
|

|

|

|
|

|
|
|

|

|
|
|
|
>
>
|

|



<
|

|
|
|



|

|
>
|

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
...
191
192
193
194
195
196
197
198


199

200
201
202
203
204
205
206
207
208
209
210
...
213
214
215
216
217
218
219
220
221
222

223
224
225
226
227
228
229

230
231
232
233
234
235
236
237
238

239
240
241
242
243
244
245

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261

262

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374

375
376
377
378
379
380
381
382
383
384
385
386
387
388
static	void	elide(void);
static	void	setup_board(void);
	int	main(int, char **);
	void	onintr(int) __attribute__((__noreturn__));
	void	usage(void) __attribute__((__noreturn__));

static const char *keys;
static char *cmd;

/*
 * Set up the initial board.  The bottom display row is completely set,
 * along with another (hidden) row underneath that.  Also, the left and
 * right edges are set.
 */
static void
................................................................................
	/* Remove shadow (if any), and piece image */
	if (showshadow) {
	    place(curshape, pos + B_COLS*c, 0);
	}
	place(curshape, pos, 0);

	c = tgetchar();

	/* Timeout or explicit step downward */
	if ((c < 0) || (c == keys[6])) {
	    /*
	     * Timeout.  Move down if possible.
	     */
	    if (fits_in(curshape, pos + B_COLS)) {
		pos += B_COLS;
		continue;
	    }
................................................................................
	    pos = A_FIRST*B_COLS + (B_COLS/2)-1;
	    if (!fits_in(curshape, pos)) {
		break;
	    }
	    continue;
	}

	/* Quit */


	if (c == keys[5]) {

	    break;
	}

	/* Pause */
	if (c == keys[4]) {
	    static char msg[] =
		"paused - press RETURN to continue";

	    place(curshape, pos, 2);
	    do {
		scr_update();
................................................................................
		(void) fflush(stdout);
	    } while (rwait((struct timeval *)NULL) == -1);
	    scr_msg(msg, 0);
	    scr_msg(key_msg, 1);
	    place(curshape, pos, 0);
	    continue;
	}

	/* Move left */
	if (c == keys[0]) {

	    if (fits_in(curshape, pos - 1))
		pos--;
	    continue;
	}

	/* Rotate piece */
	if (c == keys[1]) {

	    const struct shape *new = &shapes[curshape->rot];

	    if (fits_in(new, pos))
		curshape = new;
	    continue;
	}

	/* Move right */
	if (c == keys[2]) {

	    if (fits_in(curshape, pos + 1))
		pos++;
	    continue;
	}

	/* Move to bottom */
	if (c == keys[3]) {

	    while (fits_in(curshape, pos + B_COLS)) {
		pos += B_COLS;
		score++;
	    }
	    continue;
	}

	/* Repaint */
	if (c == '\f') {
	    scr_clear();
	    scr_msg(key_msg, 1);
	}
    }
}

int

main(int argc, char **argv)

{
    int level = 2;
    char key_write[7][10];
    int ch, i, j;
    int fd;

    cmd = argv[0];

    gid = getgid();
    egid = getegid();
    setegid(gid);

    fd = open("/dev/null", O_RDONLY);
    if (fd < 3) {
	exit(1);
    }
    close(fd);

    keys = "jkl pqn";

    while ((ch = getopt(argc, argv, "k:l:psh")) != -1) {
	switch(ch) {
	case 'k':
	    if (strlen(keys = optarg) != 7) {
		usage();
	    }
	    break;
	case 'l':
	    level = atoi(optarg);
	    if (level < MINLEVEL || level > MAXLEVEL) {
		errx(1, "level must be from %d to %d",
		     MINLEVEL, MAXLEVEL);
	    }
	    break;
	case 'p':
	    showpreview = 1;
	    break;
	case 'h':
	    showshadow = 1;
	    break;
	case 's':
	    showscores(0);
	    exit(0);
	case '?':
	default:
	    usage();
	}
    }

    argc -= optind;
    argv += optind;

    if (argc) {
	usage();
    }

    fallrate = 1000000 / level;

    for (i = 0; i <= 6; i++) {
	for (j = i+1; j <= 5; j++) {
	    if (keys[i] == keys[j]) {
		errx(1, "duplicate command keys specified.");
	    }
	}
	if (keys[i] == ' ') {
	    strcpy(key_write[i], "<space>");
	} else {
	    key_write[i][0] = keys[i];
	    key_write[i][1] = '\0';
	}
    }

    sprintf(key_msg,

"%s - left, %s - rotate, %s - right, %s - drop, %s - pause, "
"%s - quit, %s - down",
	key_write[0], key_write[1], key_write[2], key_write[3],
	key_write[4], key_write[5], key_write[6]);

    (void)signal(SIGINT, onintr);
    scr_init();
    setup_board();
    init_random();
    scr_set();

    nextshape = randshape();
    curshape = randshape();

    scr_msg(key_msg, 1);

    play_game();

    scr_clear();
    scr_end();

    (void)printf("Your score:  %d point%s  x  level %d  =  %d\n",
	score, score == 1 ? "" : "s", level, score * level);
    savescore(level);

    printf("\nHit RETURN to see high scores, ^C to skip.\n");

    while ((i = getchar()) != '\n') {
	if (i == EOF) {
	    break;
	}
    }

    showscores(level);

    exit(0);
}

void

onintr(int signo __attribute__((__unused__)))
{
    scr_clear();
    scr_end();
    exit(0);
}

void
usage(void)
{
    (void)fprintf(stderr,
	"usage: %s [-psh] [-k keys] [-l level]\n", cmd);
    exit(1);
}