Statistics
| Branch: | Revision:

scoutos / prex-0.9.0 / usr / sample / tetris / shapes.c @ 03e9c04a

History | View | Annotate | Download (3.39 KB)

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. Neither the name of the University nor the names of its contributors
17
 *    may be used to endorse or promote products derived from this software
18
 *    without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 *
32
 *        @(#)shapes.c        8.1 (Berkeley) 5/31/93
33
 */
34

    
35
/*
36
 * Tetris shapes and related routines.
37
 *
38
 * Note that the first 7 are `well known'.
39
 */
40

    
41
#include <sys/cdefs.h>
42
#include "tetris.h"
43

    
44
#define        TL        -B_COLS-1        /* top left */
45
#define        TC        -B_COLS                /* top center */
46
#define        TR        -B_COLS+1        /* top right */
47
#define        ML        -1                /* middle left */
48
#define        MR        1                /* middle right */
49
#define        BL        B_COLS-1        /* bottom left */
50
#define        BC        B_COLS                /* bottom center */
51
#define        BR        B_COLS+1        /* bottom right */
52

    
53
const struct shape shapes[] = {
54
        /* 0*/        { 7,        { TL, TC, MR, } },
55
        /* 1*/        { 8,        { TC, TR, ML, } },
56
        /* 2*/        { 9,        { ML, MR, BC, } },
57
        /* 3*/        { 3,        { TL, TC, ML, } },
58
        /* 4*/        { 12,        { ML, BL, MR, } },
59
        /* 5*/        { 15,        { ML, BR, MR, } },
60
        /* 6*/        { 18,        { ML, MR, 2   } },        /* sticks out */
61
        /* 7*/        { 0,        { TC, ML, BL, } },
62
        /* 8*/        { 1,        { TC, MR, BR, } },
63
        /* 9*/        { 10,        { TC, MR, BC, } },
64
        /*10*/        { 11,        { TC, ML, MR, } },
65
        /*11*/        { 2,        { TC, ML, BC, } },
66
        /*12*/        { 13,        { TC, BC, BR, } },
67
        /*13*/        { 14,        { TR, ML, MR, } },
68
        /*14*/        { 4,        { TL, TC, BC, } },
69
        /*15*/        { 16,        { TR, TC, BC, } },
70
        /*16*/        { 17,        { TL, MR, ML, } },
71
        /*17*/        { 5,        { TC, BC, BL, } },
72
        /*18*/        { 6,        { TC, BC, 2*B_COLS } }        /* sticks out */
73
};
74

    
75
/*
76
 * Return true iff the given shape fits in the given position,
77
 * taking the current board into account.
78
 */
79
int
80
fits_in(const struct shape *shape, int pos)
81
{
82
        const int *o = shape->off;
83

    
84
        if (board[pos] || board[pos + *o++] || board[pos + *o++] ||
85
            board[pos + *o])
86
                return 0;
87
        return 1;
88
}
89

    
90
/*
91
 * Write the given shape into the current board, turning it on
92
 * if `onoff' is 1, and off if `onoff' is 0.
93
 */
94
void
95
place(const struct shape *shape, int pos, cell onoff)
96
{
97
        const int *o = shape->off;
98

    
99
        board[pos] = onoff;
100
        board[pos + *o++] = onoff;
101
        board[pos + *o++] = onoff;
102
        board[pos + *o] = onoff;
103
}