Blame


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