Diffstat (limited to 'noncore/games/tetrix/tpiece.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/tetrix/tpiece.cpp | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/noncore/games/tetrix/tpiece.cpp b/noncore/games/tetrix/tpiece.cpp new file mode 100644 index 0000000..fe8b766 --- a/dev/null +++ b/noncore/games/tetrix/tpiece.cpp | |||
@@ -0,0 +1,201 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | |||
22 | #include "tpiece.h" | ||
23 | #include "qstring.h" | ||
24 | #include <stdlib.h> | ||
25 | #include <time.h> | ||
26 | |||
27 | void TetrixPiece::rotateLeft() | ||
28 | { | ||
29 | if ( pieceType == 5 ) // don't rotate square piece type | ||
30 | return; | ||
31 | int tmp; | ||
32 | for (int i = 0 ; i < 4 ; i++) { | ||
33 | tmp = getXCoord(i); | ||
34 | setXCoord(i,getYCoord(i)); | ||
35 | setYCoord(i,-tmp); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | void TetrixPiece::rotateRight() | ||
40 | { | ||
41 | if ( pieceType == 5 ) // don't rotate square piece type | ||
42 | return; | ||
43 | int tmp; | ||
44 | for (int i = 0 ; i < 4 ; i++) { | ||
45 | tmp = getXCoord(i); | ||
46 | setXCoord(i,-getYCoord(i)); | ||
47 | setYCoord(i,tmp); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | int TetrixPiece::getMinX() | ||
52 | { | ||
53 | int tmp = coordinates[0][0]; | ||
54 | for(int i = 1 ; i < 4 ; i++) | ||
55 | if (tmp > coordinates[i][0]) | ||
56 | tmp = coordinates[i][0]; | ||
57 | return tmp; | ||
58 | } | ||
59 | |||
60 | int TetrixPiece::getMaxX() | ||
61 | { | ||
62 | int tmp = coordinates[0][0]; | ||
63 | for(int i = 1 ; i < 4 ; i++) | ||
64 | if (tmp < coordinates[i][0]) | ||
65 | tmp = coordinates[i][0]; | ||
66 | return tmp; | ||
67 | |||
68 | } | ||
69 | |||
70 | int TetrixPiece::getMinY() | ||
71 | { | ||
72 | int tmp = coordinates[0][1]; | ||
73 | for(int i = 1 ; i < 4 ; i++) | ||
74 | if (tmp > coordinates[i][1]) | ||
75 | tmp = coordinates[i][1]; | ||
76 | return tmp; | ||
77 | } | ||
78 | |||
79 | int TetrixPiece::getMaxY() | ||
80 | { | ||
81 | int tmp = coordinates[0][1]; | ||
82 | for(int i = 1 ; i < 4 ; i++) | ||
83 | if (tmp < coordinates[i][1]) | ||
84 | tmp = coordinates[i][1]; | ||
85 | return tmp; | ||
86 | } | ||
87 | |||
88 | void TetrixPiece::initialize(int type) | ||
89 | { | ||
90 | static int pieceTypes[7][4][2] = {{{ 0,-1}, | ||
91 | { 0, 0}, | ||
92 | {-1, 0}, | ||
93 | {-1, 1}}, | ||
94 | |||
95 | {{ 0,-1}, | ||
96 | { 0, 0}, | ||
97 | { 1, 0}, | ||
98 | { 1, 1}}, | ||
99 | |||
100 | {{ 0,-1}, | ||
101 | { 0, 0}, | ||
102 | { 0, 1}, | ||
103 | { 0, 2}}, | ||
104 | |||
105 | {{-1, 0}, | ||
106 | { 0, 0}, | ||
107 | { 1, 0}, | ||
108 | { 0, 1}}, | ||
109 | |||
110 | {{ 0, 0}, | ||
111 | { 1, 0}, | ||
112 | { 0, 1}, | ||
113 | { 1, 1}}, | ||
114 | |||
115 | {{-1,-1}, | ||
116 | { 0,-1}, | ||
117 | { 0, 0}, | ||
118 | { 0, 1}}, | ||
119 | |||
120 | {{ 1,-1}, | ||
121 | { 0,-1}, | ||
122 | { 0, 0}, | ||
123 | { 0, 1}}}; | ||
124 | if (type < 1 || type > 7) | ||
125 | type = 1; | ||
126 | pieceType = type; | ||
127 | for(int i = 0 ; i < 4 ; i++) { | ||
128 | coordinates[i][0] = pieceTypes[type - 1][i][0]; | ||
129 | coordinates[i][1] = pieceTypes[type - 1][i][1]; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | |||
134 | /* | ||
135 | *Sigh, oh beautiful nostalgia! This random algorithm has | ||
136 | *been taken from the book "Adventures with your pocket calculator" | ||
137 | *and I used it in my first implemented and machine- | ||
138 | *run program of any size to speak of. Imagine how hungry I | ||
139 | *was after having programmed BASIC on paper for | ||
140 | *half a year?!!?!?!?!?!? The first program I typed in was a | ||
141 | *slot machine game and was made in BASIC on a SHARP | ||
142 | *PC-1211 with 1,47 KB RAM (one point four seven kilobytes) and | ||
143 | *a one-line LCD-display (I think it had 32 characters) in the | ||
144 | *year of our lord 1981. The man I had bought the machine from worked | ||
145 | *as a COBOL programmer and was amazed and impressed | ||
146 | *when I demonstrated the program 2 days after I had | ||
147 | *bought the machine, quote: "Gees, I have been looking so long | ||
148 | *for a "random" command in that BASIC, what is it called?" | ||
149 | *Oh, how I still get a thrill out of the thought of the | ||
150 | *explanation I then gave him... | ||
151 | */ | ||
152 | |||
153 | /* | ||
154 | *Sukk, aa vakre nostalgi! Denne random algoritmen er | ||
155 | *tatt fra boka "Adventures with your pocket calculator" | ||
156 | *og den brukte jeg i mitt foerste implementerte og maskin- | ||
157 | *kjoerte program av nevneverdig stoerrelse. Tror du jeg var | ||
158 | *noe sulten etter aa ha programmert BASIC paa papir i et | ||
159 | *halvt aar?!!?!?!?!?!? Programmet jeg tasta inn foerst var et | ||
160 | *"enarmet banditt" spill og ble laget i BASIC paa en SHARP | ||
161 | *PC-1211 med 1,47 KB RAM (en komma foertisju kilobyte) og | ||
162 | *et en-linjers LCD-display (tror det hadde 32 karakterer) i det | ||
163 | *herrens aar 1981. Mannen jeg kjoepte maskinen av jobbet til | ||
164 | *daglig med COBOL programmering og var forbloeffet og imponert | ||
165 | *da jeg demonstrerte programmet 2 dager etter at jeg hadde | ||
166 | *kjoept maskinen, sitat: "Joess, jeg som har leita saa lenge | ||
167 | *etter en random kommando i den BASICen, hva var det den | ||
168 | *het?" Aa, jeg frydes ennaa ved tanken paa forklaringen jeg | ||
169 | *deretter ga ham... | ||
170 | */ | ||
171 | |||
172 | double TetrixPiece::randomSeed = 0.33333; | ||
173 | |||
174 | void TetrixPiece::setRandomSeed(double seed) | ||
175 | { | ||
176 | #ifdef __MIPSEL__ | ||
177 | srand( clock() ); | ||
178 | #else | ||
179 | QCString buffer; | ||
180 | if (seed < 0) | ||
181 | seed = - seed; | ||
182 | if (seed >= 1) | ||
183 | seed = seed - (double) ((int) seed); | ||
184 | buffer.sprintf("%1.5f",(float) seed); | ||
185 | for (int i = 0 ; i < 5 ; i++) | ||
186 | if ((buffer[i + 2] - '0') % 2 == 0) | ||
187 | buffer[i + 2]++; | ||
188 | randomSeed = atof(buffer); | ||
189 | #endif | ||
190 | } | ||
191 | |||
192 | int TetrixPiece::randomValue(int maxPlusOne) | ||
193 | { | ||
194 | #ifdef __MIPSEL__ | ||
195 | return rand() % maxPlusOne; | ||
196 | #else | ||
197 | randomSeed = randomSeed*147; | ||
198 | randomSeed = randomSeed - (double) ((int) randomSeed); | ||
199 | return (int) (randomSeed*maxPlusOne); | ||
200 | #endif | ||
201 | } | ||