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