xref: /netbsd-src/games/tetris/shapes.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
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  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)shapes.c	8.1 (Berkeley) 5/31/93
37  */
38 
39 /*
40  * Tetris shapes and related routines.
41  *
42  * Note that the first 7 are `well known'.
43  */
44 
45 #include <sys/cdefs.h>
46 #include "tetris.h"
47 
48 #define	TL	-B_COLS-1	/* top left */
49 #define	TC	-B_COLS		/* top center */
50 #define	TR	-B_COLS+1	/* top right */
51 #define	ML	-1		/* middle left */
52 #define	MR	1		/* middle right */
53 #define	BL	B_COLS-1	/* bottom left */
54 #define	BC	B_COLS		/* bottom center */
55 #define	BR	B_COLS+1	/* bottom right */
56 
57 struct shape shapes[] = {
58 	/* 0*/	7,	TL, TC, MR,
59 	/* 1*/	8,	TC, TR, ML,
60 	/* 2*/	9,	ML, MR, BC,
61 	/* 3*/	3,	TL, TC, ML,
62 	/* 4*/	12,	ML, BL, MR,
63 	/* 5*/	15,	ML, BR, MR,
64 	/* 6*/	18,	ML, MR, /* sticks out */ 2,
65 	/* 7*/	0,	TC, ML, BL,
66 	/* 8*/	1,	TC, MR, BR,
67 	/* 9*/	10,	TC, MR, BC,
68 	/*10*/	11,	TC, ML, MR,
69 	/*11*/	2,	TC, ML, BC,
70 	/*12*/	13,	TC, BC, BR,
71 	/*13*/	14,	TR, ML, MR,
72 	/*14*/	4,	TL, TC, BC,
73 	/*15*/	16,	TR, TC, BC,
74 	/*16*/	17,	TL, MR, ML,
75 	/*17*/	5,	TC, BC, BL,
76 	/*18*/	6,	TC, BC, /* sticks out */ 2*B_COLS,
77 };
78 
79 /*
80  * Return true iff the given shape fits in the given position,
81  * taking the current board into account.
82  */
83 int
84 fits_in(shape, pos)
85 	struct shape *shape;
86 	register int pos;
87 {
88 	register int *o = shape->off;
89 
90 	if (board[pos] || board[pos + *o++] || board[pos + *o++] ||
91 	    board[pos + *o])
92 		return 0;
93 	return 1;
94 }
95 
96 /*
97  * Write the given shape into the current board, turning it on
98  * if `onoff' is 1, and off if `onoff' is 0.
99  */
100 void
101 place(shape, pos, onoff)
102 	struct shape *shape;
103 	register int pos, onoff;
104 {
105 	register int *o = shape->off;
106 
107 	board[pos] = onoff;
108 	board[pos + *o++] = onoff;
109 	board[pos + *o++] = onoff;
110 	board[pos + *o] = onoff;
111 }
112