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