1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek and Darren F. Provine.
7 *
8 * %sccs.include.redist.c%
9 *
10 * @(#)shapes.c 8.1 (Berkeley) 05/31/93
11 */
12
13 /*
14 * Tetris shapes and related routines.
15 *
16 * Note that the first 7 are `well known'.
17 */
18
19 #include <sys/cdefs.h>
20 #include "tetris.h"
21
22 #define TL -B_COLS-1 /* top left */
23 #define TC -B_COLS /* top center */
24 #define TR -B_COLS+1 /* top right */
25 #define ML -1 /* middle left */
26 #define MR 1 /* middle right */
27 #define BL B_COLS-1 /* bottom left */
28 #define BC B_COLS /* bottom center */
29 #define BR B_COLS+1 /* bottom right */
30
31 struct shape shapes[] = {
32 /* 0*/ 7, TL, TC, MR,
33 /* 1*/ 8, TC, TR, ML,
34 /* 2*/ 9, ML, MR, BC,
35 /* 3*/ 3, TL, TC, ML,
36 /* 4*/ 12, ML, BL, MR,
37 /* 5*/ 15, ML, BR, MR,
38 /* 6*/ 18, ML, MR, /* sticks out */ 2,
39 /* 7*/ 0, TC, ML, BL,
40 /* 8*/ 1, TC, MR, BR,
41 /* 9*/ 10, TC, MR, BC,
42 /*10*/ 11, TC, ML, MR,
43 /*11*/ 2, TC, ML, BC,
44 /*12*/ 13, TC, BC, BR,
45 /*13*/ 14, TR, ML, MR,
46 /*14*/ 4, TL, TC, BC,
47 /*15*/ 16, TR, TC, BC,
48 /*16*/ 17, TL, MR, ML,
49 /*17*/ 5, TC, BC, BL,
50 /*18*/ 6, TC, BC, /* sticks out */ 2*B_COLS,
51 };
52
53 /*
54 * Return true iff the given shape fits in the given position,
55 * taking the current board into account.
56 */
57 int
fits_in(shape,pos)58 fits_in(shape, pos)
59 struct shape *shape;
60 register int pos;
61 {
62 register int *o = shape->off;
63
64 if (board[pos] || board[pos + *o++] || board[pos + *o++] ||
65 board[pos + *o])
66 return 0;
67 return 1;
68 }
69
70 /*
71 * Write the given shape into the current board, turning it on
72 * if `onoff' is 1, and off if `onoff' is 0.
73 */
74 void
place(shape,pos,onoff)75 place(shape, pos, onoff)
76 struct shape *shape;
77 register int pos, onoff;
78 {
79 register int *o = shape->off;
80
81 board[pos] = onoff;
82 board[pos + *o++] = onoff;
83 board[pos + *o++] = onoff;
84 board[pos + *o] = onoff;
85 }
86