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