summaryrefslogtreecommitdiff
path: root/noncore/games/tetrix/tpiece.cpp
blob: fe8b766ea2dc9a6c61ddec312ac498c021f26ec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/


#include "tpiece.h"
#include "qstring.h"
#include <stdlib.h>
#include <time.h>

void TetrixPiece::rotateLeft()
{
    if ( pieceType == 5 )    // don't rotate square piece type
        return;
    int tmp;
    for (int i = 0 ; i < 4 ; i++) {
        tmp = getXCoord(i);
        setXCoord(i,getYCoord(i));
        setYCoord(i,-tmp);
    }
}

void TetrixPiece::rotateRight()
{
    if ( pieceType == 5 )    // don't rotate square piece type
        return;
    int tmp;
    for (int i = 0 ; i < 4 ; i++) {
        tmp = getXCoord(i);
        setXCoord(i,-getYCoord(i));
        setYCoord(i,tmp);
    }
}

int TetrixPiece::getMinX()
{
    int tmp = coordinates[0][0];
    for(int i = 1 ; i < 4 ; i++)
        if (tmp > coordinates[i][0])
            tmp = coordinates[i][0];
    return tmp;
}

int TetrixPiece::getMaxX()
{
    int tmp = coordinates[0][0];
    for(int i = 1 ; i < 4 ; i++)
        if (tmp < coordinates[i][0])
            tmp = coordinates[i][0];
    return tmp;

}

int TetrixPiece::getMinY()
{
    int tmp = coordinates[0][1];
    for(int i = 1 ; i < 4 ; i++)
        if (tmp > coordinates[i][1])
            tmp = coordinates[i][1];
    return tmp;
}

int TetrixPiece::getMaxY()
{
    int tmp = coordinates[0][1];
    for(int i = 1 ; i < 4 ; i++)
        if (tmp < coordinates[i][1])
            tmp = coordinates[i][1];
    return tmp;
}

void TetrixPiece::initialize(int type)
{
    static int pieceTypes[7][4][2] = {{{ 0,-1},
                                       { 0, 0},
                                       {-1, 0},
                                       {-1, 1}},

                                      {{ 0,-1},
                                       { 0, 0},
                                       { 1, 0},
                                       { 1, 1}},

                                      {{ 0,-1},
                                       { 0, 0},
                                       { 0, 1},
                                       { 0, 2}},

                                      {{-1, 0},
                                       { 0, 0},
                                       { 1, 0},
                                       { 0, 1}},

                                      {{ 0, 0},
                                       { 1, 0},
                                       { 0, 1},
                                       { 1, 1}},

                                      {{-1,-1},
                                       { 0,-1},
                                       { 0, 0},
                                       { 0, 1}},

                                      {{ 1,-1},
                                       { 0,-1},
                                       { 0, 0},
                                       { 0, 1}}};
    if (type < 1 || type > 7)
        type = 1;
    pieceType = type;
    for(int i = 0 ; i < 4 ; i++) {
            coordinates[i][0] = pieceTypes[type - 1][i][0];
            coordinates[i][1] = pieceTypes[type - 1][i][1];
    }
}


/*
 *	Sigh, oh beautiful nostalgia! This random algorithm has
 *	been taken from the book "Adventures with your pocket calculator"
 *	and I used it in my first implemented and machine-
 *	run program of any size to speak of. Imagine how hungry I
 *	was after having programmed BASIC on paper for
 *	half a year?!!?!?!?!?!? The first program I typed in was a
 *	slot machine game and was made in BASIC on a SHARP
 *	PC-1211 with 1,47 KB RAM (one point four seven kilobytes) and
 *	a one-line LCD-display (I think it had 32 characters) in the
 *	year of our lord 1981. The man I had bought the machine from worked
 *	as a COBOL programmer and was amazed and impressed
 *	when I demonstrated the program 2 days after I had
 *	bought the machine, quote: "Gees, I have been looking so long
 *	for a "random" command in that BASIC, what is it called?"
 *	Oh, how I still get a thrill out of the thought of the
 *	explanation I then gave him...
 */

/*
 *	Sukk, aa vakre nostalgi! Denne random algoritmen er
 *	tatt fra boka "Adventures with your pocket calculator"
 *	og den brukte jeg i mitt foerste implementerte og maskin-
 *	kjoerte program av nevneverdig stoerrelse. Tror du jeg var
 *	noe sulten etter aa ha programmert BASIC paa papir i et
 *	halvt aar?!!?!?!?!?!? Programmet jeg tasta inn foerst var et
 *	"enarmet banditt" spill og ble laget i BASIC paa en SHARP
 *	PC-1211 med 1,47 KB RAM (en komma foertisju kilobyte) og
 *	et en-linjers LCD-display (tror det hadde 32 karakterer) i det
 *	herrens aar 1981. Mannen jeg kjoepte maskinen av jobbet til
 *	daglig med COBOL programmering og var forbloeffet og imponert
 *	da jeg demonstrerte programmet 2 dager etter at jeg hadde
 *	kjoept maskinen, sitat: "Joess, jeg som har leita saa lenge
 *	etter en random kommando i den BASICen, hva var det den
 *	het?" Aa, jeg frydes ennaa ved tanken paa forklaringen jeg
 *	deretter ga ham...
 */

double TetrixPiece::randomSeed = 0.33333;

void TetrixPiece::setRandomSeed(double seed)
{
#ifdef __MIPSEL__
    srand( clock() );
#else
    QCString buffer;
    if (seed < 0)
        seed = - seed;
    if (seed >= 1)
        seed = seed - (double) ((int) seed);
    buffer.sprintf("%1.5f",(float) seed);
    for (int i = 0 ; i < 5 ; i++)
        if ((buffer[i + 2] - '0') % 2 == 0)
	    buffer[i + 2]++;
    randomSeed = atof(buffer);
#endif
}

int TetrixPiece::randomValue(int maxPlusOne)
{
#ifdef __MIPSEL__
    return rand() % maxPlusOne;
#else
    randomSeed = randomSeed*147;
    randomSeed = randomSeed - (double) ((int) randomSeed);
    return (int) (randomSeed*maxPlusOne);
#endif
}